1 package org.robolectric.integration_tests.atsl; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.app.Instrumentation; 6 import android.content.Context; 7 import android.support.test.InstrumentationRegistry; 8 import android.support.test.runner.AndroidJUnit4; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 12 /** {@link InstrumentationRegistry} tests. */ 13 @RunWith(AndroidJUnit4.class) 14 public class InstrumentationRegistryTest { 15 16 private static Instrumentation priorInstrumentation = null; 17 private static Context priorContext = null; 18 19 @Test 20 public void getArguments() { 21 assertThat(InstrumentationRegistry.getArguments()).isNotNull(); 22 } 23 24 @Test 25 public void getInstrumentation() { 26 assertThat(InstrumentationRegistry.getInstrumentation()).isNotNull(); 27 } 28 29 @Test 30 public void getTargetContext() { 31 assertThat(InstrumentationRegistry.getTargetContext()).isNotNull(); 32 assertThat(InstrumentationRegistry.getContext()).isNotNull(); 33 assertThat(InstrumentationRegistry.getTargetContext().getPackageName()).isEqualTo( 34 InstrumentationRegistry.getContext().getPackageName()); 35 } 36 37 @Test 38 public void uniqueInstancesPerTest() { 39 checkInstances(); 40 } 41 42 @Test 43 public void uniqueInstancesPerTest2() { 44 checkInstances(); 45 } 46 47 /** 48 * Verifies that each test gets a new Instrumentation and Context, by comparing against instances 49 * stored by prior test. 50 */ 51 private void checkInstances() { 52 if (priorInstrumentation == null) { 53 priorInstrumentation = InstrumentationRegistry.getInstrumentation(); 54 priorContext = InstrumentationRegistry.getTargetContext(); 55 } else { 56 assertThat(priorInstrumentation).isNotEqualTo(InstrumentationRegistry.getInstrumentation()); 57 assertThat(priorContext).isNotEqualTo(InstrumentationRegistry.getTargetContext()); 58 } 59 } 60 61 } 62