2
2
3
3
import android .Manifest ;
4
4
import android .app .Application ;
5
+ import android .app .Dialog ;
5
6
import android .app .Fragment ;
7
+ import android .widget .TextView ;
6
8
7
9
import org .junit .Before ;
8
10
import org .junit .Test ;
16
18
import org .robolectric .RuntimeEnvironment ;
17
19
import org .robolectric .annotation .Config ;
18
20
import org .robolectric .shadows .ShadowApplication ;
21
+ import org .robolectric .shadows .support .v4 .SupportFragmentController ;
19
22
20
23
import java .util .ArrayList ;
21
24
22
25
import pub .devrel .easypermissions .testhelper .TestActivity ;
26
+ import pub .devrel .easypermissions .testhelper .TestFragment ;
27
+ import pub .devrel .easypermissions .testhelper .TestSupportFragment ;
23
28
24
29
import static com .google .common .truth .Truth .assertThat ;
25
30
import static junit .framework .Assert .fail ;
38
43
public class EasyPermissionsTest {
39
44
40
45
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
+
42
51
private Application app ;
43
- private String [] allPerms ;
44
- private String [] onePerm ;
45
52
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 ;
48
59
49
60
@ Before
50
61
public void setUp () {
51
62
MockitoAnnotations .initMocks (this );
52
-
63
+ setUpActivityAndFragment ();
53
64
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
- };
62
65
}
63
66
64
67
@ Test
65
68
public void shouldNotHavePermissions_whenNoPermissionsGranted () {
66
- assertThat (EasyPermissions .hasPermissions (app , allPerms )).isFalse ();
69
+ assertThat (EasyPermissions .hasPermissions (app , ALL_PERMS )).isFalse ();
67
70
}
68
71
69
72
@ Test
70
73
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 ();
73
76
}
74
77
75
78
@ Test
76
79
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 ();
79
82
}
80
83
81
84
@ Test
82
85
public void shouldThrowException_whenHasPermissionsWithNullContext () {
83
86
try {
84
- EasyPermissions .hasPermissions (null , allPerms );
87
+ EasyPermissions .hasPermissions (null , ALL_PERMS );
85
88
fail ("IllegalStateException expected because of null context." );
86
89
} catch (IllegalArgumentException e ) {
87
90
assertThat (e ).hasMessageThat ()
@@ -90,47 +93,230 @@ public void shouldThrowException_whenHasPermissionsWithNullContext() {
90
93
}
91
94
92
95
@ Test
93
- public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermission () {
94
- grantPermissions (allPerms );
96
+ public void shouldCallbackOnPermissionGranted_whenRequestAlreadyGrantedPermissionFromActivity () {
97
+ grantPermissions (ALL_PERMS );
95
98
96
- EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , allPerms );
99
+ EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , ALL_PERMS );
97
100
98
101
verify (spyActivity , times (1 ))
99
102
.onPermissionsGranted (integerCaptor .capture (), listCaptor .capture ());
100
103
verify (spyActivity , never ()).requestPermissions (any (String [].class ), anyInt ());
101
104
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 );
103
161
}
104
162
105
163
@ 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 );
109
167
110
- EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , allPerms );
168
+ EasyPermissions .requestPermissions (spyFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
111
169
112
- verify (spyActivity , times (1 )).requestPermissions (allPerms , REQUEST_CODE );
170
+ verify (spyFragment , times (1 )).requestPermissions (ALL_PERMS , REQUEST_CODE );
113
171
}
114
172
115
173
@ 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 );
119
177
120
- EasyPermissions .requestPermissions (spyActivity , RATIONALE , REQUEST_CODE , allPerms );
178
+ EasyPermissions .requestPermissions (spyFragment , RATIONALE , REQUEST_CODE , ALL_PERMS );
121
179
122
- Fragment dialog = spyActivity . getFragmentManager ()
180
+ Fragment dialogFragment = spyFragment . getChildFragmentManager ()
123
181
.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 );
125
309
}
126
310
127
311
private void grantPermissions (String [] perms ) {
128
312
ShadowApplication .getInstance ().grantPermissions (perms );
129
313
}
130
314
131
- private void showRationale (String [] perms , boolean shouldShow ) {
315
+ private void showRationale (String [] perms , boolean show ) {
132
316
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 );
134
320
}
135
321
}
136
322
}
0 commit comments