18

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

5 Answers 5

27

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.

Sign up to request clarification or add additional context in comments.

3 Comments

There is an extra character ` ` at the end of the first adb command. I tried to remove that but edit requires minimum of 6 characters.
I removed that non-printable character, so copy-paste should work correctly now.
Link is no longer valid
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.

6 Comments

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?
@MattKranzler Are you using a real device? It won't work on non-rooted device.
@Yenchi I tried with an emulator and still have the exception
@Yenchi only with marshmallow emulator.
@MattKranzler make sure you have <uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/> in AndroidManifest.xml of the target app (you can add it only for debug/etc)
|
6

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.

4 Comments

Don't copy and paste answers that does not works. This does not disable animations.
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.
this option disables activity transition animation only, not others animations in your code.
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.
4

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}")
        }
    }
}

Comments

0
  • The testOptions animationsDisabled property would be the simplest approach but it "does not affect tests that you run using Android Studio" and it doesn't seem to work in command line tests either, or maybe it's flakey.

  • The shell settings commands adb shell settings put global window_animation_scale 0.0 etc. work but you must remember to set them on each new AVD. It's easy to forget when creating new AVDs for new OS releases and screen formats, even if you use a shell script. Also, you'd have to restore them to try out a splash screen animation.

  • The most reliable approach is a JUnit TestRule. On API 33+ it can call UiAutomation#setAnimationScale(). On API 21+ it can call UiAutomation#executeShellCommand().


    @NonNull
    @Override
    public Statement apply(@NonNull Statement base, @NonNull Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                setAnimationScale("0.0");
                try {
                    base.evaluate();
                } finally {
                    setAnimationScale("1.0");
                }
            }

            private void setAnimationScale(@NonNull String value) {
                final UiAutomation uiAutomation =
                        InstrumentationRegistry.getInstrumentation().getUiAutomation();

                // TODO: On API 33+ this could call uiAutomation.setAnimationScale(float).
                setGlobalSetting(uiAutomation, "window_animation_scale", value);
                setGlobalSetting(uiAutomation, "transition_animation_scale", value);
                setGlobalSetting(uiAutomation, "animator_duration_scale", value);
            }

            private void setGlobalSetting(@NonNull UiAutomation uiAutomation,
                                          @NonNull String key, @NonNull String value) {
                try {
                    uiAutomation.executeShellCommand(
                        "settings put global " + key + " " + value).close();
                } catch (IOException | RuntimeException e) {
                    Log.w(TAG, "Failed to set UI " + key + " = " + value, e);
                }
            }
        };
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.