模拟 Robolectric 中菜单项的点击

发布于 2024-12-06 05:58:32 字数 248 浏览 0 评论 0原文

在 Robolectric 中模拟按钮单击相当简单:

Button someButton = (Button) findViewById(R.id.some_button);
someButton.performClick();

但是,我似乎不知道如何使用菜单项执行相同的操作。我在 Activity.onCreateOptionsMenu 中创建了一个菜单,如何模拟对其其中一项的点击?

It's fairly simple to simulate a button click in Robolectric:

Button someButton = (Button) findViewById(R.id.some_button);
someButton.performClick();

However, I can't seem to figure out how to do the same thing with a menu item. I create a menu in Activity.onCreateOptionsMenu, how can I simulate a click on one of its items?

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

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

发布评论

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

评论(6

九公里浅绿 2024-12-13 05:58:32
MenuItem item = new TestMenuItem() {
  public int getItemId() {
    return R.id.hello;
  }
};

activity.onOptionsItemSelected(item);

ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedActivity();
ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent);

assertThat(shadowIntent.getComponent().getClassName(), equalTo(HelloActivity_.class.getName()));

享受!

MenuItem item = new TestMenuItem() {
  public int getItemId() {
    return R.id.hello;
  }
};

activity.onOptionsItemSelected(item);

ShadowActivity shadowActivity = Robolectric.shadowOf(activity);
Intent startedIntent = shadowActivity.getNextStartedActivity();
ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent);

assertThat(shadowIntent.getComponent().getClassName(), equalTo(HelloActivity_.class.getName()));

Enjoy!

烟燃烟灭 2024-12-13 05:58:32

在 Robolectric 3.0+ 中,您可以使用 ShadowActivity.clickMenuItem(menuItemResId)

        // Get shadow
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);

    // Click menu
    shadowActivity.clickMenuItem(R.id.settings_option_item);

    // Get intent
    Intent startedIntent = shadowActivity.getNextStartedActivity();
    ShadowIntent shadowIntent = Shadows.shadowOf(startedIntent);

// Make your assertion
assertThat(shadowIntent.getComponent().getClassName(), equalTo(HelloActivity_.class.getName()));

In Robolectric 3.0+, you can use ShadowActivity.clickMenuItem(menuItemResId):

        // Get shadow
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);

    // Click menu
    shadowActivity.clickMenuItem(R.id.settings_option_item);

    // Get intent
    Intent startedIntent = shadowActivity.getNextStartedActivity();
    ShadowIntent shadowIntent = Shadows.shadowOf(startedIntent);

// Make your assertion
assertThat(shadowIntent.getComponent().getClassName(), equalTo(HelloActivity_.class.getName()));
韶华倾负 2024-12-13 05:58:32

在 robolectric 3.0+ 中,该类称为 RoboMenuItem

In robolectric 3.0+ the class is called RoboMenuItem

泪冰清 2024-12-13 05:58:32

您已经在使用 Robolectric 那么为什么不使用 RoboMenuItem 呢?

private MenuItem menuItemName = new RoboMenuItem(R.id.action_names);
private MenuItem menuItemStar = new RoboMenuItem(R.id.action_stars);

根据需要添加尽可能多的项目。

@Test
public void onOptionItemSelectedTest() {
    activity.onOptionsItemSelected(menuItemStar);
    activity.onOptionsItemSelected(menuItemName);
}

确保您的活动不为空

 @Before
public void setUp() {
    activity = Robolectric.buildActivity(MainActivity.class)
            .create(new Bundle())
            .saveInstanceState(new Bundle())
            .restoreInstanceState(new Bundle())
            .resume()
            .get();
}

Robolectric 版本 4.3

You are already using Robolectric then why don't use RoboMenuItem ?

private MenuItem menuItemName = new RoboMenuItem(R.id.action_names);
private MenuItem menuItemStar = new RoboMenuItem(R.id.action_stars);

add as many items as required.

@Test
public void onOptionItemSelectedTest() {
    activity.onOptionsItemSelected(menuItemStar);
    activity.onOptionsItemSelected(menuItemName);
}

Make sure your activity is not null

 @Before
public void setUp() {
    activity = Robolectric.buildActivity(MainActivity.class)
            .create(new Bundle())
            .saveInstanceState(new Bundle())
            .restoreInstanceState(new Bundle())
            .resume()
            .get();
}

Robolectric version 4.3

假装爱人 2024-12-13 05:58:32

使用 robolectric 2.4:

Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
MenuItem item = new TestMenuItem(R.id.settings_option_item);
activity.onOptionsItemSelected(item);

Using robolectric 2.4:

Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
MenuItem item = new TestMenuItem(R.id.settings_option_item);
activity.onOptionsItemSelected(item);
千笙结 2024-12-13 05:58:32

如果您想减少所需的覆盖/抽象编码量,您也可以使用 Mockito。像这样(在 Kotlin 中):

val menuItem = mock(MenuItem::class.java)
`when`(menuItem.itemId).thenReturn(R.id.itemId)
activity.onOptionsItemSelected(menuItem)

You can also use Mockito if you'd like to cut down on the amount of overriding/abstract coding required. Like this (in Kotlin):

val menuItem = mock(MenuItem::class.java)
`when`(menuItem.itemId).thenReturn(R.id.itemId)
activity.onOptionsItemSelected(menuItem)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文