Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | package com.android.car.user; |
| 17 | |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 18 | import static android.car.test.mocks.AndroidMockitoHelper.getResult; |
| 19 | |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 20 | import static com.google.common.truth.Truth.assertThat; |
| 21 | |
| 22 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 24 | import static org.mockito.ArgumentMatchers.notNull; |
| 25 | import static org.mockito.Mockito.doAnswer; |
| 26 | import static org.mockito.Mockito.doThrow; |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 27 | import static org.mockito.Mockito.when; |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 28 | |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 29 | import android.annotation.UserIdInt; |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 30 | import android.car.Car; |
| 31 | import android.car.ICarUserService; |
| 32 | import android.car.test.mocks.AbstractExtendedMockitoTestCase; |
| 33 | import android.car.test.util.UserTestingHelper; |
| 34 | import android.car.user.CarUserManager; |
| 35 | import android.car.user.ExperimentalCarUserManager; |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 36 | import android.car.user.UserSwitchResult; |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 37 | import android.content.pm.UserInfo; |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 38 | import android.os.RemoteException; |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
| 40 | import android.os.UserManager; |
| 41 | |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 42 | import com.android.internal.infra.AndroidFuture; |
| 43 | |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.mockito.Mock; |
| 47 | |
| 48 | import java.util.Arrays; |
| 49 | import java.util.List; |
| 50 | |
| 51 | public final class ExperimentalCarUserManagerUnitTest extends AbstractExtendedMockitoTestCase { |
| 52 | |
| 53 | @Mock |
| 54 | private Car mCar; |
| 55 | @Mock |
| 56 | private UserManager mUserManager; |
| 57 | @Mock |
| 58 | private ICarUserService mService; |
| 59 | |
| 60 | private ExperimentalCarUserManager mManager; |
| 61 | |
| 62 | @Before public void setFixtures() { |
| 63 | mManager = |
| 64 | ExperimentalCarUserManager.from(new CarUserManager(mCar, mService, mUserManager)); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testCreateDriver_Success_Admin() throws Exception { |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 69 | expectCreateDriverSucceed(10); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 70 | int userId = mManager.createDriver("test driver", true); |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 71 | assertThat(userId).isEqualTo(10); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testCreateDriver_Success_NonAdmin() throws Exception { |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 76 | expectCreateDriverSucceed(10); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 77 | int userId = mManager.createDriver("test driver", false); |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 78 | assertThat(userId).isEqualTo(10); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testCreateDriver_Error() throws Exception { |
| 83 | expectCreateDriverFail(); |
| 84 | int userId = mManager.createDriver("test driver", false); |
| 85 | assertThat(userId).isEqualTo(UserHandle.USER_NULL); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testCreatePassenger_Success() throws Exception { |
| 90 | expectCreatePassengerSucceed(); |
| 91 | int userId = mManager.createPassenger("test passenger", 10); |
| 92 | assertThat(userId).isNotEqualTo(UserHandle.USER_NULL); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testCreatePassenger_Error() throws Exception { |
| 97 | expectCreatePassengerFail(); |
| 98 | int userId = mManager.createPassenger("test passenger", 20); |
| 99 | assertThat(userId).isEqualTo(UserHandle.USER_NULL); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testSwitchDriver_Success() throws Exception { |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 104 | expectSwitchDriverSucceed(10); |
| 105 | AndroidFuture<UserSwitchResult> future = mManager.switchDriver(10); |
| 106 | UserSwitchResult result = getResult(future); |
| 107 | assertThat(result.getStatus()).isEqualTo(UserSwitchResult.STATUS_SUCCESSFUL); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testSwitchDriver_Error() throws Exception { |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 112 | expectSwitchDriverFail(20); |
| 113 | AndroidFuture<UserSwitchResult> future = mManager.switchDriver(20); |
| 114 | assertThat(future).isNotNull(); |
| 115 | UserSwitchResult result = getResult(future); |
| 116 | assertThat(result.getStatus()).isEqualTo(UserSwitchResult.STATUS_HAL_INTERNAL_FAILURE); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testGetAllDrivers() throws Exception { |
| 121 | List<UserInfo> userInfos = UserTestingHelper.newUsers(10, 20, 30); |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 122 | when(mService.getAllDrivers()).thenReturn(userInfos); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 123 | List<Integer> drivers = mManager.getAllDrivers(); |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 124 | assertThat(drivers).containsExactly(10, 20, 30); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testGetAllPassengers() throws Exception { |
| 129 | List<UserInfo> userInfos = UserTestingHelper.newUsers(100, 101, 102); |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 130 | when(mService.getPassengers(10)).thenReturn(userInfos); |
| 131 | when(mService.getPassengers(20)).thenReturn(Arrays.asList()); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 132 | |
| 133 | List<Integer> passengers = mManager.getPassengers(10); |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 134 | assertThat(passengers).containsExactly(100, 101, 102); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 135 | |
| 136 | passengers = mManager.getPassengers(20); |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 137 | assertThat(passengers).isEmpty(); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testStartPassenger_Success() throws Exception { |
| 142 | expectStartPassengerSucceed(); |
| 143 | boolean success = mManager.startPassenger(100, /* zoneId = */ 1); |
| 144 | assertThat(success).isTrue(); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testStartPassenger_Error() throws Exception { |
| 149 | expectStartPassengerFail(); |
| 150 | boolean success = mManager.startPassenger(200, /* zoneId = */ 1); |
| 151 | assertThat(success).isFalse(); |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testStopPassenger_Success() throws Exception { |
| 156 | expectStopPassengerSucceed(); |
| 157 | boolean success = mManager.stopPassenger(100); |
| 158 | assertThat(success).isTrue(); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testStopPassenger_Error() throws Exception { |
| 163 | expectStopPassengerFail(); |
| 164 | boolean success = mManager.stopPassenger(200); |
| 165 | assertThat(success).isFalse(); |
| 166 | } |
| 167 | |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 168 | private void expectCreateDriverSucceed(@UserIdInt int userId) throws Exception { |
| 169 | UserInfo userInfo = UserTestingHelper.newUser(userId); |
| 170 | when(mService.createDriver(eq("test driver"), anyBoolean())).thenReturn(userInfo); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | private void expectCreateDriverFail() throws Exception { |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 174 | when(mService.createDriver(eq("test driver"), anyBoolean())).thenReturn(null); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private void expectCreatePassengerSucceed() throws Exception { |
| 178 | UserInfo userInfo = UserTestingHelper.newUser(100); |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 179 | when(mService.createPassenger("test passenger", /* driverId = */ 10)).thenReturn(userInfo); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private void expectCreatePassengerFail() throws Exception { |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 183 | when(mService.createPassenger("test passenger", /* driverId = */ 10)).thenReturn(null); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 186 | private void expectSwitchDriverSucceed(@UserIdInt int userId) throws Exception { |
| 187 | doAnswer((invocation) -> { |
| 188 | @SuppressWarnings("unchecked") |
| 189 | AndroidFuture<UserSwitchResult> future = (AndroidFuture<UserSwitchResult>) invocation |
| 190 | .getArguments()[1]; |
| 191 | future.complete(new UserSwitchResult(UserSwitchResult.STATUS_SUCCESSFUL, null)); |
| 192 | return null; |
| 193 | }).when(mService).switchDriver(eq(userId), notNull()); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Eric Jeong | 25666cf | 2020-05-14 15:16:27 -0700 | [diff] [blame^] | 196 | private void expectSwitchDriverFail(@UserIdInt int userId) throws Exception { |
| 197 | doThrow(new RemoteException("D'OH!")).when(mService) |
| 198 | .switchDriver(eq(userId), notNull()); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | private void expectStartPassengerSucceed() throws Exception { |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 202 | when(mService.startPassenger(100, /* zoneId = */ 1)).thenReturn(true); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private void expectStartPassengerFail() throws Exception { |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 206 | when(mService.startPassenger(200, /* zoneId = */ 1)).thenReturn(false); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | private void expectStopPassengerSucceed() throws Exception { |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 210 | when(mService.stopPassenger(100)).thenReturn(true); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | private void expectStopPassengerFail() throws Exception { |
Eric Jeong | 88fd029 | 2020-05-04 18:22:16 -0700 | [diff] [blame] | 214 | when(mService.stopPassenger(200)).thenReturn(false); |
Eric Jeong | 0120c9b | 2020-05-04 11:55:40 -0700 | [diff] [blame] | 215 | } |
| 216 | } |