下面列出了怎么用android.support.test.espresso.intent.matcher.IntentMatchers的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void cleanTaskFlag() {
onView(withText("ClearTask")).perform(ViewActions.click());
//测试是否对应的Intent被发送
intended(allOf(
IntentMatchers.hasAction("hhhh"),
hasComponent(hasShortClassName(".intenttest.IntentTestActivity")),
IntentMatchers.hasFlag(Intent.FLAG_ACTIVITY_CLEAR_TASK)));
}
@Test
public void cleanTopFlag() {
onView(withText("ClearTop")).perform(ViewActions.click());
//测试是否对应的Intent被发送
intended(allOf(
hasComponent(hasShortClassName(".intenttest.IntentTestActivity")),
IntentMatchers.hasFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
}
@Test
public void testContactPickerStubWithExtraMatcher() {
Uri dummyContactDataUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, CONTACT_ID);
EspContactStub.register(dummyContactDataUri, Activity.RESULT_OK, IntentMatchers.hasExtraWithKey("MyKey"));
Intent contactPickerIntent = createContactPickerIntent();
contactPickerIntent.putExtra("MyKey", "myValue");
activity.startForResult(contactPickerIntent, REQUEST_CODE);
requestCodeTextView.assertTextIs(String.valueOf(REQUEST_CODE));
resultCodeTextView.assertTextIs(String.valueOf(Activity.RESULT_OK));
dataTextView.assertTextIs(dummyContactDataUri.toString());
}
@Test
@Ignore("result will be delivered in tear down, after all checks are done")
public void testWhenNotMatchingExtraMatcher() {
Uri dummyContactDataUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, CONTACT_ID);
EspContactStub.register(dummyContactDataUri, Activity.RESULT_OK, IntentMatchers.hasExtraWithKey("MyKey"));
activity.startForResult(createContactPickerIntent(), REQUEST_CODE);
requestCodeTextView.assertTextIs("");
resultCodeTextView.assertTextIs("");
dataTextView.assertTextIs("");
}
@Test
public void shouldHaveBrowserAppInstalled() {
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
prepareBrowserApp(true, intentCaptor);
boolean hasBrowserApp = WebAuthProvider.hasBrowserAppInstalled(activity.getPackageManager());
MatcherAssert.assertThat(hasBrowserApp, Is.is(true));
MatcherAssert.assertThat(intentCaptor.getValue(), Is.is(IntentMatchers.hasAction(Intent.ACTION_VIEW)));
MatcherAssert.assertThat(URLUtil.isValidUrl(intentCaptor.getValue().getDataString()), Is.is(true));
}
@Test
public void shouldNotHaveBrowserAppInstalled() {
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
prepareBrowserApp(false, intentCaptor);
boolean hasBrowserApp = WebAuthProvider.hasBrowserAppInstalled(activity.getPackageManager());
MatcherAssert.assertThat(hasBrowserApp, Is.is(false));
MatcherAssert.assertThat(intentCaptor.getValue(), Is.is(IntentMatchers.hasAction(Intent.ACTION_VIEW)));
MatcherAssert.assertThat(URLUtil.isValidUrl(intentCaptor.getValue().getDataString()), Is.is(true));
}
@Override
protected boolean checkShareWentOK() {
intended(allOf(
hasAction(Intent.ACTION_SEND),
IntentMatchers.hasExtra(Intent.EXTRA_SUBJECT, mActivityRule.getActivity().getString(R.string.share_title))));
return true;
}
@Override
public void testThatDefaultBehaviorIsWorking() throws Exception {
Instrumentation.ActivityResult dummyResult = new Instrumentation.ActivityResult(0, null);
Intents.intending(allOf(
hasAction(Intent.ACTION_SEND),
IntentMatchers.hasExtra(Intent.EXTRA_SUBJECT, mActivityRule.getActivity().getString(R.string.share_title))))
.respondWith(dummyResult);
super.testThatDefaultBehaviorIsWorking();
}
@Test
public void showLoginActivity_if_not_signIn() {
Mockito.when(accountDaemon.hasAccount()).thenReturn(false);
activityRule.launchActivity(new Intent());
intended(IntentMatchers.hasComponent(LoginActivity.class.getName()));
}