keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 16 | package android.car.test; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 17 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 18 | import android.annotation.SystemApi; |
| 19 | import android.car.CarManagerBase; |
| 20 | import android.os.IBinder; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 21 | import android.os.RemoteException; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 22 | |
| 23 | import com.android.car.vehiclenetwork.IVehicleNetworkHalMock; |
| 24 | import com.android.car.vehiclenetwork.VehicleNetwork.VehicleNetworkHalMock; |
| 25 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs; |
| 26 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 27 | import com.android.car.vehiclenetwork.VehiclePropConfigsParcelable; |
| 28 | import com.android.car.vehiclenetwork.VehiclePropValueParcelable; |
| 29 | import com.android.internal.annotations.GuardedBy; |
| 30 | |
| 31 | import java.lang.ref.WeakReference; |
| 32 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 33 | /** |
| 34 | * API for testing only. Allows mocking vehicle hal. |
| 35 | * @hide |
| 36 | */ |
| 37 | @SystemApi |
Keun-young Park | 4cf6911 | 2016-10-04 10:02:59 -0700 | [diff] [blame] | 38 | public final class CarTestManager implements CarManagerBase { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 39 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Flag for {@link #startMocking(VehicleNetworkHalMock, int)}. |
| 42 | * This is for passing no flag. |
| 43 | */ |
| 44 | public static final int FLAG_MOCKING_NONE = 0x0; |
| 45 | |
| 46 | /** |
| 47 | * Flag for {@link #startMocking(VehicleNetworkHalMock, int)}. |
| 48 | * When this flag is set, shutdown request from mocked vehicle HAL will shutdown the system |
| 49 | * instead of avoiding shutdown, which is the default behavior. |
| 50 | * This can be used to test shutdown flow manually using mocking. |
| 51 | */ |
| 52 | public static final int FLAG_MOCKING_REAL_SHUTDOWN = 0x1; |
| 53 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 54 | private final ICarTest mService; |
| 55 | |
| 56 | @GuardedBy("this") |
| 57 | private VehicleNetworkHalMock mHalMock; |
| 58 | private IVehicleNetworkHalMockImpl mHalMockImpl; |
| 59 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 60 | public CarTestManager(CarTestManagerBinderWrapper wrapper) { |
| 61 | mService = ICarTest.Stub.asInterface(wrapper.binder); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onCarDisconnected() { |
| 66 | // should not happen for embedded |
| 67 | } |
| 68 | |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Inject vehicle prop value event. |
| 71 | * @param value |
| 72 | */ |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 73 | public void injectEvent(VehiclePropValue value) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 74 | try { |
| 75 | mService.injectEvent(new VehiclePropValueParcelable(value)); |
| 76 | } catch (RemoteException e) { |
| 77 | handleRemoteException(e); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 82 | * Check if given property is supported by vehicle hal. |
| 83 | * @param property |
| 84 | * @return |
| 85 | */ |
| 86 | public boolean isPropertySupported(int property) { |
| 87 | try { |
| 88 | return mService.isPropertySupported(property); |
| 89 | } catch (RemoteException e) { |
| 90 | handleRemoteException(e); |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | /** |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 95 | * Start mocking vehicle HAL. It is somewhat strange to re-use interface in lower level |
| 96 | * API, but this is only for testing, and interface is exactly the same. |
| 97 | * @param mock |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 98 | * @flags Combination of FLAG_MOCKING_* |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 99 | */ |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 100 | public void startMocking(VehicleNetworkHalMock mock, int flags) { |
| 101 | IVehicleNetworkHalMockImpl halMockImpl = new IVehicleNetworkHalMockImpl(this); |
| 102 | synchronized (this) { |
| 103 | mHalMock = mock; |
| 104 | mHalMockImpl = halMockImpl; |
| 105 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 106 | try { |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 107 | mService.startMocking(halMockImpl, flags); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 108 | } catch (RemoteException e) { |
| 109 | handleRemoteException(e); |
| 110 | } |
| 111 | } |
| 112 | |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 113 | /** |
| 114 | * Stop previously started mocking. |
| 115 | */ |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 116 | public void stopMocking() { |
| 117 | IVehicleNetworkHalMockImpl halMockImpl; |
| 118 | synchronized (this) { |
| 119 | halMockImpl = mHalMockImpl; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 120 | mHalMock = null; |
| 121 | mHalMockImpl = null; |
| 122 | } |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 123 | try { |
| 124 | mService.stopMocking(halMockImpl); |
| 125 | } catch (RemoteException e) { |
| 126 | handleRemoteException(e); |
| 127 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | private synchronized VehicleNetworkHalMock getHalMock() { |
| 131 | return mHalMock; |
| 132 | } |
| 133 | |
| 134 | private void handleRemoteException(RemoteException e) { |
| 135 | //TODO |
| 136 | } |
| 137 | |
| 138 | private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub { |
| 139 | private final WeakReference<CarTestManager> mTestManager; |
| 140 | |
| 141 | private IVehicleNetworkHalMockImpl(CarTestManager testManager) { |
| 142 | mTestManager = new WeakReference<CarTestManager>(testManager); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public VehiclePropConfigsParcelable onListProperties() { |
| 147 | CarTestManager testManager = mTestManager.get(); |
| 148 | if (testManager == null) { |
| 149 | return null; |
| 150 | } |
| 151 | VehiclePropConfigs configs = testManager.getHalMock().onListProperties(); |
| 152 | return new VehiclePropConfigsParcelable(configs); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void onPropertySet(VehiclePropValueParcelable value) { |
| 157 | CarTestManager testManager = mTestManager.get(); |
| 158 | if (testManager == null) { |
| 159 | return; |
| 160 | } |
| 161 | testManager.getHalMock().onPropertySet(value.value); |
| 162 | } |
| 163 | |
| 164 | @Override |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 165 | public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 166 | CarTestManager testManager = mTestManager.get(); |
| 167 | if (testManager == null) { |
| 168 | return null; |
| 169 | } |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 170 | VehiclePropValue retValue = testManager.getHalMock().onPropertyGet(value.value); |
| 171 | return new VehiclePropValueParcelable(retValue); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 175 | public void onPropertySubscribe(int property, float sampleRate, int zones) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 176 | CarTestManager testManager = mTestManager.get(); |
| 177 | if (testManager == null) { |
| 178 | return; |
| 179 | } |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 180 | testManager.getHalMock().onPropertySubscribe(property, sampleRate, zones); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void onPropertyUnsubscribe(int property) { |
| 185 | CarTestManager testManager = mTestManager.get(); |
| 186 | if (testManager == null) { |
| 187 | return; |
| 188 | } |
| 189 | testManager.getHalMock().onPropertyUnsubscribe(property); |
| 190 | } |
| 191 | } |
| 192 | } |