Skip to content

Commit abe3a93

Browse files
Migrate benchmarks to UiAutomator 3.0 API
1 parent 358d57c commit abe3a93

18 files changed

+246
-302
lines changed

MacrobenchmarkSample/gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ profileInstaller = "1.4.1"
1919
runtimeTracing = "1.7.8"
2020
tracing = "1.3.0-beta01"
2121
tracingPerfetto = "1.0.0"
22-
uiAutomator = "2.3.0"
22+
uiAutomator = "2.4.0-SNAPSHOT"
2323

2424
[libraries]
2525
compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@
1616

1717
package com.example.macrobenchmark.baselineprofile
1818

19-
import androidx.benchmark.macro.MacrobenchmarkScope
20-
import androidx.test.uiautomator.UiDevice
19+
import androidx.test.uiautomator.UiAutomatorTestScope
2120

2221
const val TARGET_PACKAGE = "com.example.macrobenchmark.target"
2322

2423
/**
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.
24+
* Clears the application data for the launcher package.
2825
*/
29-
fun UiDevice.clearData(scope: MacrobenchmarkScope) {
30-
val command = "pm clear ${scope.packageName}"
31-
val output = executeShellCommand(command)
32-
// Assert.assertEquals("Success", output)
26+
fun UiAutomatorTestScope.clearData() {
27+
val command = "pm clear ${uiDevice.launcherPackageName}"
28+
uiDevice.executeShellCommand(command)
3329
}

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

+9-13
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ 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
26-
import org.junit.Ignore
23+
import androidx.test.uiautomator.uiAutomator
2724
import org.junit.Rule
2825
import org.junit.Test
2926
import org.junit.runner.RunWith
3027

31-
@Ignore // TODO causing stale object excpetion on CI .. why?
3228
@RunWith(AndroidJUnit4ClassRunner::class)
3329
class ComposeActivityBaselineProfileGenerator {
3430

@@ -42,16 +38,16 @@ class ComposeActivityBaselineProfileGenerator {
4238
maxIterations = 15,
4339
stableIterations = 3
4440
) {
45-
// Start into the Compose Activity
46-
startActivityAndWait(Intent("$TARGET_PACKAGE.COMPOSE_ACTIVITY"))
41+
uiAutomator {
42+
// Start into the Compose Activity
43+
startIntent(Intent("$TARGET_PACKAGE.COMPOSE_ACTIVITY"))
4744

48-
// Scrolling through the Compose journey
49-
device.waitAndFind(By.res("myLazyColumn")).also {
50-
it.setGestureMargin(device.displayWidth / 10)
51-
it.fling(Direction.DOWN)
45+
// Scrolling through the Compose journey
46+
with(onView { viewIdResourceName == "myLazyColumn" }) {
47+
setGestureMargin(device.displayWidth / 10)
48+
setOf(Direction.DOWN, Direction.UP).forEach { fling(it) }
49+
}
5250
}
53-
54-
device.findOrFail(By.res("myLazyColumn")).fling(Direction.UP)
5551
}
5652
}
5753
}

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ 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.uiAutomator
2323
import org.junit.Rule
2424
import org.junit.Test
2525
import org.junit.runner.RunWith
@@ -37,12 +37,13 @@ class LoginBaselineProfileGenerator {
3737
maxIterations = 15,
3838
stableIterations = 3
3939
) {
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()
40+
uiAutomator {
41+
clearData()
42+
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
43+
onView { viewIdResourceName == "userName" }.text = "user"
44+
onView { viewIdResourceName == "password" }.text = "password"
45+
onView { viewIdResourceName == "login" }.click()
46+
}
4647
}
4748
}
4849
}

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ 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 org.junit.Ignore
23+
import androidx.test.uiautomator.uiAutomator
2524
import org.junit.Rule
2625
import org.junit.Test
2726
import org.junit.runner.RunWith
2827

29-
@Ignore // TODO flinging not working, ignore for now to test the pipeline
3028
@RunWith(AndroidJUnit4ClassRunner::class)
3129
class RecyclerViewActivityBaselineProfileGenerator {
3230

@@ -40,14 +38,14 @@ class RecyclerViewActivityBaselineProfileGenerator {
4038
maxIterations = 15,
4139
stableIterations = 3
4240
) {
43-
// Start into the RecyclerViewActivity
44-
startActivityAndWait(Intent("$TARGET_PACKAGE.RECYCLER_VIEW_ACTIVITY"))
41+
uiAutomator {
42+
// Start into the RecyclerViewActivity
43+
startIntent(Intent("$TARGET_PACKAGE.RECYCLER_VIEW_ACTIVITY"))
4544

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)
45+
with(onView { viewIdResourceName == "recycler" }) {
46+
setGestureMargin(device.displayWidth / 10)
47+
setOf(Direction.DOWN, Direction.UP).forEach { fling(it) }
48+
}
5149
}
5250
}
5351
}

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

+4-1
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()
47+
}
4548
}
4649
}
4750
}

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

+20-19
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ 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.uiAutomator
30+
import com.example.macrobenchmark.benchmark.permissions.allowNotifications
2931
import com.example.macrobenchmark.benchmark.util.DEFAULT_ITERATIONS
3032
import com.example.macrobenchmark.benchmark.util.TARGET_PACKAGE
31-
import com.example.macrobenchmark.benchmark.permissions.allowNotifications
3233
import org.junit.Rule
3334
import org.junit.Test
3435
import org.junit.runner.RunWith
@@ -51,33 +52,35 @@ class LoginBenchmark {
5152
@Test
5253
fun loginInSetupBlock() {
5354
benchmarkLoginActivity(setupBlock = {
54-
startActivityAndWait(Intent("$packageName.LOGIN_ACTIVITY"))
55-
login()
55+
uiAutomator {
56+
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
57+
login()
58+
}
5659
})
5760
}
5861

5962
@Test
6063
fun loginWithUiAutomator() {
6164
benchmarkLoginActivity {
62-
login()
65+
uiAutomator { login() }
6366
}
6467
}
6568

6669
@Test
6770
fun loginInAfterPermissionsGranted() {
6871
benchmarkLoginActivity(setupBlock = {
6972
allowNotifications()
70-
71-
startActivityAndWait(Intent("$packageName.LOGIN_ACTIVITY"))
72-
login()
73+
uiAutomator {
74+
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
75+
login()
76+
}
7377
})
7478
}
7579

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()
80+
private fun UiAutomatorTestScope.login() {
81+
onView { viewIdResourceName == "userName" }.text = "user"
82+
onView { viewIdResourceName == "password" }.text = "password"
83+
onView { viewIdResourceName == "login" }.click()
8184
}
8285

8386
private fun benchmarkLoginActivity(
@@ -93,12 +96,10 @@ class LoginBenchmark {
9396
iterations = DEFAULT_ITERATIONS,
9497
setupBlock = setupBlock,
9598
) {
96-
startActivityAndWait(
97-
Intent()
98-
.putExtras(extras)
99-
.setAction("$packageName.LOGIN_ACTIVITY")
100-
)
101-
measureBlock()
99+
uiAutomator {
100+
startIntent(Intent().putExtras(extras).setAction("$packageName.LOGIN_ACTIVITY"))
101+
measureBlock()
102+
}
102103
}
103104
}
104105
}

0 commit comments

Comments
 (0)