- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在为一个简单的 Android 应用程序(这是一个学校项目)编写测试,但我在测试扩展 Android 的 ListActivity 的 Activity ContactListActivity 时遇到了问题。
单击 ContactListActivity 的 ListView 中的第一项并检查 ContactDetailActivity 是否已启动。
列表数据来自 SQLite 数据库。为了进行测试,我将测试数据加载到 ListView 中,因此测试不会使用实时数据。加载测试数据工作正常。在运行测试时观察模拟器,我可以看到正在启动的 Activity 和列表中出现的测试数据。但是,尝试访问第一个(也是唯一一个)列表项失败。
@UiThreadTest
public final void testLoadContactDetail() {
ListView list = activity.getListView();
assertNotNull(list);
ContactsListAdapter adapter = new ContactsListAdapter(
getInstrumentation().getContext(),
createData() // Creates an ArrayList of test data
);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
// list.getAdapter().getCount() is expectedly 1
// list.getChildCount() is unexpectedly 0
assertNotNull(list.getChildAt(0)); // Assertion fails
// (...)
}
可以看出,我正在使用 @UIThreadTest
注释测试,以便能够操作 View 元素。使用测试数据创建一个新的 ListAdapter 并将其设置为列表。然后,adapter.notifyDataSetChanged()
确保列表知道新数据。
如何将 ActivityInstrumentationTestCase2 中的测试数据加载到 ListView 中,以便数据不仅显示在屏幕上,而且实际上“在那里”,这意味着可以使用 list.getChildAt( 0)
并被点击?
public class ContactListActivityFunctionalTest extends
ActivityInstrumentationTestCase2<ContactListActivity> {
private ContactListActivity activity;
public ContactListActivityFunctionalTest() {
super(ContactListActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
activity = getActivity();
}
protected void tearDown() throws Exception {
super.tearDown();
}
@UiThreadTest
public final void testLoadContactDetail() {
ListView list = activity.getListView();
assertNotNull(list);
ContactsListAdapter adapter = new ContactsListAdapter(
getInstrumentation().getContext(),
createData()
);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
assertNotNull(list.getChildAt(0));
// Anything beyond this point is never executed,
// because the above assertion fails, and I have no idea
// if this test code is correct at all.
ActivityMonitor monitor = getInstrumentation().addMonitor(
ContactDetailActivity.class.getName(), null, false
);
TouchUtils.clickView(this, list.getChildAt(0));
ContactDetailActivity contactDetailActivity =
(ContactDetailActivity)monitor.waitForActivityWithTimeout(2000);
assertNotNull(contactDetailActivity);
assertTrue(getInstrumentation().checkMonitorHit(monitor, 1));
contactDetailActivity.finish();
}
private List<ContactInterface> createData() {
ContactInterface contact = new Contact();
contact.setId(1L);
contact.setName("Unit Test").setPhone("0123456789").setPosition(3);
List<ContactInterface> contacts = new ArrayList<ContactInterface>();
contacts.add(contact);
return contacts;
}
}
最佳答案
看起来 listView.getChildAt
方法返回可见 View 。 https://stackoverflow.com/a/6767006/693752
所以,我的猜测是该项目还不可见。没有一个是因为 getChildCount 返回 0。也许你应该:
关于android - 如何用测试数据测试一个ListActivity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708200/
我是一名优秀的程序员,十分优秀!