Espresso和Android联系人选择器

编程入门 行业动态 更新时间:2024-10-25 14:24:11
本文介绍了Espresso和Android联系人选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用Espresso的Android联系人选择器添加联系人,但这不起作用。

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

当我运行Espresso测试时显示联系人选择器。确定,现在我尝试通过显示名称(例如Jake)选择特定的联系人条目。不幸的是,我不知道如何完成这一点。我试过以下:

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());

我也试过这个变化:

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.

任何想法?

推荐答案

您遇到的是正常的行为,因为联系人选择器属于外部活动,其用户界面无法操作。尝试声明任何东西将导致测试停顿一段时间,最后是一个

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:没有RESUMED阶段的活动。您是否忘记启动活动。 (test.getActivity()或类似的)?

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

但是,请问你出生的 Espresso-Intents ,这是为了保存我的声誉:

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

使用想要的API(Mockito.when的表弟),您可以为使用startActivityForResult启动的活动提供响应

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

UPDATE 下面是我当前的解决方案,但是需要一些不错的代码清理: / p>

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

在启动活动中,我将使用联系人Uri处理传入意图

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

此外,辅助方法,

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

更多推荐

Espresso和Android联系人选择器

本文发布于:2023-10-04 02:39:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467105.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:联系人   选择器   Espresso   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!