1 /* 2 * Copyright (C) 2018 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.car.users; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.when; 22 23 import android.app.ActivityManager; 24 import android.car.userlib.CarUserManagerHelper; 25 import android.content.Context; 26 import android.content.pm.UserInfo; 27 import android.os.UserManager; 28 29 import com.android.car.CarServiceRobolectricTestRunner; 30 import com.android.car.testutils.shadow.ShadowActivityManager; 31 import com.android.car.testutils.shadow.ShadowUserHandle; 32 import com.android.car.testutils.shadow.ShadowUserManager; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 43 @RunWith(CarServiceRobolectricTestRunner.class) 44 @Config(shadows = { ShadowActivityManager.class, 45 ShadowUserHandle.class, ShadowUserManager.class}) 46 public class CarUserManagerHelperRoboTest { 47 @Mock 48 private Context mContext; 49 50 private CarUserManagerHelper mHelper; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn( 56 RuntimeEnvironment.application.getSystemService(UserManager.class)); 57 when(mContext.getSystemService(Context.ACTIVITY_SERVICE)).thenReturn( 58 RuntimeEnvironment.application.getSystemService(ActivityManager.class)); 59 when(mContext.getApplicationContext()).thenReturn(mContext); 60 mHelper = new CarUserManagerHelper(mContext); 61 } 62 63 @After tearDown()64 public void tearDown() { 65 ShadowActivityManager.getShadow().reset(); 66 } 67 68 @Test testGetForegroundUserId()69 public void testGetForegroundUserId() { 70 ShadowActivityManager.setCurrentUser(15); 71 assertThat(mHelper.getCurrentForegroundUserId()).isEqualTo(15); 72 } 73 74 @Test testGetForegroundUserInfo()75 public void testGetForegroundUserInfo() { 76 int currentForegroundUserId = 17; 77 ShadowActivityManager.setCurrentUser(currentForegroundUserId); 78 79 assertThat(mHelper.getCurrentForegroundUserInfo().id).isEqualTo(currentForegroundUserId); 80 } 81 82 @Test testGetCurrentProcessUserId()83 public void testGetCurrentProcessUserId() { 84 int currentProcessUserId = 11; 85 ShadowUserManager.getShadow().setCurrentUser(currentProcessUserId); 86 87 assertThat(mHelper.getCurrentProcessUserId()).isEqualTo(currentProcessUserId); 88 } 89 90 @Test testGetCurrentProcessUserInfo()91 public void testGetCurrentProcessUserInfo() { 92 int currentProcessUserId = 12; 93 ShadowUserManager.getShadow().setCurrentUser(currentProcessUserId); 94 assertThat(mHelper.getCurrentProcessUserInfo().id).isEqualTo(currentProcessUserId); 95 } 96 97 @Test testGetAllUsers()98 public void testGetAllUsers() { 99 int currentProcessUserId = 12; 100 ShadowUserManager userManager = ShadowUserManager.getShadow(); 101 userManager.setCurrentUser(currentProcessUserId); 102 103 UserInfo currentProcessUser = createUserInfoForId(currentProcessUserId); 104 UserInfo systemUser = createUserInfoForId(0); 105 106 UserInfo otherUser1 = createUserInfoForId(13); 107 UserInfo otherUser2 = createUserInfoForId(14); 108 109 userManager.addUserInfo(systemUser); 110 userManager.addUserInfo(currentProcessUser); 111 userManager.addUserInfo(otherUser1); 112 userManager.addUserInfo(otherUser2); 113 114 if (mHelper.isHeadlessSystemUser()) { 115 // Should return 3 users that don't have system user id. 116 assertThat(mHelper.getAllUsers()) 117 .containsExactly(currentProcessUser, otherUser1, otherUser2); 118 } else { 119 assertThat(mHelper.getAllUsers()) 120 .containsExactly(systemUser, currentProcessUser, otherUser1, otherUser2); 121 } 122 } 123 124 @Test testGetAllUsersExceptForegroundUser()125 public void testGetAllUsersExceptForegroundUser() { 126 ShadowActivityManager.setCurrentUser(11); 127 ShadowUserManager userManager = ShadowUserManager.getShadow(); 128 129 UserInfo foregroundUser = createUserInfoForId(11); 130 UserInfo otherUser1 = createUserInfoForId(12); 131 UserInfo otherUser2 = createUserInfoForId(13); 132 UserInfo otherUser3 = createUserInfoForId(14); 133 134 userManager.addUserInfo(foregroundUser); 135 userManager.addUserInfo(otherUser1); 136 userManager.addUserInfo(otherUser2); 137 userManager.addUserInfo(otherUser3); 138 139 // Should return 3 users that don't have foregroundUser id. 140 assertThat(mHelper.getAllSwitchableUsers()).hasSize(3); 141 assertThat(mHelper.getAllSwitchableUsers()) 142 .containsExactly(otherUser1, otherUser2, otherUser3); 143 } 144 145 @Test testCheckForegroundUser()146 public void testCheckForegroundUser() { 147 ShadowActivityManager.setCurrentUser(10); 148 assertThat(mHelper.isForegroundUser(createUserInfoForId(10))).isTrue(); 149 assertThat(mHelper.isForegroundUser(createUserInfoForId(11))).isFalse(); 150 151 ShadowActivityManager.setCurrentUser(11); 152 assertThat(mHelper.isForegroundUser(createUserInfoForId(11))).isTrue(); 153 } 154 155 @Test testIsUserRunningCurrentProcess()156 public void testIsUserRunningCurrentProcess() { 157 ShadowUserManager shadowUserManager = ShadowUserManager.getShadow(); 158 UserInfo user1 = createUserInfoForId(10); 159 UserInfo user2 = createUserInfoForId(11); 160 shadowUserManager.addUserInfo(user1); 161 shadowUserManager.addUserInfo(user2); 162 shadowUserManager.setCurrentUser(10); 163 164 assertThat(mHelper.isCurrentProcessUser(user1)).isTrue(); 165 assertThat(mHelper.isCurrentProcessUser(user2)).isFalse(); 166 167 shadowUserManager.setCurrentUser(11); 168 assertThat(mHelper.isCurrentProcessUser(user2)).isTrue(); 169 assertThat(mHelper.isCurrentProcessUser(user1)).isFalse(); 170 } 171 172 @Test testRemoveCurrentProcessUserSwitchesToGuestUser()173 public void testRemoveCurrentProcessUserSwitchesToGuestUser() { 174 // Set currentProcess user to be user 10. 175 ShadowUserManager shadowUserManager = ShadowUserManager.getShadow(); 176 UserInfo user1 = createUserInfoForId(10); 177 UserInfo user2 = createUserInfoForId(11); 178 shadowUserManager.addUserInfo(user1); 179 shadowUserManager.addUserInfo(user2); 180 shadowUserManager.setCurrentUser(10); 181 182 // Removing a currentProcess user, calls "switch" to guest user 183 mHelper.removeUser(user1, "testGuest"); 184 assertThat(ShadowActivityManager.getShadow().getSwitchUserCalled()).isTrue(); 185 assertThat(ShadowActivityManager.getShadow().getUserSwitchedTo()).isEqualTo(0); 186 187 assertThat(shadowUserManager.removeUser(10)).isTrue(); 188 } 189 190 @Test testSwitchToUser()191 public void testSwitchToUser() { 192 ShadowActivityManager.setCurrentUser(20); 193 194 // Switching to foreground user doesn't do anything. 195 mHelper.switchToUser(createUserInfoForId(20)); 196 assertThat(ShadowActivityManager.getShadow().getSwitchUserCalled()).isFalse(); 197 198 // Switching to non-current, non-guest user, simply calls switchUser. 199 UserInfo userToSwitchTo = new UserInfo(22, "Test User", 0); 200 mHelper.switchToUser(userToSwitchTo); 201 assertThat(ShadowActivityManager.getShadow().getSwitchUserCalled()).isTrue(); 202 assertThat(ShadowActivityManager.getShadow().getUserSwitchedTo()).isEqualTo(22); 203 } 204 createUserInfoForId(int id)205 private UserInfo createUserInfoForId(int id) { 206 UserInfo userInfo = new UserInfo(); 207 userInfo.id = id; 208 return userInfo; 209 } 210 } 211