102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy/*
202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * Copyright (C) 2016 The Android Open Source Project
302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy *
402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * Licensed under the Apache License, Version 2.0 (the "License");
502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * you may not use this file except in compliance with the License.
602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * You may obtain a copy of the License at
702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy *
802c8990bd6125f05cb32561727d559dc9327a601Kenny Guy *      http://www.apache.org/licenses/LICENSE-2.0
902c8990bd6125f05cb32561727d559dc9327a601Kenny Guy *
1002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * Unless required by applicable law or agreed to in writing, software
1102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * distributed under the License is distributed on an "AS IS" BASIS,
1202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * See the License for the specific language governing permissions and
1402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * limitations under the License
1502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy */
1602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
1702c8990bd6125f05cb32561727d559dc9327a601Kenny Guypackage com.android.server.pm;
1802c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
19c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franzimport static org.junit.Assert.assertEquals;
20c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franzimport static org.junit.Assert.assertTrue;
21c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
2202c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport android.app.ApplicationPackageManager;
2302c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport android.content.pm.UserInfo;
2402c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport android.os.Looper;
2502c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport android.os.UserHandle;
26c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franzimport android.os.UserManagerInternal;
2702c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport android.support.test.InstrumentationRegistry;
2802c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport android.support.test.filters.MediumTest;
29c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franzimport android.support.test.runner.AndroidJUnit4;
30bab3075e2ee85de8b0ac6c3961eae5156a95c699Sunny Goyalimport android.util.IconDrawableFactory;
3102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
3202c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport com.android.server.LocalServices;
3302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
34c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franzimport java.util.List;
35c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
36c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franzimport org.junit.After;
3702c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport org.junit.Before;
3802c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport org.junit.Test;
3902c8990bd6125f05cb32561727d559dc9327a601Kenny Guyimport org.junit.runner.RunWith;
4002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
4102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy/**
4202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * <p>Run with:<pre>
4302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * runtest -c com.android.server.pm.UserManagerServiceCreateProfileTest frameworks-services
4402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy * </pre>
4502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy */
4602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy@RunWith(AndroidJUnit4.class)
4702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy@MediumTest
4802c8990bd6125f05cb32561727d559dc9327a601Kenny Guypublic class UserManagerServiceCreateProfileTest {
4902c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    private UserManagerService mUserManagerService;
5002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
5102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    @Before
5202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    public void setup() {
5302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        // Currently UserManagerService cannot be instantiated twice inside a VM without a cleanup
5402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        // TODO: Remove once UMS supports proper dependency injection
5502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        if (Looper.myLooper() == null) {
5602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy            Looper.prepare();
5702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        }
5802c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        LocalServices.removeServiceForTest(UserManagerInternal.class);
5902c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        mUserManagerService = new UserManagerService(InstrumentationRegistry.getContext());
6002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
6102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        // The tests assume that the device has one user and its the system user.
6202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false);
6302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        assertEquals("Multiple users so this test can't run.", 1, users.size());
6402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        assertEquals("Only user present isn't the system user.",
6502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy                UserHandle.USER_SYSTEM, users.get(0).id);
6602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
6702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
68c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    @After
69c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    public void tearDown() {
70c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        removeUsers();
71c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    }
72c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
7302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    @Test
7402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    public void testGetProfiles() {
75c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Pretend we have a secondary user with a profile.
76c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        UserInfo secondaryUser = addUser();
77c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        UserInfo profile = addProfile(secondaryUser);
78c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
79c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // System user should still have no profile so getProfiles should just return 1 user.
80c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        List<UserInfo> users =
81c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.getProfiles(UserHandle.USER_SYSTEM, /*excludeDying*/ false);
82c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertEquals("Profiles returned where none should exist", 1, users.size());
83c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertEquals("Missing system user from profile list of system user",
84c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                UserHandle.USER_SYSTEM, users.get(0).id);
85c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
86c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Secondary user should have 1 profile, so return that and itself.
87c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        users = mUserManagerService.getProfiles(secondaryUser.id, /*excludeDying*/ false);
88c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertEquals("Profiles returned where none should exist", 2, users.size());
89c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertTrue("Missing secondary user id", users.get(0).id == secondaryUser.id
90c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                || users.get(1).id == secondaryUser.id);
91c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertTrue("Missing profile user id", users.get(0).id == profile.id
92c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                || users.get(1).id == profile.id);
9302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
9402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
9502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    @Test
9602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    public void testProfileBadge() {
97c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // First profile for system user should get badge 0
98c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertEquals("First profile isn't given badge index 0", 0,
99c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM));
10002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
101c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Pretend we have a secondary user.
102c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        UserInfo secondaryUser = addUser();
10302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
104c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Check first profile badge for secondary user is also 0.
105c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertEquals("First profile for secondary user isn't given badge index 0", 0,
106c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id));
10702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
108c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Shouldn't impact the badge for profile in system user
109c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertEquals("First profile isn't given badge index 0 with secondary user", 0,
110c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM));
11102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
112c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Pretend a secondary user has a profile.
113c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        addProfile(secondaryUser);
11402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
115c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Shouldn't have impacted the badge for the system user
116c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertEquals("First profile isn't given badge index 0 in secondary user", 0,
117c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM));
11802c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
11902c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
12002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    @Test
12102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    public void testProfileBadgeUnique() {
122c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false);
123c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        UserInfo system = users.get(0);
124c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Badges should get allocated 0 -> max
125c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        for (int i = 0; i < UserManagerService.getMaxManagedProfiles(); ++i) {
126c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            int nextBadge = mUserManagerService.getFreeProfileBadgeLU(UserHandle.USER_SYSTEM);
127c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            assertEquals("Wrong badge allocated", i, nextBadge);
128c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            UserInfo profile = addProfile(system);
129c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            profile.profileBadge = nextBadge;
13002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        }
13102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
13202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
13302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    @Test
13402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    public void testProfileBadgeReuse() {
135c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Pretend we have a secondary user with a profile.
136c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        UserInfo secondaryUser = addUser();
137c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        UserInfo profile = addProfile(secondaryUser);
138c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Add the profile it to the users being removed.
139c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        mUserManagerService.addRemovingUserIdLocked(profile.id);
140c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // We should reuse the badge from the profile being removed.
141c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertEquals("Badge index not reused while removing a user", 0,
142c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id));
143c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
144c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // Edge case of reuse that only applies if we ever support 3 managed profiles
145c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // We should prioritise using lower badge indexes
146c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        if (UserManagerService.getMaxManagedProfiles() > 2) {
147c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            UserInfo profileBadgeOne = addProfile(secondaryUser);
148c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            profileBadgeOne.profileBadge = 1;
149c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            // 0 and 2 are free, we should reuse 0 rather than 2.
150c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            assertEquals("Lower index not used", 0,
15102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy                    mUserManagerService.getFreeProfileBadgeLU(secondaryUser.id));
15202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        }
15302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
15402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
15502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    @Test
15602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    public void testNumberOfBadges() {
15702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        assertTrue("Max profiles greater than number of badges",
15802c8990bd6125f05cb32561727d559dc9327a601Kenny Guy                UserManagerService.MAX_MANAGED_PROFILES
159bab3075e2ee85de8b0ac6c3961eae5156a95c699Sunny Goyal                <= IconDrawableFactory.CORP_BADGE_COLORS.length);
16002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        assertEquals("Num colors doesn't match number of badge labels",
161bab3075e2ee85de8b0ac6c3961eae5156a95c699Sunny Goyal                IconDrawableFactory.CORP_BADGE_COLORS.length,
16202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy                ApplicationPackageManager.CORP_BADGE_LABEL_RES_ID.length);
16302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
16402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
165c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    @Test
166c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    public void testCanAddMoreManagedProfiles_removeProfile() {
167c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // if device is low-ram or doesn't support managed profiles for some other reason, just
168c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // skip the test
169c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        if (!mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
170c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                false /* disallow remove */)) {
171c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            return;
172c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        }
173c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
174c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // GIVEN we've reached the limit of managed profiles possible on the system user
175c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        while (mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
176c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                false /* disallow remove */)) {
177c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            addProfile(mUserManagerService.getPrimaryUser());
178c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        }
179c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
180c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // THEN you should be able to add a new profile if you remove an existing one
181c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertTrue("Cannot add a managed profile by removing another one",
182c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
183c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                        true /* allow remove */));
184c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    }
185c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
186c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    @Test
187c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    public void testCanAddMoreManagedProfiles_removeDisabledProfile() {
188c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // if device is low-ram or doesn't support managed profiles for some other reason, just
189c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // skip the test
190c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        if (!mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
191c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                false /* disallow remove */)) {
192c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            return;
193c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        }
194c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
195c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // GIVEN we've reached the limit of managed profiles possible on the system user
196c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // GIVEN that the profiles are not enabled yet
197c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        while (mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
198c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                false /* disallow remove */)) {
199c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            addProfile(mUserManagerService.getPrimaryUser(), true /* disabled */);
200c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        }
201c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
202c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        // THEN you should be able to add a new profile if you remove an existing one
203c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        assertTrue("Cannot add a managed profile by removing another one",
204c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.canAddMoreManagedProfiles(UserHandle.USER_SYSTEM,
205c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                        true /* allow remove */));
206c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    }
207c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
20802c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    private void removeUsers() {
20902c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false);
21002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        for (UserInfo user: users) {
21102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy            if (user.id != UserHandle.USER_SYSTEM) {
21202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy                mUserManagerService.removeUserInfo(user.id);
21302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy            }
21402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        }
21502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
21602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
21702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    private UserInfo addProfile(UserInfo user) {
218c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        return addProfile(user, false);
219c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    }
220c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz
221c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz    private UserInfo addProfile(UserInfo user, boolean disabled) {
22202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        user.profileGroupId = user.id;
223c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        int flags = UserInfo.FLAG_MANAGED_PROFILE;
224c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        if (disabled) {
225c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz            flags |= UserInfo.FLAG_DISABLED;
226c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz        }
22702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        UserInfo profile = new UserInfo(
228c8776157eefc3764ad4a5a1964cd3a1f8c50e418Benjamin Franz                mUserManagerService.getNextAvailableId(), "profile", flags);
22902c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        profile.profileGroupId = user.id;
23002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        mUserManagerService.putUserInfo(profile);
23102c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        return profile;
23202c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
23302c8990bd6125f05cb32561727d559dc9327a601Kenny Guy
23402c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    private UserInfo addUser() {
23502c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        UserInfo secondaryUser = new UserInfo(
23602c8990bd6125f05cb32561727d559dc9327a601Kenny Guy                mUserManagerService.getNextAvailableId(), "secondary", /* flags */ 0);
23702c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        mUserManagerService.putUserInfo(secondaryUser);
23802c8990bd6125f05cb32561727d559dc9327a601Kenny Guy        return secondaryUser;
23902c8990bd6125f05cb32561727d559dc9327a601Kenny Guy    }
24002c8990bd6125f05cb32561727d559dc9327a601Kenny Guy}
241