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