diff --git a/dexter/src/main/java/com/karumi/dexter/DexterInstance.java b/dexter/src/main/java/com/karumi/dexter/DexterInstance.java index 2d5fb3b8..2c4ad668 100644 --- a/dexter/src/main/java/com/karumi/dexter/DexterInstance.java +++ b/dexter/src/main/java/com/karumi/dexter/DexterInstance.java @@ -124,7 +124,10 @@ void onActivityReady(Activity activity) { * Method called whenever the inner activity has been destroyed. */ void onActivityDestroyed() { + activity = null; isRequestingPermission.set(false); + rationaleAccepted.set(false); + isShowingNativeDialog.set(false); listener = EMPTY_LISTENER; } diff --git a/dexter/src/test/java/com/karumi/dexter/DexterInstanceTest.java b/dexter/src/test/java/com/karumi/dexter/DexterInstanceTest.java index 1f1579ba..bf8e11dd 100644 --- a/dexter/src/test/java/com/karumi/dexter/DexterInstanceTest.java +++ b/dexter/src/test/java/com/karumi/dexter/DexterInstanceTest.java @@ -38,6 +38,7 @@ import java.util.Collections; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isA; @@ -139,6 +140,25 @@ public void onCheckPermissionMoreThanOnceThenThrowException() { thenPermissionIsDenied(ANY_PERMISSION); } + @Test(expected = DexterException.class) public void onCheckPermissionTwiceThenThrowException() { + givenPermissionIsAlreadyDenied(ANY_PERMISSION); + + whenCheckPermission(permissionListener, ANY_PERMISSION); + whenCheckPermission(permissionListener, ANY_PERMISSION); + + verifyRequestPermissions(new String[]{ANY_PERMISSION}, 1); + } + + @Test public void onCheckPermissionAgainAfterActivityDestroyedThenRequestedTwice() { + givenPermissionIsAlreadyDenied(ANY_PERMISSION); + + whenCheckPermission(permissionListener, ANY_PERMISSION); + dexter.onActivityDestroyed(); + whenCheckPermission(permissionListener, ANY_PERMISSION); + + verifyRequestPermissions(new String[]{ANY_PERMISSION}, 2); + } + private void givenPermissionIsAlreadyDenied(String permission) { givenPermissionIsChecked(permission, PackageManager.PERMISSION_DENIED); } @@ -191,6 +211,10 @@ private void whenCheckPermission(PermissionListener permissionListener, String p dexter.onActivityReady(activity); } + private void verifyRequestPermissions(String[] permissions, int nTimes) { + verify(androidPermissionService, times(nTimes)).requestPermissions(eq(activity), eq(permissions), anyInt()); + } + private void thenPermissionIsGranted(String permission) { verify(permissionListener).onPermissionGranted( argThat(getPermissionGrantedResponseMatcher(permission)));