30
30
31
31
import pub .devrel .easypermissions .testhelper .TestActivity ;
32
32
import pub .devrel .easypermissions .testhelper .TestFragment ;
33
+ import pub .devrel .easypermissions .testhelper .TestSupportActivity ;
33
34
import pub .devrel .easypermissions .testhelper .TestSupportFragment ;
34
35
35
36
import static com .google .common .truth .Truth .assertThat ;
@@ -58,9 +59,11 @@ public class EasyPermissionsTest {
58
59
PackageManager .PERMISSION_DENIED , PackageManager .PERMISSION_GRANTED };
59
60
private Application app ;
60
61
private TestActivity spyActivity ;
62
+ private TestSupportActivity spySupportActivity ;
61
63
private TestFragment spyFragment ;
62
64
private TestSupportFragment spySupportFragment ;
63
65
private ActivityController <TestActivity > activityController ;
66
+ private ActivityController <TestSupportActivity > supportActivityController ;
64
67
private FragmentController <TestFragment > fragmentController ;
65
68
private SupportFragmentController <TestSupportFragment > supportFragmentController ;
66
69
@ Captor
@@ -272,6 +275,165 @@ public void shouldNotHavePermissionPermanentlyDenied_whenShowRationaleFromActivi
272
275
assertThat (EasyPermissions .permissionPermanentlyDenied (spyActivity , Manifest .permission .READ_SMS )).isFalse ();
273
276
}
274
277
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
+
275
437
// ------ From Fragment ------
276
438
277
439
@ Test
@@ -633,18 +795,22 @@ private void assertThatHasExpectedRationale(Dialog dialog, String rationale) {
633
795
private void setUpActivityAndFragment () {
634
796
activityController = Robolectric .buildActivity (TestActivity .class )
635
797
.create ().start ().resume ();
798
+ supportActivityController = Robolectric .buildActivity (TestSupportActivity .class )
799
+ .create ().start ().resume ();
636
800
fragmentController = Robolectric .buildFragment (TestFragment .class )
637
801
.create ().start ().resume ();
638
802
supportFragmentController = SupportFragmentController .of (new TestSupportFragment ())
639
803
.create ().start ().resume ();
640
804
641
805
spyActivity = Mockito .spy (activityController .get ());
806
+ spySupportActivity = Mockito .spy (supportActivityController .get ());
642
807
spyFragment = Mockito .spy (fragmentController .get ());
643
808
spySupportFragment = Mockito .spy (supportFragmentController .get ());
644
809
}
645
810
646
811
private void tearDownActivityAndFragment () {
647
812
activityController .pause ().stop ().destroy ();
813
+ supportActivityController .pause ().stop ().destroy ();
648
814
fragmentController .pause ().stop ().destroy ();
649
815
supportFragmentController .pause ().stop ().destroy ();
650
816
}
@@ -656,6 +822,7 @@ private void grantPermissions(String[] perms) {
656
822
private void showRationale (boolean show , String ... perms ) {
657
823
for (String perm : perms ) {
658
824
when (spyActivity .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
825
+ when (spySupportActivity .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
659
826
when (spyFragment .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
660
827
when (spySupportFragment .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
661
828
}
0 commit comments