EasyPermissions { public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults, @NonNull Object... receivers) { List<String> granted = new ArrayList<>(); List<String> denied = new ArrayList<>(); for (int i = 0; i < permissions.length; i++) { String perm = permissions[i]; if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { granted.add(perm); } else { denied.add(perm); } } for (Object object : receivers) { if (!granted.isEmpty()) { if (object instanceof PermissionCallbacks) { ((PermissionCallbacks) object).onPermissionsGranted(requestCode, granted); } } if (!denied.isEmpty()) { if (object instanceof PermissionCallbacks) { ((PermissionCallbacks) object).onPermissionsDenied(requestCode, denied); } } if (!granted.isEmpty() && denied.isEmpty()) { runAnnotatedMethods(object, requestCode); } } }  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 shouldCorrectlyCallback_whenOnRequestPermissionResultCalledFromAppCompatActivity() { EasyPermissions.onRequestPermissionsResult(TestAppCompatActivity.REQUEST_CODE, ALL_PERMS, SMS_DENIED_RESULT, spyAppCompatActivity); verify(spyAppCompatActivity, times(1)) .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); assertThat(integerCaptor.getValue()).isEqualTo(TestAppCompatActivity.REQUEST_CODE); assertThat(listCaptor.getValue()) .containsAllIn(new ArrayList<>(Collections.singletonList(Manifest.permission.ACCESS_FINE_LOCATION))); verify(spyAppCompatActivity, times(1)) .onPermissionsDenied(integerCaptor.capture(), listCaptor.capture()); assertThat(integerCaptor.getValue()).isEqualTo(TestAppCompatActivity.REQUEST_CODE); assertThat(listCaptor.getValue()) .containsAllIn(new ArrayList<>(Collections.singletonList(Manifest.permission.READ_SMS))); verify(spyAppCompatActivity, never()).afterPermissionGranted(); }
@Test public void shouldCorrectlyCallback_whenOnRequestPermissionResultCalledFromFragment() { EasyPermissions.onRequestPermissionsResult(TestFragment.REQUEST_CODE, ALL_PERMS, SMS_DENIED_RESULT, spyFragment); verify(spyFragment, times(1)) .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); assertThat(integerCaptor.getValue()).isEqualTo(TestFragment.REQUEST_CODE); assertThat(listCaptor.getValue()) .containsAllIn(new ArrayList<>(Collections.singletonList(Manifest.permission.ACCESS_FINE_LOCATION))); verify(spyFragment, times(1)) .onPermissionsDenied(integerCaptor.capture(), listCaptor.capture()); assertThat(integerCaptor.getValue()).isEqualTo(TestFragment.REQUEST_CODE); assertThat(listCaptor.getValue()) .containsAllIn(new ArrayList<>(Collections.singletonList(Manifest.permission.READ_SMS))); verify(spyFragment, never()).afterPermissionGranted(); }
@Test public void shouldCorrectlyCallback_whenOnRequestPermissionResultCalledFromActivity() { EasyPermissions.onRequestPermissionsResult(TestActivity.REQUEST_CODE, ALL_PERMS, SMS_DENIED_RESULT, spyActivity); verify(spyActivity, times(1)) .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); assertThat(integerCaptor.getValue()).isEqualTo(TestActivity.REQUEST_CODE); assertThat(listCaptor.getValue()) .containsAllIn(new ArrayList<>(Collections.singletonList(Manifest.permission.ACCESS_FINE_LOCATION))); verify(spyActivity, times(1)) .onPermissionsDenied(integerCaptor.capture(), listCaptor.capture()); assertThat(integerCaptor.getValue()).isEqualTo(TestActivity.REQUEST_CODE); assertThat(listCaptor.getValue()) .containsAllIn(new ArrayList<>(Collections.singletonList(Manifest.permission.READ_SMS))); verify(spyActivity, never()).afterPermissionGranted(); }