Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Update PermissionGranter to allow android 11 permissions #366

Merged
merged 9 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ env:
global:
- secure: "pus/Wv/014aNnUbUDNBpAk12o3w/zEO+/0oPomdnFs5034tsUOk53VbV69qKdWGKtH8Gm9LGBL50GLjSiB9Cxnc66BCPGj1ykBQ8RGGG/KHTDg22fuRVXmrSbEc+SqjwhpyUTpFjTf9RDmR2qlh427RbGofaNmkv7ePX14OHDjfVJW39jVNJv6+Jluyuh98zGpEbKq1SW+kNJsXUJMQjBtgyaQWN0qIjWflZWv4Rn97vxhyxFtRdGtJl1qCyhAcoIXNJyiYuabn/7sGsv4BjjmVBpFOU9npeJ/uMoXv8HQitjmV5wF9vq3VjmdPua9mrJwqG8BvREEA6r/Hj6vPpm0+qM62bDxpINZX3c9CjrvauaMeNSx3rfxK2yYfYO+f7cVaf0hsYS/pc9Pk0LeJy2FhycBt+WNlj/HSFu5Cvmh38kAj6Dm4HveF/CGFuhXYJAGr8IVH7aCPUeEC6JJLEoylNLglasitvNnfi/5f/smJWGf6e/XhFpAjlhoztRv6Hq7yRX3KZWWYmdl5blrsf6OWv7Z3R60IXfe4caPGMuBZNHToUyRmDfk89GXIJWTmVL4NLYSfO7fSaZ0YJ7nkDhoAgW/oRJuqEdbD404Sfzjp/o9YI6GoU9nxWD/RGRY/PGZ1pQs9djI6wny93kV/Y5W1h9BslEiKf1cxzj337M5s="
- secure: "pqbCHzhC2ePQt22kFqwA5RqQiJiQmlT8BEv5cPn84FVtGX0x1Qaw6NQ9k5ap2h/KGD0Axif5AdpruYRp4bicpscYOMLpk4fznXkNAX6U6MaQXR0ITgC2JTDtAZnAYuXo377uvdtHT0zPPfv/9mv5qY1bKdaCcRCSL8GX60ywf1YspJZNo7gC9yDZ6TiGTFTKycQqx1lPuPvtTopbp9oP6gDTnGhHDCrnn4Xi1NyyFnKJYVqaHSR25WgWAevNLnOA1Rbc7T67mrzzfeVv6wgS5+lJOuLCY1OgUwGrNmfqKVL67vUWYm5KuGFuHrV5pSiL6Y0CzJuI5gQl4H2h5tpadaEqOV5PswaWbqRW3aQGMsOKsLwv0oJIBcjE0NjkMx4Jusa7f1ofBkXWN2S8ozPDF8oVpJohzl8p2Fnhdrj+2f0eR+MBWFagWFwFeEKFGB1HDqF+XDQW21/Mar6nKc4tj2/wEtBcwfXL0Jo4c6O1GrUanyXLh4fXxIyuetoYIqwkG6hAvshXyoe0RP6UdifWiWnyEKu5Db6JV8IzjuR1QHCbcXjtXpl7Z6JodkRMw5bQzW9rrXTybRqK+ha+p3e/d1iynhjerxzavMadqSamWv02wQYD+vmeuf58SwZcy6XJJtJZ4M6yVL8msiVpsxpX/ln1jWHFEUKwJRRxKBkTHZo="

before_install:
- yes | sdkmanager "platforms;android-30"

android:
components:
- tools
- tools
- platform-tools
- build-tools-28.0.3
- android-29
- android-30
- extra
- addon
- add-on

licenses:
- 'android-sdk-license-.+'
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ The new Marshmallow permissions system requires checking for permissions at runt
```java
PermissionGranter.allowPermissionsIfNeeded(Manifest.permission.GET_ACCOUNTS);
```
```java
PermissionGranter.allowPermissionOneTime(Manifest.permission.GET_ACCOUNTS);
```

## Useful test rules
Barista includes a set of useful test rules to help you:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ task clean(type: Delete) {
}

ext.minSdkVersionDeclared = 19
ext.compileSdkVersionDeclared = 29
ext.compileSdkVersionDeclared = 30

ext.supportLibVersion = '27.1.1'
ext.espressoVersion = '3.0.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.schibsted.spain.barista.interaction

import android.Manifest.permission.ACCESS_COARSE_LOCATION
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
Expand All @@ -16,35 +14,50 @@ import com.schibsted.spain.barista.interaction.BaristaSleepInteractions.sleepThr
object PermissionGranter {

private val PERMISSIONS_DIALOG_DELAY = 3000
private val PERMISSIONS_DIALOG_ALLOW_ID = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
"com.android.permissioncontroller:id/permission_allow_button"
} else {

private val PERMISSION_DIALOG_ALLOW_FOREGROUND_IDS = listOf(
"com.android.permissioncontroller:id/permission_allow_foreground_only_button",
"com.android.permissioncontroller:id/permission_allow_button",
"com.android.packageinstaller:id/permission_allow_button"
}
private val PERMISSIONS_DIALOG_ALLOW_FOREGROUND_ID = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
"com.android.permissioncontroller:id/permission_allow_foreground_only_button"
} else {
PERMISSIONS_DIALOG_ALLOW_ID
}
// private static final String PERMISSIONS_DIALOG_DENY_ID = "com.android.packageinstaller:id/permission_deny_button";
)

private val PERMISSION_DIALOG_ALLOW_ONE_TIME_IDS = listOf(
"com.android.permissioncontroller:id/permission_allow_one_time_button",
"com.android.permissioncontroller:id/permission_allow_button",
"com.android.packageinstaller:id/permission_allow_button"
)

private fun List<String>.toPermissionButtonRegex() = joinToString(
prefix = "^(",
separator = "|",
postfix = ")$"
) { it }

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

@JvmStatic
fun allowPermissionOneTime(permissionNeeded: String) {
Sloy marked this conversation as resolved.
Show resolved Hide resolved
allowPermission(permissionNeeded, PERMISSION_DIALOG_ALLOW_ONE_TIME_IDS.toPermissionButtonRegex())
}

private fun allowPermission(permissionNeeded: String, permissionsIds: String) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(getApplicationContext(),
permissionNeeded)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(
getApplicationContext(),
permissionNeeded
)) {
sleepThread(PERMISSIONS_DIALOG_DELAY.toLong())
val device = UiDevice.getInstance(getInstrumentation())

val resourceId = if (permissionNeeded == ACCESS_FINE_LOCATION || permissionNeeded == ACCESS_COARSE_LOCATION) {
PERMISSIONS_DIALOG_ALLOW_FOREGROUND_ID
} else {
PERMISSIONS_DIALOG_ALLOW_ID
}
val allowPermissions = device.findObject(UiSelector()
val allowPermissions = device.findObject(
UiSelector()
.clickable(true)
.checkable(false)
.resourceId(resourceId))
.resourceIdMatches(permissionsIds)
)
if (allowPermissions.exists()) {
allowPermissions.click()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class PermissionGranterTest {
@Test
fun grants_simple_permission() {
launchActivity {
verifyPermissionNotGranted(SIMPLE_PERMISSION_1)
requestPermission(SIMPLE_PERMISSION_1)
verifyPermissionNotGranted(PERMISSION_1_CONTACTS)
requestPermission(PERMISSION_1_CONTACTS)

PermissionGranter.allowPermissionsIfNeeded(SIMPLE_PERMISSION_1)
PermissionGranter.allowPermissionsIfNeeded(PERMISSION_1_CONTACTS)

verifyPermissionGranted(SIMPLE_PERMISSION_1)
verifyPermissionGranted(PERMISSION_1_CONTACTS)
}
}

Expand All @@ -49,21 +49,21 @@ class PermissionGranterTest {
@Test
fun ignores_already_granted_permission() {
launchActivity {
verifyPermissionNotGranted(SIMPLE_PERMISSION_2)
requestPermission(SIMPLE_PERMISSION_2)
PermissionGranter.allowPermissionsIfNeeded(SIMPLE_PERMISSION_2)
verifyPermissionGranted(SIMPLE_PERMISSION_2)
verifyPermissionNotGranted(PERMISSION_2_CAMERA)
requestPermission(PERMISSION_2_CAMERA)
PermissionGranter.allowPermissionOneTime(PERMISSION_2_CAMERA)
verifyPermissionGranted(PERMISSION_2_CAMERA)

PermissionGranter.allowPermissionsIfNeeded(SIMPLE_PERMISSION_2)
PermissionGranter.allowPermissionsIfNeeded(PERMISSION_2_CAMERA)
}
}


}

// We can't reuse permission from one test to another, because they stay granted after each test
private const val SIMPLE_PERMISSION_1 = Manifest.permission.READ_CONTACTS
alorma marked this conversation as resolved.
Show resolved Hide resolved
private const val SIMPLE_PERMISSION_2 = Manifest.permission.CAMERA
private const val PERMISSION_1_CONTACTS = Manifest.permission.READ_CONTACTS
private const val PERMISSION_2_CAMERA = Manifest.permission.CAMERA
private const val LOCATION_PERMISSION = Manifest.permission.ACCESS_FINE_LOCATION

private fun ActivityScenario<*>.requestPermission(permission: String) {
Expand Down