Search in sources :

Example 1 with Sdk

use of org.robolectric.pluginapi.Sdk in project robolectric by robolectric.

the class RobolectricTestRunner method getChildren.

@Override
protected List<FrameworkMethod> getChildren() {
    List<FrameworkMethod> children = new ArrayList<>();
    for (FrameworkMethod frameworkMethod : super.getChildren()) {
        try {
            Configuration configuration = getConfiguration(frameworkMethod.getMethod());
            AndroidManifest appManifest = getAppManifest(configuration);
            List<Sdk> sdksToRun = sdkPicker.selectSdks(configuration, appManifest);
            RobolectricFrameworkMethod last = null;
            for (Sdk sdk : sdksToRun) {
                if (resModeStrategy.includeLegacy(appManifest)) {
                    children.add(last = new RobolectricFrameworkMethod(frameworkMethod.getMethod(), appManifest, sdk, configuration, ResourcesMode.LEGACY, resModeStrategy, alwaysIncludeVariantMarkersInName));
                }
                if (resModeStrategy.includeBinary(appManifest)) {
                    children.add(last = new RobolectricFrameworkMethod(frameworkMethod.getMethod(), appManifest, sdk, configuration, ResourcesMode.BINARY, resModeStrategy, alwaysIncludeVariantMarkersInName));
                }
            }
            if (last != null) {
                last.dontIncludeVariantMarkersInTestName();
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("failed to configure " + getTestClass().getName() + "." + frameworkMethod.getMethod().getName() + ": " + e.getMessage(), e);
        }
    }
    return children;
}
Also used : Configuration(org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration) InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) ArrayList(java.util.ArrayList) AndroidManifest(org.robolectric.manifest.AndroidManifest) Sdk(org.robolectric.pluginapi.Sdk) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Example 2 with Sdk

use of org.robolectric.pluginapi.Sdk in project robolectric by robolectric.

the class SandboxManager method getAndroidSandbox.

public synchronized AndroidSandbox getAndroidSandbox(InstrumentationConfiguration instrumentationConfig, Sdk sdk, ResourcesMode resourcesMode, LooperMode.Mode looperMode) {
    SandboxKey key = new SandboxKey(instrumentationConfig, sdk, resourcesMode, looperMode);
    AndroidSandbox androidSandbox = sandboxesByKey.get(key);
    if (androidSandbox == null) {
        Sdk compileSdk = sdkCollection.getMaxSupportedSdk();
        androidSandbox = sandboxBuilder.build(instrumentationConfig, sdk, compileSdk, resourcesMode, looperMode);
        sandboxesByKey.put(key, androidSandbox);
    }
    return androidSandbox;
}
Also used : Sdk(org.robolectric.pluginapi.Sdk)

Example 3 with Sdk

use of org.robolectric.pluginapi.Sdk in project robolectric by robolectric.

the class RobolectricTestRunner method beforeTest.

@Override
protected void beforeTest(Sandbox sandbox, FrameworkMethod method, Method bootstrappedMethod) throws Throwable {
    AndroidSandbox androidSandbox = (AndroidSandbox) sandbox;
    RobolectricFrameworkMethod roboMethod = (RobolectricFrameworkMethod) method;
    PerfStatsCollector perfStatsCollector = PerfStatsCollector.getInstance();
    Sdk sdk = roboMethod.getSdk();
    perfStatsCollector.putMetadata(AndroidMetadata.class, new AndroidMetadata(ImmutableMap.of("ro.build.version.sdk", "" + sdk.getApiLevel()), roboMethod.resourcesMode.name()));
    Logger.lifecycle(roboMethod.getDeclaringClass().getName() + "." + roboMethod.getMethod().getName() + ": sdk=" + sdk.getApiLevel() + "; resources=" + roboMethod.resourcesMode);
    if (roboMethod.resourcesMode == ResourcesMode.LEGACY) {
        Logger.warn("Legacy resources mode is deprecated; see" + " http://robolectric.org/migrating/#migrating-to-40");
    }
    roboMethod.setStuff(androidSandbox, androidSandbox.getTestEnvironment());
    Class<TestLifecycle> cl = androidSandbox.bootstrappedClass(getTestLifecycleClass());
    roboMethod.testLifecycle = ReflectionHelpers.newInstance(cl);
    AndroidManifest appManifest = roboMethod.getAppManifest();
    roboMethod.getTestEnvironment().setUpApplicationState(bootstrappedMethod, roboMethod.getConfiguration(), appManifest);
    roboMethod.testLifecycle.beforeTest(bootstrappedMethod);
}
Also used : AndroidSandbox(org.robolectric.internal.AndroidSandbox) AndroidManifest(org.robolectric.manifest.AndroidManifest) Sdk(org.robolectric.pluginapi.Sdk) PerfStatsCollector(org.robolectric.util.PerfStatsCollector)

Example 4 with Sdk

use of org.robolectric.pluginapi.Sdk in project robolectric by robolectric.

the class TestUtil method systemResources.

public static ResourcePath systemResources() {
    if (SYSTEM_RESOURCE_PATH == null) {
        Sdk sdk = getSdkCollection().getMaxSupportedSdk();
        Path path = sdk.getJarPath();
        SYSTEM_RESOURCE_PATH = new ResourcePath(android.R.class, path.resolve("raw-res/res"), path.resolve("raw-res/assets"));
    }
    return SYSTEM_RESOURCE_PATH;
}
Also used : Path(java.nio.file.Path) ResourcePath(org.robolectric.res.ResourcePath) R(org.robolectric.R) ResourcePath(org.robolectric.res.ResourcePath) Sdk(org.robolectric.pluginapi.Sdk)

Example 5 with Sdk

use of org.robolectric.pluginapi.Sdk 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

Sdk (org.robolectric.pluginapi.Sdk)6 Nonnull (javax.annotation.Nonnull)2 InstrumentationConfiguration (org.robolectric.internal.bytecode.InstrumentationConfiguration)2 AndroidManifest (org.robolectric.manifest.AndroidManifest)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 AssumptionViolatedException (org.junit.AssumptionViolatedException)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1 R (org.robolectric.R)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 AndroidSandbox (org.robolectric.internal.AndroidSandbox)1 ResourcesMode (org.robolectric.internal.ResourcesMode)1 UsesSdk (org.robolectric.pluginapi.UsesSdk)1 Configuration (org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration)1 ResourcePath (org.robolectric.res.ResourcePath)1 PerfStatsCollector (org.robolectric.util.PerfStatsCollector)1