EasyPermissions { public static void requestPermissions( @NonNull Activity host, @NonNull String rationale, int requestCode, @Size(min = 1) @NonNull String... perms) { requestPermissions( new PermissionRequest.Builder(host, requestCode, perms) .setRationale(rationale) .build()); }  static boolean hasPermissions(@NonNull Context context,
                                         @Size(min = 1) @NonNull String... perms); static void requestPermissions(
            @NonNull Activity host, @NonNull String rationale,
            int requestCode, @Size(min = 1) @NonNull String... perms); static void requestPermissions(
            @NonNull Fragment host, @NonNull String rationale,
            int requestCode, @Size(min = 1) @NonNull String... perms); static void requestPermissions(PermissionRequest request); static void onRequestPermissionsResult(int requestCode,
                                                  @NonNull String[] permissions,
                                                  @NonNull int[] grantResults,
                                                  @NonNull Object... receivers); static boolean somePermissionPermanentlyDenied(@NonNull Activity host,
                                                          @NonNull List<String> deniedPermissions); static boolean somePermissionPermanentlyDenied(@NonNull Fragment host,
                                                          @NonNull List<String> deniedPermissions); static boolean permissionPermanentlyDenied(@NonNull Activity host,
                                                      @NonNull String deniedPermission); static boolean permissionPermanentlyDenied(@NonNull Fragment host,
                                                      @NonNull String deniedPermission); static boolean somePermissionDenied(@NonNull Activity host,
                                               @NonNull String... perms); static boolean somePermissionDenied(@NonNull Fragment host,
                                               @NonNull String... perms);  }
@Test public void shouldShowCorrectDialogUsingRequest_whenMissingPermissionsAndShowRationaleFromActivity() { grantPermissions(ONE_PERM); showRationale(true, ALL_PERMS); PermissionRequest request = new PermissionRequest.Builder(spyActivity, TestActivity.REQUEST_CODE, ALL_PERMS) .setPositiveButtonText(android.R.string.ok) .setNegativeButtonText(android.R.string.cancel) .setRationale(android.R.string.unknownName) .setTheme(R.style.Theme_AppCompat) .build(); EasyPermissions.requestPermissions(request); Fragment dialogFragment = spyActivity.getFragmentManager() .findFragmentByTag(RationaleDialogFragment.TAG); assertThat(dialogFragment).isInstanceOf(RationaleDialogFragment.class); Dialog dialog = ((RationaleDialogFragment) dialogFragment).getDialog(); assertThatHasExpectedButtonsAndRationale(dialog, android.R.string.unknownName, android.R.string.ok, android.R.string.cancel); }
@Test public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionsFromAppCompatActivity() { grantPermissions(ALL_PERMS); EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS); verify(spyAppCompatActivity, times(1)) .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); verify(spyAppCompatActivity, never()).requestPermissions(any(String[].class), anyInt()); assertThat(integerCaptor.getValue()).isEqualTo(TestAppCompatActivity.REQUEST_CODE); assertThat(listCaptor.getValue()).containsAllIn(ALL_PERMS); }
@Test public void shouldCallbackAfterPermissionGranted_whenRequestAlreadyGrantedPermissionsFromAppCompatActivity() { grantPermissions(ALL_PERMS); EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS); verify(spyAppCompatActivity, times(2)).afterPermissionGranted(); }
@Test public void shouldNotCallbackAfterPermissionGranted_whenRequestNotGrantedPermissionsFromAppCompatActivity() { grantPermissions(ONE_PERM); EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS); verify(spyAppCompatActivity, never()).afterPermissionGranted(); }
@Test public void shouldRequestPermissions_whenMissingPermissionAndNotShowRationaleFromAppCompatActivity() { grantPermissions(ONE_PERM); showRationale(false, ALL_PERMS); EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS); verify(spyAppCompatActivity, times(1)) .requestPermissions(ALL_PERMS, TestAppCompatActivity.REQUEST_CODE); }
@Test public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromAppCompatActivity() { grantPermissions(ONE_PERM); showRationale(true, ALL_PERMS); EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS); androidx.fragment.app.Fragment dialogFragment = spyAppCompatActivity.getSupportFragmentManager() .findFragmentByTag(RationaleDialogFragmentCompat.TAG); assertThat(dialogFragment).isInstanceOf(RationaleDialogFragmentCompat.class); Dialog dialog = ((RationaleDialogFragmentCompat) dialogFragment).getDialog(); assertThatHasExpectedRationale(dialog, RATIONALE); }
@Test public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromSupportFragmentActivity() { grantPermissions(ONE_PERM); showRationale(true, ALL_PERMS); EasyPermissions.requestPermissions(spySupportFragmentActivity, RATIONALE, TestSupportFragmentActivity.REQUEST_CODE, ALL_PERMS); Fragment dialogFragment = spySupportFragmentActivity.getFragmentManager() .findFragmentByTag(RationaleDialogFragment.TAG); assertThat(dialogFragment).isInstanceOf(RationaleDialogFragment.class); Dialog dialog = ((RationaleDialogFragment) dialogFragment).getDialog(); assertThatHasExpectedRationale(dialog, RATIONALE); }
@Test public void shouldShowCorrectDialogUsingRequest_whenMissingPermissionsAndShowRationaleFromAppCompatActivity() { grantPermissions(ONE_PERM); showRationale(true, ALL_PERMS); PermissionRequest request = new PermissionRequest.Builder(spyAppCompatActivity, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS) .setPositiveButtonText(android.R.string.ok) .setNegativeButtonText(android.R.string.cancel) .setRationale(android.R.string.unknownName) .setTheme(R.style.Theme_AppCompat) .build(); EasyPermissions.requestPermissions(request); androidx.fragment.app.Fragment dialogFragment = spyAppCompatActivity.getSupportFragmentManager() .findFragmentByTag(RationaleDialogFragmentCompat.TAG); assertThat(dialogFragment).isInstanceOf(RationaleDialogFragmentCompat.class); Dialog dialog = ((RationaleDialogFragmentCompat) dialogFragment).getDialog(); assertThatHasExpectedButtonsAndRationale(dialog, android.R.string.unknownName, android.R.string.ok, android.R.string.cancel); }
@Test public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionsFromFragment() { grantPermissions(ALL_PERMS); EasyPermissions.requestPermissions(spyFragment, RATIONALE, TestFragment.REQUEST_CODE, ALL_PERMS); verify(spyFragment, times(1)) .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); verify(spyFragment, never()).requestPermissions(any(String[].class), anyInt()); assertThat(integerCaptor.getValue()).isEqualTo(TestFragment.REQUEST_CODE); assertThat(listCaptor.getValue()).containsAllIn(ALL_PERMS); }
@Test public void shouldCallbackAfterPermissionGranted_whenRequestAlreadyGrantedPermissionsFragment() { grantPermissions(ALL_PERMS); EasyPermissions.requestPermissions(spyFragment, RATIONALE, TestFragment.REQUEST_CODE, ALL_PERMS); verify(spyFragment, times(2)).afterPermissionGranted(); }
@Test public void shouldNotCallbackAfterPermissionGranted_whenRequestNotGrantedPermissionsFromFragment() { grantPermissions(ONE_PERM); EasyPermissions.requestPermissions(spyFragment, RATIONALE, TestFragment.REQUEST_CODE, ALL_PERMS); verify(spyFragment, never()).afterPermissionGranted(); }
@Test public void shouldRequestPermissions_whenMissingPermissionsAndNotShowRationaleFromFragment() { grantPermissions(ONE_PERM); showRationale(false, ALL_PERMS); EasyPermissions.requestPermissions(spyFragment, RATIONALE, TestFragment.REQUEST_CODE, ALL_PERMS); verify(spyFragment, times(1)) .requestPermissions(ALL_PERMS, TestFragment.REQUEST_CODE); }
@Test public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromFragment() { grantPermissions(ONE_PERM); showRationale(true, ALL_PERMS); EasyPermissions.requestPermissions(spyFragment, RATIONALE, TestFragment.REQUEST_CODE, ALL_PERMS); androidx.fragment.app.Fragment dialogFragment = spyFragment.getChildFragmentManager() .findFragmentByTag(RationaleDialogFragmentCompat.TAG); assertThat(dialogFragment).isInstanceOf(RationaleDialogFragmentCompat.class); Dialog dialog = ((RationaleDialogFragmentCompat) dialogFragment).getDialog(); assertThatHasExpectedRationale(dialog, RATIONALE); }
@Test public void shouldShowCorrectDialogUsingRequest_whenMissingPermissionsAndShowRationaleFromFragment() { grantPermissions(ONE_PERM); showRationale(true, ALL_PERMS); PermissionRequest request = new PermissionRequest.Builder(spyFragment, TestFragment.REQUEST_CODE, ALL_PERMS) .setPositiveButtonText(POSITIVE) .setNegativeButtonText(NEGATIVE) .setRationale(RATIONALE) .setTheme(R.style.Theme_AppCompat) .build(); EasyPermissions.requestPermissions(request); androidx.fragment.app.Fragment dialogFragment = spyFragment.getChildFragmentManager() .findFragmentByTag(RationaleDialogFragmentCompat.TAG); assertThat(dialogFragment).isInstanceOf(RationaleDialogFragmentCompat.class); Dialog dialog = ((RationaleDialogFragmentCompat) dialogFragment).getDialog(); assertThatHasExpectedButtonsAndRationale(dialog, RATIONALE, POSITIVE, NEGATIVE); }
@Test public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionsFromActivity() { grantPermissions(ALL_PERMS); EasyPermissions.requestPermissions(spyActivity, RATIONALE, TestActivity.REQUEST_CODE, ALL_PERMS); verify(spyActivity, times(1)) .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); verify(spyActivity, never()).requestPermissions(any(String[].class), anyInt()); assertThat(integerCaptor.getValue()).isEqualTo(TestActivity.REQUEST_CODE); assertThat(listCaptor.getValue()).containsAllIn(ALL_PERMS); }
@Test public void shouldCallbackAfterPermissionGranted_whenRequestAlreadyGrantedPermissionsFromActivity() { grantPermissions(ALL_PERMS); EasyPermissions.requestPermissions(spyActivity, RATIONALE, TestActivity.REQUEST_CODE, ALL_PERMS); verify(spyActivity, times(2)).afterPermissionGranted(); }
@Test public void shouldNotCallbackAfterPermissionGranted_whenRequestNotGrantedPermissionsFromActivity() { grantPermissions(ONE_PERM); EasyPermissions.requestPermissions(spyActivity, RATIONALE, TestActivity.REQUEST_CODE, ALL_PERMS); verify(spyActivity, never()).afterPermissionGranted(); }
@Test public void shouldRequestPermissions_whenMissingPermissionAndNotShowRationaleFromActivity() { grantPermissions(ONE_PERM); showRationale(false, ALL_PERMS); EasyPermissions.requestPermissions(spyActivity, RATIONALE, TestActivity.REQUEST_CODE, ALL_PERMS); verify(spyActivity, times(1)) .requestPermissions(ALL_PERMS, TestActivity.REQUEST_CODE); }
@Test public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromActivity() { grantPermissions(ONE_PERM); showRationale(true, ALL_PERMS); EasyPermissions.requestPermissions(spyActivity, RATIONALE, TestActivity.REQUEST_CODE, ALL_PERMS); Fragment dialogFragment = spyActivity.getFragmentManager() .findFragmentByTag(RationaleDialogFragment.TAG); assertThat(dialogFragment).isInstanceOf(RationaleDialogFragment.class); Dialog dialog = ((RationaleDialogFragment) dialogFragment).getDialog(); assertThatHasExpectedRationale(dialog, RATIONALE); }