1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.permission3.cts
import androidx.test.filters.FlakyTest
import org.junit.Assume
import org.junit.Before
import org.junit.Test
/**
* Runtime permission behavior tests for apps targeting API 23.
*/
class PermissionTest23 : BaseUsePermissionTest() {
companion object {
private const val NON_EXISTENT_PERMISSION = "permission.does.not.exist"
private const val INVALID_PERMISSION = "$APP_PACKAGE_NAME.abadname"
}
@Before
fun installApp23() {
installPackage(APP_APK_PATH_23)
}
@Test
fun testDefault() {
// New permission model is denied by default
assertAppHasAllOrNoPermissions(false)
}
@Test
fun testGranted() {
grantAppPermissions(android.Manifest.permission.READ_CALENDAR)
// Read/write access should be allowed
assertAppHasPermission(android.Manifest.permission.READ_CALENDAR, true)
assertAppHasPermission(android.Manifest.permission.WRITE_CALENDAR, true)
assertAppHasCalendarAccess(true)
}
@Test
fun testInteractiveGrant() {
// Start out without permission
assertAppHasPermission(android.Manifest.permission.READ_CALENDAR, false)
assertAppHasPermission(android.Manifest.permission.WRITE_CALENDAR, false)
assertAppHasCalendarAccess(false)
// Go through normal grant flow
requestAppPermissionsAndAssertResult(
android.Manifest.permission.READ_CALENDAR to true,
android.Manifest.permission.WRITE_CALENDAR to true
) {
clickPermissionRequestAllowButton()
}
// We should have permission now!
assertAppHasCalendarAccess(true)
}
@Test
fun testRuntimeGroupGrantSpecificity() {
// Start out without permission
assertAppHasPermission(android.Manifest.permission.READ_CONTACTS, false)
assertAppHasPermission(android.Manifest.permission.WRITE_CONTACTS, false)
// Request only one permission from the 'contacts' permission group
// Expect the permission is granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.WRITE_CONTACTS to true) {
clickPermissionRequestAllowButton()
}
// Make sure no undeclared as used permissions are granted
assertAppHasPermission(android.Manifest.permission.READ_CONTACTS, false)
}
@Test
fun testCancelledPermissionRequest() {
// Make sure we don't have the permission
assertAppHasPermission(android.Manifest.permission.WRITE_CONTACTS, false)
// Request the permission and cancel the request
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.WRITE_CONTACTS to false) {
clickPermissionRequestDenyButton()
}
}
@Test
fun testRequestGrantedPermission() {
// Make sure we don't have the permission
assertAppHasPermission(android.Manifest.permission.WRITE_CONTACTS, false)
// Request the permission and allow it
// Expect the permission is granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.WRITE_CONTACTS to true) {
clickPermissionRequestAllowButton()
}
// Request the permission and do nothing
// Expect the permission is granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.WRITE_CONTACTS to true) {}
}
@Test
fun testDenialWithPrejudice() {
// Make sure we don't have the permission
assertAppHasPermission(android.Manifest.permission.WRITE_CONTACTS, false)
// Request the permission and deny it
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.WRITE_CONTACTS to false) {
clickPermissionRequestDenyButton()
}
// Request the permission and choose don't ask again
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.WRITE_CONTACTS to false) {
denyPermissionRequestWithPrejudice()
}
// Request the permission and do nothing
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.WRITE_CONTACTS to false) {}
}
@FlakyTest
@Test
fun testRevokeAffectsWholeGroup() {
// Grant the group
grantAppPermissions(android.Manifest.permission.READ_CALENDAR)
// Make sure we have the permissions
assertAppHasPermission(android.Manifest.permission.READ_CALENDAR, true)
assertAppHasPermission(android.Manifest.permission.WRITE_CALENDAR, true)
// Revoke the group
revokeAppPermissions(android.Manifest.permission.READ_CALENDAR)
// Make sure we don't have the permissions
assertAppHasPermission(android.Manifest.permission.READ_CALENDAR, false)
assertAppHasPermission(android.Manifest.permission.WRITE_CALENDAR, false)
}
@Test
fun testGrantPreviouslyRevokedWithPrejudiceShowsPrompt() {
// Make sure we don't have the permission
assertAppHasPermission(android.Manifest.permission.CAMERA, false)
// Request the permission and deny it
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.CAMERA to false) {
clickPermissionRequestDenyButton()
}
// Request the permission and choose don't ask again
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.CAMERA to false) {
denyPermissionRequestWithPrejudice()
}
// Clear the denial with prejudice
grantAppPermissions(android.Manifest.permission.CAMERA)
revokeAppPermissions(android.Manifest.permission.CAMERA)
// Make sure we don't have the permission
assertAppHasPermission(android.Manifest.permission.CAMERA, false)
// Request the permission and allow it
// Make sure the permission is granted
requestAppPermissionsAndAssertResult(android.Manifest.permission.CAMERA to true) {
clickPermissionRequestAllowForegroundButton()
}
}
@Test
fun testRequestNonRuntimePermission() {
// Make sure we don't have the permission
assertAppHasPermission(android.Manifest.permission.BIND_PRINT_SERVICE, false)
// Request the permission and do nothing
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(
android.Manifest.permission.BIND_PRINT_SERVICE to false
) {}
}
@Test
fun testRequestNonExistentPermission() {
// Make sure we don't have the permission
assertAppHasPermission(NON_EXISTENT_PERMISSION, false)
// Request the permission and do nothing
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(NON_EXISTENT_PERMISSION to false) {}
}
@Test
fun testRequestPermissionFromTwoGroups() {
// Make sure we don't have the permissions
assertAppHasPermission(android.Manifest.permission.WRITE_CONTACTS, false)
assertAppHasPermission(android.Manifest.permission.WRITE_CALENDAR, false)
assertAppHasPermission(android.Manifest.permission.READ_CALENDAR, false)
// Request the permission and allow it
// Expect the permission are granted
val result = requestAppPermissionsAndAssertResult(
android.Manifest.permission.WRITE_CONTACTS to true,
android.Manifest.permission.WRITE_CALENDAR to true
) {
clickPermissionRequestAllowButton()
clickPermissionRequestAllowButton()
}
// In API < N_MR1 all permissions of a group are granted. I.e. the grant was "expanded"
assertAppHasPermission(android.Manifest.permission.READ_CALENDAR, true)
// Even the contacts group was expanded, the read-calendar permission is not in the
// manifest, hence not granted.
assertAppHasPermission(android.Manifest.permission.READ_CONTACTS, false)
}
@Test(timeout = 180000)
@FlakyTest
fun testNoResidualPermissionsOnUninstall() {
Assume.assumeFalse(packageManager.arePermissionsIndividuallyControlled())
// Grant all permissions
grantAppPermissions(
android.Manifest.permission.WRITE_CALENDAR,
android.Manifest.permission.WRITE_CONTACTS,
android.Manifest.permission.READ_SMS,
android.Manifest.permission.CALL_PHONE,
android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission.BODY_SENSORS,
android.Manifest.permission.CAMERA,
android.Manifest.permission.READ_EXTERNAL_STORAGE, targetSdk = 23
)
// Don't use UI for granting location permission as this shows another dialog
uiAutomation.grantRuntimePermission(
APP_PACKAGE_NAME, android.Manifest.permission.ACCESS_FINE_LOCATION
)
uiAutomation.grantRuntimePermission(
APP_PACKAGE_NAME, android.Manifest.permission.ACCESS_COARSE_LOCATION
)
uiAutomation.grantRuntimePermission(
APP_PACKAGE_NAME, android.Manifest.permission.ACCESS_BACKGROUND_LOCATION
)
uninstallPackage(APP_PACKAGE_NAME)
installPackage(APP_APK_PATH_23)
// Make no permissions are granted after uninstalling and installing the app
assertAppHasAllOrNoPermissions(false)
}
@Test
fun testNullPermissionRequest() {
// Go through normal grant flow
requestAppPermissionsAndAssertResult(null to false) {}
}
@Test
fun testNullAndRealPermission() {
// Make sure we don't have the permissions
assertAppHasPermission(android.Manifest.permission.WRITE_CONTACTS, false)
assertAppHasPermission(android.Manifest.permission.RECORD_AUDIO, false)
// Request the permission and allow it
// Expect the permission are granted
requestAppPermissionsAndAssertResult(
null to false,
android.Manifest.permission.WRITE_CONTACTS to true,
null to false,
android.Manifest.permission.RECORD_AUDIO to true
) {
clickPermissionRequestAllowForegroundButton()
clickPermissionRequestAllowButton()
}
}
@Test
fun testInvalidPermission() {
// Request the permission and allow it
// Expect the permission is not granted
requestAppPermissionsAndAssertResult(INVALID_PERMISSION to false) {}
}
private fun denyPermissionRequestWithPrejudice() {
if (isTv || isWatch) {
clickPermissionRequestDontAskAgainButton()
} else {
clickPermissionRequestDenyAndDontAskAgainButton()
}
}
private fun assertAppHasAllOrNoPermissions(expectPermissions: Boolean) {
arrayOf(
android.Manifest.permission.SEND_SMS,
android.Manifest.permission.RECEIVE_SMS,
android.Manifest.permission.RECEIVE_WAP_PUSH,
android.Manifest.permission.RECEIVE_MMS,
android.Manifest.permission.READ_CALENDAR,
android.Manifest.permission.WRITE_CALENDAR,
android.Manifest.permission.WRITE_CONTACTS,
android.Manifest.permission.READ_SMS,
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.READ_CALL_LOG,
android.Manifest.permission.WRITE_CALL_LOG,
android.Manifest.permission.ADD_VOICEMAIL,
android.Manifest.permission.CALL_PHONE,
android.Manifest.permission.USE_SIP,
android.Manifest.permission.PROCESS_OUTGOING_CALLS,
android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.CAMERA,
android.Manifest.permission.BODY_SENSORS,
android.Manifest.permission.READ_CELL_BROADCASTS,
// Split permissions
android.Manifest.permission.ACCESS_BACKGROUND_LOCATION,
// Storage permissions
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
).forEach {
assertAppHasPermission(it, expectPermissions)
}
}
}
|