Skip to content

Commit

Permalink
Merge pull request #233 from yschimke/grant_permission
Browse files Browse the repository at this point in the history
Add a permission grant sample
  • Loading branch information
yschimke committed Jun 26, 2023
2 parents dbdbbe4 + f731cda commit 615361c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions MacrobenchmarkSample/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
Expand Down
2 changes: 2 additions & 0 deletions MacrobenchmarkSample/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
agp = "8.0.0"
rules = "1.5.0"
targetSdk = "34"
minSdk = "21"
compileSdk = "34"
Expand All @@ -26,6 +27,7 @@ tracing = "1.1.0"
uiAutomator = "2.2.0"

[libraries]
androidx-rules = { module = "androidx.test:rules", version.ref = "rules" }
compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
activity = { group = "androidx.activity", name = "activity-ktx", version.ref = "activity" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "jUnit" }
Expand Down
1 change: 1 addition & 0 deletions MacrobenchmarkSample/macrobenchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ dependencies {
implementation(libs.espresso)
implementation(libs.ui.automator)
implementation(libs.kotlin.stdlib)
implementation(libs.androidx.rules)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.uiautomator.By
import com.example.benchmark.macro.base.util.DEFAULT_ITERATIONS
import com.example.benchmark.macro.base.util.TARGET_PACKAGE
import com.example.macrobenchmark.permissions.allowNotifications
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -62,6 +63,16 @@ class LoginBenchmark {
}
}

@Test
fun loginInAfterPermissionsGranted() {
benchmarkLoginActivity(setupBlock = {
allowNotifications()

startActivityAndWait(Intent("$packageName.LOGIN_ACTIVITY"))
login()
})
}

private fun MacrobenchmarkScope.login() {
device.findObject(By.res("userName")).text = "user"
device.findObject(By.res("password")).text = "password"
Expand Down Expand Up @@ -90,5 +101,4 @@ class LoginBenchmark {
measureBlock()
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 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
*
* https://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 com.example.macrobenchmark.permissions

import android.Manifest.permission
import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.TIRAMISU
import androidx.benchmark.macro.MacrobenchmarkScope
import org.junit.Assert

/**
* Because the app under test runs in a different process from the one running the instrumentation test,
* the permission has to be granted manually by either:
*
* - tapping the Allow button
* ```kotlin
* val obj = By.text("Allow")
* val dialog = device.wait(Until.findObject(obj), TIMEOUT)
* dialog?.let {
* it.click()
* device.wait(Until.gone(obj), 5_000)
* }
* ```
* - or (preferred) executing the grant command on the target package.
*/
fun MacrobenchmarkScope.allowNotifications() {
if (SDK_INT >= TIRAMISU) {
val command = "pm grant $packageName ${permission.POST_NOTIFICATIONS}"
val output = device.executeShellCommand(command)
Assert.assertEquals("", output)
}
}

0 comments on commit 615361c

Please sign in to comment.