我正在运行一个检测测试,以检查在意图
上设置特定参数时是否显示对话框@Test
public void shouldShowNotificationContentWhenActivityIsDestroyed() {
final String NOTIFICATION_TITLE = "Título";
final String NOTIFICATION_CONTENT = "Texto";
Intent intent = buildNotificationIntent(NOTIFICATION_TITLE, NOTIFICATION_CONTENT);
activityRule.launchActivity(intent);
onView(withId(R.id.notification_title)).check(matches(allOf(withText(NOTIFICATION_TITLE), isDisplayed())));
onView(withId(R.id.notification_content)).check(matches(allOf(withText(NOTIFICATION_CONTENT), isDisplayed())));
}
@NonNull
private Intent buildNotificationIntent(String NOTIFICATION_TITLE, String NOTIFICATION_CONTENT) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(NotificationsUtils.NOTIFICATION_TITLE_PARAMETER, NOTIFICATION_TITLE);
intent.putExtra(NotificationsUtils.NOTIFICATION_TEXT_PARAMETER, NOTIFICATION_CONTENT);
return intent;
}
它工作得很好。现在,我想检查当活动已经打开时传递意图时是否显示对话框。
@Test
public void shouldShowNotificationContentOnRunningActivity() {
final String NOTIFICATION_TITLE = "Título";
final String NOTIFICATION_CONTENT = "Texto";
activityRule.launchActivity(new Intent());
Intent intent = buildNotificationIntent(NOTIFICATION_TITLE, NOTIFICATION_CONTENT);
activityRule.launchActivity(intent);
onView(withId(R.id.notification_title)).check(matches(allOf(withText(NOTIFICATION_TITLE), isDisplayed())));
onView(withId(R.id.notification_content)).check(matches(allOf(withText(NOTIFICATION_CONTENT), isDisplayed())));
}
这里我启动没有意图参数的活动,然后使用intent参数重新启动它。我在模拟器上看到活动的行为符合预期;然而,测试既不通过也不失败。我尝试在第二次启动后添加延迟,但它不起作用。我还根据Espresso文档中的建议禁用了开发人员选项的动画。
如何调用断言,以便测试成功完成?