Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShadowAlertDialog.getLatestAlertDialog() returns null for android.support.v7.app.AlertDialog

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");
like image 637
Arvinda Avatar asked Nov 07 '22 20:11

Arvinda


1 Answers

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.

like image 65
Vipin Avatar answered Nov 15 '22 08:11

Vipin