SET_ANIMATION_SCALE PERMISSION For UI Test On Android

SET_ANIMATION_SCALE PERMISSION For UI Test On Android
Mount Etna from the Greek Theatre, Taormina, Sicily by John MacWhirter, 1890.

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:

  1. Use UiAutomator to grant permission right in the tests (in Test Runner for example)
  2. 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:

(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 😿