1 /*
2  * Copyright (C) 2016 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.settings;
18 
19 import static org.mockito.Mockito.eq;
20 import static org.mockito.Mockito.when;
21 
22 import android.content.pm.UserInfo;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.test.AndroidTestCase;
26 import android.test.suitebuilder.annotation.SmallTest;
27 
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 
34 public class UtilsTest extends AndroidTestCase {
35     private static final int TEST_PRIMARY_USER_ID = 10;
36     private static final int TEST_MANAGED_PROFILE_ID = 11;
37 
38     @Mock private UserManager mUserManager;
39 
40     @Override
setUp()41     public void setUp() throws Exception {
42         super.setUp();
43         // // this is necessary for mockito to work
44         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
45 
46         MockitoAnnotations.initMocks(this);
47         when(mUserManager.getUserHandle()).thenReturn(TEST_PRIMARY_USER_ID);
48         UserInfo primaryUser = new UserInfo(TEST_PRIMARY_USER_ID, null,
49                 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_PRIMARY);
50         when(mUserManager.getUserInfo(TEST_PRIMARY_USER_ID)).thenReturn(primaryUser);
51         UserInfo managedProfile = new UserInfo(TEST_MANAGED_PROFILE_ID, null,
52                 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
53         when(mUserManager.getUserInfo(eq(TEST_MANAGED_PROFILE_ID))).thenReturn(managedProfile);
54     }
55 
56     @SmallTest
testGetManagedProfile()57     public void testGetManagedProfile() {
58         UserHandle[] userHandles = new UserHandle[] {
59             new UserHandle(TEST_PRIMARY_USER_ID),
60             new UserHandle(TEST_MANAGED_PROFILE_ID)
61         };
62         when(mUserManager.getUserProfiles())
63                 .thenReturn(new ArrayList<UserHandle>(Arrays.asList(userHandles)));
64         assertEquals(TEST_MANAGED_PROFILE_ID,
65                 Utils.getManagedProfile(mUserManager).getIdentifier());
66     }
67 
68     @SmallTest
testGetManagedProfile_notPresent()69     public void testGetManagedProfile_notPresent() {
70         UserHandle[] userHandles = new UserHandle[] {
71             new UserHandle(TEST_PRIMARY_USER_ID)
72         };
73         when(mUserManager.getUserProfiles())
74                 .thenReturn(new ArrayList<UserHandle>(Arrays.asList(userHandles)));
75         assertNull(Utils.getManagedProfile(mUserManager));
76     }
77 }
78