Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Wait for objects
Browse files Browse the repository at this point in the history
This might slow the test down, but it should be less
flakey
  • Loading branch information
chrisbanes committed Jan 19, 2023
1 parent 681befd commit 38fad2d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package app.tivi.benchmark

import android.os.SystemClock
import androidx.benchmark.macro.ExperimentalBaselineProfilesApi
import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.uiautomator.By
import androidx.test.uiautomator.BySelector
import androidx.test.uiautomator.Direction
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject2
import androidx.test.uiautomator.Until
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -62,77 +65,74 @@ class BaselineProfileGenerator {

private fun UiDevice.testDiscover(): Boolean {
// Scroll one of the Discover Carousels
findObject(By.res("discover_carousel"))
waitForObject(By.res("discover_carousel"))
.scroll(Direction.RIGHT, 1f)
findObject(By.res("discover_carousel"))
waitForObject(By.res("discover_carousel"))
.scroll(Direction.LEFT, 1f)

return findObject(By.res("discover_carousel_item")) != null
return wait(Until.hasObject(By.res("discover_carousel_item")), 5_000)
}

private fun UiDevice.navigateFromDiscoverToShowDetails() {
// Open a show from one of the carousels
findObject(By.res("discover_carousel_item")).click()
waitForObject(By.res("discover_carousel_item")).click()
waitForIdle()
}

private fun UiDevice.testShowDetails(): Boolean {
// Scroll the main show details list down, then back up
findObject(By.scrollable(true))
.scroll(Direction.DOWN, 1f)

// Scroll the main show details list back up
findObject(By.scrollable(true))
.scroll(Direction.UP, 1f)

// Now follow the show
findObject(By.res("show_details_follow_button"))
// Follow the show
waitForObject(By.res("show_details_follow_button"))
.click()

fun seasonItemExists(): Boolean {
return findObject(By.res("show_details_season_item")) != null
}

// Wait 10 seconds for a season item to show
for (i in 1..10) {
if (seasonItemExists()) break
if (hasObject(By.res("show_details_season_item"))) {
return true
}

Thread.sleep(1000)
findObject(By.scrollable(true)).scroll(Direction.DOWN, 1f)
SystemClock.sleep(1000)

// Scroll to the end to show the seasons
waitForObject(By.res("show_details_lazycolumn"))
.scroll(Direction.DOWN, 1f)
}

return seasonItemExists()
return false
}

private fun UiDevice.navigateFromShowDetailsToSeasons() {
findObject(By.res("show_details_season_item")).click()
waitForObject(By.res("show_details_season_item")).click()
waitForIdle()
}

private fun UiDevice.testSeasons(): Boolean {
// Not much to test here at the moment
return findObject(By.res("show_seasons_episode_item")) != null
return wait(Until.hasObject(By.res("show_seasons_episode_item")), 5_000)
}

private fun UiDevice.navigateFromSeasonsToEpisodeDetails() {
findObject(By.res("show_seasons_episode_item")).click()
waitForObject(By.res("show_seasons_episode_item")).click()
waitForIdle()
}

private fun UiDevice.testEpisodeDetails(): Boolean {
wait(Until.hasObject(By.res("episode_details")), 3_000)

// Need to 'inset' the gesture so that we don't swipe
// the notification tray down
findObject(By.res("episode_details"))
.setGestureMargin(displayWidth / 10)

// Swipe the bottom sheet 'up', then 'down'
findObject(By.res("episode_details"))
.scroll(Direction.DOWN, 0.8f)
findObject(By.res("episode_details"))
.scroll(Direction.UP, 0.8f)

with(waitForObject(By.res("episode_details"))) {
// Need to 'inset' the gesture so that we don't swipe
// the notification tray down
setGestureMargin(displayWidth / 10)

// Swipe the bottom sheet 'up', then 'down'
scroll(Direction.DOWN, 0.8f)
scroll(Direction.UP, 0.8f)
}
return true
}
}

private fun UiDevice.waitForObject(selector: BySelector, timeout: Long = 5_000): UiObject2 {
if (wait(Until.hasObject(selector), timeout)) {
return findObject(selector)
}

error("Object with selector [$selector] not found")
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ internal fun ShowDetails(
unfollowPreviousSeasons = unfollowPreviousSeasons,
onMarkSeasonWatched = onMarkSeasonWatched,
onMarkSeasonUnwatched = onMarkSeasonUnwatched,
modifier = Modifier.fillMaxSize(),
modifier = Modifier
.testTag("show_details_lazycolumn")
.fillMaxSize(),
)
}
}
Expand Down

0 comments on commit 38fad2d

Please sign in to comment.