2121import android .graphics .BitmapFactory ;
2222import android .os .Bundle ;
2323import android .provider .MediaStore ;
24- import androidx .test .espresso .intent .rule .IntentsTestRule ;
24+
25+ import androidx .test .espresso .intent .Intents ;
26+ import androidx .test .ext .junit .rules .ActivityScenarioRule ;
2527import androidx .test .ext .junit .runners .AndroidJUnit4 ;
2628import androidx .test .filters .LargeTest ;
29+ import androidx .test .platform .app .InstrumentationRegistry ;
2730
31+ import org .junit .After ;
2832import org .junit .Before ;
2933import org .junit .Rule ;
3034import org .junit .Test ;
4852public class ImageViewerActivityTest {
4953
5054 /**
51- * A JUnit {@link Rule @Rule} to init and release Espresso Intents before and after each
52- * test run.
53- * <p>
54- * Rules are interceptors which are executed for each test method and will run before
55- * any of your setup code in the {@link Before @Before} method.
56- * <p>
57- * This rule is based on {@link ActivityTestRule} and will create and launch of the activity
58- * for you and also expose the activity under test.
55+ * Use {@link ActivityScenarioRule} to create and launch the activity under test, and close it
56+ * after test completes. This is a replacement for {@link androidx.test.rule.ActivityTestRule}.
5957 */
6058 @ Rule
61- public IntentsTestRule <ImageViewerActivity > mIntentsRule = new IntentsTestRule <>(
59+ public ActivityScenarioRule <ImageViewerActivity > mActivityScenarioRule = new ActivityScenarioRule <>(
6260 ImageViewerActivity .class );
6361
6462 @ Before
6563 public void stubCameraIntent () {
64+ // Initializes Intents and begins recording intents.
65+ Intents .init ();
66+
6667 ActivityResult result = createImageCaptureActivityResultStub ();
6768
6869 // Stub the Intent.
6970 intending (hasAction (MediaStore .ACTION_IMAGE_CAPTURE )).respondWith (result );
7071 }
7172
73+ @ After
74+ public void tearDown () {
75+ // Clears Intents state.
76+ Intents .release ();
77+ }
78+
7279 @ Test
7380 public void takePhoto_drawableIsApplied () {
7481 // Check that the ImageView doesn't have a drawable applied.
@@ -85,7 +92,8 @@ private ActivityResult createImageCaptureActivityResultStub() {
8592 // Put the drawable in a bundle.
8693 Bundle bundle = new Bundle ();
8794 bundle .putParcelable (ImageViewerActivity .KEY_IMAGE_DATA , BitmapFactory .decodeResource (
88- mIntentsRule .getActivity ().getResources (), R .drawable .ic_launcher ));
95+ InstrumentationRegistry .getInstrumentation ().getTargetContext ().getResources (),
96+ R .drawable .ic_launcher ));
8997
9098 // Create the Intent that will include the bundle.
9199 Intent resultData = new Intent ();
0 commit comments