Well, I figured it out, pretty sure other developers will find it useful.
First of all, to enable testing interaction with People application you can prefill the contact data with related Intent fields:
public static void addAsContactConfirmed(final Context context, final Person person) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, person.name);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, person.mobile);
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, person.email);
intent.putExtra("finishActivityOnSaveCompleted", true); // this line enables instant return People to our app
context.startActivity(intent);
}
Then you need to press back button, and this is the tricky part:
- You can't just finish(), because it is not your activity, and even not the same task
- You can't access any views of People app by the same reason
- You cry a lot, because you don't know, what to do
But here I am with the solution. You need to use UiAutomation
which can be obtained from instrumentation by calling final UiAutomation uiAutomation = getInstrumentation().getUiAutomation();
.
Then you can't send any key events to another application, because it requires INJECT_EVENTS permission, which needs system signature, which your application apparently lacks :)
So, call this one:
uiAutomation.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
It does not require any permissions and just work across application. Be sure you run your test above Android 4.3.
Additional link.