我有一个Espresso脚本,需要SYSTEM_ALERT_WINDOW
权限。我使用以下代码为所有测试脚本授予权限:
@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
我有一个特殊的AndroidManifest用于测试用例,其中包括权限。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
这在不同品牌和从Android 5.X到Android 8.x的Android版本(大约15台以上的设备)上的大多数设备上都可以正常使用。但是,在装有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)
当我检查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)
我还尝试在设备上手动更改测试应用程序和待测试应用程序的“在其他应用程序上显示”权限。但这也没有任何改变。
当我从GrantPermissionRule中删除SYSTEM_ALERT_WINDOW权限时,所有其他测试运行正常。 我不明白为什么这在某些设备上会失败。
我使用以下网站进行研究,但没有一个有用的
答案 0 :(得分:0)
我做了以下事情来解决这个问题。首先,我将以下行添加到Espresso AndroidManifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
我没有将权限规则添加到我所有的测试用例中,而是将其添加到了特定的测试用例中。
@Rule public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW);
如果设备无法绘制叠加层,我将忽略测试。
@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);
}