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.PackageManager;
24 import android.content.pm.UserInfo;
25 import android.app.ActivityManager;
26 import android.os.Bundle;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 import android.provider.Settings;
30 import android.test.AndroidTestCase;
31 import android.test.suitebuilder.annotation.LargeTest;
32 import android.test.suitebuilder.annotation.MediumTest;
33 import android.test.suitebuilder.annotation.SmallTest;
34 
35 import com.android.internal.util.ArrayUtils;
36 
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.concurrent.ExecutorService;
41 import java.util.concurrent.Executors;
42 import java.util.concurrent.TimeUnit;
43 import java.util.concurrent.atomic.AtomicInteger;
44 
45 /** Test {@link UserManager} functionality. */
46 public class UserManagerTest extends AndroidTestCase {
47     // Taken from UserManagerService
48     private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // 30 years
49 
50     private static final int REMOVE_CHECK_INTERVAL_MILLIS = 500; // 0.5 seconds
51     private static final int REMOVE_TIMEOUT_MILLIS = 60 * 1000; // 60 seconds
52     private static final int SWITCH_USER_TIMEOUT_MILLIS = 40 * 1000; // 40 seconds
53 
54     // Packages which are used during tests.
55     private static final String[] PACKAGES = new String[] {
56             "com.android.egg"
57     };
58 
59     private final Object mUserRemoveLock = new Object();
60     private final Object mUserSwitchLock = new Object();
61 
62     private UserManager mUserManager = null;
63     private PackageManager mPackageManager;
64     private List<Integer> usersToRemove;
65 
66     @Override
setUp()67     public void setUp() throws Exception {
68         super.setUp();
69         mUserManager = UserManager.get(getContext());
70         mPackageManager = getContext().getPackageManager();
71 
72         IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED);
73         filter.addAction(Intent.ACTION_USER_SWITCHED);
74         getContext().registerReceiver(new BroadcastReceiver() {
75             @Override
76             public void onReceive(Context context, Intent intent) {
77                 switch (intent.getAction()) {
78                     case Intent.ACTION_USER_REMOVED:
79                         synchronized (mUserRemoveLock) {
80                             mUserRemoveLock.notifyAll();
81                         }
82                         break;
83                     case Intent.ACTION_USER_SWITCHED:
84                         synchronized (mUserSwitchLock) {
85                             mUserSwitchLock.notifyAll();
86                         }
87                         break;
88                 }
89             }
90         }, filter);
91 
92         removeExistingUsers();
93         usersToRemove = new ArrayList<>();
94     }
95 
96     @Override
tearDown()97     protected void tearDown() throws Exception {
98         for (Integer userId : usersToRemove) {
99             removeUser(userId);
100         }
101         super.tearDown();
102     }
103 
removeExistingUsers()104     private void removeExistingUsers() {
105         List<UserInfo> list = mUserManager.getUsers();
106         for (UserInfo user : list) {
107             // Keep system and primary user.
108             // We do not have to keep primary user, but in split system user mode, we need it
109             // until http://b/22976637 is fixed.  Right now in split system user mode, you need to
110             // switch to primary user and run tests under primary user.
111             if (user.id != UserHandle.USER_SYSTEM && !user.isPrimary()) {
112                 removeUser(user.id);
113             }
114         }
115     }
116 
117     @SmallTest
testHasSystemUser()118     public void testHasSystemUser() throws Exception {
119         assertTrue(findUser(UserHandle.USER_SYSTEM));
120     }
121 
122     @MediumTest
testAddUser()123     public void testAddUser() throws Exception {
124         UserInfo userInfo = createUser("Guest 1", UserInfo.FLAG_GUEST);
125         assertTrue(userInfo != null);
126 
127         List<UserInfo> list = mUserManager.getUsers();
128         boolean found = false;
129         for (UserInfo user : list) {
130             if (user.id == userInfo.id && user.name.equals("Guest 1")
131                     && user.isGuest()
132                     && !user.isAdmin()
133                     && !user.isPrimary()) {
134                 found = true;
135                 Bundle restrictions = mUserManager.getUserRestrictions(user.getUserHandle());
136                 assertTrue("Guest user should have DISALLOW_CONFIG_WIFI=true by default",
137                         restrictions.getBoolean(UserManager.DISALLOW_CONFIG_WIFI));
138             }
139         }
140         assertTrue(found);
141     }
142 
143     @MediumTest
testAdd2Users()144     public void testAdd2Users() throws Exception {
145         UserInfo user1 = createUser("Guest 1", UserInfo.FLAG_GUEST);
146         UserInfo user2 = createUser("User 2", UserInfo.FLAG_ADMIN);
147 
148         assertTrue(user1 != null);
149         assertTrue(user2 != null);
150 
151         assertTrue(findUser(0));
152         assertTrue(findUser(user1.id));
153         assertTrue(findUser(user2.id));
154     }
155 
156     @MediumTest
testRemoveUser()157     public void testRemoveUser() throws Exception {
158         UserInfo userInfo = createUser("Guest 1", UserInfo.FLAG_GUEST);
159         removeUser(userInfo.id);
160 
161         assertFalse(findUser(userInfo.id));
162     }
163 
164     @MediumTest
testAddGuest()165     public void testAddGuest() throws Exception {
166         UserInfo userInfo1 = createUser("Guest 1", UserInfo.FLAG_GUEST);
167         UserInfo userInfo2 = createUser("Guest 2", UserInfo.FLAG_GUEST);
168         assertNotNull(userInfo1);
169         assertNull(userInfo2);
170     }
171 
172     @MediumTest
testSetUserAdmin()173     public void testSetUserAdmin() throws Exception {
174         UserInfo userInfo = createUser("SecondaryUser", /*flags=*/ 0);
175 
176         // Assert user is not admin and has SMS and calls restrictions.
177         assertFalse(userInfo.isAdmin());
178         assertTrue(mUserManager.hasUserRestriction(UserManager.DISALLOW_SMS,
179                 userInfo.getUserHandle()));
180         assertTrue(mUserManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
181                 userInfo.getUserHandle()));
182 
183         // Assign admin privileges.
184         mUserManager.setUserAdmin(userInfo.id);
185 
186         // Refresh.
187         userInfo = mUserManager.getUserInfo(userInfo.id);
188 
189         // Verify user became admin and SMS and call restrictions are lifted.
190         assertTrue(userInfo.isAdmin());
191         assertFalse(mUserManager.hasUserRestriction(UserManager.DISALLOW_SMS,
192                 userInfo.getUserHandle()));
193         assertFalse(mUserManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
194                 userInfo.getUserHandle()));
195     }
196 
197     @MediumTest
testGetProfileParent()198     public void testGetProfileParent() throws Exception {
199         final int primaryUserId = mUserManager.getPrimaryUser().id;
200 
201         UserInfo userInfo = createProfileForUser("Profile",
202                 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
203         assertNotNull(userInfo);
204         assertNull(mUserManager.getProfileParent(primaryUserId));
205         UserInfo parentProfileInfo = mUserManager.getProfileParent(userInfo.id);
206         assertNotNull(parentProfileInfo);
207         assertEquals(parentProfileInfo.id, primaryUserId);
208         removeUser(userInfo.id);
209         assertNull(mUserManager.getProfileParent(primaryUserId));
210     }
211 
212     // Make sure only one managed profile can be created
213     @MediumTest
testAddManagedProfile()214     public void testAddManagedProfile() throws Exception {
215         final int primaryUserId = mUserManager.getPrimaryUser().id;
216         UserInfo userInfo1 = createProfileForUser("Managed 1",
217                 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
218         UserInfo userInfo2 = createProfileForUser("Managed 2",
219                 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
220 
221         assertNotNull(userInfo1);
222         assertNull(userInfo2);
223         // Verify that current user is not a managed profile
224         assertFalse(mUserManager.isManagedProfile());
225     }
226 
227     // Verify that disallowed packages are not installed in the managed profile.
228     @MediumTest
testAddManagedProfile_withDisallowedPackages()229     public void testAddManagedProfile_withDisallowedPackages() throws Exception {
230         final int primaryUserId = mUserManager.getPrimaryUser().id;
231         UserInfo userInfo1 = createProfileForUser("Managed1",
232                 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
233         // Verify that the packagesToVerify are installed by default.
234         for (String pkg : PACKAGES) {
235             assertTrue("Package should be installed in managed profile: " + pkg,
236                     isPackageInstalledForUser(pkg, userInfo1.id));
237         }
238         removeUser(userInfo1.id);
239 
240         UserInfo userInfo2 = createProfileForUser("Managed2",
241                 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId, PACKAGES);
242         // Verify that the packagesToVerify are not installed by default.
243         for (String pkg : PACKAGES) {
244             assertFalse("Package should not be installed in managed profile when disallowed: "
245                     + pkg, isPackageInstalledForUser(pkg, userInfo2.id));
246         }
247     }
248 
249     // Verify that if any packages are disallowed to install during creation of managed profile can
250     // still be installed later.
251     @MediumTest
testAddManagedProfile_disallowedPackagesInstalledLater()252     public void testAddManagedProfile_disallowedPackagesInstalledLater() throws Exception {
253         final int primaryUserId = mUserManager.getPrimaryUser().id;
254         UserInfo userInfo = createProfileForUser("Managed",
255                 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId, PACKAGES);
256         // Verify that the packagesToVerify are not installed by default.
257         for (String pkg : PACKAGES) {
258             assertFalse("Package should not be installed in managed profile when disallowed: "
259                     + pkg, isPackageInstalledForUser(pkg, userInfo.id));
260         }
261 
262         // Verify that the disallowed packages during profile creation can be installed now.
263         for (String pkg : PACKAGES) {
264             assertEquals("Package could not be installed: " + pkg,
265                     PackageManager.INSTALL_SUCCEEDED,
266                     mPackageManager.installExistingPackageAsUser(pkg, userInfo.id));
267         }
268     }
269 
270     // Make sure createUser would fail if we have DISALLOW_ADD_USER.
271     @MediumTest
testCreateUser_disallowAddUser()272     public void testCreateUser_disallowAddUser() throws Exception {
273         final int primaryUserId = mUserManager.getPrimaryUser().id;
274         final UserHandle primaryUserHandle = new UserHandle(primaryUserId);
275         mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, true, primaryUserHandle);
276         try {
277             UserInfo userInfo = createUser("SecondaryUser", /*flags=*/ 0);
278             assertNull(userInfo);
279         } finally {
280             mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, false,
281                     primaryUserHandle);
282         }
283     }
284 
285     // Make sure createProfile would fail if we have DISALLOW_ADD_MANAGED_PROFILE.
286     @MediumTest
testCreateProfileForUser_disallowAddManagedProfile()287     public void testCreateProfileForUser_disallowAddManagedProfile() throws Exception {
288         final int primaryUserId = mUserManager.getPrimaryUser().id;
289         final UserHandle primaryUserHandle = new UserHandle(primaryUserId);
290         mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, true,
291                 primaryUserHandle);
292         try {
293             UserInfo userInfo = createProfileForUser("Managed",
294                     UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
295             assertNull(userInfo);
296         } finally {
297             mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, false,
298                     primaryUserHandle);
299         }
300     }
301 
302     // Make sure createProfileEvenWhenDisallowedForUser bypass DISALLOW_ADD_MANAGED_PROFILE.
303     @MediumTest
testCreateProfileForUserEvenWhenDisallowed()304     public void testCreateProfileForUserEvenWhenDisallowed() throws Exception {
305         final int primaryUserId = mUserManager.getPrimaryUser().id;
306         final UserHandle primaryUserHandle = new UserHandle(primaryUserId);
307         mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, true,
308                 primaryUserHandle);
309         try {
310             UserInfo userInfo = createProfileEvenWhenDisallowedForUser("Managed",
311                     UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
312             assertNotNull(userInfo);
313         } finally {
314             mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, false,
315                     primaryUserHandle);
316         }
317     }
318 
319     // createProfile succeeds even if DISALLOW_ADD_USER is set
320     @MediumTest
testCreateProfileForUser_disallowAddUser()321     public void testCreateProfileForUser_disallowAddUser() throws Exception {
322         final int primaryUserId = mUserManager.getPrimaryUser().id;
323         final UserHandle primaryUserHandle = new UserHandle(primaryUserId);
324         mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, true, primaryUserHandle);
325         try {
326             UserInfo userInfo = createProfileForUser("Managed",
327                     UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
328             assertNotNull(userInfo);
329         } finally {
330             mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, false,
331                     primaryUserHandle);
332         }
333     }
334 
335     @MediumTest
testAddRestrictedProfile()336     public void testAddRestrictedProfile() throws Exception {
337         assertFalse("There should be no associated restricted profiles before the test",
338                 mUserManager.hasRestrictedProfiles());
339         UserInfo userInfo = createRestrictedProfile("Profile");
340         assertNotNull(userInfo);
341 
342         Bundle restrictions = mUserManager.getUserRestrictions(UserHandle.of(userInfo.id));
343         assertTrue("Restricted profile should have DISALLOW_MODIFY_ACCOUNTS restriction by default",
344                 restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS));
345         assertTrue("Restricted profile should have DISALLOW_SHARE_LOCATION restriction by default",
346                 restrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION));
347 
348         int locationMode = Settings.Secure.getIntForUser(getContext().getContentResolver(),
349                 Settings.Secure.LOCATION_MODE,
350                 Settings.Secure.LOCATION_MODE_HIGH_ACCURACY,
351                 userInfo.id);
352         assertEquals("Restricted profile should have setting LOCATION_MODE set to "
353                 + "LOCATION_MODE_OFF by default", locationMode, Settings.Secure.LOCATION_MODE_OFF);
354 
355         assertTrue("Newly created profile should be associated with the current user",
356                 mUserManager.hasRestrictedProfiles());
357     }
358 
359     @MediumTest
testGetUserCreationTime()360     public void testGetUserCreationTime() throws Exception {
361         final int primaryUserId = mUserManager.getPrimaryUser().id;
362         final long startTime = System.currentTimeMillis();
363         UserInfo profile = createProfileForUser("Managed 1",
364                 UserInfo.FLAG_MANAGED_PROFILE, primaryUserId);
365         final long endTime = System.currentTimeMillis();
366         assertNotNull(profile);
367         if (System.currentTimeMillis() > EPOCH_PLUS_30_YEARS) {
368             assertTrue("creationTime must be set when the profile is created",
369                     profile.creationTime >= startTime && profile.creationTime <= endTime);
370         } else {
371             assertTrue("creationTime must be 0 if the time is not > EPOCH_PLUS_30_years",
372                     profile.creationTime == 0);
373         }
374         assertEquals(profile.creationTime, mUserManager.getUserCreationTime(
375                 new UserHandle(profile.id)));
376 
377         long ownerCreationTime = mUserManager.getUserInfo(primaryUserId).creationTime;
378         assertEquals(ownerCreationTime, mUserManager.getUserCreationTime(
379                 new UserHandle(primaryUserId)));
380     }
381 
382     @SmallTest
testGetUserCreationTime_nonExistentUser()383     public void testGetUserCreationTime_nonExistentUser() throws Exception {
384         try {
385             int noSuchUserId = 100500;
386             mUserManager.getUserCreationTime(new UserHandle(noSuchUserId));
387             fail("SecurityException should be thrown for nonexistent user");
388         } catch (Exception e) {
389             assertTrue("SecurityException should be thrown for nonexistent user, but was: " + e,
390                     e instanceof SecurityException);
391         }
392     }
393 
394     @SmallTest
testGetUserCreationTime_otherUser()395     public void testGetUserCreationTime_otherUser() throws Exception {
396         UserInfo user = createUser("User 1", 0);
397         try {
398             mUserManager.getUserCreationTime(new UserHandle(user.id));
399             fail("SecurityException should be thrown for other user");
400         } catch (Exception e) {
401             assertTrue("SecurityException should be thrown for other user, but was: " + e,
402                     e instanceof SecurityException);
403         }
404     }
405 
findUser(int id)406     private boolean findUser(int id) {
407         List<UserInfo> list = mUserManager.getUsers();
408 
409         for (UserInfo user : list) {
410             if (user.id == id) {
411                 return true;
412             }
413         }
414         return false;
415     }
416 
417     @MediumTest
testSerialNumber()418     public void testSerialNumber() {
419         UserInfo user1 = createUser("User 1", 0);
420         int serialNumber1 = user1.serialNumber;
421         assertEquals(serialNumber1, mUserManager.getUserSerialNumber(user1.id));
422         assertEquals(user1.id, mUserManager.getUserHandle(serialNumber1));
423         UserInfo user2 = createUser("User 2", 0);
424         int serialNumber2 = user2.serialNumber;
425         assertFalse(serialNumber1 == serialNumber2);
426         assertEquals(serialNumber2, mUserManager.getUserSerialNumber(user2.id));
427         assertEquals(user2.id, mUserManager.getUserHandle(serialNumber2));
428     }
429 
430     @MediumTest
testGetSerialNumbersOfUsers()431     public void testGetSerialNumbersOfUsers() {
432         UserInfo user1 = createUser("User 1", 0);
433         UserInfo user2 = createUser("User 2", 0);
434         long[] serialNumbersOfUsers = mUserManager.getSerialNumbersOfUsers(false);
435         String errMsg = "Array " + Arrays.toString(serialNumbersOfUsers) + " should contain ";
436         assertTrue(errMsg + user1.serialNumber,
437                 ArrayUtils.contains(serialNumbersOfUsers, user1.serialNumber));
438         assertTrue(errMsg + user2.serialNumber,
439                 ArrayUtils.contains(serialNumbersOfUsers, user2.serialNumber));
440     }
441 
442     @MediumTest
testMaxUsers()443     public void testMaxUsers() {
444         int N = UserManager.getMaxSupportedUsers();
445         int count = mUserManager.getUsers().size();
446         // Create as many users as permitted and make sure creation passes
447         while (count < N) {
448             UserInfo ui = createUser("User " + count, 0);
449             assertNotNull(ui);
450             count++;
451         }
452         // Try to create one more user and make sure it fails
453         UserInfo extra = createUser("One more", 0);
454         assertNull(extra);
455     }
456 
457     @MediumTest
testGetUserCount()458     public void testGetUserCount() {
459         int count = mUserManager.getUsers().size();
460         UserInfo user1 = createUser("User 1", 0);
461         assertNotNull(user1);
462         UserInfo user2 = createUser("User 2", 0);
463         assertNotNull(user2);
464         assertEquals(count + 2, mUserManager.getUserCount());
465     }
466 
467     @MediumTest
testRestrictions()468     public void testRestrictions() {
469         UserInfo testUser = createUser("User 1", 0);
470 
471         mUserManager.setUserRestriction(
472                 UserManager.DISALLOW_INSTALL_APPS, true, new UserHandle(testUser.id));
473         mUserManager.setUserRestriction(
474                 UserManager.DISALLOW_CONFIG_WIFI, false, new UserHandle(testUser.id));
475 
476         Bundle stored = mUserManager.getUserRestrictions(new UserHandle(testUser.id));
477         // Note this will fail if DO already sets those restrictions.
478         assertEquals(stored.getBoolean(UserManager.DISALLOW_CONFIG_WIFI), false);
479         assertEquals(stored.getBoolean(UserManager.DISALLOW_UNINSTALL_APPS), false);
480         assertEquals(stored.getBoolean(UserManager.DISALLOW_INSTALL_APPS), true);
481     }
482 
483     @MediumTest
testSetDefaultGuestRestrictions()484     public void testSetDefaultGuestRestrictions() {
485         final Bundle origGuestRestrictions = mUserManager.getDefaultGuestRestrictions();
486         Bundle restrictions = new Bundle();
487         restrictions.putBoolean(UserManager.DISALLOW_FUN, true);
488         mUserManager.setDefaultGuestRestrictions(restrictions);
489 
490         try {
491             UserInfo guest = createUser("Guest", UserInfo.FLAG_GUEST);
492             assertNotNull(guest);
493             assertTrue(mUserManager.hasUserRestriction(UserManager.DISALLOW_FUN,
494                     guest.getUserHandle()));
495         } finally {
496             mUserManager.setDefaultGuestRestrictions(origGuestRestrictions);
497         }
498     }
499 
500     @LargeTest
testSwitchUser()501     public void testSwitchUser() {
502         ActivityManager am = getContext().getSystemService(ActivityManager.class);
503         final int startUser = am.getCurrentUser();
504         UserInfo user = createUser("User", 0);
505         assertNotNull(user);
506         // Switch to the user just created.
507         switchUser(user.id);
508         // Switch back to the starting user.
509         switchUser(startUser);
510     }
511 
512     @MediumTest
testConcurrentUserCreate()513     public void testConcurrentUserCreate() throws Exception {
514         int userCount = mUserManager.getUserCount();
515         int maxSupportedUsers = UserManager.getMaxSupportedUsers();
516         int canBeCreatedCount = maxSupportedUsers - userCount;
517         // Test exceeding the limit while running in parallel
518         int createUsersCount = canBeCreatedCount + 5;
519         ExecutorService es = Executors.newCachedThreadPool();
520         AtomicInteger created = new AtomicInteger();
521         for (int i = 0; i < createUsersCount; i++) {
522             final String userName = "testConcUser" + i;
523             es.submit(() -> {
524                 UserInfo user = mUserManager.createUser(userName, 0);
525                 if (user != null) {
526                     created.incrementAndGet();
527                     synchronized (mUserRemoveLock) {
528                         usersToRemove.add(user.id);
529                     }
530                 }
531             });
532         }
533         es.shutdown();
534         es.awaitTermination(20, TimeUnit.SECONDS);
535         assertEquals(maxSupportedUsers, mUserManager.getUserCount());
536         assertEquals(canBeCreatedCount, created.get());
537     }
538 
isPackageInstalledForUser(String packageName, int userId)539     private boolean isPackageInstalledForUser(String packageName, int userId) {
540         try {
541             return mPackageManager.getPackageInfoAsUser(packageName, 0, userId) != null;
542         } catch (PackageManager.NameNotFoundException e) {
543             return false;
544         }
545     }
546 
switchUser(int userId)547     private void switchUser(int userId) {
548         synchronized (mUserSwitchLock) {
549             ActivityManager am = getContext().getSystemService(ActivityManager.class);
550             am.switchUser(userId);
551             long time = System.currentTimeMillis();
552             try {
553                 mUserSwitchLock.wait(SWITCH_USER_TIMEOUT_MILLIS);
554             } catch (InterruptedException ie) {
555                 Thread.currentThread().interrupt();
556                 return;
557             }
558             if (System.currentTimeMillis() - time > SWITCH_USER_TIMEOUT_MILLIS) {
559                 fail("Timeout waiting for the user switch to u" + userId);
560             }
561         }
562     }
563 
removeUser(int userId)564     private void removeUser(int userId) {
565         synchronized (mUserRemoveLock) {
566             mUserManager.removeUser(userId);
567             long time = System.currentTimeMillis();
568             while (mUserManager.getUserInfo(userId) != null) {
569                 try {
570                     mUserRemoveLock.wait(REMOVE_CHECK_INTERVAL_MILLIS);
571                 } catch (InterruptedException ie) {
572                     Thread.currentThread().interrupt();
573                     return;
574                 }
575                 if (System.currentTimeMillis() - time > REMOVE_TIMEOUT_MILLIS) {
576                     fail("Timeout waiting for removeUser. userId = " + userId);
577                 }
578             }
579         }
580     }
581 
createUser(String name, int flags)582     private UserInfo createUser(String name, int flags) {
583         UserInfo user = mUserManager.createUser(name, flags);
584         if (user != null) {
585             usersToRemove.add(user.id);
586         }
587         return user;
588     }
589 
createProfileForUser(String name, int flags, int userHandle)590     private UserInfo createProfileForUser(String name, int flags, int userHandle) {
591         return createProfileForUser(name, flags, userHandle, null);
592     }
593 
createProfileForUser(String name, int flags, int userHandle, String[] disallowedPackages)594     private UserInfo createProfileForUser(String name, int flags, int userHandle,
595             String[] disallowedPackages) {
596         UserInfo profile = mUserManager.createProfileForUser(
597                 name, flags, userHandle, disallowedPackages);
598         if (profile != null) {
599             usersToRemove.add(profile.id);
600         }
601         return profile;
602     }
603 
createProfileEvenWhenDisallowedForUser(String name, int flags, int userHandle)604     private UserInfo createProfileEvenWhenDisallowedForUser(String name, int flags,
605             int userHandle) {
606         UserInfo profile = mUserManager.createProfileForUserEvenWhenDisallowed(
607                 name, flags, userHandle, null);
608         if (profile != null) {
609             usersToRemove.add(profile.id);
610         }
611         return profile;
612     }
613 
createRestrictedProfile(String name)614     private UserInfo createRestrictedProfile(String name) {
615         UserInfo profile = mUserManager.createRestrictedProfile(name);
616         if (profile != null) {
617             usersToRemove.add(profile.id);
618         }
619         return profile;
620     }
621 
622 }
623