Unit Testing Permissions

3,192 views
Skip to first unread message

TFBrian

unread,
Jan 7, 2013, 12:15:44 PM1/7/13
to robol...@googlegroups.com
I am currently writing an Android SDK using robolectric for testing.  The first issue I've come across relates to unit testing different permission scenarios.  This library will provide different functionality based on the Android Permissions which are provided by the application which has integrated it.  I would like to be able to write unit tests like the following:

Example permission dependent code:

public class DeviceInfo {
    public static String getTelephonyID(final Context context) {
        if(context.checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            return manager.getDeviceId();
        }

        return "";
    }
}


Example Tests I would like to write:

@RunWith(RobolectricTestRunner.class)
public class DeviceInfoTest {
    private Activity mActivity;

    @Before
    public void setup_DeviceInfoTest() {
        mActivity = new Activity();
    }

    @Test
    public void test_get_carrier_when_permission_supplied() {
        enablePermissionSomehow(Manifest.permission.READ_PHONE_STATE);
        String carrier = DeviceInfo.getCarrier(mActivity);
        Assert.assertEquals(carrier, expectedCarrier);
    }
   
    @Test
    public void test_get_carrier_when_permission_denied() {
        disablePermissionSomehow(Manifest.permission.READ_PHONE_STATE);
        String carrier = DeviceInfo.getCarrier(mActivity);
        Assert.assertEquals(carrier, "");
    }
}


The specifics of enablePermissionSomehow and disablePermissionSomehow are what I'm interested in.

I appreciate any help.

TFBrian

unread,
Jan 7, 2013, 12:28:53 PM1/7/13
to robol...@googlegroups.com
Sorry my example is a bit off.  The tests should be calling DeviceInfo.getTelephonyID() and not DeviceInfo.getCarrier()... but you get the point.

TFBrian

unread,
Jan 12, 2013, 4:45:39 AM1/12/13
to robol...@googlegroups.com
Anyone looking to do this in the future you can grant permissions with an Application reference like so:

    public static void grantPermission(final Application app, final String permission) {
        ShadowApplication shadowApp = Robolectric.shadowOf(app);
        shadowApp.grantPermissions(permission);
    }

and in order to test if a permission is enabled you should be using this passing the Application's context:

private static boolean isPermissionGranted(final Context context, final String permission) {
        return (context.checkPermission(permission, android.os.Process.myPid(), android.os.Process.myUid()) == PackageManager.PERMISSION_GRANTED);

Christian Williams

unread,
Jan 13, 2013, 12:50:33 AM1/13/13
to robol...@googlegroups.com
Thanks Brian!

--X

Wadi Jalil Maluf

unread,
Feb 28, 2013, 10:39:02 AM2/28/13
to robol...@googlegroups.com
Brian, this doesn't seems to work anymore(ShadowApplication doesn't have a grantPermission method), do you know any other way to do it?
Thanks in advance,
Best regards,
Wadi

Wadi Jalil Maluf

unread,
Mar 5, 2013, 9:55:15 AM3/5/13
to robol...@googlegroups.com
I was using an old version(0.9.5). I'm using version 1.2.
Thanks all for the comments,
Wadi
Reply all
Reply to author
Forward
0 new messages