1 /* 2 * Copyright (C) 2017 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 com.android.car; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 22 import android.car.VehicleAreaType; 23 import android.car.vms.VmsLayer; 24 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; 25 import android.hardware.automotive.vehicle.V2_0.VehicleProperty; 26 import android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess; 27 import android.hardware.automotive.vehicle.V2_0.VehiclePropertyChangeMode; 28 import android.hardware.automotive.vehicle.V2_0.VmsBaseMessageIntegerValuesIndex; 29 import android.hardware.automotive.vehicle.V2_0.VmsMessageType; 30 import android.hardware.automotive.vehicle.V2_0.VmsMessageWithLayerIntegerValuesIndex; 31 import android.support.test.filters.MediumTest; 32 import android.support.test.runner.AndroidJUnit4; 33 import android.util.Log; 34 35 import com.android.car.vehiclehal.VehiclePropValueBuilder; 36 import com.android.car.vehiclehal.test.MockedVehicleHal; 37 import com.android.car.vehiclehal.test.MockedVehicleHal.VehicleHalPropertyHandler; 38 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.ArrayList; 43 import java.util.Arrays; 44 import java.util.concurrent.Semaphore; 45 import java.util.concurrent.TimeUnit; 46 47 @RunWith(AndroidJUnit4.class) 48 @MediumTest 49 public class VmsPublisherClientServiceTest extends MockedCarTestBase { 50 private static final String TAG = "VmsPublisherTest"; 51 private static final int MOCK_PUBLISHER_LAYER_ID = 12; 52 private static final int MOCK_PUBLISHER_LAYER_VERSION = 34; 53 private static final int MOCK_PUBLISHER_LAYER_SUBTYPE = 56; 54 public static final int MOCK_PUBLISHER_ID = 1234; 55 public static final VmsLayer MOCK_PUBLISHER_LAYER = 56 new VmsLayer(MOCK_PUBLISHER_LAYER_ID, 57 MOCK_PUBLISHER_LAYER_SUBTYPE, 58 MOCK_PUBLISHER_LAYER_VERSION); 59 public static final byte[] PAYLOAD = new byte[]{1, 1, 2, 3, 5, 8, 13}; 60 61 private HalHandler mHalHandler; 62 // Used to block until the HAL property is updated in HalHandler.onPropertySet. 63 private Semaphore mHalHandlerSemaphore; 64 65 @Override configureMockedHal()66 protected synchronized void configureMockedHal() { 67 mHalHandler = new HalHandler(); 68 addProperty(VehicleProperty.VEHICLE_MAP_SERVICE, mHalHandler) 69 .setChangeMode(VehiclePropertyChangeMode.ON_CHANGE) 70 .setAccess(VehiclePropertyAccess.READ_WRITE) 71 .addAreaConfig(VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL, 0, 0); 72 } 73 74 @Override configureResourceOverrides(MockResources resources)75 protected synchronized void configureResourceOverrides(MockResources resources) { 76 resources.overrideResource(R.array.vmsPublisherClients, 77 new String[]{ getFlattenComponent(SimpleVmsPublisherClientService.class) }); 78 } 79 getHalSubscriptionRequest()80 private VehiclePropValue getHalSubscriptionRequest() { 81 return VehiclePropValueBuilder.newBuilder(VehicleProperty.VEHICLE_MAP_SERVICE) 82 .addIntValue(VmsMessageType.SUBSCRIBE) 83 .addIntValue(MOCK_PUBLISHER_LAYER_ID) 84 .addIntValue(MOCK_PUBLISHER_LAYER_SUBTYPE) 85 .addIntValue(MOCK_PUBLISHER_LAYER_VERSION) 86 .build(); 87 } 88 89 @Override setUp()90 public void setUp() throws Exception { 91 /** 92 * First init the semaphore, setUp will start a series of events that will ultimately 93 * update the HAL layer and release this semaphore. 94 */ 95 mHalHandlerSemaphore = new Semaphore(0); 96 super.setUp(); 97 98 // Inject a subscribe event which simulates the HAL is subscribed to the Mock Publisher. 99 MockedVehicleHal mHal = getMockedVehicleHal(); 100 mHal.injectEvent(getHalSubscriptionRequest()); 101 } 102 103 /** 104 * The method setUp initializes all the Car services, including the VmsPublisherService. 105 * The VmsPublisherService will start and configure its list of clients. This list was 106 * overridden in the method getCarServiceContext. 107 * Therefore, only SimpleVmsPublisherClientService will be started. 108 * The service SimpleVmsPublisherClientService will publish one message, which is validated in 109 * this test. 110 */ 111 @Test testPublish()112 public void testPublish() throws Exception { 113 //TODO: This test is using minial synchronisation between clients. 114 // If more complexity is added this may result in publisher 115 // publishing before the subscriber subscribed, in which case 116 // the semaphore will not be released. 117 assertTrue(mHalHandlerSemaphore.tryAcquire(2L, TimeUnit.SECONDS)); 118 VehiclePropValue.RawValue rawValue = mHalHandler.getValue().value; 119 int messageType = rawValue.int32Values.get(VmsMessageWithLayerIntegerValuesIndex.MESSAGE_TYPE); 120 int layerId = rawValue.int32Values.get(VmsMessageWithLayerIntegerValuesIndex.LAYER_TYPE); 121 int layerVersion = rawValue.int32Values.get(VmsMessageWithLayerIntegerValuesIndex.LAYER_VERSION); 122 byte[] payload = new byte[rawValue.bytes.size()]; 123 for (int i = 0; i < rawValue.bytes.size(); ++i) { 124 payload[i] = rawValue.bytes.get(i); 125 } 126 assertEquals(VmsMessageType.DATA, messageType); 127 assertEquals(MOCK_PUBLISHER_LAYER_ID, layerId); 128 assertEquals(MOCK_PUBLISHER_LAYER_VERSION, layerVersion); 129 assertTrue(Arrays.equals(PAYLOAD, payload)); 130 } 131 132 private class HalHandler implements VehicleHalPropertyHandler { 133 private VehiclePropValue mValue; 134 135 @Override onPropertySet(VehiclePropValue value)136 public synchronized void onPropertySet(VehiclePropValue value) { 137 mValue = value; 138 139 // If this is the data message release the semaphore so the test can continue. 140 ArrayList<Integer> int32Values = value.value.int32Values; 141 if (int32Values.get(VmsBaseMessageIntegerValuesIndex.MESSAGE_TYPE) == 142 VmsMessageType.DATA) { 143 mHalHandlerSemaphore.release(); 144 } 145 } 146 147 @Override onPropertyGet(VehiclePropValue value)148 public synchronized VehiclePropValue onPropertyGet(VehiclePropValue value) { 149 return mValue != null ? mValue : value; 150 } 151 152 @Override onPropertySubscribe(int property, float sampleRate)153 public synchronized void onPropertySubscribe(int property, float sampleRate) { 154 Log.d(TAG, "onPropertySubscribe property " + property + " sampleRate " + sampleRate); 155 } 156 157 @Override onPropertyUnsubscribe(int property)158 public synchronized void onPropertyUnsubscribe(int property) { 159 Log.d(TAG, "onPropertyUnSubscribe property " + property); 160 } 161 getValue()162 public VehiclePropValue getValue() { 163 return mValue; 164 } 165 } 166 }