gpt4 book ai didi

AndroidTestCase assertActivityRequiresPermission() 断言不正确

转载 作者:行者123 更新时间:2023-11-28 20:30:54 25 4
gpt4 key购买 nike

https://gist.github.com/blundell/ff2ac1d5ff0a41519c36 http://developer.android.com/reference/android/test/AndroidTestCase.html#assertActivityRequiresPermission(java.lang.String, java.lang.String, java.lang.String)

我已经为 Activities 的安全权限编写了一个测试,但是它并没有达到我的预期。

我在 list 中声明我的 Activity 需要许可。我运行断言此权限的测试。

这是一个应该通过但没有通过的示例测试:

安卓 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blundell.myapplication">

<application>
<activity
android:name=".SecondActivity"
android:permission="perm.foo.bar" />
</application>

</manifest>

我的测试用例:

package com.blundell.myapplication;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {

private static final String PACKAGE = "com.blundell.myapplication";

public ApplicationTest() {
super(Application.class);
}

public void testSecondActivityRequiresFooBarPermission() throws Exception {
assertActivityRequiresPermission(PACKAGE, PACKAGE + ".SecondActivity", "perm.foo.bar");
}
}

stub Activity :

package com.blundell.myapplication;

import android.app.Activity;

public class SecondActivity extends Activity {
}

测试失败时的堆栈跟踪:

junit.framework.AssertionFailedError: expected security exception for perm.foo.bar
at android.test.AndroidTestCase.assertActivityRequiresPermission(AndroidTestCase.java:99)
at com.blundell.myapplication.ApplicationTest.testSecondActivityRequiresFooBarPermission(ApplicationTest.java:15)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

编辑:

我发现在 gradle 中你可以添加一个 AndroidManifest 到你的测试中并且它会被合并:@Manish Mulimani 我添加了检测标签,但这并没有使测试通过。 (添加一个 IntentFilter & 它确实如此)。

/src/androidTest/AndroidManifext.xml 中:

   <instrumentation
android:label="new label to prove tools:replace works"
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.blundell.myapplication.test"
tools:replace="targetPackage, label" />

最佳答案

测试应用和被测应用运行在同一个进程中。因此测试用例通过了,因为测试应用程序不需要请求启动 Activity 的权限。

enter image description here

要测试权限,测试应用程序应该在不同的进程中运行。这可以通过将测试包名称分配给 android:targetPackage 来完成在测试应用程序 list 的 instrumentation 元素中。

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.blundell.myapplication.test"
xmlns:android="http://schemas.android.com/apk/res/android">
<application android:debuggable="true">
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:label="Tests for com.blundell.myapplication.test" android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.blundell.myapplication.test" android:handleProfiling="false" android:functionalTest="false" />
</manifest>

这是 manifest旧 android 包 com.android.globalsearch 的文件,以相同的方式使用它。这是该 list 文件中留下的评论:

We are self instrumenting so the test can be run, but since this is for permissions test, we are not instrumenting any app (we want to be accessing it from the outside).

注意:如果您使用的是gradle,则需要手动生成测试包,安装并测试。原因来自docs :

The value of the targetPackage attribute of the instrumentation node in the test application manifest is automatically filled with the package name of the tested app, even if it is customized through the defaultConfig and/or the Build Type objects. This is one of the reason the manifest is generated automatically.

我按照这些步骤手动构建和执行测试包。

  1. 在 app/build.gradle 中:将 applicationId 设置为“com.blundell.myapplication.test”
  2. 构建测试包:./gradlew assembleDebugTest
  3. 安装测试包:adb install -r ./app/build/outputs/apk/app-debug-test-unaligned.apk
  4. 运行测试:adb shell am instrument -w com.blundell.myapplication.test/android.test.InstrumentationTestRunner

这是 logcat 输出:

10-28 21:06:46.749 32649 32662 I TestRunner: started: testActivityPermission(com.blundell.myapplication.ApplicationTest)
10-28 21:06:46.754 983 993 I ActivityManager: START u0 {flg=0x10000000 cmp=com.blundell.myapplication/.SecondActivity} from pid 32649
10-28 21:06:46.754 983 993 W ActivityManager: Permission Denial: starting Intent { flg=0x10000000 cmp=com.blundell.myapplication/.SecondActivity } from ProcessRecord{427f4f78 32649:com.blundell.myapplication.test/u0a10111} (pid=32649, uid=10111) requires perm.foo.bar
10-28 21:06:46.755 32649 32662 I TestRunner: finished: testActivityPermission(com.blundell.myapplication.ApplicationTest)
10-28 21:06:46.755 32649 32662 I TestRunner: passed: testActivityPermission(com.blundell.myapplication.ApplicationTest)

关于AndroidTestCase assertActivityRequiresPermission() 断言不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549915/

25 4 0
文章推荐: Javascript 函数不工作的问题
文章推荐: ios - 在 iOS 中处理对用户的付款
文章推荐: php - 如何在表单提交时将带有