Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [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 | */ |
| 16 | package com.android.car.vehiclenetwork.libtest; |
| 17 | |
| 18 | import android.os.HandlerThread; |
| 19 | import android.test.AndroidTestCase; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | import com.android.car.vehiclenetwork.VehicleNetwork; |
| 23 | import com.android.car.vehiclenetwork.VehicleNetwork.VehicleNetworkHalMock; |
| 24 | import com.android.car.vehiclenetwork.VehicleNetwork.VehicleNetworkListener; |
| 25 | import com.android.car.vehiclenetwork.VehicleNetworkConsts; |
| 26 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropAccess; |
| 27 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropChangeMode; |
| 28 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType; |
| 29 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfig; |
| 30 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs; |
| 31 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; |
| 32 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues; |
| 33 | import com.android.car.vehiclenetwork.VehiclePropConfigUtil; |
| 34 | import com.android.car.vehiclenetwork.VehiclePropValueUtil; |
| 35 | |
| 36 | import java.util.HashMap; |
| 37 | import java.util.LinkedList; |
| 38 | import java.util.concurrent.Semaphore; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | |
| 41 | public class VehicleNetworkMockedTest extends AndroidTestCase { |
| 42 | private static final String TAG = VehicleNetworkMockedTest.class.getSimpleName(); |
| 43 | |
| 44 | private static final long TIMEOUT_MS = 1000; |
| 45 | |
| 46 | private static final int CUSTOM_PROPERTY_INT32 = |
| 47 | VehicleNetworkConsts.VEHICLE_PROPERTY_CUSTOM_START; |
| 48 | |
| 49 | private final HandlerThread mHandlerThread = new HandlerThread( |
| 50 | VehicleNetworkTest.class.getSimpleName()); |
| 51 | private VehicleNetwork mVehicleNetwork; |
| 52 | private EventListener mListener = new EventListener(); |
| 53 | private final VehicleHalMock mVehicleHalMock = new VehicleHalMock(); |
| 54 | |
| 55 | @Override |
| 56 | protected void setUp() throws Exception { |
| 57 | super.setUp(); |
| 58 | mHandlerThread.start(); |
| 59 | mVehicleNetwork = VehicleNetwork.createVehicleNetwork(mListener, |
| 60 | mHandlerThread.getLooper()); |
| 61 | mVehicleHalMock.registerProperty( |
| 62 | VehiclePropConfigUtil.createProperty( |
| 63 | CUSTOM_PROPERTY_INT32, |
| 64 | VehiclePropAccess.VEHICLE_PROP_ACCESS_READ_WRITE, |
| 65 | VehiclePropChangeMode.VEHICLE_PROP_CHANGE_MODE_ON_CHANGE, |
| 66 | VehicleValueType.VEHICLE_VALUE_TYPE_INT32, 0x0), |
| 67 | new DefaultVehiclePropertyHandler(VehiclePropValueUtil.createIntValue( |
| 68 | CUSTOM_PROPERTY_INT32, 0, 0))); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected void tearDown() throws Exception { |
| 73 | super.tearDown(); |
| 74 | mHandlerThread.quit(); |
| 75 | mVehicleNetwork.stopMocking(); |
| 76 | } |
| 77 | |
| 78 | public void testHalRestartListening() throws Exception { |
| 79 | mVehicleNetwork.startHalRestartMonitoring(); |
| 80 | mVehicleNetwork.startMocking(mVehicleHalMock); |
| 81 | assertTrue(mListener.waitForHalRestartAndAssert(TIMEOUT_MS, true /*expectedInMocking*/)); |
| 82 | mVehicleNetwork.stopMocking(); |
| 83 | assertTrue(mListener.waitForHalRestartAndAssert(TIMEOUT_MS, false /*expectedInMocking*/)); |
| 84 | mVehicleNetwork.stopHalRestartMonitoring(); |
| 85 | } |
| 86 | |
| 87 | public void testGlobalErrorListening() throws Exception { |
| 88 | mVehicleNetwork.startErrorListening(); |
| 89 | mVehicleNetwork.startMocking(mVehicleHalMock); |
| 90 | final int ERROR_CODE = 0x1; |
| 91 | final int ERROR_OPERATION = 0x10; |
| 92 | mVehicleNetwork.injectHalError(ERROR_CODE, 0, ERROR_OPERATION); |
| 93 | assertTrue(mListener.waitForHalErrorAndAssert(TIMEOUT_MS, ERROR_CODE, 0, ERROR_OPERATION)); |
| 94 | mVehicleNetwork.injectHalError(ERROR_CODE, CUSTOM_PROPERTY_INT32, ERROR_OPERATION); |
| 95 | assertTrue(mListener.waitForHalErrorAndAssert(TIMEOUT_MS, |
| 96 | ERROR_CODE, CUSTOM_PROPERTY_INT32, ERROR_OPERATION)); |
| 97 | mVehicleNetwork.stopMocking(); |
| 98 | mVehicleNetwork.stopErrorListening(); |
| 99 | } |
| 100 | |
| 101 | public void testPropertyErrorListening() throws Exception { |
| 102 | mVehicleNetwork.startMocking(mVehicleHalMock); |
| 103 | mVehicleNetwork.subscribe(CUSTOM_PROPERTY_INT32, 0); |
| 104 | final int ERROR_CODE = 0x1; |
| 105 | final int ERROR_OPERATION = 0x10; |
| 106 | mVehicleNetwork.injectHalError(ERROR_CODE, CUSTOM_PROPERTY_INT32, ERROR_OPERATION); |
| 107 | assertTrue(mListener.waitForHalErrorAndAssert(TIMEOUT_MS, |
| 108 | ERROR_CODE, CUSTOM_PROPERTY_INT32, ERROR_OPERATION)); |
| 109 | mVehicleNetwork.unsubscribe(CUSTOM_PROPERTY_INT32); |
| 110 | mVehicleNetwork.stopMocking(); |
| 111 | } |
| 112 | |
| 113 | private class EventListener implements VehicleNetworkListener { |
| 114 | boolean mInMocking; |
| 115 | private final Semaphore mRestartWait = new Semaphore(0); |
| 116 | |
| 117 | int mErrorCode; |
| 118 | int mErrorProperty; |
| 119 | int mErrorOperation; |
| 120 | private final Semaphore mErrorWait = new Semaphore(0); |
| 121 | |
| 122 | @Override |
| 123 | public void onVehicleNetworkEvents(VehiclePropValues values) { |
| 124 | // TODO Auto-generated method stub |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onHalError(int errorCode, int property, int operation) { |
| 129 | mErrorCode = errorCode; |
| 130 | mErrorProperty = property; |
| 131 | mErrorOperation = operation; |
| 132 | mErrorWait.release(); |
| 133 | } |
| 134 | |
| 135 | public boolean waitForHalErrorAndAssert(long timeoutMs, int expectedErrorCode, |
| 136 | int expectedErrorProperty, int expectedErrorOperation) throws Exception { |
| 137 | if (!mErrorWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { |
| 138 | return false; |
| 139 | } |
| 140 | assertEquals(expectedErrorCode, mErrorCode); |
| 141 | assertEquals(expectedErrorProperty, mErrorProperty); |
| 142 | assertEquals(expectedErrorOperation, mErrorOperation); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void onHalRestart(boolean inMocking) { |
| 148 | mInMocking = inMocking; |
| 149 | mRestartWait.release(); |
| 150 | } |
| 151 | |
| 152 | public boolean waitForHalRestartAndAssert(long timeoutMs, boolean expectedInMocking) |
| 153 | throws Exception { |
| 154 | if (!mRestartWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { |
| 155 | return false; |
| 156 | } |
| 157 | assertEquals(expectedInMocking, mInMocking); |
| 158 | return true; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | private interface VehiclePropertyHandler { |
| 163 | void onPropertySet(VehiclePropValue value); |
| 164 | VehiclePropValue onPropertyGet(VehiclePropValue property); |
| 165 | void onPropertySubscribe(int property, int sampleRate); |
| 166 | void onPropertyUnsubscribe(int property); |
| 167 | } |
| 168 | |
| 169 | private class VehicleHalMock implements VehicleNetworkHalMock { |
| 170 | private LinkedList<VehiclePropConfig> mConfigs = new LinkedList<>(); |
| 171 | private HashMap<Integer, VehiclePropertyHandler> mHandlers = new HashMap<>(); |
| 172 | |
| 173 | public synchronized void registerProperty(VehiclePropConfig config, |
| 174 | VehiclePropertyHandler handler) { |
| 175 | int property = config.getProp(); |
| 176 | mConfigs.add(config); |
| 177 | mHandlers.put(property, handler); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public synchronized VehiclePropConfigs onListProperties() { |
| 182 | Log.i(TAG, "onListProperties, num properties:" + mConfigs.size()); |
| 183 | VehiclePropConfigs configs = |
| 184 | VehiclePropConfigs.newBuilder().addAllConfigs(mConfigs).build(); |
| 185 | return configs; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void onPropertySet(VehiclePropValue value) { |
| 190 | int property = value.getProp(); |
| 191 | VehiclePropertyHandler handler = getPropertyHandler(property); |
| 192 | if (handler == null) { |
| 193 | fail("onPropertySet for unknown property " + Integer.toHexString(property)); |
| 194 | } |
| 195 | handler.onPropertySet(value); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public VehiclePropValue onPropertyGet(VehiclePropValue value) { |
| 200 | int property = value.getProp(); |
| 201 | VehiclePropertyHandler handler = getPropertyHandler(property); |
| 202 | if (handler == null) { |
| 203 | fail("onPropertyGet for unknown property " + Integer.toHexString(property)); |
| 204 | } |
| 205 | return handler.onPropertyGet(value); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void onPropertySubscribe(int property, int sampleRate) { |
| 210 | VehiclePropertyHandler handler = getPropertyHandler(property); |
| 211 | if (handler == null) { |
| 212 | fail("onPropertySubscribe for unknown property " + Integer.toHexString(property)); |
| 213 | } |
| 214 | handler.onPropertySubscribe(property, sampleRate); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void onPropertyUnsubscribe(int property) { |
| 219 | VehiclePropertyHandler handler = getPropertyHandler(property); |
| 220 | if (handler == null) { |
| 221 | fail("onPropertyUnsubscribe for unknown property " + Integer.toHexString(property)); |
| 222 | } |
| 223 | handler.onPropertyUnsubscribe(property); |
| 224 | } |
| 225 | |
| 226 | public synchronized VehiclePropertyHandler getPropertyHandler(int property) { |
| 227 | return mHandlers.get(property); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | private class DefaultVehiclePropertyHandler implements VehiclePropertyHandler { |
| 232 | private VehiclePropValue mValue; |
| 233 | |
| 234 | DefaultVehiclePropertyHandler(VehiclePropValue initialValue) { |
| 235 | mValue = initialValue; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void onPropertySet(VehiclePropValue value) { |
| 240 | // TODO Auto-generated method stub |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public synchronized VehiclePropValue onPropertyGet(VehiclePropValue property) { |
| 245 | return mValue; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void onPropertySubscribe(int property, int sampleRate) { |
| 250 | // TODO Auto-generated method stub |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void onPropertyUnsubscribe(int property) { |
| 255 | // TODO Auto-generated method stub |
| 256 | } |
| 257 | } |
| 258 | } |