Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.server.pm;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.content.pm.UserInfo;
     24 import android.os.Bundle;
     25 import android.os.UserHandle;
     26 import android.os.UserManager;
     27 import android.test.AndroidTestCase;
     28 
     29 import java.util.ArrayList;
     30 import java.util.List;
     31 
     32 /** Test {@link UserManager} functionality. */
     33 public class UserManagerTest extends AndroidTestCase {
     34 
     35     UserManager mUserManager = null;
     36     Object mUserLock = new Object();
     37 
     38     @Override
     39     public void setUp() throws Exception {
     40         mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
     41         IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED);
     42         getContext().registerReceiver(new BroadcastReceiver() {
     43             @Override
     44             public void onReceive(Context context, Intent intent) {
     45                 synchronized (mUserLock) {
     46                     mUserLock.notifyAll();
     47                 }
     48             }
     49         }, filter);
     50     }
     51 
     52     public void testHasPrimary() throws Exception {
     53         assertTrue(findUser(0));
     54     }
     55 
     56     public void testAddUser() throws Exception {
     57         UserInfo userInfo = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST);
     58         assertTrue(userInfo != null);
     59 
     60         List<UserInfo> list = mUserManager.getUsers();
     61         boolean found = false;
     62         for (UserInfo user : list) {
     63             if (user.id == userInfo.id && user.name.equals("Guest 1")
     64                     && user.isGuest()
     65                     && !user.isAdmin()
     66                     && !user.isPrimary()) {
     67                 found = true;
     68                 Bundle restrictions = mUserManager.getUserRestrictions(user.getUserHandle());
     69                 assertFalse("New user should have DISALLOW_CONFIG_WIFI =false by default",
     70                         restrictions.getBoolean(UserManager.DISALLOW_CONFIG_WIFI));
     71             }
     72         }
     73         assertTrue(found);
     74         removeUser(userInfo.id);
     75     }
     76 
     77     public void testAdd2Users() throws Exception {
     78         UserInfo user1 = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST);
     79         UserInfo user2 = mUserManager.createUser("User 2", UserInfo.FLAG_ADMIN);
     80 
     81         assertTrue(user1 != null);
     82         assertTrue(user2 != null);
     83 
     84         assertTrue(findUser(0));
     85         assertTrue(findUser(user1.id));
     86         assertTrue(findUser(user2.id));
     87         removeUser(user1.id);
     88         removeUser(user2.id);
     89     }
     90 
     91     public void testRemoveUser() throws Exception {
     92         UserInfo userInfo = mUserManager.createUser("Guest 1", UserInfo.FLAG_GUEST);
     93         removeUser(userInfo.id);
     94 
     95         assertFalse(findUser(userInfo.id));
     96     }
     97 
     98     private boolean findUser(int id) {
     99         List<UserInfo> list = mUserManager.getUsers();
    100 
    101         for (UserInfo user : list) {
    102             if (user.id == id) {
    103                 return true;
    104             }
    105         }
    106         return false;
    107     }
    108 
    109     public void testSerialNumber() {
    110         UserInfo user1 = mUserManager.createUser("User 1", UserInfo.FLAG_RESTRICTED);
    111         int serialNumber1 = user1.serialNumber;
    112         assertEquals(serialNumber1, mUserManager.getUserSerialNumber(user1.id));
    113         assertEquals(user1.id, mUserManager.getUserHandle(serialNumber1));
    114         removeUser(user1.id);
    115         UserInfo user2 = mUserManager.createUser("User 2", UserInfo.FLAG_RESTRICTED);
    116         int serialNumber2 = user2.serialNumber;
    117         assertFalse(serialNumber1 == serialNumber2);
    118         assertEquals(serialNumber2, mUserManager.getUserSerialNumber(user2.id));
    119         assertEquals(user2.id, mUserManager.getUserHandle(serialNumber2));
    120         removeUser(user2.id);
    121     }
    122 
    123     public void testMaxUsers() {
    124         int N = UserManager.getMaxSupportedUsers();
    125         int count = mUserManager.getUsers().size();
    126         List<UserInfo> created = new ArrayList<UserInfo>();
    127         // Create as many users as permitted and make sure creation passes
    128         while (count < N) {
    129             UserInfo ui = mUserManager.createUser("User " + count, 0);
    130             assertNotNull(ui);
    131             created.add(ui);
    132             count++;
    133         }
    134         // Try to create one more user and make sure it fails
    135         UserInfo extra = null;
    136         assertNull(extra = mUserManager.createUser("One more", 0));
    137         if (extra != null) {
    138             removeUser(extra.id);
    139         }
    140         while (!created.isEmpty()) {
    141             UserInfo user = created.remove(0);
    142             removeUser(user.id);
    143         }
    144     }
    145 
    146     public void testRestrictions() {
    147         List<UserInfo> users = mUserManager.getUsers();
    148         if (users.size() > 1) {
    149             Bundle restrictions = new Bundle();
    150             restrictions.putBoolean(UserManager.DISALLOW_INSTALL_APPS, true);
    151             restrictions.putBoolean(UserManager.DISALLOW_CONFIG_WIFI, false);
    152             mUserManager.setUserRestrictions(restrictions, new UserHandle(users.get(1).id));
    153             Bundle stored = mUserManager.getUserRestrictions(new UserHandle(users.get(1).id));
    154             assertEquals(stored.getBoolean(UserManager.DISALLOW_CONFIG_WIFI), false);
    155             assertEquals(stored.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS), false);
    156             assertEquals(stored.getBoolean(UserManager.DISALLOW_INSTALL_APPS), true);
    157         }
    158     }
    159 
    160     private void removeUser(int userId) {
    161         synchronized (mUserLock) {
    162             mUserManager.removeUser(userId);
    163             while (mUserManager.getUserInfo(userId) != null) {
    164                 try {
    165                     mUserLock.wait(1000);
    166                 } catch (InterruptedException ie) {
    167                 }
    168             }
    169         }
    170     }
    171 
    172 }
    173