Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Differentiate between FragmentActivity and AppCompatActivity
Browse files Browse the repository at this point in the history
Change-Id: I826cdfca1e5ed0c33ebcba7314e3a41ddd0913b0
  • Loading branch information
samtstern committed Jan 3, 2019
1 parent d5b707c commit e64510d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

/**
* Permissions helper for {@link AppCompatActivity}.
*/
class FragmentActivityPermissionHelper extends BaseSupportPermissionsHelper<FragmentActivity> {
class AppCompatActivityPermissionsHelper extends BaseSupportPermissionsHelper<AppCompatActivity> {

public FragmentActivityPermissionHelper(FragmentActivity host) {
public AppCompatActivityPermissionsHelper(AppCompatActivity host) {
super(host);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;

import java.util.List;

Expand All @@ -23,8 +23,8 @@ public static PermissionHelper<? extends Activity> newInstance(Activity host) {
return new LowApiPermissionsHelper<>(host);
}

if (host instanceof FragmentActivity)
return new FragmentActivityPermissionHelper((FragmentActivity) host);
if (host instanceof AppCompatActivity)
return new AppCompatActivityPermissionsHelper((AppCompatActivity) host);
else {
return new ActivityPermissionHelper(host);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import pub.devrel.easypermissions.testhelper.TestActivity;
import pub.devrel.easypermissions.testhelper.TestFragment;
import pub.devrel.easypermissions.testhelper.TestSupportActivity;
import pub.devrel.easypermissions.testhelper.TestAppCompatActivity;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.times;
Expand All @@ -39,10 +39,10 @@ public class EasyPermissionsLowApiTest {
Manifest.permission.READ_SMS, Manifest.permission.ACCESS_FINE_LOCATION};

private TestActivity spyActivity;
private TestSupportActivity spySupportActivity;
private TestAppCompatActivity spySupportActivity;
private TestFragment spyFragment;
private ActivityController<TestActivity> activityController;
private ActivityController<TestSupportActivity> supportActivityController;
private ActivityController<TestAppCompatActivity> supportActivityController;
private SupportFragmentController<TestFragment> supportController;
@Captor
private ArgumentCaptor<Integer> integerCaptor;
Expand Down Expand Up @@ -84,11 +84,11 @@ public void shouldCallbackOnPermissionGranted_whenRequestFromActivity() {

@Test
public void shouldCallbackOnPermissionGranted_whenRequestFromSupportActivity() {
EasyPermissions.requestPermissions(spySupportActivity, RATIONALE, TestSupportActivity.REQUEST_CODE, ALL_PERMS);
EasyPermissions.requestPermissions(spySupportActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS);

verify(spySupportActivity, times(1))
.onPermissionsGranted(integerCaptor.capture(), listCaptor.capture());
assertThat(integerCaptor.getValue()).isEqualTo(TestSupportActivity.REQUEST_CODE);
assertThat(integerCaptor.getValue()).isEqualTo(TestAppCompatActivity.REQUEST_CODE);
assertThat(listCaptor.getValue()).containsAllIn(ALL_PERMS);
}

Expand All @@ -105,7 +105,7 @@ public void shouldCallbackOnPermissionGranted_whenRequestFromFragment() {
private void setUpActivityAndFragment() {
activityController = Robolectric.buildActivity(TestActivity.class)
.create().start().resume();
supportActivityController = Robolectric.buildActivity(TestSupportActivity.class)
supportActivityController = Robolectric.buildActivity(TestAppCompatActivity.class)
.create().start().resume();
supportController = SupportFragmentController.of(new TestFragment())
.create().start().resume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
import java.util.Collections;

import pub.devrel.easypermissions.testhelper.TestActivity;
import pub.devrel.easypermissions.testhelper.TestAppCompatActivity;
import pub.devrel.easypermissions.testhelper.TestFragment;
import pub.devrel.easypermissions.testhelper.TestSupportActivity;
import pub.devrel.easypermissions.testhelper.TestSupportFragmentActivity;

import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.fail;
Expand Down Expand Up @@ -57,10 +58,12 @@ public class EasyPermissionsTest {
PackageManager.PERMISSION_DENIED, PackageManager.PERMISSION_GRANTED};
private Application app;
private TestActivity spyActivity;
private TestSupportActivity spySupportActivity;
private TestSupportFragmentActivity spySupportFragmentActivity;
private TestAppCompatActivity spyAppCompatActivity;
private TestFragment spyFragment;
private ActivityController<TestActivity> activityController;
private ActivityController<TestSupportActivity> supportActivityController;
private ActivityController<TestSupportFragmentActivity> supportFragmentActivityController;
private ActivityController<TestAppCompatActivity> appCompatActivityController;
private SupportFragmentController<TestFragment> fragmentController;
@Captor
private ArgumentCaptor<Integer> integerCaptor;
Expand Down Expand Up @@ -254,75 +257,75 @@ public void shouldNotHavePermissionPermanentlyDenied_whenShowRationaleFromActivi
}

@Test
public void shouldCorrectlyCallback_whenOnRequestPermissionResultCalledFromSupportActivity() {
EasyPermissions.onRequestPermissionsResult(TestSupportActivity.REQUEST_CODE, ALL_PERMS, SMS_DENIED_RESULT, spySupportActivity);
public void shouldCorrectlyCallback_whenOnRequestPermissionResultCalledFromAppCompatActivity() {
EasyPermissions.onRequestPermissionsResult(TestAppCompatActivity.REQUEST_CODE, ALL_PERMS, SMS_DENIED_RESULT, spyAppCompatActivity);

verify(spySupportActivity, times(1))
verify(spyAppCompatActivity, times(1))
.onPermissionsGranted(integerCaptor.capture(), listCaptor.capture());
assertThat(integerCaptor.getValue()).isEqualTo(TestSupportActivity.REQUEST_CODE);
assertThat(integerCaptor.getValue()).isEqualTo(TestAppCompatActivity.REQUEST_CODE);
assertThat(listCaptor.getValue())
.containsAllIn(new ArrayList<>(Collections.singletonList(Manifest.permission.ACCESS_FINE_LOCATION)));

verify(spySupportActivity, times(1))
verify(spyAppCompatActivity, times(1))
.onPermissionsDenied(integerCaptor.capture(), listCaptor.capture());
assertThat(integerCaptor.getValue()).isEqualTo(TestSupportActivity.REQUEST_CODE);
assertThat(integerCaptor.getValue()).isEqualTo(TestAppCompatActivity.REQUEST_CODE);
assertThat(listCaptor.getValue())
.containsAllIn(new ArrayList<>(Collections.singletonList(Manifest.permission.READ_SMS)));

verify(spySupportActivity, never()).afterPermissionGranted();
verify(spyAppCompatActivity, never()).afterPermissionGranted();
}

@Test
public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionsFromSupportActivity() {
public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionsFromAppCompatActivity() {
grantPermissions(ALL_PERMS);

EasyPermissions.requestPermissions(spySupportActivity, RATIONALE, TestSupportActivity.REQUEST_CODE, ALL_PERMS);
EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS);

verify(spySupportActivity, times(1))
verify(spyAppCompatActivity, times(1))
.onPermissionsGranted(integerCaptor.capture(), listCaptor.capture());
verify(spySupportActivity, never()).requestPermissions(any(String[].class), anyInt());
assertThat(integerCaptor.getValue()).isEqualTo(TestSupportActivity.REQUEST_CODE);
verify(spyAppCompatActivity, never()).requestPermissions(any(String[].class), anyInt());
assertThat(integerCaptor.getValue()).isEqualTo(TestAppCompatActivity.REQUEST_CODE);
assertThat(listCaptor.getValue()).containsAllIn(ALL_PERMS);
}

@Test
public void shouldCallbackAfterPermissionGranted_whenRequestAlreadyGrantedPermissionsFromSupportActivity() {
public void shouldCallbackAfterPermissionGranted_whenRequestAlreadyGrantedPermissionsFromAppCompatActivity() {
grantPermissions(ALL_PERMS);

EasyPermissions.requestPermissions(spySupportActivity, RATIONALE, TestSupportActivity.REQUEST_CODE, ALL_PERMS);
EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS);

// Called 2 times because this is a spy and library implementation invokes super classes annotated methods as well
verify(spySupportActivity, times(2)).afterPermissionGranted();
verify(spyAppCompatActivity, times(2)).afterPermissionGranted();
}

@Test
public void shouldNotCallbackAfterPermissionGranted_whenRequestNotGrantedPermissionsFromSupportActivity() {
public void shouldNotCallbackAfterPermissionGranted_whenRequestNotGrantedPermissionsFromAppCompatActivity() {
grantPermissions(ONE_PERM);

EasyPermissions.requestPermissions(spySupportActivity, RATIONALE, TestSupportActivity.REQUEST_CODE, ALL_PERMS);
EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS);

verify(spySupportActivity, never()).afterPermissionGranted();
verify(spyAppCompatActivity, never()).afterPermissionGranted();
}

@Test
public void shouldRequestPermissions_whenMissingPermissionAndNotShowRationaleFromSupportActivity() {
public void shouldRequestPermissions_whenMissingPermissionAndNotShowRationaleFromAppCompatActivity() {
grantPermissions(ONE_PERM);
showRationale(false, ALL_PERMS);

EasyPermissions.requestPermissions(spySupportActivity, RATIONALE, TestSupportActivity.REQUEST_CODE, ALL_PERMS);
EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS);

verify(spySupportActivity, times(1))
.requestPermissions(ALL_PERMS, TestSupportActivity.REQUEST_CODE);
verify(spyAppCompatActivity, times(1))
.requestPermissions(ALL_PERMS, TestAppCompatActivity.REQUEST_CODE);
}

@Test
public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromSupportActivity() {
public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromAppCompatActivity() {
grantPermissions(ONE_PERM);
showRationale(true, ALL_PERMS);

EasyPermissions.requestPermissions(spySupportActivity, RATIONALE, TestSupportActivity.REQUEST_CODE, ALL_PERMS);
EasyPermissions.requestPermissions(spyAppCompatActivity, RATIONALE, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS);

android.support.v4.app.Fragment dialogFragment = spySupportActivity.getSupportFragmentManager()
android.support.v4.app.Fragment dialogFragment = spyAppCompatActivity.getSupportFragmentManager()
.findFragmentByTag(RationaleDialogFragmentCompat.TAG);
assertThat(dialogFragment).isInstanceOf(RationaleDialogFragmentCompat.class);

Expand All @@ -331,19 +334,34 @@ public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromSu
}

@Test
public void shouldShowCorrectDialogUsingRequest_whenMissingPermissionsAndShowRationaleFromSupportActivity() {
public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromSupportFragmentActivity() {
grantPermissions(ONE_PERM);
showRationale(true, ALL_PERMS);

PermissionRequest request = new PermissionRequest.Builder(spySupportActivity, TestSupportActivity.REQUEST_CODE, ALL_PERMS)
EasyPermissions.requestPermissions(spySupportFragmentActivity, RATIONALE, TestSupportFragmentActivity.REQUEST_CODE, ALL_PERMS);

Fragment dialogFragment = spySupportFragmentActivity.getFragmentManager()
.findFragmentByTag(RationaleDialogFragment.TAG);
assertThat(dialogFragment).isInstanceOf(RationaleDialogFragment.class);

Dialog dialog = ((RationaleDialogFragment) dialogFragment).getDialog();
assertThatHasExpectedRationale(dialog, RATIONALE);
}

@Test
public void shouldShowCorrectDialogUsingRequest_whenMissingPermissionsAndShowRationaleFromAppCompatActivity() {
grantPermissions(ONE_PERM);
showRationale(true, ALL_PERMS);

PermissionRequest request = new PermissionRequest.Builder(spyAppCompatActivity, TestAppCompatActivity.REQUEST_CODE, ALL_PERMS)
.setPositiveButtonText(android.R.string.ok)
.setNegativeButtonText(android.R.string.cancel)
.setRationale(android.R.string.unknownName)
.setTheme(R.style.Theme_AppCompat)
.build();
EasyPermissions.requestPermissions(request);

android.support.v4.app.Fragment dialogFragment = spySupportActivity.getSupportFragmentManager()
android.support.v4.app.Fragment dialogFragment = spyAppCompatActivity.getSupportFragmentManager()
.findFragmentByTag(RationaleDialogFragmentCompat.TAG);
assertThat(dialogFragment).isInstanceOf(RationaleDialogFragmentCompat.class);

Expand All @@ -353,45 +371,45 @@ public void shouldShowCorrectDialogUsingRequest_whenMissingPermissionsAndShowRat
}

@Test
public void shouldHaveSomePermissionDenied_whenShowRationaleFromSupportActivity() {
public void shouldHaveSomePermissionDenied_whenShowRationaleFromAppCompatActivity() {
showRationale(true, ALL_PERMS);

assertThat(EasyPermissions.somePermissionDenied(spySupportActivity, ALL_PERMS)).isTrue();
assertThat(EasyPermissions.somePermissionDenied(spyAppCompatActivity, ALL_PERMS)).isTrue();
}

@Test
public void shouldNotHaveSomePermissionDenied_whenNotShowRationaleFromSupportActivity() {
public void shouldNotHaveSomePermissionDenied_whenNotShowRationaleFromAppCompatActivity() {
showRationale(false, ALL_PERMS);

assertThat(EasyPermissions.somePermissionDenied(spySupportActivity, ALL_PERMS)).isFalse();
assertThat(EasyPermissions.somePermissionDenied(spyAppCompatActivity, ALL_PERMS)).isFalse();
}

@Test
public void shouldHaveSomePermissionPermanentlyDenied_whenNotShowRationaleFromSupportActivity() {
public void shouldHaveSomePermissionPermanentlyDenied_whenNotShowRationaleFromAppCompatActivity() {
showRationale(false, ALL_PERMS);

assertThat(EasyPermissions.somePermissionPermanentlyDenied(spySupportActivity, Arrays.asList(ALL_PERMS))).isTrue();
assertThat(EasyPermissions.somePermissionPermanentlyDenied(spyAppCompatActivity, Arrays.asList(ALL_PERMS))).isTrue();
}

@Test
public void shouldNotHaveSomePermissionPermanentlyDenied_whenShowRationaleFromSupportActivity() {
public void shouldNotHaveSomePermissionPermanentlyDenied_whenShowRationaleFromAppCompatActivity() {
showRationale(true, ALL_PERMS);

assertThat(EasyPermissions.somePermissionPermanentlyDenied(spySupportActivity, Arrays.asList(ALL_PERMS))).isFalse();
assertThat(EasyPermissions.somePermissionPermanentlyDenied(spyAppCompatActivity, Arrays.asList(ALL_PERMS))).isFalse();
}

@Test
public void shouldHavePermissionPermanentlyDenied_whenNotShowRationaleFromSupportActivity() {
public void shouldHavePermissionPermanentlyDenied_whenNotShowRationaleFromAppCompatActivity() {
showRationale(false, Manifest.permission.READ_SMS);

assertThat(EasyPermissions.permissionPermanentlyDenied(spySupportActivity, Manifest.permission.READ_SMS)).isTrue();
assertThat(EasyPermissions.permissionPermanentlyDenied(spyAppCompatActivity, Manifest.permission.READ_SMS)).isTrue();
}

@Test
public void shouldNotHavePermissionPermanentlyDenied_whenShowRationaleFromSupportActivity() {
public void shouldNotHavePermissionPermanentlyDenied_whenShowRationaleFromAppCompatActivity() {
showRationale(true, Manifest.permission.READ_SMS);

assertThat(EasyPermissions.permissionPermanentlyDenied(spySupportActivity, Manifest.permission.READ_SMS)).isFalse();
assertThat(EasyPermissions.permissionPermanentlyDenied(spyAppCompatActivity, Manifest.permission.READ_SMS)).isFalse();
}

@Test
Expand Down Expand Up @@ -575,19 +593,23 @@ private void assertThatHasExpectedRationale(Dialog dialog, String rationale) {
private void setUpActivityAndFragment() {
activityController = Robolectric.buildActivity(TestActivity.class)
.create().start().resume();
supportActivityController = Robolectric.buildActivity(TestSupportActivity.class)
supportFragmentActivityController = Robolectric.buildActivity(TestSupportFragmentActivity.class)
.create().start().resume();
appCompatActivityController = Robolectric.buildActivity(TestAppCompatActivity.class)
.create().start().resume();
fragmentController = SupportFragmentController.of(new TestFragment())
.create().start().resume();

spyActivity = Mockito.spy(activityController.get());
spySupportActivity = Mockito.spy(supportActivityController.get());
spySupportFragmentActivity = Mockito.spy(supportFragmentActivityController.get());
spyAppCompatActivity = Mockito.spy(appCompatActivityController.get());
spyFragment = Mockito.spy(fragmentController.get());
}

private void tearDownActivityAndFragment() {
activityController.pause().stop().destroy();
supportActivityController.pause().stop().destroy();
supportFragmentActivityController.pause().stop().destroy();
appCompatActivityController.pause().stop().destroy();
fragmentController.pause().stop().destroy();
}

Expand All @@ -598,7 +620,8 @@ private void grantPermissions(String[] perms) {
private void showRationale(boolean show, String... perms) {
for (String perm : perms) {
when(spyActivity.shouldShowRequestPermissionRationale(perm)).thenReturn(show);
when(spySupportActivity.shouldShowRequestPermissionRationale(perm)).thenReturn(show);
when(spySupportFragmentActivity.shouldShowRequestPermissionRationale(perm)).thenReturn(show);
when(spyAppCompatActivity.shouldShowRequestPermissionRationale(perm)).thenReturn(show);
when(spyFragment.shouldShowRequestPermissionRationale(perm)).thenReturn(show);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.R;

public class TestSupportActivity extends AppCompatActivity
public class TestAppCompatActivity extends AppCompatActivity
implements EasyPermissions.PermissionCallbacks, EasyPermissions.RationaleCallbacks {

public static final int REQUEST_CODE = 3;
Expand Down
Loading

0 comments on commit e64510d

Please sign in to comment.