Last active
February 15, 2022 10:28
-
-
Save lgawin/4eafb63310bdd5b9af7450dbd82d9ceb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.orderbird.testing | |
import android.app.Activity | |
import androidx.test.ext.junit.rules.ActivityScenarioRule | |
import androidx.test.platform.app.InstrumentationRegistry | |
import androidx.test.uiautomator.UiDevice | |
import com.orderbird.testing.AnimationScaleSettings.disableAnimations | |
import com.orderbird.testing.AnimationScaleSettings.restoreAnimations | |
import org.junit.rules.RuleChain | |
import org.junit.rules.TestRule | |
import org.junit.runner.Description | |
import org.junit.runners.model.Statement | |
class DisableAnimationsRule : TestRule { | |
override fun apply(base: Statement, description: Description?): Statement = object : Statement() { | |
override fun evaluate() = with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())) { | |
val animationScaleValues = disableAnimations() | |
try { | |
base.evaluate() | |
} finally { | |
restoreAnimations(animationScaleValues) | |
} | |
} | |
} | |
} | |
private object AnimationScaleSettings { | |
private const val KEY_TRANSITION_ANIMATION_SCALE = "transition_animation_scale" | |
private const val KEY_WINDOW_ANIMATION_SCALE = "window_animation_scale" | |
private const val KEY_ANIMATOR_DURATION_SCALE = "animator_duration_scale" | |
private const val VALUE_DISABLED = "0" | |
fun UiDevice.disableAnimations() = AnimationScaleValues( | |
replaceSetting(KEY_TRANSITION_ANIMATION_SCALE, VALUE_DISABLED), | |
replaceSetting(KEY_WINDOW_ANIMATION_SCALE, VALUE_DISABLED), | |
replaceSetting(KEY_ANIMATOR_DURATION_SCALE, VALUE_DISABLED), | |
) | |
fun UiDevice.restoreAnimations(animationScaleValues: AnimationScaleValues) { | |
with(animationScaleValues) { | |
setSetting(KEY_TRANSITION_ANIMATION_SCALE, transitionAnimation) | |
setSetting(KEY_WINDOW_ANIMATION_SCALE, windowAnimation) | |
setSetting(KEY_ANIMATOR_DURATION_SCALE, animatorDuration) | |
} | |
} | |
private fun UiDevice.getSetting(settingName: String) = | |
executeShellCommand("settings get global $settingName") | |
private fun UiDevice.setSetting(settingName: String, value: String) = | |
executeShellCommand("settings put global $settingName $value") | |
private fun UiDevice.replaceSetting(settingName: String, value: String): String = | |
getSetting(settingName).also { setSetting(settingName, value) } | |
} | |
private data class AnimationScaleValues( | |
val transitionAnimation: String, | |
val windowAnimation: String, | |
val animatorDuration: String, | |
) | |
val <A : Activity> ActivityScenarioRule<A>.withAnimationsDisabled: TestRule | |
get() = RuleChain.outerRule(DisableAnimationsRule()).around(this) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment