fut*_*lib 29 android robolectric
在Robolectric中模拟按钮单击非常简单:
Button someButton = (Button) findViewById(R.id.some_button);
someButton.performClick();
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法弄清楚如何用菜单项做同样的事情.我创建了一个菜单Activity.onCreateOptionsMenu,如何模拟其中一个项目的点击?
lem*_*mon 29
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()));
Run Code Online (Sandbox Code Playgroud)
请享用!
hid*_*dro 17
在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()));
Run Code Online (Sandbox Code Playgroud)
在robolectric 3.0+中,该类被称为 RoboMenuItem
| 归档时间: |
|
| 查看次数: |
9684 次 |
| 最近记录: |