2015-11-04 59 views
1

在我的應用程序中,我有一個按鈕彈出一個對話框「Call xxxx-xxxx」Yes/No。點擊Yes後,應該調用該號碼。Android Espresso 2結束通話

這是測試代碼:

@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)))); 
} 

一切工作正常,但我怎麼能取消測試後的電話?

yogurtearls我answer工作,感謝:

@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)))); 
} 

回答

2

您應該使用Intent stubbing。 您可以避免實際調出撥號程序,而是檢查是否發送了正確的意圖。

在單擊yes按鈕之前,請調用intending。