测试最近启动的活动是否确实已经开始

发布于 2024-12-09 16:18:29 字数 1591 浏览 0 评论 0原文

我已经研究了一些类似的问题,但还没有成功地测试这一点。

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

我尝试了这个,但是对我来说,origactivity 结果是 null,所以我使用 .baseIntent.getComponent() 来识别已启动的活动(我想这应该给我相同的结果?)

我的 JUnit 测试代码:

public class MainMenuTest extends
        ActivityInstrumentationTestCase2<MainMenuActivity> {

    public MainMenuTest() {
        super("package.name", MainMenuActivity.class);
    }

    private MainMenuActivity mActivity;
    private ActivityManager am;

    public void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        am = (ActivityManager) mActivity.getSystemService(Service.ACTIVITY_SERVICE);
        Intent i = new Intent(mActivity, GameActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mActivity.startActivity(i);
    }

    public void testNewGameButton() {
        final ActivityManager am2 = am;

        mActivity.runOnUiThread(new Runnable() {
            public void run() {                 
                List<ActivityManager.RecentTaskInfo> processes = am2.getRecentTasks(2, ActivityManager.RECENT_WITH_EXCLUDED);
                ActivityManager.RecentTaskInfo recentTask = processes.get(0);
                assertEquals(recentTask.baseIntent.getComponent().getClassName(), GameActivity.class.getName());
            }
        });
    }
}

不幸的是,这会导致assertEquals(recentTask.baseIntent.getComponent().getClassName(), 断言失败。

为什么会这样,有没有更好的方法来测试这个?

I have looked into a few similar questions on SO, but have not yeet managed to successfully test this.

Android - testing if another activity has started

I tried this one, but for me, origactivity turns out to be null, so I used .baseIntent.getComponent() instead to identify the started activity(I suppose this should give me the same result?)

My JUnit test code:

public class MainMenuTest extends
        ActivityInstrumentationTestCase2<MainMenuActivity> {

    public MainMenuTest() {
        super("package.name", MainMenuActivity.class);
    }

    private MainMenuActivity mActivity;
    private ActivityManager am;

    public void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        am = (ActivityManager) mActivity.getSystemService(Service.ACTIVITY_SERVICE);
        Intent i = new Intent(mActivity, GameActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mActivity.startActivity(i);
    }

    public void testNewGameButton() {
        final ActivityManager am2 = am;

        mActivity.runOnUiThread(new Runnable() {
            public void run() {                 
                List<ActivityManager.RecentTaskInfo> processes = am2.getRecentTasks(2, ActivityManager.RECENT_WITH_EXCLUDED);
                ActivityManager.RecentTaskInfo recentTask = processes.get(0);
                assertEquals(recentTask.baseIntent.getComponent().getClassName(), GameActivity.class.getName());
            }
        });
    }
}

Unfortunately, this causes the assertEquals(recentTask.baseIntent.getComponent().getClassName(), assertion to fail.

Why is this, and is there a better way to test this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

岁月静好 2024-12-16 16:18:29

断言失败,因为 ActivityManager.getRecentTasks(...) 返回导致新 要创建的任务,在您的情况下是 MainMenuActivity,而不是 GameActivity。您可以使用 Instrumentation.ActivityMonitor ,如下所示:

public class MainMenuTest extends
        ActivityInstrumentationTestCase2<MainMenuActivity> {

    public MainMenuTest() {
        super("package.name", MainMenuActivity.class);
    }

    private MainMenuActivity mActivity;
    private ActivityMonitor mGameActivityMonitor;

    public void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        Intent i  = new Intent(mActivity,
                GameActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add a monitor before we start the activity
        mGameActivityMonitor = 
                new ActivityMonitor(GameActivity.class.getName(), null, false);
        getInstrumentation().addMonitor(mGameActivityMonitor);

        mActivity.startActivity(i);
    }

    public void testNewGameButton() {
        // Use the monitor to ensure the activity was started
        Activity activity = mGameActivityMonitor.waitForActivityWithTimeout(5 * 1000);
        assertNotNull("Activity was not started", activity);
    }
}

The assert fails because ActivityManager.getRecentTasks(...) returns activities that caused new task to be created, and in your case that is MainMenuActivity, not GameActivity. You can use Instrumentation.ActivityMonitor though, like this:

public class MainMenuTest extends
        ActivityInstrumentationTestCase2<MainMenuActivity> {

    public MainMenuTest() {
        super("package.name", MainMenuActivity.class);
    }

    private MainMenuActivity mActivity;
    private ActivityMonitor mGameActivityMonitor;

    public void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        Intent i  = new Intent(mActivity,
                GameActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add a monitor before we start the activity
        mGameActivityMonitor = 
                new ActivityMonitor(GameActivity.class.getName(), null, false);
        getInstrumentation().addMonitor(mGameActivityMonitor);

        mActivity.startActivity(i);
    }

    public void testNewGameButton() {
        // Use the monitor to ensure the activity was started
        Activity activity = mGameActivityMonitor.waitForActivityWithTimeout(5 * 1000);
        assertNotNull("Activity was not started", activity);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文