forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrantStoragePermissionRule.kt
More file actions
46 lines (42 loc) · 1.74 KB
/
GrantStoragePermissionRule.kt
File metadata and controls
46 lines (42 loc) · 1.74 KB
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
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2021 Álvaro Brey <[email protected]>
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
package com.nextcloud.test
import android.Manifest
import android.os.Build
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
/**
* Rule to automatically enable the test to write to the external storage.
* Depending on the SDK version, different approaches might be necessary to achieve the full access.
*/
class GrantStoragePermissionRule private constructor() {
companion object {
@JvmStatic
fun grant(): TestRule = when {
Build.VERSION.SDK_INT < Build.VERSION_CODES.R -> GrantPermissionRule.grant(
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
else -> GrantManageExternalStoragePermissionRule()
}
}
private class GrantManageExternalStoragePermissionRule : TestRule {
override fun apply(base: Statement, description: Description): Statement = object : Statement() {
override fun evaluate() {
// Refer to https://developer.android.com/training/data-storage/manage-all-files#enable-manage-external-storage-for-testing
InstrumentationRegistry.getInstrumentation().uiAutomation.executeShellCommand(
"appops set --uid ${InstrumentationRegistry.getInstrumentation().targetContext.packageName} " +
"MANAGE_EXTERNAL_STORAGE allow"
)
base.evaluate()
}
}
}
}