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)
我已经为活动的安全权限编写了一个测试,但它并没有按照我的预期进行。
我声明我在清单中的活动需要一个权限。 我运行了为此权限声明的测试。
这是一个应该通过的示例测试,但不是:
AndroidManifest:
<?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");
}
}
存根活动:
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&amp;它确实如此)。
即。
/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" />
答案 0 :(得分:2)
测试应用程序和测试中的应用程序在同一个过程中运行。因此测试用例通过,因为测试应用程序不需要请求启动活动的权限。
要测试权限,测试应用程序应在不同的进程中运行。这可以通过将测试包名称分配给测试应用程序清单的instrumentation
元素中的android:targetPackage来实现。
<?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>
这是旧的android包com.android.globalsearch
的{{3}}文件,使用它的方式相同。这是清单文件中留下的评论:
我们自我测量所以可以运行测试,但是因为这样 对于权限测试,我们没有检测任何应用程序(我们希望成为 从外面访问它。)
注意:如果您使用的是gradle,则需要手动生成测试包,安装并测试它。来自manifest的原因:
检测节点的targetPackage属性的值 在测试应用程序清单中自动填充 被测试的应用程序的包名称,即使它是通过自定义的 defaultConfig和/或Build Type对象。这是其中一个原因 清单是自动生成的。
我按照这些步骤手动构建并执行测试包。
./gradlew assembleDebugTest
adb install -r ./app/build/outputs/apk/app-debug-test-unaligned.apk
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)