我目前正在为某个应用编写一些测试。如果该应用没有存储权限,则会弹出一个片段。我知道如何使用“授予权限规则”授予权限,但是有没有办法撤销权限?
答案 0 :(得分:0)
否,那是不可能的。来自https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule的引用:
一旦授予权限,它将适用于当前Instrumentation中运行的所有测试。授予权限后,无法撤消该权限。尝试这样做会导致检测过程崩溃。
我建议将权限方法包装到某些服务中,并模拟值检查是否授予了权限
答案 1 :(得分:-1)
是的,在测试过程中可能的... Feuby在此SO 2017问题中对同一问题给出了正确答案
Android revoke permission at start of each test
重复他的代码
公共静态布尔checkCameraPermission(MainActivity thisActivity){ 返回ContextCompat.checkSelfPermission(thisActivity, 清单权限(CAMERA) == PackageManager.PERMISSION_GRANTED; }
公共静态无效checkAndAskCameraPermission(最终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);
}
}
}