Android Espresso 2 结束 phone 通话

Android Espresso 2 end phone call

在我的应用程序中,我有一个弹出对话框的按钮"Call xxxx-xxxx"是/否。单击是后,将呼叫该号码。

这是测试代码:

@Test
public void testPhoneButton() {
    clickContactTab();

    ViewInteraction phoneButtonInteraction = Espresso.onView(ViewMatchers.withId(R.id.button_phone));
    phoneButtonInteraction.perform(ViewActions.scrollTo());
    phoneButtonInteraction.perform(ViewActions.click());
    Espresso.onView(ViewMatchers.withText(R.string.dialog_phone_title)).inRoot(RootMatchers.isDialog()).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withId(android.R.id.button2)).perform(ViewActions.click());
    Intents.assertNoUnverifiedIntents();
    phoneButtonInteraction.perform(ViewActions.click());
    Espresso.onView(ViewMatchers.withId(android.R.id.button1)).perform(ViewActions.click());
    Intents.intended(Matchers.allOf(IntentMatchers.hasAction(Intent.ACTION_CALL), IntentMatchers.hasData(Uri.parse("tel:" + tel))));
}

一切正常,但是测试后如何取消phone调用?

yogurtearls 适合我,谢谢:

@Test
public void testPhoneButton() {
    clickContactTab();

    ViewInteraction phoneButtonInteraction = Espresso.onView(ViewMatchers.withId(R.id.button_phone));
    phoneButtonInteraction.perform(ViewActions.scrollTo());
    phoneButtonInteraction.perform(ViewActions.click());
    Espresso.onView(ViewMatchers.withText(R.string.dialog_phone_title)).inRoot(RootMatchers.isDialog()).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withId(android.R.id.button2)).perform(ViewActions.click());
    Intents.assertNoUnverifiedIntents();

    phoneButtonInteraction.perform(ViewActions.click());
    Intent stubIntent = new Intent();
    Instrumentation.ActivityResult stubResult = new Instrumentation.ActivityResult(Activity.RESULT_OK, stubIntent);
    Intents.intending(IntentMatchers.hasAction(Intent.ACTION_CALL)).respondWith(stubResult);
    Espresso.onView(ViewMatchers.withId(android.R.id.button1)).perform(ViewActions.click());
    Intents.intended(Matchers.allOf(IntentMatchers.hasAction(Intent.ACTION_CALL), IntentMatchers.hasData(Uri.parse("tel:" + tel))));
}

你应该使用 Intent stubbing。 您可以避免实际调出拨号程序,而是检查是否发送了正确的意图。

在单击是按钮之前,调用 intendING 。