Android - 测试是否已开始其他活动

时间:2011-05-10 05:40:54

标签: android junit3

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

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

1 个答案:

答案 0 :(得分:3)

您需要使用此处方便演示的ActivityManagerhttps://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属性。