17

Has anybody managed to disable animations through code when running Espresso tests? I've been trying to follow the instructions in this webpage (linked to from here):
https://code.google.com/p/android-test-kit/wiki/DisablingAnimations

Unfortunately it does not appear to be working, as I keep seeing this permissions error:

04-27 15:48:28.694      303-342/system_process W/PackageManager﹕ Not granting permission android.permission.SET_ANIMATION_SCALE to package com.cookbrite.dev (protectionLevel=50 flags=0x18be46)

I was really hoping to avoid reconfiguring my device/emulators. We frequently run individual tests locally and it will annoy me if I have to keep toggling settings.

I noticed some other developers complaining that this doesn't work, so I might not be alone:
https://groups.google.com/forum/#!msg/android-test-kit-discuss/TCil7kMQRTM/QK1qCjzM6KQJ

Dan J
  • 25,433
  • 17
  • 100
  • 173

4 Answers4

26

I'm executing these three commands for each animation type and they are working for me:

adb shell settings put global window_animation_scale 0.0
adb shell settings put global transition_animation_scale 0.0
adb shell settings put global animator_duration_scale 0.0

More information here - prepare android emulator for UI test automation.

denys
  • 6,834
  • 3
  • 37
  • 36
10

I finally got this to work. Here is a Gist listing the required steps:
https://gist.github.com/daj/7b48f1b8a92abf960e7b

The key step that I had missed was running adb to grant the permission:

adb shell pm grant com.mypackage android.permission.SET_ANIMATION_SCALE    

Adding the permission to the manifest and running the reflection steps did not seem to be enough on their own.

Dan J
  • 25,433
  • 17
  • 100
  • 173
  • 3
    When attempting that command I get this error `Operation not allowed: java.lang.SecurityException: Permission android.permission.SET_ANIMATION_SCALE is not a changeable permission type`. Do you have any ideas why I cannot grant the permission? – Matt Kranzler Sep 11 '15 at 20:55
  • 2
    @MattKranzler Are you using a real device? It won't work on non-rooted device. – Yenchi Sep 11 '15 at 21:21
  • @Yenchi I tried with an emulator and still have the exception – Daniel Gomez Rico Sep 24 '15 at 15:45
  • 1
    @Yenchi only with marshmallow emulator. – Daniel Gomez Rico Sep 24 '15 at 16:15
  • @MattKranzler make sure you have `` in `AndroidManifest.xml` of the target app (you can add it only for debug/etc) – Artem Zinnatullin Mar 22 '16 at 16:54
  • Suppose I do have this permission granted, how do I disable and enable animations using ADB commands, based on the previous settings ? I wish to only temporarily disable animations, and then restore them back to what they were. – android developer Aug 13 '16 at 23:20
5

Even better approach is to update app/build.gradle, if you're running tests from command line.

android {
    ...
    ...

    testOptions {
        animationsDisabled = true
    }
}

You may have to do ./gradlew clean before rebuilding. If you used android studio, it may not update the apk on your device assuming that nothing changed in the apk. Watch out for those to ensure that the change is actually taking effect on your device.

Also read the documentation here.

Disables animations during instrumented tests you run from the cammand line.

If you set this property to true, running instrumented tests with Gradle from the command line executes am instrument with the --no-window-animation flag. By default, this property is set to false.

This property does not affect tests that you run using Android Studio.

rpattabi
  • 9,984
  • 5
  • 45
  • 53
  • Don't copy and paste answers that does not works. This does not disable animations. – Max Fratane May 11 '20 at 02:19
  • I added as answer after finding that this works for me. Why do you think google provided this option? You may have to do `./gradlew clean` before rebuilding. If you used android studio, it may not update the apk on your device assuming that nothing changed in the apk. Watch out for those to ensure that the change is actually taking effect on your device. Updated answer with these instructions. – rpattabi May 11 '20 at 05:21
  • this option disables activity transition animation only, not others animations in your code. – Max Fratane May 11 '20 at 05:44
  • That's interesting. I was running `./gradlew app:connectedAndroidTest` and it worked for my purposes. Please take a look at the info from the documentation (updated the answer). It says "This property does not affect tests that you run using Android Studio". That may be causing the confusion. – rpattabi May 11 '20 at 07:23
3

use this wayes:


1. You use this in Gradle

android {

  //...

  testOptions {
    animationsDisabled = true
  }

  // ...
}

2. Use in ADB for device

adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &

3. Use the Rule

class DisableAnimationsRule : TestRule {
    override fun apply(base: Statement, description: Description): Statement {
        return object : Statement() {
            @Throws(Throwable::class)
           override fun evaluate() {
                // disable animations for test run
                changeAnimationStatus(enable = false)
                try {
                    base.evaluate()
                } finally {
                    // enable after test run
                    changeAnimationStatus(enable = true)
                }
            }
        }
    }

    @Throws(IOException::class)
    private fun changeAnimationStatus(enable:Boolean = true) {
        with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())){
            executeShellCommand("settings put global transition_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global window_animation_scale ${if(enable) 1 else 0}")
            executeShellCommand("settings put global animator_duration_scale ${if(enable) 1 else 0}")
        }
    }
}
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78