Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Marshmallow: Test permissions with Espresso?

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?

like image 477
argenkiwi Avatar asked Nov 26 '15 03:11

argenkiwi


People also ask

Does espresso support test recording?

Espresso Test Recorder then takes the saved recording and automatically generates a corresponding UI test that you can run to test your app. Espresso Test Recorder writes tests based on the Espresso Testing framework, an API in AndroidX Test.

How do I check if permission is denied android?

To know if the user denied with "never ask again" you can check again the shouldShowRequestPermissionRationale method in your onRequestPermissionsResult when the user did not grant the permission. You can open your app setting with this code: Intent intent = new Intent(Settings.

How do I request runtime permission on Android?

checkSelfPermission(String perm); It returns an integer value of PERMISSION_GRANTED or PERMISSION_DENIED. Note: If a user declines a permission that is critical in the app, then shouldShowRequestPermissionRationale(String permission); is used to describe the user the need for the permission.


Video Answer


1 Answers

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.

like image 81
Niklas Avatar answered Sep 20 '22 02:09

Niklas