Android cancels permission at the beginning of each test

I use Espresso and UIAutomator to write my test cases. I check permissions on external storage when it denies and when it is allowed. I have different test cases that all require cancellation permission at the beginning of the test case. However, some of the test cases should and should result in permission being granted, so I need to revoke the permission on the next test. I searched, and the closest I came across is to use the pm manager to execute adb shell commands to revoke this permission. However, having done this, I will get the following error: tool failure due to a "process failure". Is there a way for permissions to be revoked at the beginning of each test case? If not, how do I resolve this issue regarding testing permissions? Thanks for your help in advance!

This is a piece of code that I currently need to cancel to resolve before each test case (which does not work):

@Before public void setUp() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getInstrumentation().getUiAutomation().executeShellCommand( "pm revoke " + getTargetContext().getPackageName() + " android.permission.WRITE_EXTERNAL_STORAGE"); } } 

Corresponding error message when trying to revoke a permission using a code snippet:

 Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''. 

I also came across these two posts: this and this .

+7
android android-permissions android-uiautomator android-testing android-espresso
source share
3 answers

I am not sure how to do this at runtime without a user request. However, you indicated in the header that it is intended for testing. Therefore, I have some options that I can offer you

a) When testing needs to be performed without permissions, simply cancel it manually using your device’s settings.

b) You can check and ask for permission, and then refuse to give permission. I would give you the code that I use to check and request camera permission. You just need to change the permission name and possibly the condition to check:

 public static boolean checkCameraPermission(MainActivity thisActivity) { return ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED; } public static void checkAndAskCameraPermission(final MainActivity thisActivity) { if (!checkCameraPermission(thisActivity)) { //No right is granted // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.CAMERA)) { //Open a dialog explaining why you are asking permission then when when positive button is triggered, call this line ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.CAMERA}, CHECK_FOR_CAMERA_PERMISSION); } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.CAMERA}, CHECK_FOR_CAMERA_PERMISSION); } } } 

I could give you some more links about b) (how to ask permission) if you need it (just comment on this). However, since I am not sure that this answer would interest you, I will not waste time documenting this option.

But then, maybe someone can explain to you how to do this directly at runtime without a hint, but I, although it may be interesting to provide you with at least this information.

+2
source share

You can not. Grant / revocation permissions are under user control.

However, you can grant / revoke permissions via adb, and you can run adb commands programmatically, but I have no idea if this works in combination? ...

 adb shell pm grant [app.package] [permission] adb shell pm grant com.app.package.path android.permission.READ_CONTACTS 

( see here )

Can you match your permissions with a boolean for testing purposes?

+2
source share

I implemented a solution that uses wrapper classes, overriding and configuring assembly options. The solution has been explained for quite some time and is located here: https://github.com/ahasbini/AndroidTestMockPermissionUtils .

It is not yet packaged in sdk, but the main idea is to override the functionality of ContextWrapper.checkSelfPermission and ActivityCompat.requestPermissions to be manipulated and return the mocked results by tricking the application into various scripts that need to be tested, for example: permission was denied therefore, the application requested it and ended with the permission granted. This scenario will happen even if the application has permission all the time, but the idea is that it was tricked by the mocked results from an overridden implementation.

In addition, there is a TestRule in the implementation, called the PermissionRule class, which can be used in test classes to easily simulate all conditions for checking permissions without problems. Claims can also be made, for example, for the application to call requestPermissions (), for example.

0
source share

All Articles