Search in sources :

Example 1 with TestEnvironmentLifecyclePlugin

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

the class AndroidTestEnvironment method setUpApplicationState.

@Override
public void setUpApplicationState(Method method, Configuration configuration, AndroidManifest appManifest) {
    for (TestEnvironmentLifecyclePlugin e : testEnvironmentLifecyclePlugins) {
        e.onSetupApplicationState();
    }
    Config config = configuration.get(Config.class);
    ConfigurationRegistry.instance = new ConfigurationRegistry(configuration.map());
    clearEnvironment();
    RuntimeEnvironment.setTempDirectory(new TempDirectory(createTestDataDirRootPath(method)));
    if (ShadowLooper.looperMode() == LooperMode.Mode.LEGACY) {
        RuntimeEnvironment.setMasterScheduler(new Scheduler());
        RuntimeEnvironment.setMainThread(Thread.currentThread());
        ShadowLegacyLooper.internalInitializeBackgroundThreadScheduler();
    }
    if (!loggingInitialized) {
        ShadowLog.setupLogging();
        loggingInitialized = true;
    }
    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.addProvider(new BouncyCastleProvider());
    }
    android.content.res.Configuration androidConfiguration = new android.content.res.Configuration();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    Bootstrap.applyQualifiers(config.qualifiers(), apiLevel, androidConfiguration, displayMetrics);
    Locale locale = apiLevel >= VERSION_CODES.N ? androidConfiguration.getLocales().get(0) : androidConfiguration.locale;
    Locale.setDefault(locale);
    // Looper needs to be prepared before the activity thread is created
    if (Looper.myLooper() == null) {
        Looper.prepareMainLooper();
    }
    if (ShadowLooper.looperMode() == LooperMode.Mode.LEGACY) {
        ShadowLooper.getShadowMainLooper().resetScheduler();
    } else {
        RuntimeEnvironment.setMasterScheduler(new LooperDelegatingScheduler(Looper.getMainLooper()));
    }
    preloadClasses(apiLevel);
    RuntimeEnvironment.setAndroidFrameworkJarPath(sdkJarPath);
    Bootstrap.setDisplayConfiguration(androidConfiguration, displayMetrics);
    RuntimeEnvironment.setActivityThread(ReflectionHelpers.newInstance(ActivityThread.class));
    ReflectionHelpers.setStaticField(ActivityThread.class, "sMainThreadHandler", new Handler(Looper.myLooper()));
    Instrumentation instrumentation = createInstrumentation();
    InstrumentationRegistry.registerInstance(instrumentation, new Bundle());
    Supplier<Application> applicationSupplier = createApplicationSupplier(appManifest, config, androidConfiguration, displayMetrics);
    RuntimeEnvironment.setApplicationSupplier(applicationSupplier);
    if (configuration.get(LazyLoad.class) == LazyLoad.ON) {
        RuntimeEnvironment.setConfiguredApplicationClass(getApplicationClass(appManifest, config, new ApplicationInfo()));
    } else {
        // force eager load of the application
        RuntimeEnvironment.getApplication();
    }
}
Also used : Locale(java.util.Locale) Configuration(org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration) Config(org.robolectric.annotation.Config) Scheduler(org.robolectric.util.Scheduler) Bundle(android.os.Bundle) LazyLoad(org.robolectric.annotation.experimental.LazyApplication.LazyLoad) ApplicationInfo(android.content.pm.ApplicationInfo) ConfigurationRegistry(org.robolectric.config.ConfigurationRegistry) Handler(android.os.Handler) Instrumentation(android.app.Instrumentation) ShadowInstrumentation(org.robolectric.shadows.ShadowInstrumentation) ShadowActivityThread(org.robolectric.shadows.ShadowActivityThread) ActivityThread(android.app.ActivityThread) DisplayMetrics(android.util.DisplayMetrics) TestEnvironmentLifecyclePlugin(org.robolectric.pluginapi.TestEnvironmentLifecyclePlugin) TempDirectory(org.robolectric.util.TempDirectory) ShadowApplication(org.robolectric.shadows.ShadowApplication) Application(android.app.Application) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Aggregations

ActivityThread (android.app.ActivityThread)1 Application (android.app.Application)1 Instrumentation (android.app.Instrumentation)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 DisplayMetrics (android.util.DisplayMetrics)1 Locale (java.util.Locale)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1 Config (org.robolectric.annotation.Config)1 LazyLoad (org.robolectric.annotation.experimental.LazyApplication.LazyLoad)1 ConfigurationRegistry (org.robolectric.config.ConfigurationRegistry)1 TestEnvironmentLifecyclePlugin (org.robolectric.pluginapi.TestEnvironmentLifecyclePlugin)1 Configuration (org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration)1 ShadowActivityThread (org.robolectric.shadows.ShadowActivityThread)1 ShadowApplication (org.robolectric.shadows.ShadowApplication)1 ShadowInstrumentation (org.robolectric.shadows.ShadowInstrumentation)1 Scheduler (org.robolectric.util.Scheduler)1 TempDirectory (org.robolectric.util.TempDirectory)1