blob: 8b4dc55bef6bf8ce6d7417c17f8a68308ca89d95 [file] [log] [blame]
Eric Jeong0120c9b2020-05-04 11:55:40 -07001/*
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 */
16package com.android.car.user;
17
Eric Jeong25666cf2020-05-14 15:16:27 -070018import static android.car.test.mocks.AndroidMockitoHelper.getResult;
19
Eric Jeong0120c9b2020-05-04 11:55:40 -070020import static com.google.common.truth.Truth.assertThat;
21
22import static org.mockito.ArgumentMatchers.anyBoolean;
23import static org.mockito.ArgumentMatchers.eq;
Eric Jeong25666cf2020-05-14 15:16:27 -070024import static org.mockito.ArgumentMatchers.notNull;
25import static org.mockito.Mockito.doAnswer;
26import static org.mockito.Mockito.doThrow;
Eric Jeong88fd0292020-05-04 18:22:16 -070027import static org.mockito.Mockito.when;
Eric Jeong0120c9b2020-05-04 11:55:40 -070028
Eric Jeong88fd0292020-05-04 18:22:16 -070029import android.annotation.UserIdInt;
Eric Jeong0120c9b2020-05-04 11:55:40 -070030import android.car.Car;
31import android.car.ICarUserService;
32import android.car.test.mocks.AbstractExtendedMockitoTestCase;
33import android.car.test.util.UserTestingHelper;
34import android.car.user.CarUserManager;
35import android.car.user.ExperimentalCarUserManager;
Eric Jeong25666cf2020-05-14 15:16:27 -070036import android.car.user.UserSwitchResult;
Eric Jeong0120c9b2020-05-04 11:55:40 -070037import android.content.pm.UserInfo;
Eric Jeong25666cf2020-05-14 15:16:27 -070038import android.os.RemoteException;
Eric Jeong0120c9b2020-05-04 11:55:40 -070039import android.os.UserHandle;
40import android.os.UserManager;
41
Eric Jeong25666cf2020-05-14 15:16:27 -070042import com.android.internal.infra.AndroidFuture;
43
Eric Jeong0120c9b2020-05-04 11:55:40 -070044import org.junit.Before;
45import org.junit.Test;
46import org.mockito.Mock;
47
48import java.util.Arrays;
49import java.util.List;
50
51public 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 Jeong88fd0292020-05-04 18:22:16 -070069 expectCreateDriverSucceed(10);
Eric Jeong0120c9b2020-05-04 11:55:40 -070070 int userId = mManager.createDriver("test driver", true);
Eric Jeong88fd0292020-05-04 18:22:16 -070071 assertThat(userId).isEqualTo(10);
Eric Jeong0120c9b2020-05-04 11:55:40 -070072 }
73
74 @Test
75 public void testCreateDriver_Success_NonAdmin() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -070076 expectCreateDriverSucceed(10);
Eric Jeong0120c9b2020-05-04 11:55:40 -070077 int userId = mManager.createDriver("test driver", false);
Eric Jeong88fd0292020-05-04 18:22:16 -070078 assertThat(userId).isEqualTo(10);
Eric Jeong0120c9b2020-05-04 11:55:40 -070079 }
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 Jeong25666cf2020-05-14 15:16:27 -0700104 expectSwitchDriverSucceed(10);
105 AndroidFuture<UserSwitchResult> future = mManager.switchDriver(10);
106 UserSwitchResult result = getResult(future);
107 assertThat(result.getStatus()).isEqualTo(UserSwitchResult.STATUS_SUCCESSFUL);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700108 }
109
110 @Test
111 public void testSwitchDriver_Error() throws Exception {
Eric Jeong25666cf2020-05-14 15:16:27 -0700112 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 Jeong0120c9b2020-05-04 11:55:40 -0700117 }
118
119 @Test
120 public void testGetAllDrivers() throws Exception {
121 List<UserInfo> userInfos = UserTestingHelper.newUsers(10, 20, 30);
Eric Jeong88fd0292020-05-04 18:22:16 -0700122 when(mService.getAllDrivers()).thenReturn(userInfos);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700123 List<Integer> drivers = mManager.getAllDrivers();
Eric Jeong88fd0292020-05-04 18:22:16 -0700124 assertThat(drivers).containsExactly(10, 20, 30);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700125 }
126
127 @Test
128 public void testGetAllPassengers() throws Exception {
129 List<UserInfo> userInfos = UserTestingHelper.newUsers(100, 101, 102);
Eric Jeong88fd0292020-05-04 18:22:16 -0700130 when(mService.getPassengers(10)).thenReturn(userInfos);
131 when(mService.getPassengers(20)).thenReturn(Arrays.asList());
Eric Jeong0120c9b2020-05-04 11:55:40 -0700132
133 List<Integer> passengers = mManager.getPassengers(10);
Eric Jeong88fd0292020-05-04 18:22:16 -0700134 assertThat(passengers).containsExactly(100, 101, 102);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700135
136 passengers = mManager.getPassengers(20);
Eric Jeong88fd0292020-05-04 18:22:16 -0700137 assertThat(passengers).isEmpty();
Eric Jeong0120c9b2020-05-04 11:55:40 -0700138 }
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 Jeong88fd0292020-05-04 18:22:16 -0700168 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 Jeong0120c9b2020-05-04 11:55:40 -0700171 }
172
173 private void expectCreateDriverFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700174 when(mService.createDriver(eq("test driver"), anyBoolean())).thenReturn(null);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700175 }
176
177 private void expectCreatePassengerSucceed() throws Exception {
178 UserInfo userInfo = UserTestingHelper.newUser(100);
Eric Jeong88fd0292020-05-04 18:22:16 -0700179 when(mService.createPassenger("test passenger", /* driverId = */ 10)).thenReturn(userInfo);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700180 }
181
182 private void expectCreatePassengerFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700183 when(mService.createPassenger("test passenger", /* driverId = */ 10)).thenReturn(null);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700184 }
185
Eric Jeong25666cf2020-05-14 15:16:27 -0700186 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 Jeong0120c9b2020-05-04 11:55:40 -0700194 }
195
Eric Jeong25666cf2020-05-14 15:16:27 -0700196 private void expectSwitchDriverFail(@UserIdInt int userId) throws Exception {
197 doThrow(new RemoteException("D'OH!")).when(mService)
198 .switchDriver(eq(userId), notNull());
Eric Jeong0120c9b2020-05-04 11:55:40 -0700199 }
200
201 private void expectStartPassengerSucceed() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700202 when(mService.startPassenger(100, /* zoneId = */ 1)).thenReturn(true);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700203 }
204
205 private void expectStartPassengerFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700206 when(mService.startPassenger(200, /* zoneId = */ 1)).thenReturn(false);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700207 }
208
209 private void expectStopPassengerSucceed() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700210 when(mService.stopPassenger(100)).thenReturn(true);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700211 }
212
213 private void expectStopPassengerFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700214 when(mService.stopPassenger(200)).thenReturn(false);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700215 }
216}