@Test public void intending_callsOnActivityResult() { intending(hasComponent(hasClassName(DummyActivity.class.getName()))) .respondWith(new ActivityResult(Activity.RESULT_OK, new Intent().putExtra("key", 123))); ActivityScenario<ResultCapturingActivity> activityScenario = ActivityScenario.launch(ResultCapturingActivity.class); activityScenario.onActivity( activity -> { activity.startActivityForResult(new Intent(activity, DummyActivity.class), 0); }); activityScenario.onActivity( activity -> { assertThat(activity.activityResult.getResultCode()).isEqualTo(Activity.RESULT_OK); assertThat(activity.activityResult.getResultData()).extras().containsKey("key"); }); } }
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
public void startActivityForResult(Intent intent, int requestCode) { if (mParent == null) { Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode); if (ar != null) { mMainThread.sendActivityResult( mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); } if (requestCode >= 0) { // If this start is requesting a result, we can avoid making // the activity visible until the result is received. Setting // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the // activity hidden during this time, to avoid flickering. // This can only be done when a result is requested because // that guarantees we will get information back when the // activity is finished, no matter what happens to it. mStartedActivity = true; } } else { mParent.startActivityFromChild(this, intent, requestCode); } }
public void startActivityForResult(Intent intent, int requestCode, Bundle options) { if (mParent == null) { Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, options); if (ar != null) { mMainThread.sendActivityResult( mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); } if (requestCode >= 0) { mStartedActivity = true; } } else { if (options != null) { mParent.startActivityFromChild(this, intent, requestCode, options); } else { // Note we want to go through this method for compatibility with // existing applications that may have overridden it. mParent.startActivityFromChild(this, intent, requestCode); } } }
@Override public void onIntentSent(Intent intent) { Matcher<Intent> intentMatcher = hasAction(PermissiveTesting.ACTION_REQUEST_PERMISSIONS); if (intentMatcher.matches(intent)) { ResettingStubber resettingStubber = (ResettingStubber) IntentStubberRegistry.getInstance(); String[] permissions = intent.getStringArrayExtra(EXTRA_REQUEST_PERMISSIONS_NAMES); if (hasStubbedIntent) { if (multiplePermissionsRequest != null) { multiplePermissionsRequest.update(resettingStubber, intentMatcher); multiplePermissionsRequest = null; } Instrumentation.ActivityResult result = resettingStubber.getActivityResultForIntent(intent); assertThat("Requested permissions should be exactly the same to the results", result.getResultData().getStringArrayExtra(EXTRA_REQUEST_PERMISSIONS_NAMES), arrayContainingInAnyOrder(permissions)); applyActivityResult(result); } else { resettingStubber.setActivityResultForIntent(intentMatcher, createResult(permissions, PackageManager.PERMISSION_DENIED)); } } }
@Test public void testSave() { onDrawingSurfaceView() .perform(touchAt(DrawingSurfaceLocationProvider.MIDDLE)); onNavigationDrawer() .performOpen(); onView(withText(R.string.menu_back)) .perform(click()); onView(withText(R.string.save_button_text)) .check(matches(isDisplayed())); onView(withText(R.string.discard_button_text)) .check(matches(isDisplayed())); onView(withText(R.string.save_button_text)) .perform(click()); String path = launchActivityRule.getActivityResult().getResultData().getStringExtra(Constants.PAINTROID_PICTURE_PATH); assertEquals(imageFile.getAbsolutePath(), path); assertTrue(imageFile.exists()); assertThat(imageFile.length(), greaterThan(0L)); }
@Test public void testCameraIntentCanceled() { PointF testPixel = new PointF(0, 0); intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith( new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, null)); onDrawingSurfaceView() .perform(touchAt(DrawingSurfaceLocationProvider.MIDDLE)); getWorkingBitmap().setPixel((int) testPixel.x, (int) testPixel.y, Color.BLACK); onNavigationDrawer() .performOpen(); onView(withText(R.string.menu_new_image)) .perform(click()); onView(withText(R.string.discard_button_text)) .perform(click()); onView(withText(R.string.menu_new_image_from_camera)) .perform(click()); assertEquals("Color on drawing surface wrong", Color.BLACK, PaintroidApplication.drawingSurface.getPixel(testPixel)); } }
@Test public void testSave() { onDrawingSurfaceView() .perform(touchAt(DrawingSurfaceLocationProvider.MIDDLE)); onNavigationDrawer() .performOpen(); onView(withText(R.string.menu_back)).perform(click()); onView(withText(R.string.save_button_text)).check(matches(isDisplayed())); onView(withText(R.string.discard_button_text)).check(matches(isDisplayed())); long lastModifiedBefore = imageFile.lastModified(); long fileSizeBefore = imageFile.length(); onView(withText(R.string.save_button_text)).perform(click()); assertTrue(launchActivityRule.getActivity().isFinishing()); String path = launchActivityRule.getActivityResult().getResultData().getStringExtra(Constants.PAINTROID_PICTURE_PATH); assertEquals(imageFile.getAbsolutePath(), path); assertThat("Image modification not saved", imageFile.lastModified(), greaterThan(lastModifiedBefore)); assertThat("Saved image length not changed", imageFile.length(), greaterThan(fileSizeBefore)); }
public void test000EmptyStart() throws InterruptedException { onView(withText("No account has been added yet")).check(matches(isDisplayed())); onView(withText("Add")).check(matches(isDisplayed())); Intents.init(); String qr = "XXX" ; // Build a result to return from the ZXING app Intent resultData = new Intent(); resultData.putExtra(com.google.zxing.client.android.Intents.Scan.RESULT, qr); Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData); // Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond // with the ActivityResult we just created intending(hasAction("com.google.zxing.client.android.SCAN")).respondWith(result); onView(withText("Add")).check(matches(isDisplayed())); onView(withText("Add")).perform(click()); // We can also validate that an intent resolving to the "camera" activity has been sent out by our app intended(hasAction("com.google.zxing.client.android.SCAN")); Intents.release(); }
@Test public void testLoadImageTransparency() { Intent intent = new Intent(); intent.setData(createTestImageFile()); Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, intent); intending(hasAction(Intent.ACTION_GET_CONTENT)).respondWith(result); onNavigationDrawer() .performOpen(); onView(withText(R.string.menu_load_image)) .perform(click()); onToolBarView() .performSelectTool(ToolType.ERASER); onDrawingSurfaceView() .checkPixelColor(Color.BLACK, BitmapLocationProvider.MIDDLE); onDrawingSurfaceView() .perform(touchAt(DrawingSurfaceLocationProvider.MIDDLE)); onDrawingSurfaceView() .checkPixelColor(Color.TRANSPARENT, BitmapLocationProvider.MIDDLE); }
public void test001InvalidQRCode() throws InterruptedException { Intents.init(); String qr ="invalid qr code"; // Build a result to return from the ZXING app Intent resultData = new Intent(); resultData.putExtra(com.google.zxing.client.android.Intents.Scan.RESULT, qr); Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData); // Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond // with the ActivityResult we just created intending(hasAction("com.google.zxing.client.android.SCAN")).respondWith(result); // Now that we have the stub in place, click on the button in our app that launches into the Camera onView(withId(R.id.action_scan)).perform(click()); // We can also validate that an intent resolving to the "camera" activity has been sent out by our app intended(hasAction("com.google.zxing.client.android.SCAN")); onView(withText("Invalid QR Code")).check(matches(isDisplayed())); Thread.sleep(5000); onView(withText("No account has been added yet")).check(matches(isDisplayed())); Intents.release(); }
@Test public void testLoadImageDialogIntentOK() { onDrawingSurfaceView() .perform(touchAt(DrawingSurfaceLocationProvider.HALFWAY_RIGHT_MIDDLE)); onDrawingSurfaceView() .checkPixelColor(Color.TRANSPARENT, BitmapLocationProvider.MIDDLE); Intent intent = new Intent(); intent.setData(createTestImageFile()); Instrumentation.ActivityResult resultOK = new Instrumentation.ActivityResult(Activity.RESULT_OK, intent); intending(hasAction(Intent.ACTION_GET_CONTENT)).respondWith(resultOK); onNavigationDrawer() .performOpen(); onView(withText(R.string.menu_load_image)).perform(click()); onView(withText(R.string.discard_button_text)).perform(click()); onView(withText(R.string.dialog_warning_new_image)).check(doesNotExist()); onDrawingSurfaceView() .checkPixelColor(Color.BLACK, BitmapLocationProvider.MIDDLE); }
@Test public void testCameraIntentSimulated() { intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith( new Instrumentation.ActivityResult(Activity.RESULT_OK, null)); IntentMonitorRegistry.getInstance().addIntentCallback(cameraIntentCallback); onNavigationDrawer() .performOpen(); onView(withText(R.string.menu_new_image)) .perform(click()); onView(withText(R.string.menu_new_image_from_camera)) .perform(click()); assertEquals("Color on drawing surface wrong", Color.BLACK, PaintroidApplication.drawingSurface.getPixel(new PointF(0, 0))); IntentMonitorRegistry.getInstance().removeIntentCallback(cameraIntentCallback); }
@Test public void testLoadImageDialogIntentCancel() { onDrawingSurfaceView() .perform(touchAt(DrawingSurfaceLocationProvider.MIDDLE)); Instrumentation.ActivityResult resultCancel = new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, new Intent()); intending(hasAction(Intent.ACTION_GET_CONTENT)).respondWith(resultCancel); onNavigationDrawer() .performOpen(); onView(withText(R.string.menu_load_image)).perform(click()); onView(withText(R.string.discard_button_text)).perform(click()); onView(withText(R.string.dialog_warning_new_image)).check(doesNotExist()); onDrawingSurfaceView() .checkPixelColor(Color.BLACK, BitmapLocationProvider.MIDDLE); }
/** * Register new stub for contact data requests with specific activity result and extra Intent matcher. * * @param contactDataUri Contact uri which will be accessible as intent data. * @param activityResult {@link Activity#RESULT_OK} or {@link Activity#RESULT_CANCELED}. * @param extraMatcher Additional intent matcher to match specific requests. * * @since Espresso Macchiato 0.6 */ public static void register(Uri contactDataUri, int activityResult, Matcher<? super Intent> extraMatcher) { Intent contactData = new Intent(); contactData.setData(contactDataUri); intending(allOf(hasAction(Intent.ACTION_PICK), extraMatcher)).respondWith(new Instrumentation.ActivityResult(activityResult, contactData)); } }
public void test002NocodeScanned() throws InterruptedException { Intents.init(); // Build a result to return from the ZXING app Intent resultData = new Intent(); Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, resultData); // Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond // with the ActivityResult we just created intending(hasAction("com.google.zxing.client.android.SCAN")).respondWith(result); // Now that we have the stub in place, click on the button in our app that launches into the Camera onView(withId(R.id.action_scan)).perform(click()); // We can also validate that an intent resolving to the "camera" activity has been sent out by our app intended(hasAction("com.google.zxing.client.android.SCAN")); onView(withText("No account has been added yet")).check(matches(isDisplayed())); Intents.release(); }
@Test public void testLinksClickable() throws Exception { Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(EXPECTED_URL)); intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null)); onView(withId(R.id.text)).perform(openLinkWithText(LINK_TEXT)); intended(expectedIntent); }
/** * Register new stub for camera picture requests with {@link Activity#RESULT_OK}. * * @param pictureLocation Picture uri which will be accessible as intent data. * * @since Espresso Macchiato 0.6 */ public static void register(Uri pictureLocation) { Intent resultData = new Intent(); resultData.setData(pictureLocation); intending(allOf(hasAction(Intent.ACTION_GET_CONTENT), hasType(MediaType.ANY_IMAGE_TYPE.toString()))).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)); } }
/** * Register new stub for camera picture requests with {@link Activity#RESULT_OK}. * * @param pictureLocation Picture uri which will be accessible as intent data. * * @since Espresso Macchiato 0.6 */ public static void register(Uri pictureLocation) { Intent resultData = new Intent(); resultData.setData(pictureLocation); intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)); } }
@Test public void SecondActivityResultHandler_Failure() { intending(hasAction(equalTo(Intent.ACTION_GET_CONTENT))) .respondWith(new ActivityResult(Activity.RESULT_CANCELED, null)); onView(withText(R.string.get_photo_uri)).perform(click()); onView(withId(R.id.contact_uri)).check(matches(withText(R.string.pick_a_contact))); onView(withId(R.id.photo_uri)).check(matches(withText(R.string.an_error_has_occurred))); } }