blob: 62dbf5cc70971e6c634a542d417bb15b77292b76 [file] [log] [blame]
Chao Yanf2c109c2018-05-11 12:16:14 -07001/*
2 * Copyright (C) 2018 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 */
16package com.android.car.vehiclehal.test;
17
Chao Yana9160682018-06-14 11:05:45 -070018import android.car.hardware.CarPropertyValue;
Chao Yanf2c109c2018-05-11 12:16:14 -070019import android.os.ConditionVariable;
20
21import java.util.ArrayList;
22import java.util.List;
23
24/**
25 * The verifier class is used to verify received VHAL events against expected events on-the-fly.
26 * It is initialized with a list of expected events and moving down the list to verify received
27 * events. The verifier object not reusable and should be discarded once the verification is done.
28 * The verifier will provide formatted result for all mismatched events in sequence.
29 */
30class VhalEventVerifier {
Chao Yana9160682018-06-14 11:05:45 -070031 private List<CarPropertyValue> mExpectedEvents;
Chao Yanf2c109c2018-05-11 12:16:14 -070032 // A pointer to keep track of the next expected event in the list
33 private int mIdx;
34 private List<MismatchedEventPair> mMismatchedEvents;
35 // Condition variable to notify waiting threads when verification is done or timeout.
36 private ConditionVariable mCond;
37
38 static class MismatchedEventPair {
39 public final int idx;
Chao Yana9160682018-06-14 11:05:45 -070040 public final CarPropertyValue expectedEvent;
41 public final CarPropertyValue mismatchedEvent;
Chao Yanf2c109c2018-05-11 12:16:14 -070042
Chao Yana9160682018-06-14 11:05:45 -070043 MismatchedEventPair(CarPropertyValue expectedEvent, CarPropertyValue mismatchedEvent,
Chao Yanf2c109c2018-05-11 12:16:14 -070044 int idx) {
45 this.idx = idx;
46 this.expectedEvent = expectedEvent;
47 this.mismatchedEvent = mismatchedEvent;
48 }
49 }
50
Chao Yana9160682018-06-14 11:05:45 -070051 VhalEventVerifier(List<CarPropertyValue> expectedEvents) {
Chao Yanf2c109c2018-05-11 12:16:14 -070052 mExpectedEvents = expectedEvents;
53 mIdx = 0;
54 mMismatchedEvents = new ArrayList<>();
55 mCond = new ConditionVariable(expectedEvents.isEmpty());
56 }
57
58 /**
59 * Verification method that checks the equality of received event against expected event. Once
60 * it reaches to the end of list, it will unblock the waiting threads. Note, the verification
61 * method is not thread-safe. It assumes only a single thread is calling the method at all time.
62 *
63 * @param nextEvent to be verified
64 */
Chao Yana9160682018-06-14 11:05:45 -070065 public void verify(CarPropertyValue nextEvent) {
Chao Yanf2c109c2018-05-11 12:16:14 -070066 if (mIdx >= mExpectedEvents.size()) {
67 return;
68 }
Chao Yana9160682018-06-14 11:05:45 -070069 CarPropertyValue expectedEvent = mExpectedEvents.get(mIdx);
70 if (!Utils.areCarPropertyValuesEqual(expectedEvent, nextEvent)) {
Chao Yanf2c109c2018-05-11 12:16:14 -070071 mMismatchedEvents.add(new MismatchedEventPair(expectedEvent, nextEvent, mIdx));
72 }
73 if (++mIdx == mExpectedEvents.size()) {
74 mCond.open();
75 }
76 }
77
78 public List<MismatchedEventPair> getMismatchedEvents() {
79 return mMismatchedEvents;
80 }
81
82 public void waitForEnd(long timeout) {
83 mCond.block(timeout);
84 }
85
86 public String getResultString() {
87 StringBuilder resultBuilder = new StringBuilder();
88 for (MismatchedEventPair pair : mMismatchedEvents) {
89 resultBuilder.append("Index " + pair.idx + ": Expected "
Chao Yana9160682018-06-14 11:05:45 -070090 + pair.expectedEvent + ", Received "
91 + pair.mismatchedEvent + "\n");
Chao Yanf2c109c2018-05-11 12:16:14 -070092 }
93 return resultBuilder.toString();
94 }
95}