Skip to content

Commit a2a9ac8

Browse files
Upgrade macrobenchmark sample to UiAutomator 2.4-alpha05
1 parent 2717559 commit a2a9ac8

18 files changed

+228
-341
lines changed

MacrobenchmarkSample/gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ profileInstaller = "1.4.1"
1919
runtimeTracing = "1.8.3"
2020
tracing = "1.3.0"
2121
tracingPerfetto = "1.0.0"
22-
uiAutomator = "2.3.0"
22+
uiAutomator = "2.4.0-alpha05"
2323

2424
[libraries]
2525
compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 The Android Open Source Project
2+
* Copyright 2025 The Android Open Source Project
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,18 +16,4 @@
1616

1717
package com.example.macrobenchmark.baselineprofile
1818

19-
import androidx.benchmark.macro.MacrobenchmarkScope
20-
import androidx.test.uiautomator.UiDevice
21-
22-
const val TARGET_PACKAGE = "com.example.macrobenchmark.target"
23-
24-
/**
25-
* Clears the application data for the package specified in the [MacrobenchmarkScope].
26-
* @param scope The [MacrobenchmarkScope] providing information about the benchmark,
27-
* including the package name of the app under test.
28-
*/
29-
fun UiDevice.clearData(scope: MacrobenchmarkScope) {
30-
val command = "pm clear ${scope.packageName}"
31-
val output = executeShellCommand(command)
32-
// Assert.assertEquals("Success", output)
33-
}
19+
const val TARGET_PACKAGE = "com.example.macrobenchmark.target"

MacrobenchmarkSample/macrobenchmark/src/main/kotlin/com/example/macrobenchmark/baselineprofile/ComposeActivityBaselineProfileGenerator.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ package com.example.macrobenchmark.baselineprofile
1919
import android.content.Intent
2020
import androidx.benchmark.macro.junit4.BaselineProfileRule
2121
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
22-
import androidx.test.uiautomator.By
2322
import androidx.test.uiautomator.Direction
24-
import com.example.macrobenchmark.benchmark.util.findOrFail
25-
import com.example.macrobenchmark.benchmark.util.waitAndFind
23+
import androidx.test.uiautomator.uiAutomator
2624
import org.junit.Ignore
2725
import org.junit.Rule
2826
import org.junit.Test
@@ -42,16 +40,13 @@ class ComposeActivityBaselineProfileGenerator {
4240
maxIterations = 15,
4341
stableIterations = 3
4442
) {
45-
// Start into the Compose Activity
46-
startActivityAndWait(Intent("$TARGET_PACKAGE.COMPOSE_ACTIVITY"))
47-
48-
// Scrolling through the Compose journey
49-
device.waitAndFind(By.res("myLazyColumn")).also {
50-
it.setGestureMargin(device.displayWidth / 10)
51-
it.fling(Direction.DOWN)
43+
uiAutomator {
44+
// Start into the Compose Activity
45+
startIntent(Intent("$TARGET_PACKAGE.COMPOSE_ACTIVITY"))
46+
// Scrolling through the Compose journey
47+
onElement { viewIdResourceName == "myLazyColumn" }.fling(Direction.DOWN)
48+
onElement { viewIdResourceName == "myLazyColumn" }.fling(Direction.UP)
5249
}
53-
54-
device.findOrFail(By.res("myLazyColumn")).fling(Direction.UP)
5550
}
5651
}
5752
}

MacrobenchmarkSample/macrobenchmark/src/main/kotlin/com/example/macrobenchmark/baselineprofile/LoginBaselineProfileGenerator.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ package com.example.macrobenchmark.baselineprofile
1919
import android.content.Intent
2020
import androidx.benchmark.macro.junit4.BaselineProfileRule
2121
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
22-
import androidx.test.uiautomator.By
22+
import androidx.test.uiautomator.textAsString
23+
import androidx.test.uiautomator.uiAutomator
2324
import org.junit.Rule
2425
import org.junit.Test
2526
import org.junit.runner.RunWith
@@ -37,12 +38,13 @@ class LoginBaselineProfileGenerator {
3738
maxIterations = 15,
3839
stableIterations = 3
3940
) {
40-
device.clearData(this)
41-
startActivityAndWait(Intent("$packageName.LOGIN_ACTIVITY"))
42-
device.findObject(By.res("userName")).text = "user"
43-
device.findObject(By.res("password")).text = "password"
44-
device.findObject(By.res("login")).click()
45-
device.waitForIdle()
41+
uiAutomator {
42+
clearAppData()
43+
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
44+
onElement { textAsString() == "User: " }.text = "user"
45+
onElement { textAsString() == "Password: " }.text = "password"
46+
onElement { textAsString() == "Login" }.click()
47+
}
4648
}
4749
}
4850
}

MacrobenchmarkSample/macrobenchmark/src/main/kotlin/com/example/macrobenchmark/baselineprofile/RecyclerViewActivityBaselineProfileGenerator.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package com.example.macrobenchmark.baselineprofile
1919
import android.content.Intent
2020
import androidx.benchmark.macro.junit4.BaselineProfileRule
2121
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
22-
import androidx.test.uiautomator.By
2322
import androidx.test.uiautomator.Direction
23+
import androidx.test.uiautomator.uiAutomator
2424
import org.junit.Ignore
2525
import org.junit.Rule
2626
import org.junit.Test
@@ -40,14 +40,13 @@ class RecyclerViewActivityBaselineProfileGenerator {
4040
maxIterations = 15,
4141
stableIterations = 3
4242
) {
43-
// Start into the RecyclerViewActivity
44-
startActivityAndWait(Intent("$TARGET_PACKAGE.RECYCLER_VIEW_ACTIVITY"))
43+
uiAutomator {
44+
// Start into the RecyclerViewActivity
45+
startIntent(Intent("$TARGET_PACKAGE.RECYCLER_VIEW_ACTIVITY"))
4546

46-
// Scrolling RecyclerView journey
47-
device.findObject(By.res(packageName, "recycler")).also {
48-
it.setGestureMargin(device.displayWidth / 10)
49-
it.fling(Direction.DOWN)
50-
it.fling(Direction.UP)
47+
// Scrolling RecyclerView journey
48+
onElement { viewIdResourceName == "recycler" }.fling(Direction.DOWN)
49+
onElement { viewIdResourceName == "recycler" }.fling(Direction.UP)
5150
}
5251
}
5352
}

MacrobenchmarkSample/macrobenchmark/src/main/kotlin/com/example/macrobenchmark/baselineprofile/StartupProfileGenerator.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.example.macrobenchmark.baselineprofile
1818

1919
import androidx.benchmark.macro.junit4.BaselineProfileRule
2020
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
21+
import androidx.test.uiautomator.uiAutomator
2122
import org.junit.Rule
2223
import org.junit.Test
2324
import org.junit.runner.RunWith
@@ -41,7 +42,9 @@ class StartupProfileGenerator {
4142
stableIterations = 3,
4243
includeInStartupProfile = true
4344
) {
44-
startActivityAndWait()
45+
uiAutomator {
46+
startApp(TARGET_PACKAGE)
47+
}
4548
}
4649
}
4750
}

MacrobenchmarkSample/macrobenchmark/src/main/kotlin/com/example/macrobenchmark/benchmark/LoginBenchmark.kt

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import androidx.benchmark.macro.StartupMode
2525
import androidx.benchmark.macro.StartupTimingMetric
2626
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
2727
import androidx.test.ext.junit.runners.AndroidJUnit4
28-
import androidx.test.uiautomator.By
28+
import androidx.test.uiautomator.UiAutomatorTestScope
29+
import androidx.test.uiautomator.textAsString
30+
import androidx.test.uiautomator.uiAutomator
31+
import androidx.test.uiautomator.watcher.PermissionDialog
2932
import com.example.macrobenchmark.benchmark.util.DEFAULT_ITERATIONS
3033
import com.example.macrobenchmark.benchmark.util.TARGET_PACKAGE
31-
import com.example.macrobenchmark.benchmark.permissions.allowNotifications
3234
import org.junit.Rule
3335
import org.junit.Test
3436
import org.junit.runner.RunWith
@@ -51,33 +53,39 @@ class LoginBenchmark {
5153
@Test
5254
fun loginInSetupBlock() {
5355
benchmarkLoginActivity(setupBlock = {
54-
startActivityAndWait(Intent("$packageName.LOGIN_ACTIVITY"))
55-
login()
56+
uiAutomator {
57+
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
58+
login()
59+
}
5660
})
5761
}
5862

5963
@Test
6064
fun loginWithUiAutomator() {
6165
benchmarkLoginActivity {
62-
login()
66+
uiAutomator { login() }
6367
}
6468
}
6569

6670
@Test
6771
fun loginInAfterPermissionsGranted() {
6872
benchmarkLoginActivity(setupBlock = {
69-
allowNotifications()
73+
uiAutomator {
74+
watchFor(PermissionDialog) {
75+
clickAllow()
76+
}
77+
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
78+
login()
79+
}
80+
7081

71-
startActivityAndWait(Intent("$packageName.LOGIN_ACTIVITY"))
72-
login()
7382
})
7483
}
7584

76-
private fun MacrobenchmarkScope.login() {
77-
device.findObject(By.res("userName")).text = "user"
78-
device.findObject(By.res("password")).text = "password"
79-
device.findObject(By.res("login")).click()
80-
device.waitForIdle()
85+
private fun UiAutomatorTestScope.login() {
86+
onElement { textAsString() == "User: " }.text = "user"
87+
onElement { textAsString() == "Password" }.text = "password"
88+
onElement { textAsString() == "Login" }.click()
8189
}
8290

8391
private fun benchmarkLoginActivity(
@@ -93,11 +101,11 @@ class LoginBenchmark {
93101
iterations = DEFAULT_ITERATIONS,
94102
setupBlock = setupBlock,
95103
) {
96-
startActivityAndWait(
97-
Intent()
104+
uiAutomator {
105+
startIntent(Intent()
98106
.putExtras(extras)
99-
.setAction("$packageName.LOGIN_ACTIVITY")
100-
)
107+
.setAction("$packageName.LOGIN_ACTIVITY"))
108+
}
101109
measureBlock()
102110
}
103111
}

0 commit comments

Comments
 (0)