Skip to content

Commit

Permalink
maestro.client.Maestro: reset cachedDeviceInfo when continuous flow i…
Browse files Browse the repository at this point in the history
…s rerun
  • Loading branch information
bartekpacia committed Sep 9, 2024
1 parent 5e5a810 commit ea5e718
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ object MaestroCommandRunner {
}

refreshUi()
maestro.clearCachedDeviceInfo()

val orchestra = Orchestra(
maestro = maestro,
Expand Down
46 changes: 25 additions & 21 deletions maestro-client/src/main/java/maestro/Maestro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,28 @@ class Maestro(

private val sessionId = UUID.randomUUID()

private val cachedDeviceInfo by lazy {
fetchDeviceInfo()
}

private var screenRecordingInProgress = false
val deviceName: String
get() = driver.name()

private var _cachedDeviceInfo: DeviceInfo? = null
fun cachedDeviceInfo(): DeviceInfo {
var deviceInfo: DeviceInfo? = _cachedDeviceInfo
if (deviceInfo == null) {
LOGGER.info("Getting device info")
deviceInfo = driver.deviceInfo()
_cachedDeviceInfo = deviceInfo
LOGGER.info("Got device info: $deviceInfo")
}

fun deviceName(): String {
return driver.name()
return deviceInfo
}

fun deviceInfo(): DeviceInfo {
return cachedDeviceInfo
fun clearCachedDeviceInfo() {
LOGGER.info("Clearing cached device info")
_cachedDeviceInfo = null
}

private fun fetchDeviceInfo(): DeviceInfo {
LOGGER.info("Getting device info")

return driver.deviceInfo()
}
private var screenRecordingInProgress = false

fun launchApp(
appId: String,
Expand Down Expand Up @@ -135,14 +138,14 @@ class Maestro(
startRelative != null && endRelative != null -> {
val startPoints = startRelative.replace("%", "")
.split(",").map { it.trim().toInt() }
val startX = cachedDeviceInfo.widthGrid * startPoints[0] / 100
val startY = cachedDeviceInfo.heightGrid * startPoints[1] / 100
val startX = cachedDeviceInfo().widthGrid * startPoints[0] / 100
val startY = cachedDeviceInfo().heightGrid * startPoints[1] / 100
val start = Point(startX, startY)

val endPoints = endRelative.replace("%", "")
.split(",").map { it.trim().toInt() }
val endX = cachedDeviceInfo.widthGrid * endPoints[0] / 100
val endY = cachedDeviceInfo.heightGrid * endPoints[1] / 100
val endX = cachedDeviceInfo().widthGrid * endPoints[0] / 100
val endY = cachedDeviceInfo().heightGrid * endPoints[1] / 100
val end = Point(endX, endY)

driver.swipe(start, end, duration)
Expand All @@ -161,7 +164,7 @@ class Maestro(

fun swipeFromCenter(swipeDirection: SwipeDirection, durationMs: Long) {
LOGGER.info("Swiping ${swipeDirection.name} from center")
val center = Point(x = cachedDeviceInfo.widthGrid / 2, y = cachedDeviceInfo.heightGrid / 2)
val center = Point(x = cachedDeviceInfo().widthGrid / 2, y = cachedDeviceInfo().heightGrid / 2)
driver.swipe(center, swipeDirection, durationMs)
waitForAppToSettle()
}
Expand Down Expand Up @@ -235,8 +238,8 @@ class Maestro(
tapRepeat: TapRepeat? = null,
waitToSettleTimeoutMs: Int? = null
) {
val x = cachedDeviceInfo.widthGrid * percentX / 100
val y = cachedDeviceInfo.heightGrid * percentY / 100
val x = cachedDeviceInfo().widthGrid * percentX / 100
val y = cachedDeviceInfo().heightGrid * percentY / 100
tap(
x = x,
y = y,
Expand Down Expand Up @@ -574,6 +577,7 @@ class Maestro(
}

fun waitForAnimationToEnd(timeout: Long?) {
@Suppress("NAME_SHADOWING")
val timeout = timeout ?: ANIMATION_TIMEOUT_MS
LOGGER.info("Waiting for animation to end with timeout $timeout")

Expand Down
18 changes: 7 additions & 11 deletions maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class Orchestra(
private var copiedText: String? = null

private var timeMsOfLastInteraction = System.currentTimeMillis()
private var deviceInfo: DeviceInfo? = null

private var screenRecording: ScreenRecording? = null

Expand Down Expand Up @@ -224,7 +223,7 @@ class Orchestra(
}
val shouldUseGraalJs =
config?.ext?.get("jsEngine") == "graaljs" || System.getenv("MAESTRO_USE_GRAALJS") == "true"
val platform = maestro.deviceInfo().platform.toString().lowercase()
val platform = maestro.cachedDeviceInfo().platform.toString().lowercase()
jsEngine = if (shouldUseGraalJs) {
httpClient?.let { GraalJsEngine(it, platform) } ?: GraalJsEngine(platform = platform)
} else {
Expand Down Expand Up @@ -475,7 +474,7 @@ class Orchestra(
private fun scrollUntilVisible(command: ScrollUntilVisibleCommand): Boolean {
val endTime = System.currentTimeMillis() + command.timeout.toLong()
val direction = command.direction.toSwipeDirection()
val deviceInfo = maestro.deviceInfo()
val deviceInfo = maestro.cachedDeviceInfo()

var retryCenterCount = 0
val maxRetryCenterCount =
Expand Down Expand Up @@ -591,7 +590,7 @@ class Orchestra(
}

condition.platform?.let {
if (it != maestro.deviceInfo().platform) {
if (it != maestro.cachedDeviceInfo().platform) {
return false
}
}
Expand Down Expand Up @@ -929,8 +928,8 @@ class Orchestra(
)

val (description, filterFunc) = buildFilter(
selector,
deviceInfo(),
selector = selector,
deviceInfo = maestro.cachedDeviceInfo(),
)
if (selector.childOf != null) {
val parentViewHierarchy = findElementViewHierarchy(
Expand Down Expand Up @@ -966,8 +965,8 @@ class Orchestra(
}
val parentViewHierarchy = findElementViewHierarchy(selector.childOf, timeout);
val (description, filterFunc) = buildFilter(
selector,
deviceInfo(),
selector = selector,
deviceInfo = maestro.cachedDeviceInfo(),
)
return maestro.findElementWithTimeout(
timeout,
Expand All @@ -979,9 +978,6 @@ class Orchestra(
)
}

private fun deviceInfo() = deviceInfo
?: maestro.deviceInfo().also { deviceInfo = it }

private fun buildFilter(
selector: ElementSelector,
deviceInfo: DeviceInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ object DeviceService {
}
}

val deviceInfo = maestro.deviceInfo()
val deviceInfo = maestro.cachedDeviceInfo()

val deviceWidth = deviceInfo.widthGrid
val deviceHeight = deviceInfo.heightGrid
Expand Down Expand Up @@ -231,4 +231,4 @@ object DeviceService {
screenshotDir.createDirectories()
return screenshotDir
}
}
}

0 comments on commit ea5e718

Please sign in to comment.