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