Espresso and Android contact picker
Asked Answered
L

1

2

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?

Lenwood answered 21/12, 2014 at 13:4 Comment(1)
you have a complete answer now, let us know if it works for you and don't forget to accept it if it's helpful.Spectroheliograph
S
3

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;
    }
Spectroheliograph answered 28/4, 2015 at 15:45 Comment(1)
The mocked intent didn't work for me. This is presumably because of the intent I'm using to bring up the contact picker. I create the intent with "new Intent(Intent.ACTION_PICK)", and accordingly what does work to mock the intent is "intending(hasAction(Intent.ACTION_PICK)).respondWith(result);".Clardy

© 2022 - 2024 — McMap. All rights reserved.