3

我正在尝试测试以下场景,在自动完成文本视图中输入一个字母,向下滚动并选择其中一个选项,然后单击一个按钮,单击按钮会启动一个新活动。我想检查新活动是否已经开始。这是测试方法。

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()) 处失败。您能否指导我如何测试新活动是否已开始?

4

1 回答 1

3

您需要使用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 权限)。您可以在该列表中搜索您的 Activity:其中一项任务应具有origActivity与您的 Activity 同名的属性。

于 2011-05-10T06:54:37.697 回答