Skip to content

Commit 8e3e3ad

Browse files
committed
Migrate MacrobenchmarkSample to UiAutomator 2.4-alpha05
This commit updates the MacrobenchmarkSample to use UiAutomator 2.4-alpha05, which introduces a breaking change. The `measureBlock` and `setupBlock` in `MacrobenchmarkRule.measureRepeated` no longer have `MacrobenchmarkScope` as a receiver. The following changes were made to adapt to this: - Removed `MacrobenchmarkScope` as a receiver from helper functions. - Updated UI interactions to use new UiAutomator APIs where necessary (e.g., using `textAsString()` for text matching in `TextInputFrameTimingBenchmark` and `StartupBenchmark`). - Refactored `LoginBenchmark` to separate UI interaction logic (inputting details and clicking login) into distinct functions for clarity and to accommodate the `measureBlock` changes. - Updated the benchmark library version in `libs.versions.toml`.
1 parent 31ddfef commit 8e3e3ad

5 files changed

Lines changed: 39 additions & 23 deletions

File tree

MacrobenchmarkSample/gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
agp = "8.11.1"
33
activity = "1.10.1"
44
appcompat = "1.7.1"
5-
benchmark = "1.3.4"
5+
benchmark = "1.4.0"
66
composeBom = "2025.07.00"
77
constraintLayout = "2.2.1"
88
core = "1.16.0"

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,47 +47,59 @@ class LoginBenchmark {
4747
putString("user", "benchUser")
4848
putString("password", "benchPassword")
4949
}
50-
benchmarkLoginActivity(extras = extras)
50+
benchmarkLoginActivity(
51+
extras = extras,
52+
measureBlock = {
53+
uiAutomator {
54+
clickOnLogin()
55+
}
56+
}
57+
)
5158
}
5259

5360
@Test
5461
fun loginInSetupBlock() {
55-
benchmarkLoginActivity(setupBlock = {
62+
benchmarkLoginActivity(measureBlock = {
5663
uiAutomator {
57-
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
58-
login()
64+
inputLoginDetails()
65+
clickOnLogin()
5966
}
6067
})
6168
}
6269

6370
@Test
6471
fun loginWithUiAutomator() {
65-
benchmarkLoginActivity {
66-
uiAutomator { login() }
67-
}
72+
benchmarkLoginActivity(
73+
measureBlock = {
74+
uiAutomator {
75+
inputLoginDetails()
76+
clickOnLogin()
77+
}
78+
}
79+
)
6880
}
6981

7082
@Test
7183
fun loginInAfterPermissionsGranted() {
72-
benchmarkLoginActivity(setupBlock = {
84+
benchmarkLoginActivity(measureBlock = {
7385
uiAutomator {
7486
watchFor(PermissionDialog) {
7587
clickAllow()
7688
}
77-
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
78-
login()
89+
inputLoginDetails()
90+
clickOnLogin()
7991
}
80-
81-
8292
})
8393
}
8494

85-
private fun UiAutomatorTestScope.login() {
95+
private fun UiAutomatorTestScope.inputLoginDetails() {
8696
onElement { viewIdResourceName == "userName" }.text = "user"
8797
onElement { viewIdResourceName == "password" }.text = "password"
88-
onElement { textAsString() == "Login" }.click()
8998
}
9099

100+
private fun UiAutomatorTestScope.clickOnLogin() =
101+
onElement { textAsString() == "Login" }.click()
102+
91103
private fun benchmarkLoginActivity(
92104
extras: Bundle = Bundle(),
93105
setupBlock: MacrobenchmarkScope.() -> Unit = {},
@@ -102,9 +114,11 @@ class LoginBenchmark {
102114
setupBlock = setupBlock,
103115
) {
104116
uiAutomator {
105-
startIntent(Intent()
106-
.putExtras(extras)
107-
.setAction("$packageName.LOGIN_ACTIVITY"))
117+
startIntent(
118+
Intent()
119+
.putExtras(extras)
120+
.setAction("$packageName.LOGIN_ACTIVITY")
121+
)
108122
}
109123
measureBlock()
110124
}

MacrobenchmarkSample/macrobenchmark/src/main/kotlin/com/example/macrobenchmark/benchmark/frames/NestedRecyclerFrameTimingBenchmarks.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package com.example.macrobenchmark.benchmark.frames
1818

1919
import androidx.benchmark.macro.ExperimentalMetricApi
2020
import androidx.benchmark.macro.FrameTimingMetric
21-
import androidx.benchmark.macro.MacrobenchmarkScope
2221
import androidx.benchmark.macro.StartupMode
2322
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
2423
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -62,15 +61,15 @@ class NestedRecyclerFrameTimingBenchmarks {
6261
) { measureScrollingNestedRecycler() }
6362
}
6463

65-
private fun MacrobenchmarkScope.navigateToNestedRvScreen(useRecyclerViewPool: Boolean) {
64+
private fun navigateToNestedRvScreen(useRecyclerViewPool: Boolean) {
6665
uiAutomator {
6766
startApp(TARGET_PACKAGE)
6867
onElement { textAsString() == if (useRecyclerViewPool) "Nested RecyclerView with Pools" else "Nested RecyclerView" }.click()
6968
waitForStableInActiveWindow()
7069
}
7170
}
7271

73-
private fun MacrobenchmarkScope.measureScrollingNestedRecycler() {
72+
private fun measureScrollingNestedRecycler() {
7473
uiAutomator {
7574
onElement { className == "androidx.recyclerview.widget.RecyclerView" }.run {
7675
repeat(3) { index ->

MacrobenchmarkSample/macrobenchmark/src/main/kotlin/com/example/macrobenchmark/benchmark/frames/TextInputFrameTimingBenchmark.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.benchmark.macro.StartupMode
2323
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
2424
import androidx.test.ext.junit.runners.AndroidJUnit4
2525
import androidx.test.filters.LargeTest
26+
import androidx.test.uiautomator.textAsString
2627
import androidx.test.uiautomator.uiAutomator
2728
import com.example.macrobenchmark.benchmark.util.DEFAULT_ITERATIONS
2829
import com.example.macrobenchmark.benchmark.util.TARGET_PACKAGE
@@ -37,7 +38,7 @@ class TextInputFrameTimingBenchmark {
3738
val benchmarkRule = MacrobenchmarkRule()
3839

3940
@Test
40-
fun inputText() {
41+
fun reinputText() {
4142
benchmarkRule.measureRepeated(
4243
packageName = TARGET_PACKAGE,
4344
metrics = listOf(FrameTimingMetric()),
@@ -54,7 +55,7 @@ class TextInputFrameTimingBenchmark {
5455
}
5556
) {
5657
uiAutomator {
57-
onElement { isEditable }.text = "Benchmark input"
58+
onElement { textAsString() == "Enter text here" }.text = "Benchmark input"
5859
}
5960
}
6061
}

MacrobenchmarkSample/macrobenchmark/src/main/kotlin/com/example/macrobenchmark/benchmark/startup/StartupBenchmark.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.StartupTimingMetric
2323
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
2424
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
25+
import androidx.test.uiautomator.textAsString
2526
import androidx.test.uiautomator.uiAutomator
2627
import com.example.macrobenchmark.benchmark.util.DEFAULT_ITERATIONS
2728
import com.example.macrobenchmark.benchmark.util.TARGET_PACKAGE
@@ -89,6 +90,7 @@ abstract class AbstractStartupBenchmark(private val startupMode: StartupMode) {
8990
) {
9091
uiAutomator {
9192
startApp(TARGET_PACKAGE)
93+
onElement { textAsString() == "Fully Drawn" && isVisibleToUser }
9294
}
9395
}
9496
}

0 commit comments

Comments
 (0)