Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
assertActivityRequiresPermission() Why doesn't this test pass?
<?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");
}
}
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)
package com.blundell.myapplication;
import android.app.Activity;
public class SecondActivity extends Activity {
}
@blundell

This comment has been minimized.

Copy link
Owner Author

@blundell blundell commented Oct 24, 2014

I'm sure this test case should pass 😕

@Electryc

This comment has been minimized.

Copy link

@Electryc Electryc commented Oct 24, 2014

What if you move your test in another package? Not to be in the same one with the activity. From the implementation (https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/test/AndroidTestCase.java#91) I see it uses getContext() to launch the activity. Maybe the context has already the permission and it doesn't throw 😕

@blundell

This comment has been minimized.

Copy link
Owner Author

@blundell blundell commented Oct 24, 2014

Hmm it does run under another application (android instrumentation) so it's install package is com.blundell.myapplication.test but I changed the java package anyway to give it a try and that didn't work

@blundell

This comment has been minimized.

Copy link
Owner Author

@blundell blundell commented Oct 24, 2014

I also changed the testApplicationId = "com.foo" as a sanity check with no change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment