78

The new permissions scheme introduced by Android Marshmallow requires checking for specific permissions at runtime, which implies the need to provide different flows depending on whether the user denies or allows access.

As we use Espresso to run automated UI tests on our app, how can we mock or update the state of the permissions in order to test different scenarios?

2

13 Answers 13

119

With the new release of the Android Testing Support Library 1.0, there's a GrantPermissionRule that you can use in your tests to grant a permission before starting any tests.

@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);

Kotlin solution

@get:Rule var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION)

@get:Rule must be used in order to avoid java.lang.Exception: The @Rule 'permissionRule' must be public. More info here.

8
  • Works like a charm! Check this blog post for further info.
    – Andras K
    Commented Oct 12, 2017 at 14:03
  • 2
    I keep encountering the following error despite having declared the permission in the Manifest: 12-28 14:09:35.063 7193-7215/com.blaha.test E/GrantPermissionCallable: Permission: android.permission.SET_TIME cannot be granted!
    – Faux Pas
    Commented Dec 28, 2017 at 19:09
  • 3
    This should be accepted as correct answer because it uses appropriate framework and actually work comparing to other solutions. Commented Jan 11, 2018 at 23:43
  • 1
    Do not forget to add permission on Manifest, you could also create a new AndroidManifest.xml file in path: /src/debug
    – Kunami
    Commented Mar 6, 2018 at 11:15
  • 1
    Is there any way to do this on per-Method/Test level? Do I really need to split my tests for app setup class in two test classes, one for pre-permission-granted and one for post-permission-granted??
    – allofmex
    Commented Apr 28, 2020 at 7:28
54

The accepted answer doesn't actually test the permissions dialog; it just bypasses it. So, if the permissions dialog fails for some reason, your test will give a false green. I encourage actually clicking the "give permissions" button to test the whole app behaviour.

Have a look at this solution:

public static void allowPermissionsIfNeeded(String permissionNeeded) {
    try { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(permissionNeeded)) {
        sleep(PERMISSIONS_DIALOG_DELAY);
        UiDevice device = UiDevice.getInstance(getInstrumentation());
        UiObject allowPermissions = device.findObject(new UiSelector()
          .clickable(true) 
          .checkable(false) 
          .index(GRANT_BUTTON_INDEX));
        if (allowPermissions.exists()) {
          allowPermissions.click();
        } 
      } 
    } catch (UiObjectNotFoundException e) {
      System.out.println("There is no permissions dialog to interact with");
    } 
  } 

Find the whole class here: https://gist.github.com/rocboronat/65b1187a9fca9eabfebb5121d818a3c4

By the way, as this answer has been a popular one, we added PermissionGranter to Barista, our tool above Espresso and UiAutomator to make instrumental tests green: https://github.com/SchibstedSpain/Barista check it out, because we will maintain it release by release.

15
  • 1
    Works wonders! Also for testing with different locales, which is my case.
    – Sloy
    Commented Jun 8, 2016 at 10:37
  • 1
    Awesome! In most answers people don't take in care device's language.. So, well done and thanks!!
    – alxsimo
    Commented Jun 9, 2016 at 9:46
  • You can duplicate the code but change index to 0 if you want to test denying permission as well. Index 2 is the checkbox for never ask me again.
    – Ethan
    Commented Jul 14, 2016 at 19:56
  • 1
    I've just checked the Barista and I really recommend this library. In the latest version IDs are used instead of indices which are subject to change, also the code is in Kotlin now. Commented Nov 19, 2020 at 9:38
  • 1
    thanks @RocBoronat . With Barista this was solved with just one line! PermissionGranter.allowPermissionsIfNeeded(PERMISSION_1_CONTACTS) Commented Feb 1, 2023 at 13:49
30

Give a try with such static method when your phone is on English locale:

private static void allowPermissionsIfNeeded() {
    if (Build.VERSION.SDK_INT >= 23) {
        UiDevice device = UiDevice.getInstance(getInstrumentation());
        UiObject allowPermissions = device.findObject(new UiSelector().text("Allow"));
        if (allowPermissions.exists()) {
            try {
                allowPermissions.click();
            } catch (UiObjectNotFoundException e) {
                Timber.e(e, "There is no permissions dialog to interact with ");
            }
        }
    }
}

I found it here

5
  • 3
    Note that this won't work with a non EN locale or if the button text changes in the future. It might even fail with some devices if the manufacturers customise the dialog. Commented Aug 3, 2016 at 11:09
  • 6
    Strangely this not work on LG Nexus 5 with Android 6. the text "ALLOW" is not found. Using following it works: new UiSelector().clickable(true).checkable(false).index(1)
    – David
    Commented Sep 22, 2016 at 11:26
  • 1
    How do you do this for espresso tests?
    – Bhargav
    Commented Oct 21, 2016 at 6:32
  • @Bharga there is no way to do it with Espresso. But you can use both UIAutomation and Espresso together.
    – Thanh Le
    Commented Jan 12, 2017 at 8:12
  • If you want to deal with localization (or even possible string changes due to manufacturers), the string resource id they're probably using is android.R.string.allow, found here: raw.githubusercontent.com/android/platform_frameworks_base/…
    – David Liu
    Commented Jun 12, 2017 at 19:07
21

You can grant permissions before the test is run with something like:

@Before
public void grantPhonePermission() {
    // In M+, trying to call a number will trigger a runtime dialog. Make sure
    // the permission is granted before running this test.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().getUiAutomation().executeShellCommand(
                "pm grant " + getTargetContext().getPackageName()
                        + " android.permission.CALL_PHONE");
    }
}

But you can't revoke. If you try pm reset-permissions or pm revoke... the process is killed.

5
  • 2
    I found similar code in google test sample code. It works for the call phone sample app in that package, but it did not work if the permissions are asked as soon as the app starts, such as the camera permission. Anybody has any idea why?
    – fangmobile
    Commented Oct 24, 2016 at 23:51
  • You could try @BeforeClass. Commented Oct 30, 2016 at 11:56
  • @fangmobile.com perhaps because the activity is also started in an @ Before rule? Commented Nov 29, 2016 at 18:14
  • 1
    You should be able to revoke permissions using the same method so long as you do it with the @org.junit.AfterClass or @org.junit.BeforeClass annotations Commented Apr 28, 2017 at 16:32
  • Sadly this does not work very well. It works for permission checks itself, so WRITE_EXTERNAL_STORAGE is reported as granted afterwards. But permission is not use-able, /sdcard is still not writeable. It would require a app restart after pm grand to work, which is impossible within a test..
    – allofmex
    Commented Apr 28, 2020 at 7:33
11

Actually there are 2 ways of doing this I know so far:

  1. Grant the permission using adb command before test starts (documentation):

adb shell pm grant "com.your.package" android.permission.your_permission

  1. You can click on permission dialog and set the permission using UIAutomator (documentation). If your tests are written with Espresso for android you can combine Espresso and UIAutomator steps in one test easily.
5
  • I don't see how we could use the adb shell command along with our test suite. And about clicking the dialog, Espresso should be able to handle that itself. The problem is, after you run the test and the permission is enabled, the next time you run the test it will fail because the setting is persisted and the dialog won't show up again.
    – argenkiwi
    Commented Nov 26, 2015 at 21:36
  • 1
    Espresso can't handle interactions with the permission dialog since dialog is an instance of other application - com.android.packageinstaller.
    – denys
    Commented Nov 26, 2015 at 22:28
  • Have you tried to accept the popup with UIAutomator? For me it doesn't seem to find the "ALLOW" button. Any ideas how to solve this?
    – conca
    Commented Dec 18, 2015 at 22:53
  • 1
    @conca you have to look for "Allow" text, I guess, not "ALLOW". The system makes string upper case at runtime but actually it can be saved as "Allow".
    – denys
    Commented Jan 4, 2016 at 15:16
  • @denys, on Nexus 6 it works using ALLOW, but on LG Nexus 5 this won't work, this type of disparities are really anoying.
    – David
    Commented Sep 22, 2016 at 11:32
7

You can achieve this easily by granting permission before starting the test. For example if you are supposed to use camera during the test run, you can grant permission as follows

@Before
public void grantPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        getInstrumentation().getUiAutomation().executeShellCommand(
                "pm grant " + getTargetContext().getPackageName()
                        + " android.permission.CAMERA");
    }
}
1
  • This works for me! I had to look in the manifest to find out exactly which permissions to grant using this technique. Commented Mar 21, 2017 at 23:50
4

ESPRESSO UPDATE

This single line of code grants every permission listed as parameter in the grant method with immediate effect. In other words, the app will be treated like if the permissions were already granted - no more dialogs

@Rule @JvmField
val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION)

and gradle

dependencies {
  ...
  testImplementation "junit:junit:4.12"
  androidTestImplementation "com.android.support.test:runner:1.0.0"
  androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.0"
  ...
}

reference: https://www.kotlindevelopment.com/runtime-permissions-espresso-done-right/

2
  • Is there a similar method to deny a permission in order to test that case during a test? Commented Apr 15, 2018 at 8:50
  • Not mentioning it at GrantPermissionRule.grant() makes this Commented Jun 23, 2021 at 11:59
2

If you need to set a permission for a single test or during runtime rather than a rule you can use this:

PermissionRequester().apply {
    addPermissions(android.Manifest.permission.RECORD_AUDIO)
    requestPermissions()
}

e.g.

@Test
fun openWithGrantedPermission_NavigatesHome() {
    launchFragmentInContainer<PermissionsFragment>().onFragment {
        setViewNavController(it.requireView(), mockNavController)
        PermissionRequester().apply {
            addPermissions(android.Manifest.permission.RECORD_AUDIO)
            requestPermissions()
        }
    }

    verify {
        mockNavController.navigate(R.id.action_permissionsFragment_to_homeFragment)
    }
}
1
  • Is this still working? At least for WRITE_EXTERNAL_STORAGE it seems not, since this usually need the app to be restarted to apply!?
    – allofmex
    Commented Apr 28, 2020 at 7:17
1

There is GrantPermissionRule in Android Testing Support Library, that you can use in your tests to grant a permission before starting any tests.

@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION);
1

Thank you @niklas for the solution. In case anyone looking to grant multiple permissions in Java:

 @Rule
public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.CAMERA);
0

I know an answer has been accepted, however, instead of the if statement that has been suggested over and over again, another more elegant approach would be to do the following in the actual test you want for a specific version of OS:

@Test
fun yourTestFunction() {
    Assume.assumeTrue(Build.VERSION.SDK_INT >= 23)
    // the remaining assertions...
}

If the assumeTrue function is called with an expression evaluating to false, the test will halt and be ignored, which I am assuming is what you want in case the test is being executed on a device pre SDK 23.

0

I've implemented a solution which leverages wrapper classes, overriding and build variant configuration. The solution is quite long to explain and is found over here: https://github.com/ahasbini/AndroidTestMockPermissionUtils.

It is not yet packed in an sdk but the main idea is to override the functionalities of ContextWrapper.checkSelfPermission and ActivityCompat.requestPermissions to be manipulated and return mocked results tricking the app into the different scenarios to be tested like: permission was denied hence the app requested it and ended with granted permission. This scenario will occur even if the app had the permission all along but the idea is that it was tricked by the mocked results from the overriding implementation.

Furthermore the implementation has a TestRule called PermissionRule class which can be used in the test classes to easily simulate all of the conditions to test the permissions seamlessly. Also assertions can be made like ensuring the app has called requestPermissions() for example.

0

For allowing the permission, when necessary, I think the easiest way is to use Barista's PermissionGranter.allowPermissionsIfNeeded(Manifest.permission.GET_ACCOUNTS) directly in the test which requires this permission.

Not the answer you're looking for? Browse other questions tagged or ask your own question.