Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android revoke permission at start of each test

I'm using Espresso and UIAutomator to write my test cases. I'm testing external storage permissions when it's denied and when it's allowed. I have different test cases which all require the permission to be revoked at the start of the test case. However, some of the test cases should and do result in the permission being granted, so I need to revoke the permission when the next test is being executed. I have searched around and the closest thing I came across is using the pm manager to execute adb shell commands to revoke the permission. However by doing so, I'll get the following error, Instrumentation run failed due to 'process crash'. Is there any way I can ensure permissions are revoked at the beginning of every test case? If not, how can this issue be resolved regarding testing permissions? Thank you for your help in advance!

This is the code snippet I currently have to revoke permission before each test case (which doesn't 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 permission with above code snippet:

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

I have also come across these two posts: this and this.

like image 958
Charles Li Avatar asked Apr 18 '17 01:04

Charles Li


People also ask

What is auto revoke?

Android 11 Developer Preview 3 has a new setting for app permissions, allowing them to be automatically revoked if you don't use the app for long enough. Disabled by default, the descriptively-named "Auto revoke permissions" setting will revoke permissions for a given app if it isn't used "for a few months."

How do I turn on auto accept permissions on Android?

You must ask the user to grant access to the dangerous permissions at runtime - when you do ask your user to give access to those permissions on run time he will see system dialog that asks him to accept or decline the permissions.

What does remove permission if app is unused mean?

Android 11 (and newer) can automatically remove permissions from “unused apps” to limit access to sensitive personal data, including location, camera, contacts, files, microphone, and phone.


1 Answers

You should not revoke any permission before the tests start. This will restart the entire test app process and the test will be marked as failure.

Instead you can revoke the permission after test execution is completed i.e. in @After method as follows:

@After
fun tearDown(){
    InstrumentationRegistry.getInstrumentation().uiAutomation.
            executeShellCommand("pm revoke ${getTargetContext().packageName} android.permission.WRITE_EXTERNAL_STORAGE")
}

Alternatively, you can use Android Test Orchestrator version 1.0.2 and set

 testInstrumentationRunnerArguments clearPackageData: 'true'

this will clear the package data after each test.

Note: Android Test Orchestrator v1.0.2 has issues when it comes to managing code coverage reports via Jacoco.

like image 100
Sagar Avatar answered Oct 21 '22 10:10

Sagar