use of org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration 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();
}
}
use of org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration 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;
}
use of org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration in project robolectric by robolectric.
the class RobolectricTestRunner method createClassLoaderConfig.
/**
* Create an {@link InstrumentationConfiguration} suitable for the provided {@link
* FrameworkMethod}.
*
* <p>Adds configuration for Android using {@link AndroidConfigurer}.
*
* <p>Custom TestRunner subclasses may wish to override this method to provide additional
* configuration.
*
* @param method the test method that's about to run
* @return an {@link InstrumentationConfiguration}
*/
@Override
@Nonnull
protected InstrumentationConfiguration createClassLoaderConfig(final FrameworkMethod method) {
Configuration configuration = ((RobolectricFrameworkMethod) method).getConfiguration();
Config config = configuration.get(Config.class);
Builder builder = new Builder(super.createClassLoaderConfig(method));
androidConfigurer.configure(builder, getInterceptors());
androidConfigurer.withConfig(builder, config);
return builder.build();
}
use of org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration in project robolectric by robolectric.
the class RobolectricTestRunner method getConfiguration.
/**
* Calculate the configuration for a given test method.
*/
private Configuration getConfiguration(Method method) {
Configuration configuration = configurationStrategy.getConfig(getTestClass().getJavaClass(), method);
// in case #getConfig(Method) has been overridden...
try {
Config config = getConfig(method);
((ConfigurationImpl) configuration).put(Config.class, config);
} catch (UnsupportedOperationException e) {
// no problem
}
return configuration;
}
use of org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration in project robolectric by robolectric.
the class HierarchicalConfigurationStrategyTest method configFor.
private Config configFor(Class<?> testClass, String methodName, final Map<String, String> configProperties, Config.Implementation globalConfig) {
Method info = getMethod(testClass, methodName);
PackagePropertiesLoader packagePropertiesLoader = new PackagePropertiesLoader() {
@Override
InputStream getResourceAsStream(String resourceName) {
String properties = configProperties.get(resourceName);
return properties == null ? null : new ByteArrayInputStream(properties.getBytes(UTF_8));
}
};
ConfigurationStrategy defaultConfigStrategy = new HierarchicalConfigurationStrategy(new ConfigConfigurer(packagePropertiesLoader, () -> globalConfig == null ? Config.Builder.defaults().build() : globalConfig));
Configuration config = defaultConfigStrategy.getConfig(testClass, info);
return config.get(Config.class);
}
Aggregations