Android - 测试另一个活动是否已开始

发布于 2024-11-05 18:19:56 字数 1226 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2024-11-12 18:19:56

您需要使用 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 属性。

You'll need to use the ActivityManager as conveniently demonstrated here: https://stackoverflow.com/questions/3908029/android-get-icons-of-running-activities

The short summary:

ActivityManager am = (ActivityManager) getSystemService(Service.ACTIVITY_SERVICE);
List<ActivityManager.RecentTaskInfo> processes = am.getRecentTasks(5);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文