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