Skip to content

Commit 31ddfef

Browse files
Upgrade macrobenchmark sample to UiAutomator 2.4-alpha05
1 parent 58a12f9 commit 31ddfef

19 files changed

+233
-432
lines changed

MacrobenchmarkSample/gradle/libs.versions.toml

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

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

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ 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
24+
import com.example.macrobenchmark.benchmark.util.TARGET_PACKAGE
2625
import org.junit.Ignore
2726
import org.junit.Rule
2827
import org.junit.Test
@@ -42,16 +41,13 @@ class ComposeActivityBaselineProfileGenerator {
4241
maxIterations = 15,
4342
stableIterations = 3
4443
) {
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)
44+
uiAutomator {
45+
// Start into the Compose Activity
46+
startIntent(Intent("$TARGET_PACKAGE.COMPOSE_ACTIVITY"))
47+
// Scrolling through the Compose journey
48+
onElement { viewIdResourceName == "myLazyColumn" }.fling(Direction.DOWN)
49+
onElement { viewIdResourceName == "myLazyColumn" }.fling(Direction.UP)
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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ 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
24+
import com.example.macrobenchmark.benchmark.util.TARGET_PACKAGE
2325
import org.junit.Rule
2426
import org.junit.Test
2527
import org.junit.runner.RunWith
@@ -37,12 +39,13 @@ class LoginBaselineProfileGenerator {
3739
maxIterations = 15,
3840
stableIterations = 3
3941
) {
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()
42+
uiAutomator {
43+
clearAppData()
44+
startIntent(Intent("$packageName.LOGIN_ACTIVITY"))
45+
onElement { textAsString() == "User: " }.text = "user"
46+
onElement { textAsString() == "Password: " }.text = "password"
47+
onElement { textAsString() == "Login" }.click()
48+
}
4649
}
4750
}
4851
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ 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
24+
import com.example.macrobenchmark.benchmark.util.TARGET_PACKAGE
2425
import org.junit.Ignore
2526
import org.junit.Rule
2627
import org.junit.Test
@@ -40,14 +41,13 @@ class RecyclerViewActivityBaselineProfileGenerator {
4041
maxIterations = 15,
4142
stableIterations = 3
4243
) {
43-
// Start into the RecyclerViewActivity
44-
startActivityAndWait(Intent("$TARGET_PACKAGE.RECYCLER_VIEW_ACTIVITY"))
44+
uiAutomator {
45+
// Start into the RecyclerViewActivity
46+
startIntent(Intent("$TARGET_PACKAGE.RECYCLER_VIEW_ACTIVITY"))
4547

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)
48+
// Scrolling RecyclerView journey
49+
onElement { viewIdResourceName == "recycler" }.fling(Direction.DOWN)
50+
onElement { viewIdResourceName == "recycler" }.fling(Direction.UP)
5151
}
5252
}
5353
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ 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
22+
import com.example.macrobenchmark.benchmark.util.TARGET_PACKAGE
2123
import org.junit.Rule
2224
import org.junit.Test
2325
import org.junit.runner.RunWith
@@ -41,7 +43,9 @@ class StartupProfileGenerator {
4143
stableIterations = 3,
4244
includeInStartupProfile = true
4345
) {
44-
startActivityAndWait()
46+
uiAutomator {
47+
startApp(TARGET_PACKAGE)
48+
}
4549
}
4650
}
4751
}

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 { viewIdResourceName == "userName" }.text = "user"
87+
onElement { viewIdResourceName == "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)