Search in sources :

Example 1 with InstrumentationConfiguration

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

the class SandboxFactory method getSdkEnvironment.

public synchronized SdkEnvironment getSdkEnvironment(InstrumentationConfiguration instrumentationConfig, DependencyResolver dependencyResolver, SdkConfig sdkConfig) {
    Pair<InstrumentationConfiguration, SdkConfig> key = Pair.create(instrumentationConfig, sdkConfig);
    SdkEnvironment sdkEnvironment = sdkToEnvironment.get(key);
    if (sdkEnvironment == null) {
        URL url = dependencyResolver.getLocalArtifactUrl(sdkConfig.getAndroidSdkDependency());
        ClassLoader robolectricClassLoader = createClassLoader(instrumentationConfig, url);
        sdkEnvironment = new SdkEnvironment(sdkConfig, robolectricClassLoader);
        sdkToEnvironment.put(key, sdkEnvironment);
    }
    return sdkEnvironment;
}
Also used : InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) URLClassLoader(java.net.URLClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) URL(java.net.URL)

Example 2 with InstrumentationConfiguration

use of org.robolectric.internal.bytecode.InstrumentationConfiguration 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();
}
Also used : Configuration(org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration) InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) Config(org.robolectric.annotation.Config) Builder(org.robolectric.internal.bytecode.InstrumentationConfiguration.Builder) Nonnull(javax.annotation.Nonnull)

Example 3 with InstrumentationConfiguration

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

the class SandboxClassLoaderTest method shouldRemapClassesWhileInterceptingMethods.

@Test
public void shouldRemapClassesWhileInterceptingMethods() throws Exception {
    InstrumentationConfiguration config = configureBuilder().addClassNameTranslation(AClassToForget.class.getName(), AClassToRemember.class.getName()).addInterceptedMethod(new MethodRef(AClassThatCallsAMethodReturningAForgettableClass.class, "getAForgettableClass")).build();
    setClassLoader(new SandboxClassLoader(config));
    Class<?> theClass = loadClass(AClassThatCallsAMethodReturningAForgettableClass.class);
    theClass.getMethod("callSomeMethod").invoke(shadow.directlyOn(theClass.newInstance(), (Class<Object>) theClass));
}
Also used : MethodRef(org.robolectric.internal.bytecode.MethodRef) AClassToForget(org.robolectric.testing.AClassToForget) InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) AnUninstrumentedClass(org.robolectric.testing.AnUninstrumentedClass) AClassThatRefersToAForgettableClass(org.robolectric.testing.AClassThatRefersToAForgettableClass) AFinalClass(org.robolectric.testing.AFinalClass) AClassThatCallsAMethodReturningAForgettableClass(org.robolectric.testing.AClassThatCallsAMethodReturningAForgettableClass) AnExampleClass(org.robolectric.testing.AnExampleClass) AClassToRemember(org.robolectric.testing.AClassToRemember) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 4 with InstrumentationConfiguration

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

the class SandboxClassLoaderTest method shouldDelegateClassLoadForUnacquiredClasses.

@Test
public void shouldDelegateClassLoadForUnacquiredClasses() throws Exception {
    InstrumentationConfiguration config = mock(InstrumentationConfiguration.class);
    when(config.shouldAcquire(anyString())).thenReturn(false);
    when(config.shouldInstrument(any(ClassInfo.class))).thenReturn(false);
    ClassLoader classLoader = new SandboxClassLoader(config);
    Class<?> exampleClass = classLoader.loadClass(AnExampleClass.class.getName());
    assertSame(getClass().getClassLoader(), exampleClass.getClassLoader());
}
Also used : AnExampleClass(org.robolectric.testing.AnExampleClass) InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) ClassInfo(org.robolectric.internal.bytecode.ClassInfo) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Test(org.junit.Test)

Example 5 with InstrumentationConfiguration

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

the class SandboxTestRunner method getSandbox.

@NotNull
protected Sandbox getSandbox(FrameworkMethod method) {
    InstrumentationConfiguration instrumentationConfiguration = createClassLoaderConfig(method);
    URLClassLoader systemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    ClassLoader sandboxClassLoader = new SandboxClassLoader(systemClassLoader, instrumentationConfiguration);
    Sandbox sandbox = new Sandbox(sandboxClassLoader);
    configureShadows(method, sandbox);
    return sandbox;
}
Also used : InstrumentationConfiguration(org.robolectric.internal.bytecode.InstrumentationConfiguration) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) SandboxClassLoader(org.robolectric.internal.bytecode.SandboxClassLoader) Sandbox(org.robolectric.internal.bytecode.Sandbox) NotNull(org.jetbrains.annotations.NotNull)

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