Search in sources :

Example 1 with ShadowProvider

use of org.robolectric.internal.ShadowProvider in project robolectric by robolectric.

the class RobolectricTest method reset_shouldResetShadows_beforeClearingPackageManager.

@Test
public void reset_shouldResetShadows_beforeClearingPackageManager() {
    Iterable<ShadowProvider> oldProviders = ReflectionHelpers.getStaticField(Robolectric.class, "providers");
    ;
    ShadowProvider mockProvider = new MockProvider();
    List<ShadowProvider> mockProviders = Collections.singletonList(mockProvider);
    ReflectionHelpers.setStaticField(Robolectric.class, "providers", mockProviders);
    RobolectricPackageManager mockManager = mock(RobolectricPackageManager.class);
    doAnswer(new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) {
            order.add("packageManager");
            return null;
        }
    }).when(mockManager).reset();
    RuntimeEnvironment.setRobolectricPackageManager(mockManager);
    try {
        Robolectric.reset();
    } finally {
        // Make sure we clean up after ourselves
        ReflectionHelpers.setStaticField(Robolectric.class, "providers", oldProviders);
    }
    assertThat(order).as("reset order").containsExactly("shadowProvider", "packageManager");
    assertThat(RuntimeEnvironment.application).as("app after reset").isNull();
    assertThat(RuntimeEnvironment.getRobolectricPackageManager()).as("packageManager after reset").isNull();
    assertThat(RuntimeEnvironment.getActivityThread()).as("activityThread after reset").isNull();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) RobolectricPackageManager(org.robolectric.res.builder.RobolectricPackageManager) ShadowProvider(org.robolectric.internal.ShadowProvider) Test(org.junit.Test)

Example 2 with ShadowProvider

use of org.robolectric.internal.ShadowProvider in project robolectric by robolectric.

the class Robolectric method reset.

public static void reset() {
    if (providers == null) {
        providers = ServiceLoader.load(ShadowProvider.class);
    }
    for (ShadowProvider provider : providers) {
        provider.reset();
    }
    RuntimeEnvironment.application = null;
    RuntimeEnvironment.setRobolectricPackageManager(null);
    RuntimeEnvironment.setActivityThread(null);
}
Also used : ShadowProvider(org.robolectric.internal.ShadowProvider)

Aggregations

ShadowProvider (org.robolectric.internal.ShadowProvider)2 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 RobolectricPackageManager (org.robolectric.res.builder.RobolectricPackageManager)1