在Android的ListView与ActivityUnitTestCase测试点击 [英] Testing a click on an Android ListView with ActivityUnitTestCase

查看:133
本文介绍了在Android的ListView与ActivityUnitTestCase测试点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个的ListView 启动一个新的活动列表项被点击时。当我手动测试,它完美的作品。但是,当我尝试做 ActivityUnitTestCase 自动测试,得到了一个 NullPointerException异常好像的ListView 是空的。

I have implemented a ListView that starts a new Activity when a list item is clicked. When I test it manually, it works perfectly. But when I try to do an automated test with ActivityUnitTestCase, I get a NullPointerException as though the ListView was empty.

MainActivity(部分):

MainActivity (partial):

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

测试code失败:

The test code that fails:

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)返回。为什么的ListView 空?我怎样才能迫使它用正确的儿童进行自我更新?

Both test methods fail because listView.getChildAt(0) returns null. Why is the ListView empty? How can I force it to update itself with the right children?

推荐答案

根据该的文档,如果preFER功能测试,你应该使用ActivityInstrumentationTestCase2。要执行单击它工作得更好。该@fewe答案是正确的,你必须等待列表中可以得出,等待加载你可以使用getInstrumentation()片段waitForIdleSync();

According to the documentation, If you prefer a functional test you should use ActivityInstrumentationTestCase2. To perform a click it works better. The @fewe answer is right, you have to wait the list to be drawn, to wait the fragment to be loaded you can use 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();

然后,你不能测试,例如,如果加载的片段。

Then you cant test, for example, if a fragment was loaded.

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

这篇关于在Android的ListView与ActivityUnitTestCase测试点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆