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())处失败。您能指导我如何测试新活动是否已开始吗?
I am trying to test the following scenario, enter a letter in the autocomplete textview, scroll down and select one of the options, then click a button, The button click starts a new activity. I want to check if the new activity has begun or not. This is the test method.
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());
}
The test fails at the last assertEquals(com.myApp.RouteListActivity.class, getActivity()). Can you please guide me on how to test if the new activity has been started or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
ActivityManager
,如下所示: https://stackoverflow.com/questions/3908029/android-get-icons-of-running-activities简短摘要:
为您提供所有正在运行的进程的列表(您需要 GET_TASKS 权限)。您可以在该列表中搜索您的活动:其中一个任务应该具有与您的活动同名的
origActivity
属性。You'll need to use the
ActivityManager
as conveniently demonstrated here: https://stackoverflow.com/questions/3908029/android-get-icons-of-running-activitiesThe short summary:
gets you a list of all the running processes (you'll need the GET_TASKS permission). You can search through that list for your Activity: one of those tasks should have an
origActivity
property with the same name as your Activity.