22
33import android .Manifest ;
44import android .app .Application ;
5+ import android .app .Dialog ;
56import android .app .Fragment ;
7+ import android .widget .TextView ;
68
79import org .junit .Before ;
810import org .junit .Test ;
1618import org .robolectric .RuntimeEnvironment ;
1719import org .robolectric .annotation .Config ;
1820import org .robolectric .shadows .ShadowApplication ;
21+ import org .robolectric .shadows .support .v4 .SupportFragmentController ;
1922
2023import java .util .ArrayList ;
2124
2225import pub .devrel .easypermissions .testhelper .TestActivity ;
26+ import pub .devrel .easypermissions .testhelper .TestFragment ;
27+ import pub .devrel .easypermissions .testhelper .TestSupportFragment ;
2328
2429import static com .google .common .truth .Truth .assertThat ;
2530import static junit .framework .Assert .fail ;
3843public class EasyPermissionsTest {
3944
4045 private static final int REQUEST_CODE = 10 ;
41- private static final String RATIONALE = "some rationale" ;
46+ private static final String RATIONALE = "RATIONALE" ;
47+ private static final String [] ONE_PERM = new String []{Manifest .permission .READ_SMS };
48+ private static final String [] ALL_PERMS = new String []{
49+ Manifest .permission .READ_SMS , Manifest .permission .ACCESS_FINE_LOCATION };
50+
4251 private Application app ;
43- private String [] allPerms ;
44- private String [] onePerm ;
4552 private TestActivity spyActivity ;
46- @ Captor private ArgumentCaptor <Integer > integerCaptor ;
47- @ Captor private ArgumentCaptor <ArrayList <String >> listCaptor ;
53+ private TestFragment spyFragment ;
54+ private TestSupportFragment spySupportFragment ;
55+ @ Captor
56+ private ArgumentCaptor <Integer > integerCaptor ;
57+ @ Captor
58+ private ArgumentCaptor <ArrayList <String >> listCaptor ;
4859
4960 @ Before
5061 public void setUp () {
5162 MockitoAnnotations .initMocks (this );
52-
63+ setUpActivityAndFragment ();
5364 app = RuntimeEnvironment .application ;
54- spyActivity = Mockito .spy (Robolectric .buildActivity (TestActivity .class ).get ());
55- allPerms = new String []{
56- Manifest .permission .READ_SMS ,
57- Manifest .permission .ACCESS_FINE_LOCATION
58- };
59- onePerm = new String []{
60- Manifest .permission .READ_SMS ,
61- };
6265 }
6366
6467 @ Test
6568 public void shouldNotHavePermissions_whenNoPermissionsGranted () {
66- assertThat (EasyPermissions .hasPermissions (app , allPerms )).isFalse ();
69+ assertThat (EasyPermissions .hasPermissions (app , ALL_PERMS )).isFalse ();
6770 }
6871
6972 @ Test
7073 public void shouldNotHavePermissions_whenNotAllPermissionsGranted () {
71- ShadowApplication .getInstance ().grantPermissions (onePerm );
72- assertThat (EasyPermissions .hasPermissions (app , allPerms )).isFalse ();
74+ ShadowApplication .getInstance ().grantPermissions (ONE_PERM );
75+ assertThat (EasyPermissions .hasPermissions (app , ALL_PERMS )).isFalse ();
7376 }
7477
7578 @ Test
7679 public void shouldHavePermissions_whenAllPermissionsGranted () {
77- ShadowApplication .getInstance ().grantPermissions (allPerms );
78- assertThat (EasyPermissions .hasPermissions (app , allPerms )).isTrue ();
80+ ShadowApplication .getInstance ().grantPermissions (ALL_PERMS );
81+ assertThat (EasyPermissions .hasPermissions (app , ALL_PERMS )).isTrue ();
7982 }
8083
8184 @ Test
8285 public void shouldThrowException_whenHasPermissionsWithNullContext () {
8386 try {
84- EasyPermissions .hasPermissions (null , allPerms );
87+ EasyPermissions .hasPermissions (null , ALL_PERMS );
8588 fail ("IllegalStateException expected because of null context." );
8689 } catch (IllegalArgumentException e ) {
8790 assertThat (e ).hasMessageThat ()
@@ -90,47 +93,230 @@ public void shouldThrowException_whenHasPermissionsWithNullContext() {
9093 }
9194
9295 @ Test
93- public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermission () {
94- grantPermissions (allPerms );
96+ public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionFromActivity () {
97+ grantPermissions (ALL_PERMS );
9598
96- EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , allPerms );
99+ EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , ALL_PERMS );
97100
98101 verify (spyActivity , times (1 ))
99102 .onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
100103 verify (spyActivity , never ()).requestPermissions (any (String [].class ), anyInt ());
101104 assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
102- assertThat (listCaptor .getValue ()).containsAllIn (allPerms );
105+ assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
106+ }
107+
108+ @ Test
109+ public void shouldRequestPermissions_whenMissingPermissionAndNotShowRationaleFromActivity () {
110+ grantPermissions (ONE_PERM );
111+ showRationale (ONE_PERM , false );
112+
113+ EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , ALL_PERMS );
114+
115+ verify (spyActivity , times (1 )).requestPermissions (ALL_PERMS , REQUEST_CODE );
116+ }
117+
118+ @ Test
119+ public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromActivity () {
120+ grantPermissions (ONE_PERM );
121+ showRationale (ONE_PERM , true );
122+
123+ EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , ALL_PERMS );
124+
125+ Fragment dialogFragment = spyActivity .getFragmentManager ()
126+ .findFragmentByTag (RationaleDialogFragment .TAG );
127+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragment .class );
128+
129+ Dialog dialog = ((RationaleDialogFragment ) dialogFragment ).getDialog ();
130+ assertThatHasExpectedRationale (dialog , RATIONALE );
131+ }
132+
133+ @ Test
134+ public void shouldShowCorrectDialogUsingDeprecated_whenMissingPermissionsAndShowRationaleFromActivity () {
135+ grantPermissions (ONE_PERM );
136+ showRationale (ONE_PERM , true );
137+
138+ EasyPermissions .requestPermissions (spyActivity , RATIONALE , android .R .string .ok ,
139+ android .R .string .cancel , REQUEST_CODE , ALL_PERMS );
140+
141+ Fragment dialogFragment = spyActivity .getFragmentManager ()
142+ .findFragmentByTag (RationaleDialogFragment .TAG );
143+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragment .class );
144+
145+ Dialog dialog = ((RationaleDialogFragment ) dialogFragment ).getDialog ();
146+ assertThatHasExpectedButtonsAndRationale (dialog , RATIONALE ,
147+ android .R .string .ok , android .R .string .cancel );
148+ }
149+
150+ @ Test
151+ public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionFromFragment () {
152+ grantPermissions (ALL_PERMS );
153+
154+ EasyPermissions .requestPermissions (spyFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
155+
156+ verify (spyFragment , times (1 ))
157+ .onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
158+ verify (spyFragment , never ()).requestPermissions (any (String [].class ), anyInt ());
159+ assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
160+ assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
103161 }
104162
105163 @ Test
106- public void shouldRequestPermissions_whenMissingPermissionsAndShouldNotShowRationale () {
107- grantPermissions (onePerm );
108- showRationale (onePerm , false );
164+ public void shouldRequestPermissions_whenMissingPermissionsAndNotShowRationaleFromFragment () {
165+ grantPermissions (ONE_PERM );
166+ showRationale (ONE_PERM , false );
109167
110- EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , allPerms );
168+ EasyPermissions .requestPermissions (spyFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
111169
112- verify (spyActivity , times (1 )).requestPermissions (allPerms , REQUEST_CODE );
170+ verify (spyFragment , times (1 )).requestPermissions (ALL_PERMS , REQUEST_CODE );
113171 }
114172
115173 @ Test
116- public void shouldRequestPermissions_whenMissingPermissionsAndShouldShowRationale () {
117- grantPermissions (onePerm );
118- showRationale (onePerm , true );
174+ public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromFragment () {
175+ grantPermissions (ONE_PERM );
176+ showRationale (ONE_PERM , true );
119177
120- EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , allPerms );
178+ EasyPermissions .requestPermissions (spyFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
121179
122- Fragment dialog = spyActivity . getFragmentManager ()
180+ Fragment dialogFragment = spyFragment . getChildFragmentManager ()
123181 .findFragmentByTag (RationaleDialogFragment .TAG );
124- assertThat (dialog ).isInstanceOf (RationaleDialogFragment .class );
182+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragment .class );
183+
184+ Dialog dialog = ((RationaleDialogFragment ) dialogFragment ).getDialog ();
185+ assertThatHasExpectedRationale (dialog , RATIONALE );
186+ }
187+
188+ @ Test
189+ public void shouldShowCorrectDialogUsingDeprecated_whenMissingPermissionsAndShowRationaleFromFragment () {
190+ grantPermissions (ONE_PERM );
191+ showRationale (ONE_PERM , true );
192+
193+ EasyPermissions .requestPermissions (spyFragment , RATIONALE , android .R .string .ok ,
194+ android .R .string .cancel , REQUEST_CODE , ALL_PERMS );
195+
196+ Fragment dialogFragment = spyFragment .getChildFragmentManager ()
197+ .findFragmentByTag (RationaleDialogFragment .TAG );
198+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragment .class );
199+
200+ Dialog dialog = ((RationaleDialogFragment ) dialogFragment ).getDialog ();
201+ assertThatHasExpectedButtonsAndRationale (dialog , RATIONALE ,
202+ android .R .string .ok , android .R .string .cancel );
203+ }
204+
205+ @ Test
206+ public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionFromSupportFragment () {
207+ grantPermissions (ALL_PERMS );
208+
209+ EasyPermissions .requestPermissions (spySupportFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
210+
211+ verify (spySupportFragment , times (1 ))
212+ .onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
213+ verify (spySupportFragment , never ()).requestPermissions (any (String [].class ), anyInt ());
214+ assertThat (integerCaptor .getValue ()).isEqualTo (REQUEST_CODE );
215+ assertThat (listCaptor .getValue ()).containsAllIn (ALL_PERMS );
216+ }
217+
218+ @ Test
219+ public void shouldRequestPermissions_whenMissingPermissionsAndNotShowRationaleFromSupportFragment () {
220+ grantPermissions (ONE_PERM );
221+ showRationale (ONE_PERM , false );
222+
223+ EasyPermissions .requestPermissions (spySupportFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
224+
225+ verify (spySupportFragment , times (1 )).requestPermissions (ALL_PERMS , REQUEST_CODE );
226+ }
227+
228+ @ Test
229+ public void shouldShowCorrectDialogUsingDeprecated_whenMissingPermissionsAndShowRationaleFromSupportFragment () {
230+ grantPermissions (ONE_PERM );
231+ showRationale (ONE_PERM , true );
232+
233+ EasyPermissions .requestPermissions (spySupportFragment , RATIONALE , android .R .string .ok ,
234+ android .R .string .cancel , REQUEST_CODE , ALL_PERMS );
235+
236+ android .support .v4 .app .Fragment dialogFragment = spySupportFragment .getChildFragmentManager ()
237+ .findFragmentByTag (RationaleDialogFragmentCompat .TAG );
238+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragmentCompat .class );
239+
240+ Dialog dialog = ((RationaleDialogFragmentCompat ) dialogFragment ).getDialog ();
241+ assertThatHasExpectedButtonsAndRationale (dialog , RATIONALE ,
242+ android .R .string .ok , android .R .string .cancel );
243+ }
244+
245+ @ Test
246+ public void shouldShowCorrectDialog_whenMissingPermissionsAndShowRationaleFromSupportFragment () {
247+ grantPermissions (ONE_PERM );
248+ showRationale (ONE_PERM , true );
249+
250+ EasyPermissions .requestPermissions (spySupportFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
251+
252+ android .support .v4 .app .Fragment dialogFragment = spySupportFragment .getChildFragmentManager ()
253+ .findFragmentByTag (RationaleDialogFragmentCompat .TAG );
254+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragmentCompat .class );
255+
256+ Dialog dialog = ((RationaleDialogFragmentCompat ) dialogFragment ).getDialog ();
257+ assertThatHasExpectedRationale (dialog , RATIONALE );
258+ }
259+
260+ @ Test
261+ public void shouldShowCorrectDialogUsingRequest_whenMissingPermissionsAndShowRationaleFromSupportFragment () {
262+ grantPermissions (ONE_PERM );
263+ showRationale (ONE_PERM , true );
264+
265+ PermissionRequest request = new PermissionRequest .Builder (spyFragment , REQUEST_CODE , ALL_PERMS )
266+ .setPositiveButtonText (android .R .string .ok )
267+ .setNegativeButtonText (android .R .string .cancel )
268+ .setRationale (RATIONALE )
269+ .setTheme (R .style .Theme_AppCompat )
270+ .build ();
271+ EasyPermissions .requestPermissions (request );
272+
273+ Fragment dialogFragment = spyFragment .getChildFragmentManager ()
274+ .findFragmentByTag (RationaleDialogFragment .TAG );
275+ assertThat (dialogFragment ).isInstanceOf (RationaleDialogFragment .class );
276+
277+ Dialog dialog = ((RationaleDialogFragment ) dialogFragment ).getDialog ();
278+ assertThatHasExpectedButtonsAndRationale (dialog , RATIONALE ,
279+ android .R .string .ok , android .R .string .cancel );
280+ }
281+
282+ private void assertThatHasExpectedButtonsAndRationale (Dialog dialog , String rationale ,
283+ int positive , int negative ) {
284+ TextView dialogMessage = dialog .findViewById (android .R .id .message );
285+ assertThat (dialogMessage .getText ().toString ()).isEqualTo (rationale );
286+ TextView positiveMessage = dialog .findViewById (android .R .id .button1 );
287+ assertThat (positiveMessage .getText ().toString ()).isEqualTo (app .getString (positive ));
288+ TextView negativeMessage = dialog .findViewById (android .R .id .button2 );
289+ assertThat (negativeMessage .getText ().toString ()).isEqualTo (app .getString (negative ));
290+
291+ }
292+
293+ private void assertThatHasExpectedRationale (Dialog dialog , String rationale ) {
294+ TextView dialogMessage = dialog .findViewById (android .R .id .message );
295+ assertThat (dialogMessage .getText ().toString ()).isEqualTo (rationale );
296+ }
297+
298+ private void setUpActivityAndFragment () {
299+ TestActivity activity = Robolectric .buildActivity (TestActivity .class )
300+ .create ().start ().resume ().get ();
301+ TestFragment fragment = Robolectric .buildFragment (TestFragment .class )
302+ .create ().start ().resume ().get ();
303+ TestSupportFragment supportFragment = SupportFragmentController .of (new TestSupportFragment ())
304+ .create ().start ().resume ().get ();
305+
306+ spyActivity = Mockito .spy (activity );
307+ spyFragment = Mockito .spy (fragment );
308+ spySupportFragment = Mockito .spy (supportFragment );
125309 }
126310
127311 private void grantPermissions (String [] perms ) {
128312 ShadowApplication .getInstance ().grantPermissions (perms );
129313 }
130314
131- private void showRationale (String [] perms , boolean shouldShow ) {
315+ private void showRationale (String [] perms , boolean show ) {
132316 for (String perm : perms ) {
133- when (spyActivity .shouldShowRequestPermissionRationale (perm )).thenReturn (shouldShow );
317+ when (spyActivity .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
318+ when (spyFragment .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
319+ when (spySupportFragment .shouldShowRequestPermissionRationale (perm )).thenReturn (show );
134320 }
135321 }
136322}
0 commit comments