Skip to content

Commit

Permalink
Add method for denying permissions (#378)
Browse files Browse the repository at this point in the history
* Add method for denying permissions

In some cases we also need to test the app's behavior when the user denies a permission. This change adds the denyPermissions() method.

* Add test to cover the new method

Co-authored-by: Rafa Vázquez <[email protected]>
  • Loading branch information
milhauscz and Sloy authored May 28, 2021
1 parent d2fe208 commit 23b1eeb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ object PermissionGranter {
"com.android.packageinstaller:id/permission_allow_button"
)

// In API 30 the "Deny" button has the first ID when it is shown the first time. Second time
// it has the second ID (do not ask again), although text is the same. In API 29 the buttons
// are shown separately.
private val PERMISSION_DIALOG_DENY_IDS = listOf(
"com.android.permissioncontroller:id/permission_deny_button",
"com.android.permissioncontroller:id/permission_deny_and_dont_ask_again_button",
"com.android.packageinstaller:id/permission_deny_button"
)

private fun List<String>.toPermissionButtonRegex() = joinToString(
prefix = "^(",
separator = "|",
Expand All @@ -35,15 +44,20 @@ object PermissionGranter {

@JvmStatic
fun allowPermissionsIfNeeded(permissionNeeded: String) {
allowPermission(permissionNeeded, PERMISSION_DIALOG_ALLOW_FOREGROUND_IDS.toPermissionButtonRegex())
clickPermissionDialogButton(permissionNeeded, PERMISSION_DIALOG_ALLOW_FOREGROUND_IDS.toPermissionButtonRegex())
}

@JvmStatic
fun allowPermissionOneTime(permissionNeeded: String) {
allowPermission(permissionNeeded, PERMISSION_DIALOG_ALLOW_ONE_TIME_IDS.toPermissionButtonRegex())
clickPermissionDialogButton(permissionNeeded, PERMISSION_DIALOG_ALLOW_ONE_TIME_IDS.toPermissionButtonRegex())
}

@JvmStatic
fun denyPermissions(permissionRequested: String) {
clickPermissionDialogButton(permissionRequested, PERMISSION_DIALOG_DENY_IDS.toPermissionButtonRegex())
}

private fun allowPermission(permissionNeeded: String, permissionsIds: String) {
private fun clickPermissionDialogButton(permissionNeeded: String, permissionsIds: String) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(
getApplicationContext(),
Expand Down Expand Up @@ -75,4 +89,4 @@ object PermissionGranter {
private fun checkSelfPermission(context: Context, permission: String): Int {
return context.checkPermission(permission, android.os.Process.myPid(), android.os.Process.myUid())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,25 @@ class PermissionGranterTest {
}
}

@Test
fun denies_simple_permission() {
launchActivity {
verifyPermissionNotGranted(PERMISSION_3_CALLS)
requestPermission(PERMISSION_3_CALLS)

PermissionGranter.denyPermissions(PERMISSION_3_CALLS)

verifyPermissionNotGranted(PERMISSION_3_CALLS)
}
}


}

// We can't reuse permission from one test to another, because they stay granted after each test
private const val PERMISSION_1_CONTACTS = Manifest.permission.READ_CONTACTS
private const val PERMISSION_2_CAMERA = Manifest.permission.CAMERA
private const val PERMISSION_3_CALLS = Manifest.permission.ANSWER_PHONE_CALLS
private const val LOCATION_PERMISSION = Manifest.permission.ACCESS_FINE_LOCATION

private fun ActivityScenario<*>.requestPermission(permission: String) {
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
Expand Down

0 comments on commit 23b1eeb

Please sign in to comment.