gpt4 book ai didi

android - 使用 ActivityUnitTestCase 测试对 Android ListView 的点击

转载 作者:太空狗 更新时间:2023-10-29 14:19:08 24 4
gpt4 key购买 nike

我实现了一个 ListView,它在单击列表项时启动一个新的 Activity。当我手动测试它时,它工作得很好。但是,当我尝试使用 ActivityUnitTestCase 进行自动化测试时,我得到了一个 NullPointerException,就好像 ListView 是空的一样。

MainActivity(部分):

public class MainActivity extends ListActivity {

@Override
protected void onResume() {
super.onResume();

String[] items = new String[] {"item 1", "item 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
setListAdapter(adapter);

getListView().setOnItemClickListener(new OnItemClickListener () {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView clickedItem = (TextView) view;
CharSequence clickedItemText = clickedItem.getText(); // throws a NullPointerException when running testWithPerformItemClick()!
Intent intent = new Intent(MainActivity.this, ItemDisplayActivity.class);
intent.putExtra("parameter", clickedItemText);
startActivity(intent);
}
});
}
}

失败的测试代码:

public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {

ListView listView;
View child0;

public MainActivityTest() {
super(MainActivity.class);
}

private void setUpTest() {
MainActivity activity = startActivity(new Intent(), null, null);
getInstrumentation().callActivityOnStart(activity);
getInstrumentation().callActivityOnResume(activity);
listView = (ListView) activity.findViewById(android.R.id.list);
child0 = listView.getChildAt(0); // returns null!
}

public void testWithPerformClick() {
setUpTest();

child0.performClick(); // throws a NullPointerException!

Intent startedIntent = getStartedActivityIntent();
assertEquals("item 1", startedIntent.getStringExtra("parameter"));
}

public void testWithPerformItemClick() {
setUpTest();
long itemId = listView.getAdapter().getItemId(0);

listView.performItemClick(child0, 0, itemId); // throws a NullPointerException!

Intent startedIntent = getStartedActivityIntent();
assertEquals("item 1", startedIntent.getStringExtra("parameter"));
}

两种测试方法均失败,因为 listView.getChildAt(0) 返回 null。为什么 ListView 是空的?我怎样才能强制它用正确的 child 更新自己?

最佳答案

根据documentation ,如果您更喜欢功能测试,您应该使用 ActivityInstrumentationTestCase2。要执行单击,效果会更好。 @fewe 的回答是正确的,你必须等待列表被绘制,等待 fragment 被加载你可以使用 getInstrumentation().waitForIdleSync();

尝试这样的事情。

public class ActivityTests extends
ActivityInstrumentationTestCase2<Activity>

final ListView list = (ListView) mActivity.findViewById(R.id.listId);
assertNotNull("The list was not loaded", list);
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {

list.performItemClick(list.getAdapter().getView(position, null, null),
position, list.getAdapter().getItemId(position));
}

});

getInstrumentation().waitForIdleSync();

那么你就不能测试,例如,如果一个 fragment 被加载了。

  mFragment frag = mActivity.getFragment();
assertNotNull("Fragment was not loaded", frag);

关于android - 使用 ActivityUnitTestCase 测试对 Android ListView 的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18547077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com