Android Instrumentation 测试如何判断当前 Activity 是否是主屏幕(启动器)?

发布于 2024-12-14 14:40:18 字数 1168 浏览 3 评论 0原文

我正在尝试使用 Robotium 测试应用程序功能。其中一个功能是,当我的初始活动从活动堆栈顶部的视图启动时,它应该清除堆栈顶部并重用现有的 Activity ig(“MainActivity”)。

流程:

FirstScreen ->登录活动屏幕 ->注册屏幕 -> FirstScreen

解决方案非常简单:

   Intent intent = new Intent(getBaseContext(), FirstScreen.class);
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   startActivity(intent);

通过设置标志 Intent.FLAG_ACTIVITY_CLEAR_TOP 将 FirstScreen 放回到我的应用程序堆栈的顶部。

我试图编写的测试是确认当按下硬件后退按钮时,应用程序消失,并且本机Home(Launcher)应用程序是当前活动。

我的仪器测试用例:

    @Smoke
    public void testshouldBeOnLauncherHomeScreen() {
        // Monitor the Home (Launcher) Activity being Launched
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.MAIN");
        filter.addCategory("android.intent.category.HOME");
        ActivityMonitor monitor = getInstrumentation().addMonitor(filter, null, false);

        // go back to the launcher home
        robotium.goBack();

        assertEquals(1, monitor.getHits());
    }

我更愿意断言启动器应用程序的活动是当前活动。任何想法或建议将不胜感激。

I am trying to test drive an application feature using Robotium. One of the features is that when my initial activity is launched from a view on top of the activity stack it should clear the top of the stack and reuse the existing Activity i.g.("MainActivity").

Flow:

FirstScreen -> LoginActivityScreen -> RegistrationScreen -> FirstScreen

The solution is simple enough:

   Intent intent = new Intent(getBaseContext(), FirstScreen.class);
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   startActivity(intent);

By setting the flag Intent.FLAG_ACTIVITY_CLEAR_TOP puts FirstScreen back on the top of my application stack.

The Test I am trying to write is to confirm that when the Hardware Back Button is pressed then the app is gone and the native Home(Launcher) application is the current Activity.

My Instrumentation TestCase:

    @Smoke
    public void testshouldBeOnLauncherHomeScreen() {
        // Monitor the Home (Launcher) Activity being Launched
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.MAIN");
        filter.addCategory("android.intent.category.HOME");
        ActivityMonitor monitor = getInstrumentation().addMonitor(filter, null, false);

        // go back to the launcher home
        robotium.goBack();

        assertEquals(1, monitor.getHits());
    }

I would prefer to assert that the activity of the Launcher app is the current activity. Any ideas or suggestions would be much appreciated.

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

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

发布评论

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

评论(1

玩物 2024-12-21 14:40:18

我能够使用 ActivityUnitTestCase 而不是 InstrumentationTestCase2 解决这个问题。我相信 Android 操作系统会通过添加该标志将我的 FirstScreen 带到顶部。在发出启动第一个屏幕的意图时验证该标志是否已设置足以让我确信我的代码正在执行我所期望的操作。

    Public void testThatStartedIntentHasClearTopFlag() {
        Activity activity startActivity(new Intent(), null, null);
        activity.findViewById(R.id.button).performClick();
        Intent intent = getStartedActivityIntent();
        assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TOP, intent.getFlags());
    }

I have was able to solve this using ActivityUnitTestCase rather than InstrumentationTestCase2. I trust the android OS to bring my FirstScreen to the top by adding the flag. Validating that the flag is set when issuing the intent to start my first screen is enough to give me confidence that my code is doing what I expect.

    Public void testThatStartedIntentHasClearTopFlag() {
        Activity activity startActivity(new Intent(), null, null);
        activity.findViewById(R.id.button).performClick();
        Intent intent = getStartedActivityIntent();
        assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TOP, intent.getFlags());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文