android - Testing using Espresso -


how can write test cases following code using espresso. have following code executed when clicked on icon. know can check launch of intent using intended(topackage(.... , possibly mocking result of intent launched if launched via startactivityforresult how handle case.

try {  intent = new intent(intent.action_dial);  intent.setdata(uri.parse("tel:" +"xxxxxx"); // 12 digit mobile no     if (intent.resolveactivity(context.getpackagemanager()) != null) {                         startactivity(intent)       }     } catch (exception e) {     toast.maketext(getactivity(), "no phone number available", toast.length_short).show();                 } 

an answer use 'intended' method after test code verify activity result meets requirements. looks this:

@test public void typenumber_validinput_initiatescall() {     // types phone number dialer edit text field , presses call button.     onview(withid(r.id.edit_text_caller_number))             .perform(typetext(valid_phone_number), closesoftkeyboard());     onview(withid(r.id.button_call_number)).perform(click());      // verify intent dialer sent correct action, phone     // number , package. think of intents intended api equivalent mockito's verify.     intended(allof(             hasaction(intent.action_call),             hasdata(intent_data_phone_number),             topackage(package_android_dialer))); } 

however, part of automated test, you'll need stub out response activity can run without actual blocking user input required. you'll need set intent stubbing before running tests:

@before     public void stuballexternalintents() {         // default espresso intents not stub intents. stubbing needs setup before         // every test run. in case external intents blocked.         intending(not(isinternal())).respondwith(new activityresult(activity.result_ok, null));     } 

then can write corresponding part of test this:

@test     public void pickcontactbutton_click_selectsphonenumber() {         // stub intents contactsactivity return valid_phone_number. note activity         // never launched , result stubbed.         intending(hascomponent(hasshortclassname(".contactsactivity")))                 .respondwith(new activityresult(activity.result_ok,                         contactsactivity.createresultdata(valid_phone_number)));          // click pick contact button.         onview(withid(r.id.button_pick_contact)).perform(click());          // check number displayed in ui.         onview(withid(r.id.edit_text_caller_number))                 .check(matches(withtext(valid_phone_number)));     } 

if need verify actual user input application (like phone dialer), outside scope of espresso. since work vendor helps these kinds of cases, hesitate name names , tools, lots of people need write tests simulate real end-to-end experience.

mike evans has great write-up testing intents here , there's android documentation here.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -