FileDocCategorySizeDatePackage
UserManagerTest.javaAPI DocAndroid 5.1 API7640Thu Mar 12 22:22:42 GMT 2015com.android.server.pm

UserManagerTest

public class UserManagerTest extends android.test.AndroidTestCase
Test {@link UserManager} functionality.

Fields Summary
android.os.UserManager
mUserManager
Object
mUserLock
Constructors Summary
Methods Summary
private booleanfindUser(int id)

        List<UserInfo> list = mUserManager.getUsers();

        for (UserInfo user : list) {
            if (user.id == id) {
                return true;
            }
        }
        return false;
    
private voidremoveExistingUsers()

        List<UserInfo> list = mUserManager.getUsers();
        boolean found = false;
        for (UserInfo user : list) {
            if (user.id != UserHandle.USER_OWNER) {
                removeUser(user.id);
            }
        }
    
private voidremoveUser(int userId)

        synchronized (mUserLock) {
            mUserManager.removeUser(userId);
            while (mUserManager.getUserInfo(userId) != null) {
                try {
                    mUserLock.wait(1000);
                } catch (InterruptedException ie) {
                }
            }
        }
    
public voidsetUp()


    
         
        mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
        IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED);
        getContext().registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                synchronized (mUserLock) {
                    mUserLock.notifyAll();
                }
            }
        }, filter);

        removeExistingUsers();
    
public voidtestAdd2Users()

        UserInfo user1 = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST);
        UserInfo user2 = mUserManager.createUser("User 2", UserInfo.FLAG_ADMIN);

        assertTrue(user1 != null);
        assertTrue(user2 != null);

        assertTrue(findUser(0));
        assertTrue(findUser(user1.id));
        assertTrue(findUser(user2.id));
        removeUser(user1.id);
        removeUser(user2.id);
    
public voidtestAddGuest()

        UserInfo userInfo1 = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST);
        UserInfo userInfo2 = mUserManager.createUser("Guest 2", UserInfo.FLAG_GUEST);
        assertNotNull(userInfo1);
        assertNull(userInfo2);

        // Cleanup
        removeUser(userInfo1.id);
    
public voidtestAddManagedProfile()

        UserInfo userInfo1 = mUserManager.createProfileForUser("Managed 1",
                UserInfo.FLAG_MANAGED_PROFILE, UserHandle.USER_OWNER);
        UserInfo userInfo2 = mUserManager.createProfileForUser("Managed 2",
                UserInfo.FLAG_MANAGED_PROFILE, UserHandle.USER_OWNER);
        assertNotNull(userInfo1);
        assertNull(userInfo2);
        // Verify that current user is not a managed profile
        assertFalse(mUserManager.isManagedProfile());
        // Cleanup
        removeUser(userInfo1.id);
    
public voidtestAddUser()

        UserInfo userInfo = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST);
        assertTrue(userInfo != null);

        List<UserInfo> list = mUserManager.getUsers();
        boolean found = false;
        for (UserInfo user : list) {
            if (user.id == userInfo.id && user.name.equals("Guest 1")
                    && user.isGuest()
                    && !user.isAdmin()
                    && !user.isPrimary()) {
                found = true;
                Bundle restrictions = mUserManager.getUserRestrictions(user.getUserHandle());
                assertFalse("New user should have DISALLOW_CONFIG_WIFI =false by default",
                        restrictions.getBoolean(UserManager.DISALLOW_CONFIG_WIFI));
            }
        }
        assertTrue(found);
        removeUser(userInfo.id);
    
public voidtestHasPrimary()

        assertTrue(findUser(0));
    
public voidtestMaxUsers()

        int N = UserManager.getMaxSupportedUsers();
        int count = mUserManager.getUsers().size();
        List<UserInfo> created = new ArrayList<UserInfo>();
        // Create as many users as permitted and make sure creation passes
        while (count < N) {
            UserInfo ui = mUserManager.createUser("User " + count, 0);
            assertNotNull(ui);
            created.add(ui);
            count++;
        }
        // Try to create one more user and make sure it fails
        UserInfo extra = null;
        assertNull(extra = mUserManager.createUser("One more", 0));
        if (extra != null) {
            removeUser(extra.id);
        }
        while (!created.isEmpty()) {
            UserInfo user = created.remove(0);
            removeUser(user.id);
        }
    
public voidtestRemoveUser()

        UserInfo userInfo = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST);
        removeUser(userInfo.id);

        assertFalse(findUser(userInfo.id));
    
public voidtestRestrictions()

        List<UserInfo> users = mUserManager.getUsers();
        if (users.size() > 1) {
            Bundle restrictions = new Bundle();
            restrictions.putBoolean(UserManager.DISALLOW_INSTALL_APPS, true);
            restrictions.putBoolean(UserManager.DISALLOW_CONFIG_WIFI, false);
            mUserManager.setUserRestrictions(restrictions, new UserHandle(users.get(1).id));
            Bundle stored = mUserManager.getUserRestrictions(new UserHandle(users.get(1).id));
            assertEquals(stored.getBoolean(UserManager.DISALLOW_CONFIG_WIFI), false);
            assertEquals(stored.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS), false);
            assertEquals(stored.getBoolean(UserManager.DISALLOW_INSTALL_APPS), true);
        }
    
public voidtestSerialNumber()

        UserInfo user1 = mUserManager.createUser("User 1", UserInfo.FLAG_RESTRICTED);
        int serialNumber1 = user1.serialNumber;
        assertEquals(serialNumber1, mUserManager.getUserSerialNumber(user1.id));
        assertEquals(user1.id, mUserManager.getUserHandle(serialNumber1));
        removeUser(user1.id);
        UserInfo user2 = mUserManager.createUser("User 2", UserInfo.FLAG_RESTRICTED);
        int serialNumber2 = user2.serialNumber;
        assertFalse(serialNumber1 == serialNumber2);
        assertEquals(serialNumber2, mUserManager.getUserSerialNumber(user2.id));
        assertEquals(user2.id, mUserManager.getUserHandle(serialNumber2));
        removeUser(user2.id);