[英]Android Espresso java.lang.SecurityException: Permission android.permission.SYSTEM_ALERT_WINDOW is not a changeable permission type
I have an Espresso script that needs the SYSTEM_ALERT_WINDOW
permission. 我有一个需要
SYSTEM_ALERT_WINDOW
权限的Espresso脚本。 I use the following code to grant the permission for all test scripts: 我使用以下代码为所有测试脚本授予权限:
@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
And I have a special AndroidManifest for test cases that includes the permissions. 我有一个特殊的AndroidManifest用于测试用例,其中包括权限。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
This works fine on most devices from different brands and Android versions varying from Android 5.X up to Android 8.x (about 15+ devices). 这在不同品牌和从Android 5.X到Android 8.x的Android版本(大约15台以上的设备)的大多数设备上都能正常运行。 However on my Huawei P8 Lite with Android 6.0 I get the following error:
但是,在装有Android 6.0的Huawei P8 Lite上,出现以下错误:
junit.framework.AssertionFailedError: Failed to grant permissions, see logcat for details
at junit.framework.Assert.fail(Assert.java:50)
at android.support.test.runner.permission.PermissionRequester.requestPermissions(PermissionRequester.java:110)
at android.support.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:128)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1890)
When I check the logcat the following additional errors are shown: 当我检查logcat时,还会显示以下其他错误:
I/GrantPermissionCallable: Permission: android.permission.WRITE_EXTERNAL_STORAGE is already granted!
I/GrantPermissionCallable: Permission: android.permission.CAMERA is already granted!
I/UiAutomationShellCmd: Requesting permission: pm grant [PACKAGE_NAME_MY_APP] android.permission.SYSTEM_ALERT_WINDOW
E/GrantPermissionCallable: Permission: android.permission.SYSTEM_ALERT_WINDOW cannot be granted!
java.lang.SecurityException: Permission android.permission.SYSTEM_ALERT_WINDOW is not a changeable permission type
at com.android.server.pm.PackageManagerService.enforceDeclaredAsUsedAndRuntimeOrDevelopmentPermission(PackageManagerService.java:3665)
at com.android.server.pm.PackageManagerService.grantRuntimePermission(PackageManagerService.java:3698)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:398)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:2807)
at com.android.server.pm.HwPackageManagerService.onTransact(HwPackageManagerService.java:506)
at android.os.Binder.execTransact(Binder.java:453)
I also tried changing the 'display over other apps' permission manually on the device for the test-app and the to-be-tested-app. 我还尝试了在测试应用程序和要测试的应用程序的设备上手动更改“在其他应用程序上显示”权限。 But this also didn't change anything.
但这也没有任何改变。
When I remove the SYSTEM_ALERT_WINDOW permission from the GrantPermissionRule all other tests run fine. 当我从GrantPermissionRule中删除SYSTEM_ALERT_WINDOW权限时,所有其他测试运行正常。 I don't see why this would fail on some devices.
我不明白为什么这在某些设备上会失败。
I used the following websites for research but none of them were useful: 我使用以下网站进行研究,但没有一个有用的网站:
I did the following to solve this issue. 我做了以下事情来解决这个问题。 First I added the following line to the Espresso AndroidManifest
首先,我向Espresso AndroidManifest添加了以下行
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Instead of adding the permission rule to all my testcases I added it to the specific test case. 我没有将权限规则添加到我的所有测试用例中,而是将其添加到特定的测试用例中。
@Rule public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW);
And I ignore the test if the device cannot draw overlays. 如果设备无法绘制叠加层,我将忽略测试。
@Before
public void shouldPerformTest()
{
boolean shouldPerformTest = true;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
shouldPerformTest = Settings.canDrawOverlays(getContext());
}
Assume.assumeTrue(shouldPerformTest);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.