Android-测试是否已开始其他活动
作者:互联网
我正在尝试测试以下情况,在自动完成文本视图中输入一个字母,向下滚动并选择一个选项,然后单击一个按钮,单击按钮将启动一个新活动.我想检查新活动是否已经开始.这是测试方法.
public void testSpinnerUI() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
mFromLocation.requestFocusFromTouch(); // use reqestFocusFromTouch() and not requestFocus()
}
});
//set a busstop that has W in it, and scroll to the 4th position of the list - In this case Welcome
this.sendKeys(KeyEvent.KEYCODE_W);
try {
Thread.sleep(2000); //wait for 2 seconds so that the autocomplete loads
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 1; i <= 4; i++) {
this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
}
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
assertEquals("Welcome", mFromLocation.getText().toString());
//hit down arrow twice and centre button
this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
assertEquals(com.myApp.RouteListActivity.class, getActivity());
}
测试在最后一个assertEquals(com.myApp.RouteListActivity.class,getActivity())处失败.您能指导我如何测试新活动是否开始吗?
解决方法:
您需要使用在此处方便演示的ActivityManager:https://stackoverflow.com/questions/3908029/android-get-icons-of-running-activities
简短摘要:
ActivityManager am = (ActivityManager) getSystemService(Service.ACTIVITY_SERVICE);
List<ActivityManager.RecentTaskInfo> processes = am.getRecentTasks(5);
获取所有正在运行的进程的列表(您将需要GET_TASKS权限).您可以在该列表中搜索“活动”:其中一项任务应具有与活动名称相同的origActivity属性.
标签:android,junit3 来源: https://codeday.me/bug/20191208/2091386.html