blob: 026754c00da81c837ca108a9e9a2180a58de23e6 [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 Jeong0120c9b2020-05-04 11:55:40 -070018import static com.google.common.truth.Truth.assertThat;
19
20import static org.mockito.ArgumentMatchers.anyBoolean;
21import static org.mockito.ArgumentMatchers.eq;
Eric Jeong88fd0292020-05-04 18:22:16 -070022import static org.mockito.Mockito.when;
Eric Jeong0120c9b2020-05-04 11:55:40 -070023
Eric Jeong88fd0292020-05-04 18:22:16 -070024import android.annotation.UserIdInt;
Eric Jeong0120c9b2020-05-04 11:55:40 -070025import android.car.Car;
26import android.car.ICarUserService;
27import android.car.test.mocks.AbstractExtendedMockitoTestCase;
28import android.car.test.util.UserTestingHelper;
29import android.car.user.CarUserManager;
30import android.car.user.ExperimentalCarUserManager;
31import android.content.pm.UserInfo;
32import android.os.UserHandle;
33import android.os.UserManager;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.mockito.Mock;
38
39import java.util.Arrays;
40import java.util.List;
41
42public 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 Jeong88fd0292020-05-04 18:22:16 -070060 expectCreateDriverSucceed(10);
Eric Jeong0120c9b2020-05-04 11:55:40 -070061 int userId = mManager.createDriver("test driver", true);
Eric Jeong88fd0292020-05-04 18:22:16 -070062 assertThat(userId).isEqualTo(10);
Eric Jeong0120c9b2020-05-04 11:55:40 -070063 }
64
65 @Test
66 public void testCreateDriver_Success_NonAdmin() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -070067 expectCreateDriverSucceed(10);
Eric Jeong0120c9b2020-05-04 11:55:40 -070068 int userId = mManager.createDriver("test driver", false);
Eric Jeong88fd0292020-05-04 18:22:16 -070069 assertThat(userId).isEqualTo(10);
Eric Jeong0120c9b2020-05-04 11:55:40 -070070 }
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 Jeong88fd0292020-05-04 18:22:16 -0700110 when(mService.getAllDrivers()).thenReturn(userInfos);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700111 List<Integer> drivers = mManager.getAllDrivers();
Eric Jeong88fd0292020-05-04 18:22:16 -0700112 assertThat(drivers).containsExactly(10, 20, 30);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700113 }
114
115 @Test
116 public void testGetAllPassengers() throws Exception {
117 List<UserInfo> userInfos = UserTestingHelper.newUsers(100, 101, 102);
Eric Jeong88fd0292020-05-04 18:22:16 -0700118 when(mService.getPassengers(10)).thenReturn(userInfos);
119 when(mService.getPassengers(20)).thenReturn(Arrays.asList());
Eric Jeong0120c9b2020-05-04 11:55:40 -0700120
121 List<Integer> passengers = mManager.getPassengers(10);
Eric Jeong88fd0292020-05-04 18:22:16 -0700122 assertThat(passengers).containsExactly(100, 101, 102);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700123
124 passengers = mManager.getPassengers(20);
Eric Jeong88fd0292020-05-04 18:22:16 -0700125 assertThat(passengers).isEmpty();
Eric Jeong0120c9b2020-05-04 11:55:40 -0700126 }
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 Jeong88fd0292020-05-04 18:22:16 -0700156 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 Jeong0120c9b2020-05-04 11:55:40 -0700159 }
160
161 private void expectCreateDriverFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700162 when(mService.createDriver(eq("test driver"), anyBoolean())).thenReturn(null);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700163 }
164
165 private void expectCreatePassengerSucceed() throws Exception {
166 UserInfo userInfo = UserTestingHelper.newUser(100);
Eric Jeong88fd0292020-05-04 18:22:16 -0700167 when(mService.createPassenger("test passenger", /* driverId = */ 10)).thenReturn(userInfo);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700168 }
169
170 private void expectCreatePassengerFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700171 when(mService.createPassenger("test passenger", /* driverId = */ 10)).thenReturn(null);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700172 }
173
174 private void expectSwitchDriverSucceed() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700175 when(mService.switchDriver(10)).thenReturn(true);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700176 }
177
178 private void expectSwitchDriverFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700179 when(mService.switchDriver(20)).thenReturn(false);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700180 }
181
182 private void expectStartPassengerSucceed() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700183 when(mService.startPassenger(100, /* zoneId = */ 1)).thenReturn(true);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700184 }
185
186 private void expectStartPassengerFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700187 when(mService.startPassenger(200, /* zoneId = */ 1)).thenReturn(false);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700188 }
189
190 private void expectStopPassengerSucceed() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700191 when(mService.stopPassenger(100)).thenReturn(true);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700192 }
193
194 private void expectStopPassengerFail() throws Exception {
Eric Jeong88fd0292020-05-04 18:22:16 -0700195 when(mService.stopPassenger(200)).thenReturn(false);
Eric Jeong0120c9b2020-05-04 11:55:40 -0700196 }
197}