Skip to content

Commit f6688fa

Browse files
authored
Removed repetition in DisableAnimationsRule. (#395)
1 parent f12b403 commit f6688fa

1 file changed

Lines changed: 25 additions & 31 deletions

File tree

architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/DisableAnimationsRule.kt

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,52 @@ import org.junit.rules.TestRule
77
import org.junit.runner.Description
88
import org.junit.runners.model.Statement
99

10+
private val animationKeys = setOf(
11+
"transition_animation_scale",
12+
"window_animation_scale",
13+
"animator_duration_scale"
14+
)
15+
16+
private const val DEFAULT_SCALE = 1f
17+
1018
class DisableAnimationsRule : TestRule {
11-
private var transitionAnimationScale: Float = 0f
12-
private var windowAnimationScale: Float = 0f
13-
private var animatorDurationScale: Float = 0f
19+
private val savedScaleValues = mutableMapOf<String, Float>()
20+
private val device: UiDevice
21+
get() = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
1422

1523
override fun apply(base: Statement, description: Description): Statement =
1624
object : Statement() {
1725
@Throws(Throwable::class)
1826
override fun evaluate() {
19-
disableAnimations()
27+
saveAndDisableAnimations()
2028
try {
2129
base.evaluate()
2230
} finally {
23-
enableAnimations()
31+
restoreAnimations()
2432
}
2533
}
2634
}
2735

2836
@Throws(IOException::class)
29-
private fun disableAnimations() {
30-
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).apply {
31-
transitionAnimationScale =
32-
executeShellCommand("settings get global transition_animation_scale")
33-
.orDefault().toFloat()
34-
windowAnimationScale =
35-
executeShellCommand("settings get global window_animation_scale")
36-
.orDefault().toFloat()
37-
animatorDurationScale =
38-
executeShellCommand("settings get global animator_duration_scale")
39-
.orDefault().toFloat()
40-
executeShellCommand("settings put global transition_animation_scale 0")
41-
executeShellCommand("settings put global window_animation_scale 0")
42-
executeShellCommand("settings put global animator_duration_scale 0")
37+
private fun saveAndDisableAnimations() {
38+
animationKeys.forEach { key ->
39+
savedScaleValues[key] =
40+
device.executeShellCommand("settings get global $key").orDefault()
41+
device.executeShellCommand("settings put global $key 0")
4342
}
4443
}
4544

4645
@Throws(IOException::class)
47-
private fun enableAnimations() {
48-
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).apply {
49-
executeShellCommand(
50-
"settings put global transition_animation_scale $transitionAnimationScale"
51-
)
52-
executeShellCommand("settings put global window_animation_scale $windowAnimationScale")
53-
executeShellCommand(
54-
"settings put global animator_duration_scale $animatorDurationScale"
55-
)
46+
private fun restoreAnimations() {
47+
animationKeys.forEach { key ->
48+
val savedValue = savedScaleValues[key] ?: DEFAULT_SCALE
49+
device.executeShellCommand("settings put global $key $savedValue")
5650
}
5751
}
5852

59-
private fun String.orDefault() = if (isEmpty() || trim() == "null") {
60-
"1"
53+
private fun String?.orDefault() = if (isNullOrEmpty() || trim() == "null") {
54+
DEFAULT_SCALE
6155
} else {
62-
this
56+
toFloat()
6357
}
6458
}

0 commit comments

Comments
 (0)