3030
3131import pub .devrel .easypermissions .testhelper .TestActivity ;
3232import pub .devrel .easypermissions .testhelper .TestFragment ;
33+ import pub .devrel .easypermissions .testhelper .TestSupportActivity ;
3334import pub .devrel .easypermissions .testhelper .TestSupportFragment ;
3435
3536import static com .google .common .truth .Truth .assertThat ;
@@ -58,9 +59,11 @@ public class EasyPermissionsTest {
5859 PackageManager .PERMISSION_DENIED , PackageManager .PERMISSION_GRANTED };
5960 private Application app ;
6061 private TestActivity spyActivity ;
62+ private TestSupportActivity spySupportActivity ;
6163 private TestFragment spyFragment ;
6264 private TestSupportFragment spySupportFragment ;
6365 private ActivityController <TestActivity > activityController ;
66+ private ActivityController <TestSupportActivity > supportActivityController ;
6467 private FragmentController <TestFragment > fragmentController ;
6568 private SupportFragmentController <TestSupportFragment > supportFragmentController ;
6669 @ Captor
@@ -272,6 +275,165 @@ public void shouldNotHavePermissionPermanentlyDenied_whenShowRationaleFromActivi
272275 assertThat (EasyPermissions .permissionPermanentlyDenied (spyActivity , Manifest .permission .READ_SMS )).isFalse ();
273276 }
274277
278+ @ Test
279+ public void shouldCorrectlyCallback_whenOnRequestPermissionResultCalledFromSupportActivity () {
280+ EasyPermissions .onRequestPermissionsResult (TestSupportActivity .REQUEST_CODE , ALL_PERMS , SMS_DENIED_RESULT , spySupportActivity );
281+
282+ verify (spySupportActivity , times (1 ))
283+ .onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
284+ assertThat (integerCaptor .getValue ()).isEqualTo (TestSupportActivity .REQUEST_CODE );
285+ assertThat (listCaptor .getValue ())
286+ .containsAllIn (new ArrayList <>(Collections .singletonList (Manifest .permission .ACCESS_FINE_LOCATION )));
287+
288+ verify (spySupportActivity , times (1 ))
289+ .onPermissionsDenied (integerCaptor .capture (), listCaptor .capture ());
290+ assertThat (integerCaptor .getValue ()).isEqualTo (TestSupportActivity .REQUEST_CODE );
291+ assertThat (listCaptor .getValue ())
292+ .containsAllIn (new ArrayList <>(Collections .singletonList (Manifest .permission .READ_SMS )));
293+
294+ verify (spySupportActivity , never ()).afterPermissionGranted ();
295+ }
296+
297+ @ Test
298+ public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionsFromSupportActivity () {
299+ grantPermissions (ALL_PERMS );
300+
301+ EasyPermissions .requestPermissions (spySupportActivity , RATIONALE , TestSupportActivity .REQUEST_CODE , ALL_PERMS );
302+
303+ verify (spySupportActivity , times (1 ))
304+ .onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
305+ verify (spySupportActivity , never ()).requestPermissions (any (String [].class ), anyInt ());
306+ assertThat (integerCaptor .getValue ()).isEqualTo (TestSupportActivity .REQUEST_CODE );
307+ assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
308+ }
309+
310+ @ Test
311+ public void shouldCallbackAfterPermissionGranted_whenRequestAlreadyGrantedPermissionsFromSupportActivity () {
312+ grantPermissions (ALL_PERMS );
313+
314+ EasyPermissions .requestPermissions (spySupportActivity , RATIONALE , TestSupportActivity .REQUEST_CODE , ALL_PERMS );
315+
316+ // Called 2 times because this is a spy and library implementation invokes super classes annotated methods as well
317+ verify (spySupportActivity , times (2 )).afterPermissionGranted ();
318+ }
319+
320+ @ Test
321+ public void shouldNotCallbackAfterPermissionGranted_whenRequestNotGrantedPermissionsFromSupportActivity () {
322+ grantPermissions (ONE_PERM );
323+
324+ EasyPermissions .requestPermissions (spySupportActivity , RATIONALE , TestSupportActivity .REQUEST_CODE , ALL_PERMS );
325+
326+ verify (spySupportActivity , never ()).afterPermissionGranted ();
327+ }
328+
329+ @ Test
330+ public void shouldRequestPermissions_whenMissingPermissionAndNotShowRationaleFromSupportActivity () {
331+ grantPermissions (ONE_PERM );
332+ showRationale (false , ALL_PERMS );
333+
334+ EasyPermissions .requestPermissions (spySupportActivity , RATIONALE , TestSupportActivity .REQUEST_CODE , ALL_PERMS );
335+
336+ verify (spySupportActivity , times (1 ))
337+ .requestPermissions (ALL_PERMS , TestSupportActivity .REQUEST_CODE );
338+ }
339+
340+ @ Test
341+ public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromSupportActivity () {
342+ grantPermissions (ONE_PERM );
343+ showRationale (true , ALL_PERMS );
344+
345+ EasyPermissions .requestPermissions (spySupportActivity , RATIONALE , TestSupportActivity .REQUEST_CODE , ALL_PERMS );
346+
347+ android .support .v4 .app .Fragment dialogFragment = spySupportActivity .getSupportFragmentManager ()
348+ .findFragmentByTag (RationaleDialogFragmentCompat .TAG );
349+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragmentCompat .class );
350+
351+ Dialog dialog = ((RationaleDialogFragmentCompat ) dialogFragment ).getDialog ();
352+ assertThatHasExpectedRationale (dialog , RATIONALE );
353+ }
354+
355+ @ SuppressWarnings ("deprecation" )
356+ @ Test
357+ public void shouldShowCorrectDialogUsingDeprecated_whenMissingPermissionsAndShowRationaleFromSupportActivity () {
358+ grantPermissions (ONE_PERM );
359+ showRationale (true , ALL_PERMS );
360+
361+ EasyPermissions .requestPermissions (spySupportActivity , RATIONALE , android .R .string .ok ,
362+ android .R .string .cancel , TestSupportActivity .REQUEST_CODE , ALL_PERMS );
363+
364+ android .support .v4 .app .Fragment dialogFragment = spySupportActivity .getSupportFragmentManager ()
365+ .findFragmentByTag (RationaleDialogFragmentCompat .TAG );
366+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragmentCompat .class );
367+
368+ Dialog dialog = ((RationaleDialogFragmentCompat ) dialogFragment ).getDialog ();
369+ assertThatHasExpectedButtonsAndRationale (dialog , RATIONALE ,
370+ android .R .string .ok , android .R .string .cancel );
371+ }
372+
373+ @ Test
374+ public void shouldShowCorrectDialogUsingRequest_whenMissingPermissionsAndShowRationaleFromSupportActivity () {
375+ grantPermissions (ONE_PERM );
376+ showRationale (true , ALL_PERMS );
377+
378+ PermissionRequest request = new PermissionRequest .Builder (spySupportActivity , TestSupportActivity .REQUEST_CODE , ALL_PERMS )
379+ .setPositiveButtonText (android .R .string .ok )
380+ .setNegativeButtonText (android .R .string .cancel )
381+ .setRationale (android .R .string .unknownName )
382+ .setTheme (R .style .Theme_AppCompat )
383+ .build ();
384+ EasyPermissions .requestPermissions (request );
385+
386+ android .support .v4 .app .Fragment dialogFragment = spySupportActivity .getSupportFragmentManager ()
387+ .findFragmentByTag (RationaleDialogFragmentCompat .TAG );
388+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragmentCompat .class );
389+
390+ Dialog dialog = ((RationaleDialogFragmentCompat ) dialogFragment ).getDialog ();
391+ assertThatHasExpectedButtonsAndRationale (dialog , android .R .string .unknownName ,
392+ android .R .string .ok , android .R .string .cancel );
393+ }
394+
395+ @ Test
396+ public void shouldHaveSomePermissionDenied_whenShowRationaleFromSupportActivity () {
397+ showRationale (true , ALL_PERMS );
398+
399+ assertThat (EasyPermissions .somePermissionDenied (spySupportActivity , ALL_PERMS )).isTrue ();
400+ }
401+
402+ @ Test
403+ public void shouldNotHaveSomePermissionDenied_whenNotShowRationaleFromSupportActivity () {
404+ showRationale (false , ALL_PERMS );
405+
406+ assertThat (EasyPermissions .somePermissionDenied (spySupportActivity , ALL_PERMS )).isFalse ();
407+ }
408+
409+ @ Test
410+ public void shouldHaveSomePermissionPermanentlyDenied_whenNotShowRationaleFromSupportActivity () {
411+ showRationale (false , ALL_PERMS );
412+
413+ assertThat (EasyPermissions .somePermissionPermanentlyDenied (spySupportActivity , Arrays .asList (ALL_PERMS ))).isTrue ();
414+ }
415+
416+ @ Test
417+ public void shouldNotHaveSomePermissionPermanentlyDenied_whenShowRationaleFromSupportActivity () {
418+ showRationale (true , ALL_PERMS );
419+
420+ assertThat (EasyPermissions .somePermissionPermanentlyDenied (spySupportActivity , Arrays .asList (ALL_PERMS ))).isFalse ();
421+ }
422+
423+ @ Test
424+ public void shouldHavePermissionPermanentlyDenied_whenNotShowRationaleFromSupportActivity () {
425+ showRationale (false , Manifest .permission .READ_SMS );
426+
427+ assertThat (EasyPermissions .permissionPermanentlyDenied (spySupportActivity , Manifest .permission .READ_SMS )).isTrue ();
428+ }
429+
430+ @ Test
431+ public void shouldNotHavePermissionPermanentlyDenied_whenShowRationaleFromSupportActivity () {
432+ showRationale (true , Manifest .permission .READ_SMS );
433+
434+ assertThat (EasyPermissions .permissionPermanentlyDenied (spySupportActivity , Manifest .permission .READ_SMS )).isFalse ();
435+ }
436+
275437 // ------ From Fragment ------
276438
277439 @ Test
@@ -633,18 +795,22 @@ private void assertThatHasExpectedRationale(Dialog dialog, String rationale) {
633795 private void setUpActivityAndFragment () {
634796 activityController = Robolectric .buildActivity (TestActivity .class )
635797 .create ().start ().resume ();
798+ supportActivityController = Robolectric .buildActivity (TestSupportActivity .class )
799+ .create ().start ().resume ();
636800 fragmentController = Robolectric .buildFragment (TestFragment .class )
637801 .create ().start ().resume ();
638802 supportFragmentController = SupportFragmentController .of (new TestSupportFragment ())
639803 .create ().start ().resume ();
640804
641805 spyActivity = Mockito .spy (activityController .get ());
806+ spySupportActivity = Mockito .spy (supportActivityController .get ());
642807 spyFragment = Mockito .spy (fragmentController .get ());
643808 spySupportFragment = Mockito .spy (supportFragmentController .get ());
644809 }
645810
646811 private void tearDownActivityAndFragment () {
647812 activityController .pause ().stop ().destroy ();
813+ supportActivityController .pause ().stop ().destroy ();
648814 fragmentController .pause ().stop ().destroy ();
649815 supportFragmentController .pause ().stop ().destroy ();
650816 }
@@ -656,6 +822,7 @@ private void grantPermissions(String[] perms) {
656822 private void showRationale (boolean show , String ... perms ) {
657823 for (String perm : perms ) {
658824 when (spyActivity .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
825+ when (spySupportActivity .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
659826 when (spyFragment .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
660827 when (spySupportFragment .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
661828 }
0 commit comments