Skip to content

Commit

Permalink
More convenient waiting for objects
Browse files Browse the repository at this point in the history
Change-Id: I21c4f78bd963c4ed7c6d937cc60b3cf6c5c42b89
  • Loading branch information
keyboardsurfer committed Oct 10, 2023
1 parent 6f7a1e9 commit 4bc53fb
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import android.Manifest.permission
import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.TIRAMISU
import androidx.benchmark.macro.MacrobenchmarkScope
import androidx.test.uiautomator.By
import androidx.test.uiautomator.BySelector
import androidx.test.uiautomator.UiObject2
import androidx.test.uiautomator.Until

/**
* Because the app under test is different from the one running the instrumentation test,
Expand All @@ -42,3 +46,27 @@ fun MacrobenchmarkScope.allowNotifications() {
device.executeShellCommand(command)
}
}

/**
* Wraps starting the default activity, waiting for it to start and then allowing notifications in
* one convenient call.
*/
fun MacrobenchmarkScope.startActivityAndAllowNotifications() {
startActivityAndWait()
allowNotifications()
}

/**
* Waits for and returns the `niaTopAppBar`
*/
fun MacrobenchmarkScope.getTopAppBar(): UiObject2 {
device.wait(Until.hasObject(By.res("niaTopAppBar")), 2_000)
return device.findObject(By.res("niaTopAppBar"))
}

/**
* Waits for an object on the top app bar, passed in as [selector].
*/
fun MacrobenchmarkScope.waitForObjectOnTopAppBar(selector: BySelector, timeout: Long = 2_000) {
getTopAppBar().wait(Until.hasObject(selector), timeout)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.samples.apps.nowinandroid.baselineprofile
import androidx.benchmark.macro.junit4.BaselineProfileRule
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
import com.google.samples.apps.nowinandroid.bookmarks.goToBookmarksScreen
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
import org.junit.Rule
import org.junit.Test

Expand All @@ -31,7 +32,7 @@ class BookmarksBaselineProfile {
@Test
fun generate() =
baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndWait()
startActivityAndAllowNotifications()

// Navigate to saved screen
goToBookmarksScreen()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package com.google.samples.apps.nowinandroid.baselineprofile

import androidx.benchmark.macro.junit4.BaselineProfileRule
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
import com.google.samples.apps.nowinandroid.allowNotifications
import com.google.samples.apps.nowinandroid.foryou.forYouScrollFeedDownUp
import com.google.samples.apps.nowinandroid.foryou.forYouSelectTopics
import com.google.samples.apps.nowinandroid.foryou.forYouWaitForContent
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
import org.junit.Rule
import org.junit.Test

Expand All @@ -34,8 +34,7 @@ class ForYouBaselineProfile {
@Test
fun generate() =
baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndWait()
allowNotifications()
startActivityAndAllowNotifications()

// Scroll the feed critical user journey
forYouWaitForContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.benchmark.macro.junit4.BaselineProfileRule
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
import com.google.samples.apps.nowinandroid.interests.goToInterestsScreen
import com.google.samples.apps.nowinandroid.interests.interestsScrollTopicsDownUp
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
import org.junit.Rule
import org.junit.Test

Expand All @@ -32,7 +33,7 @@ class InterestsBaselineProfile {
@Test
fun generate() =
baselineProfileRule.collect(PACKAGE_NAME) {
startActivityAndWait()
startActivityAndAllowNotifications()

// Navigate to interests screen
goToInterestsScreen()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.samples.apps.nowinandroid.baselineprofile

import androidx.benchmark.macro.junit4.BaselineProfileRule
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
import org.junit.Rule
import org.junit.Test

Expand All @@ -33,6 +34,6 @@ class StartupBaselineProfile {
PACKAGE_NAME,
includeInStartupProfile = true,
) {
startActivityAndWait() // Just wait
startActivityAndAllowNotifications()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ package com.google.samples.apps.nowinandroid.bookmarks

import androidx.benchmark.macro.MacrobenchmarkScope
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
import com.google.samples.apps.nowinandroid.waitAndFindObject
import com.google.samples.apps.nowinandroid.waitForObjectOnTopAppBar

fun MacrobenchmarkScope.goToBookmarksScreen() {
val savedButton = device.waitAndFindObject(By.text("Saved"), 5_000)
val savedSelector = By.text("Saved")
val savedButton = device.findObject(savedSelector)
savedButton.click()
device.waitForIdle()
// Wait until saved title are shown on screen
device.wait(Until.hasObject(By.res("niaTopAppBar")), 2_000)
val topAppBar = device.findObject(By.res("niaTopAppBar"))
topAppBar.wait(Until.hasObject(By.text("Saved")), 2_000)
}
waitForObjectOnTopAppBar(savedSelector)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.test.uiautomator.Until
import androidx.test.uiautomator.untilHasChildren
import com.google.samples.apps.nowinandroid.flingElementDownUp
import com.google.samples.apps.nowinandroid.waitAndFindObject
import com.google.samples.apps.nowinandroid.waitForObjectOnTopAppBar
import org.junit.Assert.fail

fun MacrobenchmarkScope.forYouWaitForContent() {
Expand Down Expand Up @@ -103,7 +104,5 @@ fun MacrobenchmarkScope.setAppTheme(isDark: Boolean) {
device.findObject(By.text("OK")).click()

// Wait until the top app bar is visible on screen
device.wait(Until.hasObject(By.res("niaTopAppBar")), 2_000)
val topAppBar = device.findObject(By.res("niaTopAppBar"))
topAppBar.wait(Until.hasObject(By.text("Now in Android")), 2_000)
waitForObjectOnTopAppBar(By.text("Now in Android"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import androidx.benchmark.macro.StartupMode
import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
import com.google.samples.apps.nowinandroid.allowNotifications
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -47,8 +47,7 @@ class ScrollForYouFeedBenchmark {
setupBlock = {
// Start the app
pressHome()
startActivityAndWait()
allowNotifications()
startActivityAndAllowNotifications()
},
) {
forYouWaitForContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import androidx.benchmark.macro.MacrobenchmarkScope
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
import com.google.samples.apps.nowinandroid.flingElementDownUp
import com.google.samples.apps.nowinandroid.waitForObjectOnTopAppBar

fun MacrobenchmarkScope.goToInterestsScreen() {
device.findObject(By.text("Interests")).click()
device.waitForIdle()
// Wait until interests are shown on screen
device.wait(Until.hasObject(By.res("niaTopAppBar")), 2_000)
val topAppBar = device.findObject(By.res("niaTopAppBar"))
topAppBar.wait(Until.hasObject(By.text("Interests")), 2_000)
waitForObjectOnTopAppBar(By.text("Interests"))

// Wait until content is loaded by checking if interests are loaded
device.wait(Until.gone(By.res("loadingWheel")), 5_000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.uiautomator.By
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
import com.google.samples.apps.nowinandroid.allowNotifications
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -47,8 +47,7 @@ class ScrollTopicListBenchmark {
setupBlock = {
// Start the app
pressHome()
startActivityAndWait()
allowNotifications()
startActivityAndAllowNotifications()
// Navigate to interests screen
device.findObject(By.text("Interests")).click()
device.waitForIdle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import androidx.benchmark.macro.junit4.MacrobenchmarkRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.uiautomator.By
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
import com.google.samples.apps.nowinandroid.allowNotifications
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -47,8 +47,7 @@ class TopicsScreenRecompositionBenchmark {
setupBlock = {
// Start the app
pressHome()
startActivityAndWait()
allowNotifications()
startActivityAndAllowNotifications()
// Navigate to interests screen
device.findObject(By.text("Interests")).click()
device.waitForIdle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
import com.google.samples.apps.nowinandroid.allowNotifications
import com.google.samples.apps.nowinandroid.foryou.forYouWaitForContent
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -66,8 +67,7 @@ class StartupBenchmark {
allowNotifications()
},
) {
startActivityAndWait()
allowNotifications()
startActivityAndAllowNotifications()
// Waits until the content is ready to capture Time To Full Display
forYouWaitForContent()
}
Expand Down

0 comments on commit 4bc53fb

Please sign in to comment.