Skip to content

Commit 0c84570

Browse files
authored
Merge pull request #733 from android/stamato/clicks-notification-dialog
Grant `permission.POST_NOTIFICATIONS` in Macrobenchmark tests
2 parents f914254 + 49c8ce7 commit 0c84570

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.nowinandroid
18+
19+
import android.Manifest.permission
20+
import android.os.Build.VERSION.SDK_INT
21+
import android.os.Build.VERSION_CODES.TIRAMISU
22+
import androidx.benchmark.macro.MacrobenchmarkScope
23+
24+
/**
25+
* Because the app under test is different from the one running the instrumentation test,
26+
* the permission has to be granted manually by either:
27+
*
28+
* - tapping the Allow button
29+
* ```kotlin
30+
* val obj = By.text("Allow")
31+
* val dialog = device.wait(Until.findObject(obj), TIMEOUT)
32+
* dialog?.let {
33+
* it.click()
34+
* device.wait(Until.gone(obj), 5_000)
35+
* }
36+
* ```
37+
* - or (preferred) executing the grant command on the target package.
38+
*/
39+
fun MacrobenchmarkScope.allowNotifications() {
40+
if (SDK_INT >= TIRAMISU) {
41+
val command = "pm grant $packageName ${permission.POST_NOTIFICATIONS}"
42+
device.executeShellCommand(command)
43+
}
44+
}

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/foryou/ScrollForYouFeedBenchmark.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.benchmark.macro.StartupMode
2222
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
2323
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
2424
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
25+
import com.google.samples.apps.nowinandroid.allowNotifications
2526
import org.junit.Rule
2627
import org.junit.Test
2728
import org.junit.runner.RunWith
@@ -47,6 +48,7 @@ class ScrollForYouFeedBenchmark {
4748
// Start the app
4849
pressHome()
4950
startActivityAndWait()
51+
allowNotifications()
5052
},
5153
) {
5254
forYouWaitForContent()

benchmarks/src/main/java/com/google/samples/apps/nowinandroid/interests/TopicsScreenRecompositionBenchmark.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.benchmark.macro.junit4.MacrobenchmarkRule
2323
import androidx.test.ext.junit.runners.AndroidJUnit4
2424
import androidx.test.uiautomator.By
2525
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
26+
import com.google.samples.apps.nowinandroid.allowNotifications
2627
import org.junit.Rule
2728
import org.junit.Test
2829
import org.junit.runner.RunWith
@@ -47,7 +48,7 @@ class TopicsScreenRecompositionBenchmark {
4748
// Start the app
4849
pressHome()
4950
startActivityAndWait()
50-
51+
allowNotifications()
5152
// Navigate to interests screen
5253
device.findObject(By.text("Interests")).click()
5354
device.waitForIdle()

0 commit comments

Comments
 (0)