Search in sources :

Example 6 with InstrumentationConfiguration

use of org.robolectric.internal.bytecode.InstrumentationConfiguration in project robolectric by robolectric.

the class SandboxTestRunner method createClassLoaderConfig.

/**
 * Create an {@link InstrumentationConfiguration} suitable for the provided {@link
 * FrameworkMethod}.
 *
 * <p>Custom TestRunner subclasses may wish to override this method to provide alternate
 * configuration.
 *
 * @param method the test method that's about to run
 * @return an {@link InstrumentationConfiguration}
 */
@Nonnull
protected InstrumentationConfiguration createClassLoaderConfig(FrameworkMethod method) {
    InstrumentationConfiguration.Builder builder = InstrumentationConfiguration.newBuilder().doNotAcquirePackage("java.").doNotAcquirePackage("jdk.internal.").doNotAcquirePackage("sun.").doNotAcquirePackage("org.robolectric.annotation.").doNotAcquirePackage("org.robolectric.internal.").doNotAcquirePackage("org.robolectric.pluginapi.").doNotAcquirePackage("org.robolectric.util.").doNotAcquirePackage("org.junit");
    String customPackages = System.getProperty("org.robolectric.packagesToNotAcquire", "");
    for (String pkg : customPackages.split(",")) {
        if (!pkg.isEmpty()) {
            builder.doNotAcquirePackage(pkg);
        }
    }
    String customClassesRegex = System.getProperty("org.robolectric.classesToNotInstrumentRegex", "");
    if (!customClassesRegex.isEmpty()) {
        builder.setDoNotInstrumentClassRegex(customClassesRegex);
    }
    for (Class<?> shadowClass : getExtraShadows(method)) {
        ShadowInfo shadowInfo = ShadowMap.obtainShadowInfo(shadowClass);
        builder.addInstrumentedClass(shadowInfo.shadowedClassName);
    }
    addInstrumentedPackages(method, builder);
    return builder.build();
}
Also used : InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) ShadowInfo(org.robolectric.internal.bytecode.ShadowInfo) Nonnull(javax.annotation.Nonnull)

Example 7 with InstrumentationConfiguration

use of org.robolectric.internal.bytecode.InstrumentationConfiguration in project robolectric by robolectric.

the class RobolectricTestRunner method getSandbox.

@Override
@Nonnull
protected AndroidSandbox getSandbox(FrameworkMethod method) {
    RobolectricFrameworkMethod roboMethod = (RobolectricFrameworkMethod) method;
    Sdk sdk = roboMethod.getSdk();
    InstrumentationConfiguration classLoaderConfig = createClassLoaderConfig(method);
    ResourcesMode resourcesMode = roboMethod.getResourcesMode();
    if (resourcesMode == ResourcesMode.LEGACY && sdk.getApiLevel() > Build.VERSION_CODES.P) {
        throw new AssumptionViolatedException("Robolectric doesn't support legacy mode after P");
    }
    LooperMode.Mode looperMode = roboMethod.configuration == null ? Mode.LEGACY : roboMethod.configuration.get(LooperMode.Mode.class);
    sdk.verifySupportedSdk(method.getDeclaringClass().getName());
    return sandboxManager.getAndroidSandbox(classLoaderConfig, sdk, resourcesMode, looperMode);
}
Also used : ResourcesMode(org.robolectric.internal.ResourcesMode) InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) AssumptionViolatedException(org.junit.AssumptionViolatedException) Mode(org.robolectric.annotation.LooperMode.Mode) SQLiteMode(org.robolectric.annotation.SQLiteMode) ResourcesMode(org.robolectric.internal.ResourcesMode) LooperMode(org.robolectric.annotation.LooperMode) Sdk(org.robolectric.pluginapi.Sdk) LooperMode(org.robolectric.annotation.LooperMode) Mode(org.robolectric.annotation.LooperMode.Mode) Nonnull(javax.annotation.Nonnull)

Aggregations

InstrumentationConfiguration (org.robolectric.internal.bytecode.InstrumentationConfiguration)7 SandboxClassLoader (org.robolectric.internal.bytecode.SandboxClassLoader)4 Nonnull (javax.annotation.Nonnull)3 URLClassLoader (java.net.URLClassLoader)2 Test (org.junit.Test)2 AnExampleClass (org.robolectric.testing.AnExampleClass)2 URL (java.net.URL)1 NotNull (org.jetbrains.annotations.NotNull)1 AssumptionViolatedException (org.junit.AssumptionViolatedException)1 Config (org.robolectric.annotation.Config)1 LooperMode (org.robolectric.annotation.LooperMode)1 Mode (org.robolectric.annotation.LooperMode.Mode)1 SQLiteMode (org.robolectric.annotation.SQLiteMode)1 ResourcesMode (org.robolectric.internal.ResourcesMode)1 ClassInfo (org.robolectric.internal.bytecode.ClassInfo)1 Builder (org.robolectric.internal.bytecode.InstrumentationConfiguration.Builder)1 MethodRef (org.robolectric.internal.bytecode.MethodRef)1 Sandbox (org.robolectric.internal.bytecode.Sandbox)1 ShadowInfo (org.robolectric.internal.bytecode.ShadowInfo)1 Sdk (org.robolectric.pluginapi.Sdk)1