From ea5e718ebb59039a1b80628c516ee99cfcc9f2f6 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Mon, 9 Sep 2024 12:00:29 +0200 Subject: [PATCH] maestro.client.Maestro: reset cachedDeviceInfo when continuous flow is rerun --- .../cli/runner/MaestroCommandRunner.kt | 1 + .../src/main/java/maestro/Maestro.kt | 46 ++++++++++--------- .../main/java/maestro/orchestra/Orchestra.kt | 18 +++----- .../main/java/maestro/studio/DeviceService.kt | 4 +- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/maestro-cli/src/main/java/maestro/cli/runner/MaestroCommandRunner.kt b/maestro-cli/src/main/java/maestro/cli/runner/MaestroCommandRunner.kt index 7347150c52..15612f0188 100644 --- a/maestro-cli/src/main/java/maestro/cli/runner/MaestroCommandRunner.kt +++ b/maestro-cli/src/main/java/maestro/cli/runner/MaestroCommandRunner.kt @@ -86,6 +86,7 @@ object MaestroCommandRunner { } refreshUi() + maestro.clearCachedDeviceInfo() val orchestra = Orchestra( maestro = maestro, diff --git a/maestro-client/src/main/java/maestro/Maestro.kt b/maestro-client/src/main/java/maestro/Maestro.kt index 846041a136..f9eedb7f07 100644 --- a/maestro-client/src/main/java/maestro/Maestro.kt +++ b/maestro-client/src/main/java/maestro/Maestro.kt @@ -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, @@ -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) @@ -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() } @@ -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, @@ -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") diff --git a/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt b/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt index ac21e48d46..ad3b45b82e 100644 --- a/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt +++ b/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt @@ -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 @@ -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 { @@ -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 = @@ -591,7 +590,7 @@ class Orchestra( } condition.platform?.let { - if (it != maestro.deviceInfo().platform) { + if (it != maestro.cachedDeviceInfo().platform) { return false } } @@ -929,8 +928,8 @@ class Orchestra( ) val (description, filterFunc) = buildFilter( - selector, - deviceInfo(), + selector = selector, + deviceInfo = maestro.cachedDeviceInfo(), ) if (selector.childOf != null) { val parentViewHierarchy = findElementViewHierarchy( @@ -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, @@ -979,9 +978,6 @@ class Orchestra( ) } - private fun deviceInfo() = deviceInfo - ?: maestro.deviceInfo().also { deviceInfo = it } - private fun buildFilter( selector: ElementSelector, deviceInfo: DeviceInfo, diff --git a/maestro-studio/server/src/main/java/maestro/studio/DeviceService.kt b/maestro-studio/server/src/main/java/maestro/studio/DeviceService.kt index ddc62748a6..ddd78f2ac0 100644 --- a/maestro-studio/server/src/main/java/maestro/studio/DeviceService.kt +++ b/maestro-studio/server/src/main/java/maestro/studio/DeviceService.kt @@ -178,7 +178,7 @@ object DeviceService { } } - val deviceInfo = maestro.deviceInfo() + val deviceInfo = maestro.cachedDeviceInfo() val deviceWidth = deviceInfo.widthGrid val deviceHeight = deviceInfo.heightGrid @@ -231,4 +231,4 @@ object DeviceService { screenshotDir.createDirectories() return screenshotDir } -} \ No newline at end of file +}