blob: 3b3a94fc488b81d89da562ea4a6fa94b81ac7381 [file] [log] [blame]
Antonio Cortes7e914102017-03-10 09:09:07 -08001/*
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.test;
18
19import android.annotation.ArrayRes;
20import android.car.Car;
21import android.car.VehicleAreaType;
22import android.car.annotation.FutureFeature;
Asaf Rosenfeld2ca6f8c2017-03-20 12:59:33 -070023import android.car.vms.VmsLayer;
Antonio Cortes7e914102017-03-10 09:09:07 -080024import android.car.vms.VmsSubscriberManager;
25import android.content.Context;
26import android.content.ContextWrapper;
27import android.content.pm.PackageManager;
28import android.content.res.Resources;
29import android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess;
30import android.hardware.automotive.vehicle.V2_0.VehiclePropertyChangeMode;
31import android.hardware.automotive.vehicle.V2_1.VehicleProperty;
Antonio Cortes7e914102017-03-10 09:09:07 -080032import com.android.car.vehiclehal.test.MockedVehicleHal;
33
Asaf Rosenfeldeb541d42017-03-31 14:32:10 -070034import java.util.ArrayList;
Antonio Cortes7e914102017-03-10 09:09:07 -080035import java.util.Arrays;
Asaf Rosenfeld2ca6f8c2017-03-20 12:59:33 -070036import java.util.List;
Asaf Rosenfeld60284892017-05-19 12:59:56 -070037import java.util.Map;
Antonio Cortes7e914102017-03-10 09:09:07 -080038import java.util.concurrent.Semaphore;
39import java.util.concurrent.TimeUnit;
40
41@FutureFeature
42public class VmsPublisherSubscriberTest extends MockedCarTestBase {
Antonio Cortes01a60392017-03-22 13:00:02 -070043 private static final int LAYER_ID = 88;
44 private static final int LAYER_VERSION = 19;
Asaf Rosenfeld60284892017-05-19 12:59:56 -070045 private static final int EXPECTED_PUBLISHER_ID = 0;
Antonio Cortes09bfb002017-03-30 09:46:36 -070046 private static final String TAG = "VmsPubSubTest";
47
Antonio Cortes01a60392017-03-22 13:00:02 -070048 public static final VmsLayer LAYER = new VmsLayer(LAYER_ID, LAYER_VERSION);
Antonio Cortes7e914102017-03-10 09:09:07 -080049 public static final byte[] PAYLOAD = new byte[]{2, 3, 5, 7, 11, 13, 17};
Asaf Rosenfeldeb541d42017-03-31 14:32:10 -070050 private static final List<VmsLayer> AVAILABLE_LAYERS = new ArrayList<>(Arrays.asList(LAYER));
Antonio Cortes7e914102017-03-10 09:09:07 -080051
52 private HalHandler mHalHandler;
53 // Used to block until a value is propagated to the TestListener.onVmsMessageReceived.
54 private Semaphore mSubscriberSemaphore;
Asaf Rosenfeldeb541d42017-03-31 14:32:10 -070055 private Semaphore mAvailabilitySemaphore;
Antonio Cortes7e914102017-03-10 09:09:07 -080056
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 Cortes09bfb002017-03-30 09:46:36 -070096 if (!VmsTestUtils.canRunTest(TAG)) return;
Antonio Cortes7e914102017-03-10 09:09:07 -080097 super.setUp();
98 mSubscriberSemaphore = new Semaphore(0);
Asaf Rosenfeldeb541d42017-03-31 14:32:10 -070099 mAvailabilitySemaphore = new Semaphore(0);
Antonio Cortes7e914102017-03-10 09:09:07 -0800100 }
101
Antonio Cortes09bfb002017-03-30 09:46:36 -0700102 @Override
103 protected synchronized void tearDown() throws Exception {
104 if (!VmsTestUtils.canRunTest(TAG)) return;
105 super.tearDown();
106 }
107
Antonio Cortes7e914102017-03-10 09:09:07 -0800108 /**
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 Cortes09bfb002017-03-30 09:46:36 -0700117 if (!VmsTestUtils.canRunTest(TAG)) return;
Antonio Cortes7e914102017-03-10 09:09:07 -0800118 VmsSubscriberManager vmsSubscriberManager = (VmsSubscriberManager) getCar().getCarManager(
119 Car.VMS_SUBSCRIBER_SERVICE);
120 TestListener listener = new TestListener();
121 vmsSubscriberManager.setListener(listener);
Antonio Cortes01a60392017-03-22 13:00:02 -0700122 vmsSubscriberManager.subscribe(LAYER);
Antonio Cortes7e914102017-03-10 09:09:07 -0800123
124 assertTrue(mSubscriberSemaphore.tryAcquire(2L, TimeUnit.SECONDS));
Antonio Cortes01a60392017-03-22 13:00:02 -0700125 assertEquals(LAYER, listener.getLayer());
Antonio Cortes7e914102017-03-10 09:09:07 -0800126 assertTrue(Arrays.equals(PAYLOAD, listener.getPayload()));
127 }
128
Asaf Rosenfeldeb541d42017-03-31 14:32:10 -0700129 /**
Asaf Rosenfeld60284892017-05-19 12:59:56 -0700130 * 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 Rosenfeldeb541d42017-03-31 14:32:10 -0700151 * 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 Cortes7e914102017-03-10 09:09:07 -0800167 private class HalHandler implements MockedVehicleHal.VehicleHalPropertyHandler {
168 }
169
Asaf Rosenfeld2ca6f8c2017-03-20 12:59:33 -0700170 private class TestListener implements VmsSubscriberManager.VmsSubscriberClientListener {
Antonio Cortes01a60392017-03-22 13:00:02 -0700171 private VmsLayer mLayer;
Antonio Cortes7e914102017-03-10 09:09:07 -0800172 private byte[] mPayload;
Asaf Rosenfeldeb541d42017-03-31 14:32:10 -0700173 private List<VmsLayer> mAvailableLayers;
Antonio Cortes7e914102017-03-10 09:09:07 -0800174
175 @Override
Antonio Cortes01a60392017-03-22 13:00:02 -0700176 public void onVmsMessageReceived(VmsLayer layer, byte[] payload) {
177 assertEquals(LAYER, layer);
Antonio Cortes7e914102017-03-10 09:09:07 -0800178 assertTrue(Arrays.equals(PAYLOAD, payload));
Antonio Cortes01a60392017-03-22 13:00:02 -0700179 mLayer = layer;
Antonio Cortes7e914102017-03-10 09:09:07 -0800180 mPayload = payload;
181 mSubscriberSemaphore.release();
182 }
183
Asaf Rosenfeld2ca6f8c2017-03-20 12:59:33 -0700184 @Override
185 public void onLayersAvailabilityChange(List<VmsLayer> availableLayers) {
Asaf Rosenfeldeb541d42017-03-31 14:32:10 -0700186 assertEquals(AVAILABLE_LAYERS, availableLayers);
187 mAvailableLayers = availableLayers;
188 mAvailabilitySemaphore.release();
Asaf Rosenfeld2ca6f8c2017-03-20 12:59:33 -0700189 }
190
Antonio Cortes2ec3e352017-03-29 16:20:33 -0700191 @Override
192 public void onCarDisconnected() {
193
194 }
195
Antonio Cortes01a60392017-03-22 13:00:02 -0700196 public VmsLayer getLayer() {
197 return mLayer;
Antonio Cortes7e914102017-03-10 09:09:07 -0800198 }
199
200 public byte[] getPayload() {
201 return mPayload;
202 }
Asaf Rosenfeldeb541d42017-03-31 14:32:10 -0700203
204 public List<VmsLayer> getAvailalbeLayers() {
205 return mAvailableLayers;
206 }
Antonio Cortes7e914102017-03-10 09:09:07 -0800207 }
208}