SET_ANIMATION_SCALE PERMISSION For UI Test On Android

As you know, Espresso requires animations to be disabled. Through years I've tried different solutions to do that:
- Pre setup Device/Emulator image so animations are disabled already. HARD TO DO AND MAINTAIN.
- Use UiAutomator to go to Device/Emulator developer options and disable animations there (wth…). VERY UGLY and system-dependent.
- Run
pm grant package.com android.permission.SET_ANIMATION_SCALE
after installation of the app but before test run and then disable/enable animations in the test code. YOU HAVE TO DEAL WITH BUILD SYSTEM AND ADB.
Today I've figured out much easier way to do that:
- Use UiAutomator to grant permission right in the tests (in Test Runner for example)
- Disable/enable animations from tests.
UiDevice
.getInstance(instrumentation)
.executeShellCommand(
"pm grant "
+ instrumentation.getTargetContext().getPackageName()
+ " android.permission.SET_ANIMATION_SCALE"
)
And something like this to control animations:
- https://gist.github.com/xrigau/11284124
- https://gist.github.com/danielgomezrico/9371a79a7222a156ddad
(You will need only animation enable/disable code, no Gradle/etc scripts are required.)
UiAutomator client library is part of support-libs.
😿 Took so long to realize that it was that easy 😿