blob: 6f71d3de0e2456537acf565b2de3924b665afdbe [file] [log] [blame]
Pavel Maltsev15d5ba32019-01-18 15:28:50 -08001/*
2 * Copyright (C) 2019 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
17package android.car.testapi;
18
19import android.car.Car;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -080020import android.car.ICar;
21import android.car.ICarBluetooth;
22import android.car.cluster.IInstrumentClusterManagerService;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -080023import android.car.content.pm.ICarPackageManager;
24import android.car.diagnostic.ICarDiagnostic;
25import android.car.drivingstate.ICarDrivingState;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -080026import android.car.hardware.power.ICarPower;
27import android.car.media.ICarAudio;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -080028import android.car.storagemonitoring.ICarStorageMonitoring;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -080029import android.content.Context;
30import android.os.IBinder;
31import android.os.RemoteException;
32import android.util.Log;
33
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36
Keun young Park9a91efb2019-11-15 18:10:47 -080037import java.util.Collections;
38import java.util.List;
39
Pavel Maltsev15d5ba32019-01-18 15:28:50 -080040/*
41 The idea behind this class is that we can fake-out interfaces between Car*Manager and
42 Car Service. Effectively creating a fake version of Car Service that can run under Robolectric
43 environment (thus running on the desktop rather than on a real device).
44
45 By default all interfaces are mocked out just to allow dummy implementation and avoid crashes.
46 This will allow production code to call into Car*Manager w/o crashes because managers will just
47 pass the call into mocked version of the interface. However, in many cases
48 developers would like to have more sophisticated test cases and ability to simulate vehicle as
49 they need. In this case mocked version of particular service needs to be replaced with the fake
50 one which will have fake implementation to satisfy test needs and additional interface needs
51 to be exposed to the app developers such that they can simulate fake car behavior, this
52 interface has -Controller suffix and defined as inner interface in this class.
53 */
54
55/**
56 * Test API to get Car Managers backed by fake car service.
57 *
58 * <p>In order to use it in your tests you should create Car object by calling static method
59 * {@link FakeCar#createFakeCar(Context)}. It will effectively create {@link FakeCar} object and
60 * you can get access to {@link Car} by calling {@link FakeCar#getCar()}. Also, {@code FakeCar}
61 * provides additional testing API that will allow you to simulate vehicle's behavior as you need.
62 *
63 * <p>Here's an example of usage:
64 * <code>
65 * FakeCar fakeCar = FakeCar.createFakeCar(appContext);
66 * Car realCar = fakeCar.getCar(); // pass this instance to your DI framework or class to test
67 *
68 * // Then you can obtain different controllers to modify behavior of your fake car.
69 * PropertyController propertyController = fakeCar.getPropertyController();
70 * propertyController.setProperties(listOfSupportedProperties)
71 * </code>
72 */
73public class FakeCar {
74 private static final String TAG = FakeCar.class.getSimpleName();
75
76 private final Car mCar;
77 private final FakeCarService mService;
78
79 /** Creates an instance of {@link FakeCar} */
80 public static FakeCar createFakeCar(Context context) {
81 FakeCarService service = new FakeCarService(context);
82 Car car = new Car(context, service, null);
83
84 return new FakeCar(car, service);
85 }
86
87 private FakeCar(Car car, FakeCarService service) {
88 mCar = car;
89 mService = service;
90 }
91
92 /** Returns Car object which is backed by fake implementation. */
93 public Car getCar() {
94 return mCar;
95 }
96
97 /** Returns test controller to modify car properties */
98 public CarPropertyController getCarPropertyController() {
99 return mService.mCarProperty;
100 }
101
102 /** Returns test controller to change behavior of {@link android.car.CarProjectionManager} */
103 public CarProjectionController getCarProjectionController() {
104 return mService.mCarProjection;
105 }
106
tadvana3b9ed202019-07-17 19:21:28 -0700107 /**
108 * Returns the test controller to change the behavior of the underlying
109 * {@link android.car.CarAppFocusManager}
110 */
111 public CarAppFocusController getAppFocusController() {
112 return mService.mAppFocus;
113 }
114
tadvana409bd1c2019-08-30 18:23:04 -0700115 /**
116 * Returns the test controller to change the behavior of as well as query the underlying {@link
117 * android.car.navigation.CarNavigationStatusManager}.
118 */
119 public CarNavigationStatusController getCarNavigationStatusController() {
120 return mService.mInstrumentClusterNavigation;
121 }
122
tadvana69358152019-11-20 14:36:51 -0800123 /**
124 * Returns a test controller that can modify and query the underlying service for the {@link
125 * android.car.drivingstate.CarUxRestrictionsManager}.
126 */
127 public CarUxRestrictionsController getCarUxRestrictionController() {
128 return mService.mCarUxRestrictionService;
129 }
130
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800131 private static class FakeCarService extends ICar.Stub {
132 @Mock ICarAudio.Stub mCarAudio;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800133 @Mock ICarPackageManager.Stub mCarPackageManager;
134 @Mock ICarDiagnostic.Stub mCarDiagnostic;
135 @Mock ICarPower.Stub mCarPower;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800136 @Mock IInstrumentClusterManagerService.Stub mClusterService;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800137 @Mock ICarBluetooth.Stub mCarBluetooth;
138 @Mock ICarStorageMonitoring.Stub mCarStorageMonitoring;
139 @Mock ICarDrivingState.Stub mCarDrivingState;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800140
tadvana3b9ed202019-07-17 19:21:28 -0700141 private final FakeAppFocusService mAppFocus;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800142 private final FakeCarPropertyService mCarProperty;
143 private final FakeCarProjectionService mCarProjection;
tadvana409bd1c2019-08-30 18:23:04 -0700144 private final FakeInstrumentClusterNavigation mInstrumentClusterNavigation;
tadvana69358152019-11-20 14:36:51 -0800145 private final FakeCarUxRestrictionsService mCarUxRestrictionService;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800146
147 FakeCarService(Context context) {
148 MockitoAnnotations.initMocks(this);
tadvana3b9ed202019-07-17 19:21:28 -0700149 mAppFocus = new FakeAppFocusService(context);
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800150 mCarProperty = new FakeCarPropertyService();
151 mCarProjection = new FakeCarProjectionService(context);
tadvana409bd1c2019-08-30 18:23:04 -0700152 mInstrumentClusterNavigation = new FakeInstrumentClusterNavigation();
tadvana69358152019-11-20 14:36:51 -0800153 mCarUxRestrictionService = new FakeCarUxRestrictionsService();
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800154 }
155
156 @Override
157 public void setCarServiceHelper(IBinder helper) throws RemoteException {
158 // Nothing to do yet.
159 }
160
161 @Override
Felipe Lemee93218b2020-02-13 14:10:52 -0800162 public void onUserLifecycleEvent(int eventType, long timestampMs, int fromUserId,
163 int toUserId) {
164 // Nothing to do yet.
165 }
166
167 @Override
Felipe Leme30b4e1e2020-02-25 10:26:07 -0800168 public void onFirstUserUnlocked(int userId, long timestampMs, long duration) {
169 // Nothing to do yet.
170 }
171
172 @Override
Felipe Lemee2600fc2020-02-26 11:06:04 -0800173 public void getInitialUserInfo(int requestType, int timeoutMs, IBinder binder) {
174 // Nothing to do yet.
175 }
176
177 @Override
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800178 public IBinder getCarService(String serviceName) throws RemoteException {
179 switch (serviceName) {
180 case Car.AUDIO_SERVICE:
181 return mCarAudio;
182 case Car.APP_FOCUS_SERVICE:
183 return mAppFocus;
184 case Car.PACKAGE_SERVICE:
185 return mCarPackageManager;
186 case Car.DIAGNOSTIC_SERVICE:
187 return mCarDiagnostic;
188 case Car.POWER_SERVICE:
189 return mCarPower;
190 case Car.CABIN_SERVICE:
191 case Car.HVAC_SERVICE:
192 case Car.INFO_SERVICE:
193 case Car.PROPERTY_SERVICE:
194 case Car.SENSOR_SERVICE:
195 case Car.VENDOR_EXTENSION_SERVICE:
196 return mCarProperty;
197 case Car.CAR_NAVIGATION_SERVICE:
tadvana409bd1c2019-08-30 18:23:04 -0700198 return mInstrumentClusterNavigation;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800199 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
200 return mClusterService;
201 case Car.PROJECTION_SERVICE:
202 return mCarProjection;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800203 case Car.BLUETOOTH_SERVICE:
204 return mCarBluetooth;
205 case Car.STORAGE_MONITORING_SERVICE:
206 return mCarStorageMonitoring;
207 case Car.CAR_DRIVING_STATE_SERVICE:
208 return mCarDrivingState;
209 case Car.CAR_UX_RESTRICTION_SERVICE:
tadvana69358152019-11-20 14:36:51 -0800210 return mCarUxRestrictionService;
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800211 default:
212 Log.w(TAG, "getCarService for unknown service:" + serviceName);
213 return null;
214 }
215 }
216
217 @Override
218 public int getCarConnectionType() throws RemoteException {
219 return Car.CONNECTION_TYPE_EMBEDDED;
220 }
Keun young Park9a91efb2019-11-15 18:10:47 -0800221
222 @Override
223 public boolean isFeatureEnabled(String featureName) {
224 return false;
225 }
226
227 @Override
228 public int enableFeature(String featureName) {
229 return Car.FEATURE_REQUEST_SUCCESS;
230 }
231
232 @Override
233 public int disableFeature(String featureName) {
234 return Car.FEATURE_REQUEST_SUCCESS;
235 }
236
237 @Override
238 public List<String> getAllEnabledFeatures() {
239 return Collections.emptyList();
240 }
241
242 @Override
243 public List<String> getAllPendingDisabledFeatures() {
244 return Collections.emptyList();
245 }
246
247 @Override
248 public List<String> getAllPendingEnabledFeatures() {
249 return Collections.emptyList();
250 }
251
252 @Override
253 public String getCarManagerClassForFeature(String featureName) {
254 return null;
255 }
Pavel Maltsev15d5ba32019-01-18 15:28:50 -0800256 }
257
258}