Is there any workaround that will allow us to test android.support.v7.app.AlertDialog
in Robolectric?
someActivity.findViewById(R.id.alet_btn).performClick();
AlertDialog alert = ShadowAlertDialog.getLatestAlertDialog();
ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(alert);
assertThat(shadowAlertDialog.getTitle()).isEqualTo("Hello");
Try the below
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();
}
or
if (ShadowAlertDialog.getLatestDialog() instanceof AlertDialog) {
AlertDialog dialog = (android.support.v7.app.AlertDialog) ShadowAlertDialog.getLatestDialog();
assertThat(dialog).isShowing();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
}
It allows you to click the buttons, but can't verify title or message AFAIK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With