如何在JUnit测试中模拟用户点击ListView项?

5
我正在尝试编写一个junit测试用例,来选择列表项并意图到下一个活动,但我不知道如何通过junit编码模拟此用户操作。有人可以帮忙吗?
另外,我想问一下,是否有任何教授在junit中模拟不同用户操作的功能或语法的材料?
以下是我学校教程笔记中的示例,我想做类似于这个示例,但是在列表视图项上。
public void testKilosToPounds() { 

 /* INTERACTIONS */ 
 TouchUtils.tapView(this, textKilos); // tap the EditText textKilos 
 sendKeys("1"); // sent the number 1 
 TouchUtils.clickView(this, buttonPounds); // click the button buttonPounds 

 /*CHECK THE RESULT*/ 
 double pounds; 
 try { 
 pounds = Double.parseDouble(textPounds.getText().toString()); 
 } catch (NumberFormatException e) { 
 pounds = -1; 
 } 

 //JUnit Assert equals 

 // message expected actual delta for comparing doubles 
 assertEquals("1 kilo is 2.20462262 pounds", 2.20462262, pounds, DELTA); 
 }

你是在使用仪器测试还是像Robolectric这样的东西? - marcus.ramsden
我想通过编码执行一些用户操作,希望这个链接可以帮助你理解我的问题。http://developer.android.com/reference/android/test/TouchUtils.html@marcus.ramsden - Chung
啊,好的,我对TouchUtils没有太多经验,但是使用TouchUtils的替代方法可能是AbsListView.performItemClick方法(http://developer.android.com/reference/android/widget/AbsListView.html#performItemClick(android.view.View, int, long))。只需在您的测试用例中获取列表视图,然后在其上调用该方法即可。 - marcus.ramsden
我编辑了我的问题并添加了一些代码,希望能有所帮助。@marcus.ramsden - Chung
好的,你给了我另一种解决方案,我稍后会尝试。谢谢。 - Chung
显示剩余2条评论
2个回答

2

我最近几个月一直在使用JUnit测试安卓应用程序。现在,我已经能够测试几乎所有的东西,如Web服务和视图。无论如何,我要分享我的代码来测试带有项目点击的ListView,并在下一个活动(InfoActivity)中检查使用意图发送的数据。InfoActivity是我从ListActivity发送所选项目的数据的活动。

public class ListActivityTest extends ActivityInstrumentationTestCase2<ListActivity> {

private Activity activity;
private ListView lv;
private InfoActivity contextInfoActivity;
private TextView tvInfo;

public  ListActivityTest(){
    super(ListActivity.class);
}



@Override
protected void setUp() throws Exception {
    super.setUp();
    activity = (ListActivity)getActivity();
    lv = (ListView)activity.findViewById(R.id.lv);

}

public void testCase1(){
    assertNotNull(activity);
    assertNotNull(lv);
}

public void testCase2(){

    Instrumentation instrumentation = getInstrumentation();
    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(InfoActivity.class.getName(), null, false);

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {

            lv.performItemClick(lv,4,0);
            //lv is listview,4 is item position,0 is default id
        }
    });

    Activity currentActivity = getInstrumentation().waitForMonitor(monitor);
    contextInfoActivity = (InfoActivity) currentActivity;

    assertNotNull(contextInfoActivity);
    tvInfo = (TextView)contextInfoActivity.findViewById(R.id.tvInfo);

    assertNotNull(tvInfo);
    assertEquals("Karan",tvInfo.getText().toString());
    //karan is name at position 4 in listview and i am checking it with name set in textview of next activity i.e infoActivity.

}


@Override
protected void tearDown() throws Exception {
    super.tearDown();

    activity = null;
    lv = null;
    tvInfo = null;
    contextInfoActivity = null;

}

希望这对你有所帮助。如果你有任何问题可以自由地问。谢谢。

1
您可以通过获取包含子项的视图,然后将该视图传递给TouchUtils.clickView来单击ListView中特定行。

如果您有一个ListView viewActivityInstrumentationTestCase2 this,并且想在视图中点击位置p

TouchUtils.clickView(this, view.getChildAt(p));

您可能还希望检查视图是否实际上显示在屏幕上。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接