Chris Boyle
Chris Boyle

Reputation: 11551

Android Studio: grant permission between installing test APK and running tests with graphical test runner

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?

Upvotes: 7

Views: 1452

Answers (1)

albodelu
albodelu

Reputation: 7971

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.

Upvotes: 1

Related Questions