Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Fix request permission again after activity destroyed (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
komitake authored and pedrovgs committed Sep 17, 2019
1 parent 9c9fa35 commit 0cb2423
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dexter/src/main/java/com/karumi/dexter/DexterInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 24 additions & 0 deletions dexter/src/test/java/com/karumi/dexter/DexterInstanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 0cb2423

Please sign in to comment.