有什么变通办法可以让我们测试中的android.support.v7.app.AlertDialog
Robolectric 吗?
someActivity.findViewById(R.id.alet_btn).performClick();
AlertDialog alert = ShadowAlertDialog.getLatestAlertDialog();
ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(alert);
assertThat(shadowAlertDialog.getTitle()).isEqualTo("Hello");
最佳答案
尝试以下
List shownDialogs = ShadowAlertDialog.getShownDialogs();
if (shownDialogs.get(0) instanceof AlertDialog) {
AlertDialog dialog = (android.support.v7.app.AlertDialog) shownDialogs.get(0);
assertThat(dialog).isShowing();
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick();
}
或者
if (ShadowAlertDialog.getLatestDialog() instanceof AlertDialog) {
AlertDialog dialog = (android.support.v7.app.AlertDialog) ShadowAlertDialog.getLatestDialog();
assertThat(dialog).isShowing();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
}
它允许您单击按钮,但不能验证标题或消息AFAIK。