7

I'm trying to automate the disabling of animations as described in this post, but that only seems to work for command-line invocation of connectedAndroidTest. I want to use the graphical test runner in Studio, with the list box showing passed/failed tests. With that runner, the permission grant (adb shell pm grant ... android.permission.SET_ANIMATION_SCALE) is never run, seemingly because the gradle task installDebugAndroidTest is never run, instead the runner is running Gradle as far as assembleDebugAndroidTest (or whatever alternate gradle task I specify in my run configuration), and then installing com.mypackage.test by some other (non-Gradle?) method immediately before running tests. So any prior permission grant is reset by that installation.

How can I grant SET_ANIMATION_SCALE between the graphical test runner's installation of the test package and the running of the test?

Chris Boyle
  • 11,423
  • 7
  • 48
  • 63
  • Did you find a solution to this? I have exactly the same issue, really annoying. Started looking into the artifactory build step in Android Studio, but that isnt present in Android Studio lets it is in normal IntelliJ. Thanks. – Thomas Vervik Nov 28 '15 at 14:39
  • I'd really like to know a solution for this as well. Seems like a pretty annoying oversight from the tools team to make this a pre-run-but-post-install requirement and not offer a hookin with IDE. – Zac Sweers Jan 13 '16 at 22:47
  • 1
    I opened a feature request: https://code.google.com/p/android/issues/detail?id=198813 – Zac Sweers Jan 13 '16 at 22:53
  • I hope that my solution in http://stackoverflow.com/questions/41382575/how-to-request-permissions-on-android-marshmallow-for-junit-tests/41565899#41565899 will help – pbelov Jan 10 '17 at 09:57

1 Answers1

1

You can do it using reflection, adding the permission to the manifest, creating an Espresso TestRule and a task (explained here in detail).

Add the permission to the manifest of a debug/mock variant:

<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>

Create your own task depending on installDebug and make connectedDebugAndroidTest depend on your task. You also need to grant the SET_ANIMATION_SCALE permission for testing.

Create a test rule that uses internally reflection to retrieve and restore animation scales (code):

public class AnimationAwareWonderTestRule extends AnimationAwareAwesomeTestRule {

    private float[] mAnimationScales;

    @Override
    protected void before() throws Throwable {
        mAnimationScales = AnimationAwareWonder.tryToRetrieveAndDisableAnimationsAndTransitions();
    }

    @Override
    protected void after() throws Throwable {
        AnimationAwareWonder.tryToRestoreAndEnableAnimationsAndTransitions(mAnimationScales);
    }
}

It works but seems it's not possible at the moment to use this permission in MarshMallow.

Community
  • 1
  • 1
albodelu
  • 7,931
  • 7
  • 41
  • 84