|
| 1 | +package pub.devrel.easypermissions; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.app.Activity; |
| 5 | +import android.app.Dialog; |
| 6 | +import android.app.Fragment; |
| 7 | +import android.content.DialogInterface; |
| 8 | + |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.mockito.ArgumentMatchers; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.MockitoAnnotations; |
| 15 | +import org.robolectric.RobolectricTestRunner; |
| 16 | +import org.robolectric.annotation.Config; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | +import static org.mockito.Mockito.never; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +@RunWith(RobolectricTestRunner.class) |
| 27 | +@Config(sdk = 23) |
| 28 | +public class RationaleDialogClickListenerTest { |
| 29 | + |
| 30 | + private static final int REQUEST_CODE = 5; |
| 31 | + private static final String[] PERMS = new String[]{ |
| 32 | + Manifest.permission.READ_SMS, Manifest.permission.ACCESS_FINE_LOCATION}; |
| 33 | + @Mock |
| 34 | + private RationaleDialogFragment dialogFragment; |
| 35 | + @Mock |
| 36 | + private RationaleDialogFragmentCompat dialogFragmentCompat; |
| 37 | + @Mock |
| 38 | + private RationaleDialogConfig dialogConfig; |
| 39 | + @Mock |
| 40 | + private EasyPermissions.PermissionCallbacks permissionCallbacks; |
| 41 | + @Mock |
| 42 | + private EasyPermissions.RationaleCallbacks rationaleCallbacks; |
| 43 | + @Mock |
| 44 | + private DialogInterface dialogInterface; |
| 45 | + @Mock |
| 46 | + private Activity activity; |
| 47 | + @Mock |
| 48 | + private Fragment fragment; |
| 49 | + @Mock |
| 50 | + private android.support.v4.app.Fragment supportFragment; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() { |
| 54 | + MockitoAnnotations.initMocks(this); |
| 55 | + |
| 56 | + when(dialogFragment.getActivity()).thenReturn(activity); |
| 57 | + dialogConfig.requestCode = REQUEST_CODE; |
| 58 | + dialogConfig.permissions = PERMS; |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void shouldOnRationaleAccepted_whenPositiveButtonWithRationaleCallbacks() { |
| 63 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, |
| 64 | + permissionCallbacks, rationaleCallbacks); |
| 65 | + listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); |
| 66 | + |
| 67 | + verify(rationaleCallbacks, times(1)).onRationaleAccepted(REQUEST_CODE); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void shouldNotOnRationaleAccepted_whenPositiveButtonWithoutRationaleCallbacks() { |
| 72 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, |
| 73 | + permissionCallbacks, null); |
| 74 | + listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); |
| 75 | + |
| 76 | + verify(rationaleCallbacks, never()).onRationaleAccepted(anyInt()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void shouldRequestPermissions_whenPositiveButtonFromActivity() { |
| 81 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, |
| 82 | + permissionCallbacks, rationaleCallbacks); |
| 83 | + listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); |
| 84 | + |
| 85 | + verify(activity, times(1)).requestPermissions(PERMS, REQUEST_CODE); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void shouldRequestPermissions_whenPositiveButtonFromFragment() { |
| 90 | + when(dialogFragment.getParentFragment()).thenReturn(fragment); |
| 91 | + |
| 92 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, |
| 93 | + permissionCallbacks, rationaleCallbacks); |
| 94 | + listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); |
| 95 | + |
| 96 | + verify(fragment, times(1)).requestPermissions(PERMS, REQUEST_CODE); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void shouldRequestPermissions_whenPositiveButtonFromSupportFragment() { |
| 101 | + when(dialogFragmentCompat.getParentFragment()).thenReturn(supportFragment); |
| 102 | + |
| 103 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragmentCompat, dialogConfig, |
| 104 | + permissionCallbacks, rationaleCallbacks); |
| 105 | + listener.onClick(dialogInterface, Dialog.BUTTON_POSITIVE); |
| 106 | + |
| 107 | + verify(supportFragment, times(1)).requestPermissions(PERMS, REQUEST_CODE); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void shouldOnRationaleDenied_whenNegativeButtonWithRationaleCallbacks() { |
| 112 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, |
| 113 | + permissionCallbacks, rationaleCallbacks); |
| 114 | + listener.onClick(dialogInterface, Dialog.BUTTON_NEGATIVE); |
| 115 | + |
| 116 | + verify(rationaleCallbacks, times(1)).onRationaleDenied(REQUEST_CODE); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void shouldNotOnRationaleDenied_whenNegativeButtonWithoutRationaleCallbacks() { |
| 121 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, |
| 122 | + permissionCallbacks, null); |
| 123 | + listener.onClick(dialogInterface, Dialog.BUTTON_NEGATIVE); |
| 124 | + |
| 125 | + verify(rationaleCallbacks, never()).onRationaleDenied(anyInt()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void shouldOnPermissionsDenied_whenNegativeButtonWithPermissionCallbacks() { |
| 130 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, |
| 131 | + permissionCallbacks, rationaleCallbacks); |
| 132 | + listener.onClick(dialogInterface, Dialog.BUTTON_NEGATIVE); |
| 133 | + |
| 134 | + verify(permissionCallbacks, times(1)) |
| 135 | + .onPermissionsDenied(REQUEST_CODE, Arrays.asList(PERMS)); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void shouldNotOnPermissionsDenied_whenNegativeButtonWithoutPermissionCallbacks() { |
| 140 | + RationaleDialogClickListener listener = new RationaleDialogClickListener(dialogFragment, dialogConfig, |
| 141 | + null, rationaleCallbacks); |
| 142 | + listener.onClick(dialogInterface, Dialog.BUTTON_NEGATIVE); |
| 143 | + |
| 144 | + verify(permissionCallbacks, never()).onPermissionsDenied(anyInt(), ArgumentMatchers.<String>anyList()); |
| 145 | + } |
| 146 | +} |
0 commit comments