How to disable animations in code when running Esp

2019-02-05 06:19发布

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

3条回答
欢心
2楼-- · 2019-02-05 06:31

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-05 06:46

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.

查看更多
Luminary・发光体
4楼-- · 2019-02-05 06:53

Well, this is a workaround I came into... my app only has one animation which only stops a few tests.

So I prefered to set a SharedPreference, PREFS_ANIMATIONS_DISABLED_FOR_TESTING...

I set it true before my method starts and to false before ends.

@Test
public void startingAnimationTest() {
     mSettings.edit().putBoolean("PREFS_ANIMATIONS_DISABLED_FOR_TESTING", true).apply();

    //Actual testing

    mSettings.edit().putBoolean("PREFS_ANIMATIONS_DISABLED_FOR_TESTING", false).apply();
}

I simply ask for it before starting the animation.

And that's it, surely not the best solution because I know tests shouldn't affect real code but it's been enough for me.

查看更多
登录 后发表回答