如何在junit测试中模拟用户单击到列表视图项?

Chu*_*ung 5 junit android android-listview

我试图在选择列表项和下一个活动的意图上编写一个junit测试用例,但是我不知道如何通过junit编码来模拟此用户操作。有人可以帮忙吗?

我也想问,是否有任何材料可以在junit中模拟不同的用户操作来教授其功能或语法?

以下是我的学校教程笔记中的一个示例,我想做一个类似的事情,但要在listview项上做。

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); 
 }
Run Code Online (Sandbox Code Playgroud)

小智 1

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

如果您有ListView viewActivityInstrumentationTestCase2 this,并且您想要单击p视图中的位置:

TouchUtils.clickView(this, view.getChildAt(p));
Run Code Online (Sandbox Code Playgroud)

您可能还想检查视图是否确实也在屏幕上。