RationaleDialogClickListener implements Dialog.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { int requestCode = mConfig.requestCode; if (which == Dialog.BUTTON_POSITIVE) { String[] permissions = mConfig.permissions; if (mRationaleCallbacks != null) { mRationaleCallbacks.onRationaleAccepted(requestCode); } if (mHost instanceof Fragment) { PermissionHelper.newInstance((Fragment) mHost).directRequestPermissions(requestCode, permissions); } else if (mHost instanceof Activity) { PermissionHelper.newInstance((Activity) mHost).directRequestPermissions(requestCode, permissions); } else { throw new RuntimeException("Host must be an Activity or Fragment!"); } } else { if (mRationaleCallbacks != null) { mRationaleCallbacks.onRationaleDenied(requestCode); } notifyPermissionDenied(); } } RationaleDialogClickListener(RationaleDialogFragmentCompat compatDialogFragment,
                                 RationaleDialogConfig config,
                                 EasyPermissions.PermissionCallbacks callbacks,
                                 EasyPermissions.RationaleCallbacks rationaleCallbacks);   RationaleDialogClickListener(RationaleDialogFragment dialogFragment,
                                 RationaleDialogConfig config,
                                 EasyPermissions.PermissionCallbacks callbacks,
                                 EasyPermissions.RationaleCallbacks dialogCallback); @Override void onClick(DialogInterface dialog, int which);  }
@Test public void shouldOnRationaleAccepted_whenPositiveButtonWithRationaleCallbacks() { RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, permissionCallbacks, rationaleCallbacks); listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); verify(rationaleCallbacks, times(1)).onRationaleAccepted(REQUEST_CODE); }
@Test public void shouldNotOnRationaleAccepted_whenPositiveButtonWithoutRationaleCallbacks() { RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, permissionCallbacks, null); listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); verify(rationaleCallbacks, never()).onRationaleAccepted(anyInt()); }
@Test public void shouldRequestPermissions_whenPositiveButtonFromActivity() { RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, permissionCallbacks, rationaleCallbacks); listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); verify(activity, times(1)).requestPermissions(PERMS, REQUEST_CODE); }
@Test public void shouldRequestPermissions_whenPositiveButtonFromFragment() { when(dialogFragmentCompat.getParentFragment()).thenReturn(fragment); RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragmentCompat, dialogConfig, permissionCallbacks, rationaleCallbacks); listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); verify(fragment, times(1)).requestPermissions(PERMS, REQUEST_CODE); }
@Test public void shouldOnRationaleDenied_whenNegativeButtonWithRationaleCallbacks() { RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, permissionCallbacks, rationaleCallbacks); listener.onClick(dialogInterface, Dialog.BUTTON_NEGATIVE); verify(rationaleCallbacks, times(1)).onRationaleDenied(REQUEST_CODE); }
@Test public void shouldNotOnRationaleDenied_whenNegativeButtonWithoutRationaleCallbacks() { RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, permissionCallbacks, null); listener.onClick(dialogInterface, Dialog.BUTTON_NEGATIVE); verify(rationaleCallbacks, never()).onRationaleDenied(anyInt()); }
@Test public void shouldOnPermissionsDenied_whenNegativeButtonWithPermissionCallbacks() { RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, permissionCallbacks, rationaleCallbacks); listener.onClick(dialogInterface, Dialog.BUTTON_NEGATIVE); verify(permissionCallbacks, times(1)) .onPermissionsDenied(REQUEST_CODE, Arrays.asList(PERMS)); }
@Test public void shouldNotOnPermissionsDenied_whenNegativeButtonWithoutPermissionCallbacks() { RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, null, rationaleCallbacks); listener.onClick(dialogInterface, Dialog.BUTTON_NEGATIVE); verify(permissionCallbacks, never()).onPermissionsDenied(anyInt(), ArgumentMatchers.<String>anyList()); }