* Rules are interceptors which are executed for each test method and will run before * any of your setup code in the {@link Before @Before} method. *
* This rule is based on {@link ActivityTestRule} and will create and launch of the activity * for you and also expose the activity under test. */ @Rule public IntentsTestRule mIntentsRule = new IntentsTestRule<>( ImageViewerActivity.class); @Before public void stubCameraIntent() { ActivityResult result = createImageCaptureActivityResultStub(); // Stub the Intent. intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result); } @Test public void takePhoto_drawableIsApplied() { // Check that the ImageView doesn't have a drawable applied. onView(withId(R.id.imageView)).check(matches(not(hasDrawable()))); // Click on the button that will trigger the stubbed intent. onView(withId(R.id.button_take_photo)).perform(click()); // With no user interaction, the ImageView will have a drawable. onView(withId(R.id.imageView)).check(matches(hasDrawable())); } private ActivityResult createImageCaptureActivityResultStub() { // Put the drawable in a bundle. Bundle bundle = new Bundle(); bundle.putParcelable(ImageViewerActivity.KEY_IMAGE_DATA, BitmapFactory.decodeResource( mIntentsRule.getActivity().getResources(), R.drawable.ic_launcher)); // Create the Intent that will include the bundle. Intent resultData = new Intent(); resultData.putExtras(bundle); // Create the ActivityResult with the Intent. return new ActivityResult(Activity.RESULT_OK, resultData); } }