From 8a8d618081b5cf6cd0ae5c767c2fc266703e0654 Mon Sep 17 00:00:00 2001 From: Eran Boudjnah Date: Mon, 7 Apr 2025 15:26:10 +0100 Subject: [PATCH 1/2] Removed repetition in DisableAnimationsRule. --- .../whoami/test/rule/DisableAnimationsRule.kt | 52 +++++++++---------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt b/architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt index d67f6c7c..37bc5cfe 100644 --- a/architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt +++ b/architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt @@ -7,10 +7,18 @@ import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement +private val animationKeys = setOf( + "transition_animation_scale", + "window_animation_scale", + "animator_duration_scale" +) + +private const val DEFAULT_SCALE = 1f + class DisableAnimationsRule : TestRule { - private var transitionAnimationScale: Float = 0f - private var windowAnimationScale: Float = 0f - private var animatorDurationScale: Float = 0f + private val savedScaleValues = mutableMapOf() + private val device: UiDevice + get() = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) override fun apply(base: Statement, description: Description): Statement = object : Statement() { @@ -20,45 +28,33 @@ class DisableAnimationsRule : TestRule { try { base.evaluate() } finally { - enableAnimations() + restoreAnimations() } } } @Throws(IOException::class) private fun disableAnimations() { - UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).apply { - transitionAnimationScale = - executeShellCommand("settings get global transition_animation_scale") - .orDefault().toFloat() - windowAnimationScale = - executeShellCommand("settings get global window_animation_scale") - .orDefault().toFloat() - animatorDurationScale = - executeShellCommand("settings get global animator_duration_scale") - .orDefault().toFloat() - executeShellCommand("settings put global transition_animation_scale 0") - executeShellCommand("settings put global window_animation_scale 0") - executeShellCommand("settings put global animator_duration_scale 0") + device.apply { + animationKeys.forEach { key -> + savedScaleValues[key] = executeShellCommand("settings get global $key").orDefault() + executeShellCommand("settings put global $key 0") + } } } @Throws(IOException::class) - private fun enableAnimations() { - UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).apply { - executeShellCommand( - "settings put global transition_animation_scale $transitionAnimationScale" - ) - executeShellCommand("settings put global window_animation_scale $windowAnimationScale") - executeShellCommand( - "settings put global animator_duration_scale $animatorDurationScale" + private fun restoreAnimations() { + animationKeys.forEach { key -> + device.executeShellCommand( + "settings put global $key ${savedScaleValues[key] ?: DEFAULT_SCALE}" ) } } - private fun String.orDefault() = if (isEmpty() || trim() == "null") { - "1" + private fun String?.orDefault() = if (isNullOrEmpty() || trim() == "null") { + DEFAULT_SCALE } else { - this + toFloat() } } From 757ae2e26780325ef25772c897baa3cead7e3e6a Mon Sep 17 00:00:00 2001 From: Eran Boudjnah Date: Mon, 7 Apr 2025 16:13:24 +0100 Subject: [PATCH 2/2] Improved naming, reduced indentation. --- .../whoami/test/rule/DisableAnimationsRule.kt | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt b/architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt index 37bc5cfe..22101b18 100644 --- a/architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt +++ b/architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt @@ -24,7 +24,7 @@ class DisableAnimationsRule : TestRule { object : Statement() { @Throws(Throwable::class) override fun evaluate() { - disableAnimations() + saveAndDisableAnimations() try { base.evaluate() } finally { @@ -34,21 +34,19 @@ class DisableAnimationsRule : TestRule { } @Throws(IOException::class) - private fun disableAnimations() { - device.apply { - animationKeys.forEach { key -> - savedScaleValues[key] = executeShellCommand("settings get global $key").orDefault() - executeShellCommand("settings put global $key 0") - } + private fun saveAndDisableAnimations() { + animationKeys.forEach { key -> + savedScaleValues[key] = + device.executeShellCommand("settings get global $key").orDefault() + device.executeShellCommand("settings put global $key 0") } } @Throws(IOException::class) private fun restoreAnimations() { animationKeys.forEach { key -> - device.executeShellCommand( - "settings put global $key ${savedScaleValues[key] ?: DEFAULT_SCALE}" - ) + val savedValue = savedScaleValues[key] ?: DEFAULT_SCALE + device.executeShellCommand("settings put global $key $savedValue") } }