在意图中使用 Robotium [英] Using Robotium with intents

查看:39
本文介绍了在意图中使用 Robotium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个列表视图活动的应用程序,第一个列表视图中的选择决定了第二个列表视图的内容,第二个列表视图确定了第三个列表视图的内容,依此类推.

I have an application with several activities with list views, the selection from the first list view determines the content of the second list view, and the second list view determines the contents of the third, etc.

我想测试第三个列表视图,但由于它需要一个意图,因此该列表不返回任何内容.为了解决这个问题,我可以手动将意图添加到测试中,这确实意味着它有效

I want to test the third list view, but as it requires an intent the list returns nothing. To remedy this I can manually add the intent to the test which does mean it works

public InspectionListActivityTest() {
    super(InspectionListActivity.class);
    Intent i = new Intent();
    i.putExtra("guid", "abcbbf2b-5e14-4cb1-af1b-e3084b45d4cf");
    setActivityIntent(i);
}

正如您从代码中看到的,它使用 guid 来确定我想要避免的列表 - 我在测试时清除了很多数据库,因此我必须一直更改此字段.

As you can see from the code, it uses guids to determine the list which is what I want to avoid - I clear the database a lot while I'm testing so I have to change this field all of the time.

理想情况下,我想使用 ContentResolver 从另一个表中获取第一个 guid,这意味着我可以始终在测试中提取信息,即

Ideally I want to use a ContentResolver to get the first guid from another table which would then mean I would be able to always pull back information in my tests, ie

public InspectionListActivityTest() {
    super(InspectionListActivity.class);

    ContentResolver cr = getActivity().getContentResolver();
    Cursor cursor = cr.query(Locations.CONTENT_URI, null, null, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            String guid = cursor.getString(cursor.getColumnIndex(Locations.GUID));
            Intent i = new Intent();
            i.putExtra(IntentFilters.LOCATION.getIntent(), guid);
            setActivityIntent(i);
        }
    }
}

但是,我在 getActivity() 方法上得到了 nullpointerexception,而且我似乎无法将此 setActivityIntent 放在其他任何地方.

However, I get a nullpointerexception on the getActivity() method, and I don't seem to be able to put this setActivityIntent anywhere else.

推荐答案

可以做到,但是有点乱.基本上从我想要的数据库中获取了 guid,为原始测试类创建了一个新意图,将 guid 附加到意图,然后启动了意图.

It can be done, but it's a bit messy. Basically got the guid from the database that I wanted, created a new intent to the original test class, attached the guid to the intent and then started the intent.

public void setUp() throws Exception {
     super.setUp(); 

    solo = new Solo(getInstrumentation(), getActivity());

    activity = getActivity();

    UsefulFunctions.insertDummyData(getActivity());

    ContentResolver cr = getActivity().getContentResolver();
    Cursor cursor = cr.query(Locations.CONTENT_URI, null, null, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            guid = cursor.getString(cursor.getColumnIndex(Locations.GUID));

        }
    }

    solo.goBack(); 

    Intent i = new Intent(activity.getApplicationContext(), InspectionListActivity.class);
    i.putExtra(IntentFilters.LOCATION.getIntent(), guid);
    setActivityIntent(i);
    activity.startActivity(i); 


}

在某种程度上,从我的第一个列表开始,然后让 Robotium 在列表中单击"一直到我想要的屏幕更容易,即

In a way, it was just easier to start on my first list and then get Robotium to 'click' in the list all the way to the screen that I wanted, i.e.

solo.clickInList(0);

// Locations
solo.clickInList(0);

ListView ls = solo.getCurrentListViews().get(0);

solo.waitForActivity("InspectionListActivity");

这篇关于在意图中使用 Robotium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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