Espresso and Android contact picker

谁都会走 提交于 2019-12-02 11:04:23

问题


I try to add a contact with an Android contact picker by Espresso, but this does not work.

This is the command to invoke the contact picker:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, RC_PICK_CONTACT);

The contact picker is shown, when I run Espresso test. OK, now I try to select a specific contact entry by display name (e.g. "Jake"). Unfortunately I don't know how to accomplish this. I've tried the following:

onData(withItemContent("Jake")).inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))).perform(click()); 

I also tried this variation:

onView(withText("Jake")).inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))).perform(click());

No success with both approaches. As already mentioned the contact picker is shown, but nothing is selected.

Any idea?


回答1:


What you're experiencing is normal behavior, since the contact picker belongs to an external activity, whose user interface cannot be manipulated. Trying to assert anything will result in the tests stalling for some time and ending up with a

android.support.test.espresso.NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)?

However, say hello to the new born Espresso-Intents, which is here to save my reputation:

Using the intending API (cousin of Mockito.when), you can provide a response for activities that are launched with startActivityForResult

UPDATE Below is my current solution which works fine but would need some decent code clean up:

    @Test
    public void testContactPickerResult(){

        Intent resultData = new Intent();
        resultData.setData(getContactUriByName("Joah"));
        Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);

        intending(toPackage("com.google.android.contacts")).respondWith(result);

        onView(withId(R.id.contactPickerLauncher))
                .check(matches(isDisplayed()))
                .perform(click());

        onView(withId(R.id.pickedContact))
                .check(matches(withText(getContactNumberByName("Joah"))));
    }

In the launching activity, I would handle the incoming intent with the contact Uri and do whatever is necessary with it.

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        TextView result = (TextView) findViewById(R.id.pickedContact);

        if (requestCode == 42 && resultCode == RESULT_OK){
            Uri contactUri = data.getData();             
            String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
            Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
            cursor.moveToFirst();
            int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(column);
            result.setText(number);
        }
    }

Also, the helper methods, to be modified accordingly:

public Uri getContactUriByName(String contactName) {
        Cursor cursor = mActivityRule.getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                if (name.equals(contactName)) {
                    return Uri.withAppendedPath(ContactsContract.Data.CONTENT_URI, id);
                }
            }
        }
        return null;
    }


来源:https://stackoverflow.com/questions/27589635/espresso-and-android-contact-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!