FileDocCategorySizeDatePackage
ApplicationRestrictionsTest.javaAPI DocAndroid 5.1 API5950Thu Mar 12 22:22:42 GMT 2015com.android.server.devicepolicy

ApplicationRestrictionsTest

public class ApplicationRestrictionsTest extends android.test.AndroidTestCase
Tests for application restrictions persisting via profile owner: make -j FrameworksServicesTests runtest --path frameworks/base/services/tests/servicestests/ \ src/com/android/server/devicepolicy/ApplicationRestrictionsTest.java

Fields Summary
static android.app.admin.DevicePolicyManager
sDpm
static android.content.ComponentName
sAdminReceiver
private static final String
RESTRICTED_APP
static boolean
sAddBack
Constructors Summary
Methods Summary
public voidsetUp()

        final Context context = getContext();
        sAdminReceiver = new ComponentName(mContext.getPackageName(),
                AdminReceiver.class.getName());
        sDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        Settings.Secure.putInt(context.getContentResolver(),
                Settings.Secure.USER_SETUP_COMPLETE, 0);
        sDpm.setProfileOwner(context.getPackageName(), "Test", UserHandle.myUserId());
        Settings.Secure.putInt(context.getContentResolver(),
                Settings.Secure.USER_SETUP_COMPLETE, 1);
        // Remove the admin if already registered. It's async, so add it back
        // when the admin gets a broadcast. Otherwise add it back right away.
        if (sDpm.isAdminActive(sAdminReceiver)) {
            sAddBack = true;
            sDpm.removeActiveAdmin(sAdminReceiver);
        } else {
            sDpm.setActiveAdmin(sAdminReceiver, false);
        }
    
public voidtearDown()

        Settings.Secure.putInt(getContext().getContentResolver(),
                Settings.Secure.USER_SETUP_COMPLETE, 0);
        sDpm.removeActiveAdmin(sAdminReceiver);
        Settings.Secure.putInt(getContext().getContentResolver(),
                Settings.Secure.USER_SETUP_COMPLETE, 1);
    
public voidtestRestrictionTypes()

        Bundle restrictions = new Bundle();
        restrictions.putString("KEY_STRING", "Foo");
        restrictions.putInt("KEY_INT", 7);
        restrictions.putBoolean("KEY_BOOLEAN", true);
        restrictions.putBoolean("KEY_BOOLEAN_2", false);
        restrictions.putString("KEY_STRING_2", "Bar");
        restrictions.putStringArray("KEY_STR_ARRAY", new String[] { "Foo", "Bar" });
        sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions);
        Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP);
        assertTrue(returned.getBoolean("KEY_BOOLEAN"));
        assertFalse(returned.getBoolean("KEY_BOOLEAN_2"));
        assertFalse(returned.getBoolean("KEY_BOOLEAN_3"));
        assertEquals(returned.getInt("KEY_INT"), 7);
        assertTrue(returned.get("KEY_BOOLEAN") instanceof Boolean);
        assertTrue(returned.get("KEY_INT") instanceof Integer);
        assertEquals(returned.get("KEY_STRING"), "Foo");
        assertEquals(returned.get("KEY_STRING_2"), "Bar");
        assertTrue(returned.getStringArray("KEY_STR_ARRAY") instanceof String[]);
        sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, new Bundle());
    
public voidtestSettingRestrictions()

        Bundle restrictions = new Bundle();
        restrictions.putString("KEY_STRING", "Foo");
        assertNotNull(sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP));
        sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions);
        Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP);
        assertNotNull(returned);
        assertEquals(returned.size(), 1);
        assertEquals(returned.get("KEY_STRING"), "Foo");
        sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, new Bundle());
        returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP);
        assertEquals(returned.size(), 0);
    
public voidtestTextEscaping()

        String fancyText = "<This contains XML/> <JSON> "
                + "{ \"One\": { \"OneOne\": \"11\", \"OneTwo\": \"12\" }, \"Two\": \"2\" } <JSON/>";
        Bundle restrictions = new Bundle();
        restrictions.putString("KEY_FANCY_TEXT", fancyText);
        sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions);
        Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP);
        assertEquals(returned.getString("KEY_FANCY_TEXT"), fancyText);