Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.car.testapi; |
| 18 | |
| 19 | import android.car.Car; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 20 | import android.car.ICar; |
| 21 | import android.car.ICarBluetooth; |
| 22 | import android.car.cluster.IInstrumentClusterManagerService; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 23 | import android.car.content.pm.ICarPackageManager; |
| 24 | import android.car.diagnostic.ICarDiagnostic; |
| 25 | import android.car.drivingstate.ICarDrivingState; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 26 | import android.car.hardware.power.ICarPower; |
| 27 | import android.car.media.ICarAudio; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 28 | import android.car.storagemonitoring.ICarStorageMonitoring; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.os.IBinder; |
| 31 | import android.os.RemoteException; |
| 32 | import android.util.Log; |
| 33 | |
| 34 | import org.mockito.Mock; |
| 35 | import org.mockito.MockitoAnnotations; |
| 36 | |
Keun young Park | 9a91efb | 2019-11-15 18:10:47 -0800 | [diff] [blame] | 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 40 | /* |
| 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 | */ |
| 73 | public 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 | |
tadvana | 3b9ed20 | 2019-07-17 19:21:28 -0700 | [diff] [blame] | 107 | /** |
| 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 | |
tadvana | 409bd1c | 2019-08-30 18:23:04 -0700 | [diff] [blame] | 115 | /** |
| 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 | |
tadvana | 6935815 | 2019-11-20 14:36:51 -0800 | [diff] [blame] | 123 | /** |
| 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 Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 131 | private static class FakeCarService extends ICar.Stub { |
| 132 | @Mock ICarAudio.Stub mCarAudio; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 133 | @Mock ICarPackageManager.Stub mCarPackageManager; |
| 134 | @Mock ICarDiagnostic.Stub mCarDiagnostic; |
| 135 | @Mock ICarPower.Stub mCarPower; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 136 | @Mock IInstrumentClusterManagerService.Stub mClusterService; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 137 | @Mock ICarBluetooth.Stub mCarBluetooth; |
| 138 | @Mock ICarStorageMonitoring.Stub mCarStorageMonitoring; |
| 139 | @Mock ICarDrivingState.Stub mCarDrivingState; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 140 | |
tadvana | 3b9ed20 | 2019-07-17 19:21:28 -0700 | [diff] [blame] | 141 | private final FakeAppFocusService mAppFocus; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 142 | private final FakeCarPropertyService mCarProperty; |
| 143 | private final FakeCarProjectionService mCarProjection; |
tadvana | 409bd1c | 2019-08-30 18:23:04 -0700 | [diff] [blame] | 144 | private final FakeInstrumentClusterNavigation mInstrumentClusterNavigation; |
tadvana | 6935815 | 2019-11-20 14:36:51 -0800 | [diff] [blame] | 145 | private final FakeCarUxRestrictionsService mCarUxRestrictionService; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 146 | |
| 147 | FakeCarService(Context context) { |
| 148 | MockitoAnnotations.initMocks(this); |
tadvana | 3b9ed20 | 2019-07-17 19:21:28 -0700 | [diff] [blame] | 149 | mAppFocus = new FakeAppFocusService(context); |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 150 | mCarProperty = new FakeCarPropertyService(); |
| 151 | mCarProjection = new FakeCarProjectionService(context); |
tadvana | 409bd1c | 2019-08-30 18:23:04 -0700 | [diff] [blame] | 152 | mInstrumentClusterNavigation = new FakeInstrumentClusterNavigation(); |
tadvana | 6935815 | 2019-11-20 14:36:51 -0800 | [diff] [blame] | 153 | mCarUxRestrictionService = new FakeCarUxRestrictionsService(); |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void setCarServiceHelper(IBinder helper) throws RemoteException { |
| 158 | // Nothing to do yet. |
| 159 | } |
| 160 | |
| 161 | @Override |
Felipe Leme | e93218b | 2020-02-13 14:10:52 -0800 | [diff] [blame] | 162 | public void onUserLifecycleEvent(int eventType, long timestampMs, int fromUserId, |
| 163 | int toUserId) { |
| 164 | // Nothing to do yet. |
| 165 | } |
| 166 | |
| 167 | @Override |
Felipe Leme | 30b4e1e | 2020-02-25 10:26:07 -0800 | [diff] [blame] | 168 | public void onFirstUserUnlocked(int userId, long timestampMs, long duration) { |
| 169 | // Nothing to do yet. |
| 170 | } |
| 171 | |
| 172 | @Override |
Felipe Leme | e2600fc | 2020-02-26 11:06:04 -0800 | [diff] [blame] | 173 | public void getInitialUserInfo(int requestType, int timeoutMs, IBinder binder) { |
| 174 | // Nothing to do yet. |
| 175 | } |
| 176 | |
| 177 | @Override |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 178 | 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: |
tadvana | 409bd1c | 2019-08-30 18:23:04 -0700 | [diff] [blame] | 198 | return mInstrumentClusterNavigation; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 199 | case Car.CAR_INSTRUMENT_CLUSTER_SERVICE: |
| 200 | return mClusterService; |
| 201 | case Car.PROJECTION_SERVICE: |
| 202 | return mCarProjection; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 203 | 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: |
tadvana | 6935815 | 2019-11-20 14:36:51 -0800 | [diff] [blame] | 210 | return mCarUxRestrictionService; |
Pavel Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 211 | 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 Park | 9a91efb | 2019-11-15 18:10:47 -0800 | [diff] [blame] | 221 | |
| 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 Maltsev | 15d5ba3 | 2019-01-18 15:28:50 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | } |