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 21 import android.car.vms.VmsLayer; 22 import android.hardware.automotive.vehicle.V2_0.VmsBaseMessageIntegerValuesIndex; 23 import android.hardware.automotive.vehicle.V2_0.VmsMessageType; 24 import android.hardware.automotive.vehicle.V2_0.VmsSubscriptionsStateIntegerValuesIndex; 25 26 import androidx.test.ext.junit.runners.AndroidJUnit4; 27 import androidx.test.filters.MediumTest; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.HashSet; 36 import java.util.List; 37 38 @RunWith(AndroidJUnit4.class) 39 @MediumTest 40 public class VmsHalServiceSubscriptionEventTest extends MockedVmsTestBase { 41 @Test testEmptySubscriptions()42 public void testEmptySubscriptions() throws Exception { 43 List<VmsLayer> layers = new ArrayList<>(); 44 subscriptionTestLogic(layers); 45 } 46 47 @Test testOneSubscription()48 public void testOneSubscription() throws Exception { 49 List<VmsLayer> layers = 50 Collections.singletonList(new VmsLayer(8, 0, 3)); 51 subscriptionTestLogic(layers); 52 } 53 54 @Test testManySubscriptions()55 public void testManySubscriptions() throws Exception { 56 List<VmsLayer> layers = Arrays.asList( 57 new VmsLayer(8, 1, 3), 58 new VmsLayer(5, 2, 1), 59 new VmsLayer(3, 3, 9), 60 new VmsLayer(2, 4, 7), 61 new VmsLayer(9, 5, 3)); 62 subscriptionTestLogic(layers); 63 } 64 65 /** 66 * First, it subscribes to the given layers. Then it validates that a subscription request 67 * responds with the same layers. 68 */ subscriptionTestLogic(List<VmsLayer> layers)69 private void subscriptionTestLogic(List<VmsLayer> layers) throws Exception { 70 int sequenceNumber = 0; 71 for (VmsLayer layer : layers) { 72 sequenceNumber++; 73 subscribeViaHal(sequenceNumber, layer); 74 } 75 // Send subscription request. 76 getMockHalClient().sendMessage(VmsMessageType.SUBSCRIPTIONS_REQUEST); 77 78 // Validate response. 79 List<Integer> v = getMockHalClient().receiveMessage().value.int32Values; 80 assertEquals(VmsMessageType.SUBSCRIPTIONS_RESPONSE, 81 (int) v.get(VmsBaseMessageIntegerValuesIndex.MESSAGE_TYPE)); 82 assertEquals(sequenceNumber, 83 (int) v.get(VmsSubscriptionsStateIntegerValuesIndex.SEQUENCE_NUMBER)); 84 assertEquals(layers.size(), 85 (int) v.get(VmsSubscriptionsStateIntegerValuesIndex.NUMBER_OF_LAYERS)); 86 List<VmsLayer> receivedLayers = new ArrayList<>(); 87 int start = VmsSubscriptionsStateIntegerValuesIndex.SUBSCRIPTIONS_START; 88 int end = VmsSubscriptionsStateIntegerValuesIndex.SUBSCRIPTIONS_START + 3 * layers.size(); 89 while (start < end) { 90 int type = v.get(start++); 91 int subtype = v.get(start++); 92 int version = v.get(start++); 93 receivedLayers.add(new VmsLayer(type, subtype, version)); 94 } 95 assertEquals(new HashSet<>(layers), new HashSet<>(receivedLayers)); 96 } 97 98 /** 99 * Subscribes to a layer, waits for the state change to propagate back to the HAL layer and 100 * validates the propagated message. 101 */ subscribeViaHal(int sequenceNumber, VmsLayer layer)102 private void subscribeViaHal(int sequenceNumber, VmsLayer layer) throws Exception { 103 // Send subscribe request. 104 getMockHalClient().sendMessage( 105 VmsMessageType.SUBSCRIBE, 106 layer.getType(), 107 layer.getSubtype(), 108 layer.getVersion()); 109 110 // Validate response. 111 List<Integer> v = getMockHalClient().receiveMessage().value.int32Values; 112 assertEquals(VmsMessageType.SUBSCRIPTIONS_CHANGE, 113 (int) v.get(VmsBaseMessageIntegerValuesIndex.MESSAGE_TYPE)); 114 assertEquals(sequenceNumber, 115 (int) v.get(VmsSubscriptionsStateIntegerValuesIndex.SEQUENCE_NUMBER)); 116 assertEquals(sequenceNumber, 117 (int) v.get(VmsSubscriptionsStateIntegerValuesIndex.NUMBER_OF_LAYERS)); 118 } 119 } 120