如何测试菜单

发布于 2024-09-06 21:38:19 字数 230 浏览 6 评论 0原文

我需要通过单元测试来覆盖菜单功能,但是我正在努力获取菜单对象。

以下测试用例失败(mMenu 为空):

sendKeys(KeyEvent.KEYCODE_MENU);
mMenu = (Menu) mActivity.findViewById(com.###.###.R.id.main_menu);
assertNotNull(mMenu);

请指教。谢谢。

I need to cover Menu functionality by unit tests, however I'm struggling to obtain Menu object.

Following test case fails (mMenu is null):

sendKeys(KeyEvent.KEYCODE_MENU);
mMenu = (Menu) mActivity.findViewById(com.###.###.R.id.main_menu);
assertNotNull(mMenu);

Please advice. Thank you.

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

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

发布评论

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

评论(5

如梦初醒的夏天 2024-09-13 21:38:20

我遇到了同样的场景,并在 ActivityInstrumentationTestCase 的实现中提出了以下(非常简单)的解决方案:

...
ActivityMonitor am = getInstrumentation().addMonitor(LoginActivity.class.getName(), null, false);

// Click the menu option
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
getInstrumentation().invokeMenuActionSync(mActivity, R.id.logout, 0);

Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
a.finish();
...

这段代码做了三件事:

  1. 单击菜单选项,
  2. 确保我们在单击菜单选项后转到适当的活动,并
  3. 完成已开始的活动(对于接下来的测试非常重要)。

我希望这有帮助。

I ran into this same scenario and came up with the following (very simple) solution in my implementation of ActivityInstrumentationTestCase:

...
ActivityMonitor am = getInstrumentation().addMonitor(LoginActivity.class.getName(), null, false);

// Click the menu option
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
getInstrumentation().invokeMenuActionSync(mActivity, R.id.logout, 0);

Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
a.finish();
...

This snippet of code does three things:

  1. Clicks the menu option,
  2. Ensures we go to the appropriate activity after the menu option is clicked, and
  3. finishes the activity that was started (very important for the tests following this one).

I hope this helps.

半仙 2024-09-13 21:38:20

您到底想测试什么?该菜单项执行正确的操作吗?

您可以调用 Activity.openOptionsMenu() 打开菜单并通过重写 onMenu 方法之一来获取对菜单的引用。此时您可以使用 Menu.performIdentifierAction 来选择菜单项。

What exactly are you trying to test? That menu items do the correct action?

You can call Activity.openOptionsMenu() to open the menu and get a reference to the menu by overriding one of the onMenu methods. At that point you can use Menu.performIdentifierAction to select menu items.

云淡风轻 2024-09-13 21:38:20

如果你想做 UI、系统或功能测试,我建议你使用 Robotium。然后您可以使用 sendKey(Solo.MENU),然后使用 clickOnText() 或 clickOnView() 单击菜单项。当你这样做时,你就可以断言正确的行为。仅仅断言它不应该为 null 是不够的。您应该检查一下 Robotium,它更适合在测试此类内容时使用。

If you want to do UI, system or function tests I would recommend you to use Robotium. Then you can just use sendKey(Solo.MENU) and then click on the menu items by using clickOnText() or clickOnView(). When you have done that you can assert right behaviour. Just asserting that it should not be null is not enough. You should check Robotium out, its way more appropriate to use when testing things like this.

诺曦 2024-09-13 21:38:20
    Activity act =launchActivity(intent);

    MenuBuilder builder=new MenuBuilder(mInst.getTargetContext());

    act.onCreateOptionsMenu(builder);



    act.onPrepareOptionsMenu(builder);

    Log.i(TAG, "BuilderSize: "+builder.size());
    int visible=0;
    for(int i=0;i<builder.size();i++)
    {
        MenuItem item=builder.getItem(i);

        if(item.isVisible()&& item.isEnabled())
        {
            Log.i(TAG, item.getTitle().toString());
            visible++;
        }
    }
    act.finish();
    Activity act =launchActivity(intent);

    MenuBuilder builder=new MenuBuilder(mInst.getTargetContext());

    act.onCreateOptionsMenu(builder);



    act.onPrepareOptionsMenu(builder);

    Log.i(TAG, "BuilderSize: "+builder.size());
    int visible=0;
    for(int i=0;i<builder.size();i++)
    {
        MenuItem item=builder.getItem(i);

        if(item.isVisible()&& item.isEnabled())
        {
            Log.i(TAG, item.getTitle().toString());
            visible++;
        }
    }
    act.finish();
凉薄对峙 2024-09-13 21:38:20

使用仪器为您测试菜单项的按下情况。

这是我的一个示例测试用例,它调用“设置”菜单,该菜单启动另一个活动。

public void testCanGoToSettings() {
    final MainActivity activity = getActivity();

    Instrumentation.ActivityMonitor am = getInstrumentation().addMonitor(ConfigureActivity.class.getName(),
            null /* result */, true /* block */);

    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
    getInstrumentation().invokeMenuActionSync(activity, R.id.menu_settings, 0 /* flags */);

            /* if not block in addMonitor() above, then comment out this...
    Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
    a.finish();
            */ 

}

Use instrumentation to test the pressing of the menu item for you.

Here is a sample test case of mine that invokes the "Settings" menu, which starts another activity.

public void testCanGoToSettings() {
    final MainActivity activity = getActivity();

    Instrumentation.ActivityMonitor am = getInstrumentation().addMonitor(ConfigureActivity.class.getName(),
            null /* result */, true /* block */);

    getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
    getInstrumentation().invokeMenuActionSync(activity, R.id.menu_settings, 0 /* flags */);

            /* if not block in addMonitor() above, then comment out this...
    Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
    a.finish();
            */ 

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