Skip to content

Instantly share code, notes, and snippets.

@balachandarlinks
Last active May 21, 2020 17:21
Show Gist options
  • Save balachandarlinks/91c4ff8a907824d75f3feadccd774c87 to your computer and use it in GitHub Desktop.
Save balachandarlinks/91c4ff8a907824d75f3feadccd774c87 to your computer and use it in GitHub Desktop.
Disable animations for Android UI tests
# Don't forget to change test runner in app's build.gradle file
...
android {
...
defaultConfig {
...
testInstrumentationRunner "in.bala.TestRunner"
}
...
}
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.provider.Settings.Global.*
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.AndroidJUnitRunner
class TestRunner : AndroidJUnitRunner() {
override fun onCreate(arguments: Bundle?) {
super.onCreate(arguments)
setAnimations(false)
}
override fun finish(resultCode: Int, results: Bundle?) {
setAnimations(true)
super.finish(resultCode, results)
}
private fun setAnimations(enabled: Boolean) {
val value = if (enabled) "1.0" else "0.0"
InstrumentationRegistry.getInstrumentation().uiAutomation.run {
this.executeShellCommand("settings put global $WINDOW_ANIMATION_SCALE $value")
this.executeShellCommand("settings put global $TRANSITION_ANIMATION_SCALE $value")
this.executeShellCommand("settings put global $ANIMATOR_DURATION_SCALE $value")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment