|
2 | 2 |
|
3 | 3 | import android.Manifest;
|
4 | 4 |
|
| 5 | +import org.junit.Before; |
5 | 6 | import org.junit.Test;
|
6 | 7 | import org.junit.runner.RunWith;
|
| 8 | +import org.mockito.ArgumentCaptor; |
| 9 | +import org.mockito.Captor; |
| 10 | +import org.mockito.Mockito; |
| 11 | +import org.mockito.MockitoAnnotations; |
| 12 | +import org.robolectric.Robolectric; |
7 | 13 | import org.robolectric.RobolectricTestRunner;
|
8 | 14 | import org.robolectric.RuntimeEnvironment;
|
9 | 15 | import org.robolectric.annotation.Config;
|
| 16 | +import org.robolectric.shadows.ShadowApplication; |
| 17 | +import org.robolectric.shadows.support.v4.SupportFragmentController; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | + |
| 21 | +import pub.devrel.easypermissions.testhelper.TestActivity; |
| 22 | +import pub.devrel.easypermissions.testhelper.TestFragment; |
| 23 | +import pub.devrel.easypermissions.testhelper.TestSupportFragment; |
10 | 24 |
|
11 | 25 | import static com.google.common.truth.Truth.assertThat;
|
| 26 | +import static org.mockito.Mockito.times; |
| 27 | +import static org.mockito.Mockito.verify; |
12 | 28 |
|
13 | 29 | /**
|
14 | 30 | * Low-API (SDK = 19) tests for {@link pub.devrel.easypermissions.EasyPermissions}.
|
|
17 | 33 | @Config(sdk = 19)
|
18 | 34 | public class EasyPermissionsLowApiTest {
|
19 | 35 |
|
| 36 | + private static final int REQUEST_CODE = 5; |
| 37 | + private static final String RATIONALE = "RATIONALE"; |
| 38 | + private static final String[] ALL_PERMS = new String[]{ |
| 39 | + Manifest.permission.READ_SMS, Manifest.permission.ACCESS_FINE_LOCATION}; |
| 40 | + |
| 41 | + private TestActivity spyActivity; |
| 42 | + private TestFragment spyFragment; |
| 43 | + private TestSupportFragment spySupportFragment; |
| 44 | + @Captor |
| 45 | + private ArgumentCaptor<Integer> integerCaptor; |
| 46 | + @Captor |
| 47 | + private ArgumentCaptor<ArrayList<String>> listCaptor; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() { |
| 51 | + MockitoAnnotations.initMocks(this); |
| 52 | + setUpActivityAndFragment(); |
| 53 | + } |
| 54 | + |
20 | 55 | @Test
|
21 | 56 | public void shouldHavePermission_whenHasPermissionsBeforeMarshmallow() {
|
22 |
| - // On low-API devices, we should always get 'true' when we call 'hasPermissions' |
23 | 57 | assertThat(EasyPermissions.hasPermissions(RuntimeEnvironment.application,
|
24 | 58 | Manifest.permission.ACCESS_COARSE_LOCATION)).isTrue();
|
25 | 59 | }
|
| 60 | + |
| 61 | + @Test |
| 62 | + public void shouldCallbackOnPermissionGranted_whenRequestFromActivity() { |
| 63 | + EasyPermissions.requestPermissions(spyActivity, RATIONALE, REQUEST_CODE, ALL_PERMS); |
| 64 | + |
| 65 | + verify(spyActivity, times(1)) |
| 66 | + .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); |
| 67 | + assertThat(integerCaptor.getValue()).isEqualTo(REQUEST_CODE); |
| 68 | + assertThat(listCaptor.getValue()).containsAllIn(ALL_PERMS); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void shouldCallbackOnPermissionGranted_whenRequestFromFragment() { |
| 73 | + EasyPermissions.requestPermissions(spyFragment, RATIONALE, REQUEST_CODE, ALL_PERMS); |
| 74 | + |
| 75 | + verify(spyFragment, times(1)) |
| 76 | + .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); |
| 77 | + assertThat(integerCaptor.getValue()).isEqualTo(REQUEST_CODE); |
| 78 | + assertThat(listCaptor.getValue()).containsAllIn(ALL_PERMS); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void shouldCallbackOnPermissionGranted_whenRequestFromSupportedFragment() { |
| 83 | + EasyPermissions.requestPermissions(spySupportFragment, RATIONALE, REQUEST_CODE, ALL_PERMS); |
| 84 | + |
| 85 | + verify(spySupportFragment, times(1)) |
| 86 | + .onPermissionsGranted(integerCaptor.capture(), listCaptor.capture()); |
| 87 | + assertThat(integerCaptor.getValue()).isEqualTo(REQUEST_CODE); |
| 88 | + assertThat(listCaptor.getValue()).containsAllIn(ALL_PERMS); |
| 89 | + } |
| 90 | + |
| 91 | + private void setUpActivityAndFragment() { |
| 92 | + TestActivity activity = Robolectric.buildActivity(TestActivity.class) |
| 93 | + .create().start().resume().get(); |
| 94 | + TestFragment fragment = Robolectric.buildFragment(TestFragment.class) |
| 95 | + .create().start().resume().get(); |
| 96 | + TestSupportFragment supportFragment = SupportFragmentController.of(new TestSupportFragment()) |
| 97 | + .create().start().resume().get(); |
| 98 | + |
| 99 | + spyActivity = Mockito.spy(activity); |
| 100 | + spyFragment = Mockito.spy(fragment); |
| 101 | + spySupportFragment = Mockito.spy(supportFragment); |
| 102 | + } |
26 | 103 | }
|
0 commit comments