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.''.