blob: e8188e74c1b1733fa27c9e11ce5bb872fab24f70 [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
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
17package com.android.server.pm;
18
Amith Yamasani0b285492011-04-14 17:35:23 -070019import com.android.server.pm.UserManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070020
21import android.content.pm.UserInfo;
22import android.os.Debug;
23import android.os.Environment;
24import android.test.AndroidTestCase;
25
26import java.util.List;
27
Amith Yamasani0b285492011-04-14 17:35:23 -070028/** Test {@link UserManager} functionality. */
29public class UserManagerTest extends AndroidTestCase {
Amith Yamasani4b2e9342011-03-31 12:38:53 -070030
Amith Yamasani0b285492011-04-14 17:35:23 -070031 UserManager mUserManager = null;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070032
33 @Override
34 public void setUp() throws Exception {
Amith Yamasani0b285492011-04-14 17:35:23 -070035 mUserManager = new UserManager(Environment.getExternalStorageDirectory(),
36 Environment.getExternalStorageDirectory());
Amith Yamasani4b2e9342011-03-31 12:38:53 -070037 }
38
39 @Override
40 public void tearDown() throws Exception {
Amith Yamasani0b285492011-04-14 17:35:23 -070041 List<UserInfo> users = mUserManager.getUsers();
Amith Yamasani4b2e9342011-03-31 12:38:53 -070042 // Remove all except the primary user
43 for (UserInfo user : users) {
44 if (!user.isPrimary()) {
Amith Yamasani0b285492011-04-14 17:35:23 -070045 mUserManager.removeUser(user.id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -070046 }
47 }
48 }
49
50 public void testHasPrimary() throws Exception {
51 assertTrue(findUser(0));
52 }
53
54 public void testAddUser() throws Exception {
Amith Yamasani0b285492011-04-14 17:35:23 -070055 final UserManager details = mUserManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070056
Amith Yamasani0b285492011-04-14 17:35:23 -070057 UserInfo userInfo = details.createUser("Guest 1", UserInfo.FLAG_GUEST, null);
Amith Yamasani4b2e9342011-03-31 12:38:53 -070058 assertTrue(userInfo != null);
59
60 List<UserInfo> list = details.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 }
69 }
70 assertTrue(found);
71 }
72
73 public void testAdd2Users() throws Exception {
Amith Yamasani0b285492011-04-14 17:35:23 -070074 final UserManager details = mUserManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070075
Amith Yamasani0b285492011-04-14 17:35:23 -070076 UserInfo user1 = details.createUser("Guest 1", UserInfo.FLAG_GUEST, null);
77 UserInfo user2 = details.createUser("User 2", UserInfo.FLAG_ADMIN, null);
Amith Yamasani4b2e9342011-03-31 12:38:53 -070078
79 assertTrue(user1 != null);
80 assertTrue(user2 != null);
81
82 assertTrue(findUser(0));
83 assertTrue(findUser(user1.id));
84 assertTrue(findUser(user2.id));
85 }
86
87 public void testRemoveUser() throws Exception {
Amith Yamasani0b285492011-04-14 17:35:23 -070088 final UserManager details = mUserManager;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070089
Amith Yamasani0b285492011-04-14 17:35:23 -070090 UserInfo userInfo = details.createUser("Guest 1", UserInfo.FLAG_GUEST, null);
Amith Yamasani4b2e9342011-03-31 12:38:53 -070091
92 details.removeUser(userInfo.id);
93
94 assertFalse(findUser(userInfo.id));
95 }
96
97 private boolean findUser(int id) {
Amith Yamasani0b285492011-04-14 17:35:23 -070098 List<UserInfo> list = mUserManager.getUsers();
Amith Yamasani4b2e9342011-03-31 12:38:53 -070099
100 for (UserInfo user : list) {
101 if (user.id == id) {
102 return true;
103 }
104 }
105 return false;
106 }
107}