My android app uses the ACCESS_NOTIFICATION_POLICY permission which is a “special permission” checked through NotificationManager.isNotificationPolicyAccessGranted
. I can’t find a way to grant this permission programmatically for a UI test. I have tried
uiAutomation.grantRuntimePermission(
instrumentation.context.packageName,
Manifest.permission.ACCESS_NOTIFICATION_POLICY)
but this throws
java.lang.SecurityException: Error granting runtime permission
at android.app.UiAutomation.grantRuntimePermissionAsUser(UiAutomation.java:1574)
at android.app.UiAutomation.grantRuntimePermission(UiAutomation.java:1540)
...
Caused by: java.lang.SecurityException: Permission android.permission.ACCESS_NOTIFICATION_POLICY requested by package my.package.name is not a changeable permission type
I also found docs for one RuntimePermissionsStubber which looks like it might solve my problem, but the class doesn’t seem to actually exist in the espresso-intents package (using version 3.6.1).
4
I was able to navigate the Settings app to allow the permission using UI Automator. It seems like there ought to be an easier solution but oh well. Assuming your app does
startActivity(Intent(ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS))
then in the UI test you can allow the permission with the following:
val instrumentation = InstrumentationRegistry.getInstrumentation()
val device = UiDevice.getInstance(instrumentation)
device.wait(Until.findObject(By.text("Do Not Disturb access")), 3000)
device.findObject(By.text("Your App Name")).click()
device.wait(Until.findObject(By.text("Allow Do Not Disturb")), 3000).click()
device.wait(Until.findObject(By.text("Allow")), 3000).click()
device.wait(Until.findObject(By.text("Allow Do Not Disturb")), 3000)
device.pressBack()
device.wait(Until.gone(By.text("Allow Do Not Disturb")), 3000)
device.pressBack()