VmsPublisherSubscriberTest.java revision 43024de8d8c089824e595bcf692f62376ce72e6a
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 17package com.android.car; 18 19import static com.android.car.VmsPublisherClientServiceTest.javaClassToComponent; 20 21import android.annotation.ArrayRes; 22import android.car.Car; 23import android.car.VehicleAreaType; 24import android.car.vms.VmsAssociatedLayer; 25import android.car.vms.VmsLayer; 26import android.car.vms.VmsSubscriberManager; 27import android.content.Context; 28import android.content.ContextWrapper; 29import android.content.pm.PackageManager; 30import android.content.res.Resources; 31import android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess; 32import android.hardware.automotive.vehicle.V2_0.VehiclePropertyChangeMode; 33import android.hardware.automotive.vehicle.V2_0.VehicleProperty; 34import android.test.suitebuilder.annotation.MediumTest; 35 36import com.android.car.vehiclehal.test.MockedVehicleHal; 37 38import java.util.ArrayList; 39import java.util.Arrays; 40import java.util.HashSet; 41import java.util.List; 42import java.util.concurrent.Semaphore; 43import java.util.concurrent.TimeUnit; 44 45@MediumTest 46public class VmsPublisherSubscriberTest extends MockedCarTestBase { 47 private static final int LAYER_ID = 88; 48 private static final int LAYER_VERSION = 19; 49 private static final int LAYER_SUBTYPE = 55; 50 private static final String TAG = "VmsPubSubTest"; 51 52 // The expected publisher ID is 0 since it the expected assigned ID from the VMS core. 53 public static final int EXPECTED_PUBLISHER_ID = 0; 54 public static final VmsLayer LAYER = new VmsLayer(LAYER_ID, LAYER_SUBTYPE, LAYER_VERSION); 55 public static final VmsAssociatedLayer ASSOCIATED_LAYER = 56 new VmsAssociatedLayer(LAYER, new HashSet<>(Arrays.asList(EXPECTED_PUBLISHER_ID))); 57 public static final byte[] PAYLOAD = new byte[]{2, 3, 5, 7, 11, 13, 17}; 58 59 private static final List<VmsAssociatedLayer> AVAILABLE_ASSOCIATED_LAYERS = 60 new ArrayList<>(Arrays.asList(ASSOCIATED_LAYER)); 61 62 63 private static final int SUBSCRIBED_LAYER_ID = 89; 64 public static final VmsLayer SUBSCRIBED_LAYER = 65 new VmsLayer(SUBSCRIBED_LAYER_ID, LAYER_SUBTYPE, LAYER_VERSION); 66 public static final VmsAssociatedLayer ASSOCIATED_SUBSCRIBED_LAYER = 67 new VmsAssociatedLayer(SUBSCRIBED_LAYER, new HashSet<>(Arrays.asList(EXPECTED_PUBLISHER_ID))); 68 private static final List<VmsAssociatedLayer> AVAILABLE_ASSOCIATED_LAYERS_WITH_SUBSCRIBED_LAYER = 69 new ArrayList<>(Arrays.asList(ASSOCIATED_LAYER, ASSOCIATED_SUBSCRIBED_LAYER)); 70 71 72 private HalHandler mHalHandler; 73 // Used to block until a value is propagated to the TestClientCallback.onVmsMessageReceived. 74 private Semaphore mSubscriberSemaphore; 75 private Semaphore mAvailabilitySemaphore; 76 77 @Override 78 protected synchronized void configureMockedHal() { 79 mHalHandler = new HalHandler(); 80 addProperty(VehicleProperty.VEHICLE_MAP_SERVICE, mHalHandler) 81 .setChangeMode(VehiclePropertyChangeMode.ON_CHANGE) 82 .setAccess(VehiclePropertyAccess.READ_WRITE) 83 .setSupportedAreas(VehicleAreaType.VEHICLE_AREA_TYPE_NONE); 84 } 85 86 @Override 87 protected synchronized void configureResourceOverrides(MockResources resources) { 88 resources.overrideResource(com.android.car.R.array.vmsPublisherClients, 89 new String[]{"com.android.car/.VmsPublisherClientMockService"}); 90 } 91 92 @Override 93 protected void setUp() throws Exception { 94 super.setUp(); 95 mSubscriberSemaphore = new Semaphore(0); 96 mAvailabilitySemaphore = new Semaphore(0); 97 } 98 99 @Override 100 protected synchronized void tearDown() throws Exception { 101 super.tearDown(); 102 } 103 104 /** 105 * The method setUp initializes all the Car services, including the VmsPublisherService. 106 * The VmsPublisherService will start and configure its list of clients. This list was 107 * overridden in the method getCarServiceContext. Therefore, only VmsPublisherClientMockService 108 * will be started. This test method subscribes to a layer and triggers 109 * VmsPublisherClientMockService.onVmsSubscriptionChange. In turn, the mock service will publish 110 * a message, which is validated in this test. 111 */ 112 public void testPublisherToSubscriber() throws Exception { 113 VmsSubscriberManager vmsSubscriberManager = (VmsSubscriberManager) getCar().getCarManager( 114 Car.VMS_SUBSCRIBER_SERVICE); 115 TestClientCallback clientCallback = new TestClientCallback(); 116 vmsSubscriberManager.registerClientCallback(clientCallback); 117 vmsSubscriberManager.subscribe(LAYER); 118 119 assertTrue(mSubscriberSemaphore.tryAcquire(2L, TimeUnit.SECONDS)); 120 assertEquals(LAYER, clientCallback.getLayer()); 121 assertTrue(Arrays.equals(PAYLOAD, clientCallback.getPayload())); 122 } 123 124 /** 125 * The Mock service will get a publisher ID by sending its information when it will get 126 * ServiceReady as well as on SubscriptionChange. Since clients are not notified when 127 * publishers are assigned IDs, this test waits until the availability is changed which indicates 128 * that the Mock service has gotten its ServiceReady and publisherId. 129 */ 130 public void testPublisherInfo() throws Exception { 131 VmsSubscriberManager vmsSubscriberManager = (VmsSubscriberManager) getCar().getCarManager( 132 Car.VMS_SUBSCRIBER_SERVICE); 133 // Subscribe to layer as a way to make sure the mock client completed setting the information. 134 TestClientCallback clientCallback = new TestClientCallback(); 135 vmsSubscriberManager.registerClientCallback(clientCallback); 136 vmsSubscriberManager.subscribe(LAYER); 137 138 assertTrue(mAvailabilitySemaphore.tryAcquire(2L, TimeUnit.SECONDS)); 139 140 byte[] info = vmsSubscriberManager.getPublisherInfo(EXPECTED_PUBLISHER_ID); 141 assertTrue(Arrays.equals(PAYLOAD, info)); 142 } 143 144 /** 145 * The Mock service offers all the subscribed layers as available layers. 146 * In this test the client subscribes to a layer and verifies that it gets the 147 * notification that it is available. 148 */ 149 public void testAvailabilityWithSubscription() throws Exception { 150 VmsSubscriberManager vmsSubscriberManager = (VmsSubscriberManager) getCar().getCarManager( 151 Car.VMS_SUBSCRIBER_SERVICE); 152 TestClientCallback clientCallback = new TestClientCallback(); 153 vmsSubscriberManager.registerClientCallback(clientCallback); 154 vmsSubscriberManager.subscribe(SUBSCRIBED_LAYER); 155 156 assertTrue(mAvailabilitySemaphore.tryAcquire(2L, TimeUnit.SECONDS)); 157 assertEquals(AVAILABLE_ASSOCIATED_LAYERS_WITH_SUBSCRIBED_LAYER, clientCallback.getAvailableLayers()); 158 } 159 160 /** 161 * The Mock service offers all the subscribed layers as available layers, so in this 162 * test the client subscribes to a layer and verifies that it gets the notification that it 163 * is available. 164 */ 165 public void testAvailabilityWithoutSubscription() throws Exception { 166 VmsSubscriberManager vmsSubscriberManager = (VmsSubscriberManager) getCar().getCarManager( 167 Car.VMS_SUBSCRIBER_SERVICE); 168 TestClientCallback clientCallback = new TestClientCallback(); 169 vmsSubscriberManager.registerClientCallback(clientCallback); 170 171 assertTrue(mAvailabilitySemaphore.tryAcquire(2L, TimeUnit.SECONDS)); 172 assertEquals(AVAILABLE_ASSOCIATED_LAYERS, clientCallback.getAvailableLayers()); 173 } 174 175 private class HalHandler implements MockedVehicleHal.VehicleHalPropertyHandler { 176 } 177 178 private class TestClientCallback implements VmsSubscriberManager.VmsSubscriberClientCallback { 179 private VmsLayer mLayer; 180 private byte[] mPayload; 181 private List<VmsLayer> mAvailableLayers; 182 183 @Override 184 public void onVmsMessageReceived(VmsLayer layer, byte[] payload) { 185 assertEquals(LAYER, layer); 186 assertTrue(Arrays.equals(PAYLOAD, payload)); 187 mLayer = layer; 188 mPayload = payload; 189 mSubscriberSemaphore.release(); 190 } 191 192 @Override 193 public void onLayersAvailabilityChanged(List<VmsLayer> availableLayers) { 194 mAvailableLayers = availableLayers; 195 mAvailabilitySemaphore.release(); 196 } 197 198 public VmsLayer getLayer() { 199 return mLayer; 200 } 201 202 public byte[] getPayload() { 203 return mPayload; 204 } 205 206 public List<VmsLayer> getAvailableLayers() { 207 return mAvailableLayers; 208 } 209 } 210} 211