@Override public ActivityResult getActivityResult() { checkNotNull(controller); checkState(controller.get().isFinishing(), "You must finish your Activity first"); ShadowActivity shadowActivity = Shadow.extract(controller.get()); return new ActivityResult(shadowActivity.getResultCode(), shadowActivity.getResultIntent()); }
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); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); activityResult = new ActivityResult(resultCode, data); } }
/** * 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)); } }
/** * 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 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); }
private static Instrumentation.ActivityResult createResult(String[] permissions, int[] grantPermissions) { Intent data = new Intent(); data.putExtra(EXTRA_REQUEST_PERMISSIONS_NAMES, permissions); data.putExtra(EXTRA_REQUEST_PERMISSIONS_RESULTS, grantPermissions); return new Instrumentation.ActivityResult(Activity.RESULT_OK, data); }
@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))); } }
@Test public void SecondActivityResultHandler() { intending(hasAction(equalTo(Intent.ACTION_GET_CONTENT))) .respondWith(new ActivityResult(Activity.RESULT_OK, successIntent)); 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(FAKE_VALUE))); }
/** * 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)); } }
@Test public void FirstActivityResultHandler_Failure() { intending(hasAction(equalTo(Intent.ACTION_PICK))) .respondWith(new ActivityResult(Activity.RESULT_CANCELED, null)); onView(withText(R.string.get_contact_uri)).perform(click()); onView(withId(R.id.contact_uri)).check(matches(withText(R.string.an_error_has_occurred))); onView(withId(R.id.photo_uri)).check(matches(withText(R.string.pick_a_photo))); }
@Test public void FirstActivityResultHandler() { intending(hasAction(equalTo(Intent.ACTION_PICK))) .respondWith(new ActivityResult(Activity.RESULT_OK, successIntent)); onView(withText(R.string.get_contact_uri)).perform(click()); onView(withId(R.id.contact_uri)).check(matches(withText(FAKE_VALUE))); onView(withId(R.id.photo_uri)).check(matches(withText(R.string.pick_a_photo))); }
@Override public ActivityResult getActivityResult() { checkNotNull(controller); checkState(controller.get().isFinishing(), "You must finish your Activity first"); ShadowActivity shadowActivity = Shadow.extract(controller.get()); return new ActivityResult(shadowActivity.getResultCode(), shadowActivity.getResultIntent()); }
private void stubAccountPickerIntent() { // Stub the account picker using Espresso intents. // It requires the test devices to be signed in into at least 1 Google account. Intent data = new Intent(); data.putExtra(AccountManager.KEY_ACCOUNT_NAME, mSelectedAccount.name); ActivityResult result = new ActivityResult(Activity.RESULT_OK, data); // The account picker intent is a bit special and it doesn't seem to have an Action or // package, so we have to match it by some of the extra keys. // This is not ideal but I couldn't find a better way. intending(hasExtraWithKey("allowableAccountTypes")) .respondWith(result); }
@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"); }); } }
@Test public void pickContactButton_click_SelectsPhoneNumber() { // Stub all Intents to ContactsActivity to return VALID_PHONE_NUMBER. Note that the Activity // is never launched and result is stubbed. intending(hasComponent(hasShortClassName(".ContactsActivity"))) .respondWith(new ActivityResult(Activity.RESULT_OK, ContactsActivity.createResultData(VALID_PHONE_NUMBER))); // Click the pick contact button. onView(withId(R.id.button_pick_contact)).perform(click()); // Check that the number is displayed in the UI. onView(withId(R.id.edit_text_caller_number)) .check(matches(withText(VALID_PHONE_NUMBER))); } }
@Test public void til() { onView(withTILHint("URL")) .perform(typeText(URL), closeSoftKeyboard()); Instrumentation.ActivityResult result= new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, null); intending(hasAction(Intent.ACTION_VIEW)).respondWith(result); onView(withId(R.id.browse)).perform(click()); intended(allOf(hasAction(Intent.ACTION_VIEW), hasData(URL))); }
@Test public void canceledPick() { Instrumentation.ActivityResult result= new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, null); intending(hasAction(Intent.ACTION_PICK)).respondWith(result); onView(withId(R.id.pick)).perform(click()); intended(allOf( hasAction(Intent.ACTION_PICK), hasData(ContactsContract.Contacts.CONTENT_URI))); onView(withId(R.id.view)).check(matches(not(isEnabled()))); }
@Before public void stubAllExternalIntents() { // By default Espresso Intents does not stub any Intents. Stubbing needs to be setup before // every test run. In this case all external Intents will be blocked. intending(not(isInternal())).respondWith(new ActivityResult(Activity.RESULT_OK, null)); }
@Test public void stubPick() { Instrumentation.ActivityResult result= new Instrumentation.ActivityResult(Activity.RESULT_OK, new Intent(null, ContactsContract.Contacts.CONTENT_URI)); intending(hasAction(Intent.ACTION_PICK)).respondWith(result); onView(withId(R.id.pick)).perform(click()); intended(allOf( hasAction(Intent.ACTION_PICK), hasData(ContactsContract.Contacts.CONTENT_URI))); onView(withId(R.id.view)).check(matches(isEnabled())); }