如何测试ListActivity? [英] How to test a ListActivity?

查看:93
本文介绍了如何测试ListActivity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid开发以及测试驱动开发。我想写以下ListActivity单元测试:

I'm new to Android development as well as test-driven development. I want to write unit tests for the following ListActivity:

public class TrendsMainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    String[] list_items = getResources().getStringArray(R.array.trend_menu_names);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.main, list_items));
}

@Override
protected void onListItemClick(ListView listView, View view, int position, long id)
{
    Intent intent = null;

    switch(position)
    {
        case 0:
            intent = new Intent(this, TrendingActivity.class);
            break;
        case 1:
            intent = new Intent(this, SearchActivity.class);
            break;
        case 2:
            intent = new Intent(this, TimelineActivity.class);
            break;
    }
    if(intent != null)
    {
        startActivity(intent);
    }
    else
    {
        Log.e(getClass().getSimpleName(), "There was an error retrieving request.");
    }
}}

我已经走遍所有我能找到的文件,但我无法弄清楚如何测试这个活动。该onListItemClick方法还没有完成,但它给了什么我要完成的想法。我想测试一下在ListView中的第一项,并测试了正确的活动正在启动。

I have scoured all of the documentation that I can find, but I can not figure out how to test this Activity. The onListItemClick method is not finished, but it gives the idea of what I want to accomplish. I want to test clicking the first item in the ListView, and test that the correct Activity is being started.

我怎样才能做到这一点?

How can I accomplish this?

编辑:我想我的测试点击关于在ListView的项目。那么我想断言活动开始是正确的行为(例如点击ListView项0开始TrendingActivity专)

I want my test to "click" on an item in the ListView. I then want to assert that the activity started is the correct activity (e.g. Clicking ListView item 0 starts the TrendingActivity specifically)

推荐答案

我说,如果你在申请TDD你应该开始写测试而不是应用程序。

I should say that if you were applying TDD you should have started writing the tests not the application.

总之, Android应用程序测试指南在第三章中包含3两个例子结合起来能够给你你正在寻找解决方案。该想法是使用一个<一href=\"http://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor.html\">ActivityMonitor验证预期活动开始

Anyway, Android Application Testing Guide contains in chapter 3 two examples that combined together can give you the solution you are looking for. The idea is to use an ActivityMonitor to verify that the expected activity was started.

@UiThreadTest
public void testListItemClickStartsActivity() {
    final Instrumentation inst = getInstrumentation();
    final IntentFilter intentFilter = new IntentFilter();
    // here add conditions to your filter, i.e. intentFilter.addAction()
    ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);
    assertEquals(0, monitor.getHits());
    // here perform desired click on list
    monitor.waitForActivityWithTimeout(5000);
    assertEquals(1, monitor.getHits());
    inst.removeMonitor(monitor);
}

这篇关于如何测试ListActivity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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