AppSettingsDialog implements Parcelable { AlertDialog showDialog(DialogInterface.OnClickListener positiveListener, DialogInterface.OnClickListener negativeListener) { AlertDialog.Builder builder; if (mThemeResId != -1) { builder = new AlertDialog.Builder(mContext, mThemeResId); } else { builder = new AlertDialog.Builder(mContext); } return builder .setCancelable(false) .setTitle(mTitle) .setMessage(mRationale) .setPositiveButton(mPositiveButtonText, positiveListener) .setNegativeButton(mNegativeButtonText, negativeListener) .show(); } private  AppSettingsDialog(Parcel in); private  AppSettingsDialog(@NonNull final Object activityOrFragment,
                              @StyleRes int themeResId,
                              @Nullable String rationale,
                              @Nullable String title,
                              @Nullable String positiveButtonText,
                              @Nullable String negativeButtonText,
                              int requestCode,
                              int intentFlags); void show(); @Override int describeContents(); @Override void writeToParcel(@NonNull Parcel dest, int flags); static final int DEFAULT_SETTINGS_REQ_CODE; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    static final Parcelable.Creator<AppSettingsDialog> CREATOR; }
@Test public void shouldPositiveListener_whenClickingPositiveButtonFromActivity() { AlertDialog alertDialog = new AppSettingsDialog.Builder(spyActivity) .setTitle(TITLE) .setRationale(RATIONALE) .setPositiveButton(POSITIVE) .setNegativeButton(NEGATIVE) .setThemeResId(R.style.Theme_AppCompat) .build() .showDialog(positiveListener, negativeListener); Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); positive.performClick(); verify(positiveListener, times(1)) .onClick(any(DialogInterface.class), anyInt()); }
@Test public void shouldNegativeListener_whenClickingPositiveButtonFromActivity() { AlertDialog alertDialog = new AppSettingsDialog.Builder(spyActivity) .setTitle(TITLE) .setRationale(RATIONALE) .setPositiveButton(POSITIVE) .setNegativeButton(NEGATIVE) .setThemeResId(R.style.Theme_AppCompat) .build() .showDialog(positiveListener, negativeListener); Button positive = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); positive.performClick(); verify(negativeListener, times(1)) .onClick(any(DialogInterface.class), anyInt()); }
@Test public void shouldPositiveListener_whenClickingPositiveButtonFromSupportFragment() { AlertDialog alertDialog = new AppSettingsDialog.Builder(spyFragment) .setTitle(TITLE) .setRationale(RATIONALE) .setPositiveButton(POSITIVE) .setNegativeButton(NEGATIVE) .setThemeResId(R.style.Theme_AppCompat) .build() .showDialog(positiveListener, negativeListener); Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); positive.performClick(); verify(positiveListener, times(1)) .onClick(any(DialogInterface.class), anyInt()); }
@Test public void shouldNegativeListener_whenClickingPositiveButtonFromSupportFragment() { AlertDialog alertDialog = new AppSettingsDialog.Builder(spyFragment) .setTitle(TITLE) .setRationale(RATIONALE) .setPositiveButton(POSITIVE) .setNegativeButton(NEGATIVE) .setThemeResId(R.style.Theme_AppCompat) .build() .showDialog(positiveListener, negativeListener); Button positive = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); positive.performClick(); verify(negativeListener, times(1)) .onClick(any(DialogInterface.class), anyInt()); }