Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 951
Chris Boyle Avatar asked Sep 26 '15 15:09

Chris Boyle


People also ask

What is Android test runner?

The test runner handles loading your test package and the app under test to a device, running your tests, and reporting test results. This test runner supports several common testing tasks, including the following: Writing JUnit tests. Accessing the app's context. Filtering tests.

Why do you use the Android JUnit runner when running UI test?

It will determine which class runner is set with @RunWith , initialize it, and use it to run the tests in that class. In Android's case, the AndroidJUnitRunner explicitly checks if the AndroidJUnit4 class runner is set to allow passing configuration parameters to it.

What is Robolectric?

Robolectric is a framework that allows you to write unit tests and run them on a desktop JVM while still using Android API. Robolectric provides a JVM compliant version of the android. jar file.


1 Answers

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.

like image 126
albodelu Avatar answered Sep 17 '22 13:09

albodelu