Skip to content

Instantly share code, notes, and snippets.

@blundell
Last active August 29, 2015 14:08
Show Gist options
  • Save blundell/ff2ac1d5ff0a41519c36 to your computer and use it in GitHub Desktop.
Save blundell/ff2ac1d5ff0a41519c36 to your computer and use it in GitHub Desktop.
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
Copy link
Author

I'm sure this test case should pass 😕

@Electryc
Copy link

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
Copy link
Author

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
Copy link
Author

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