1 /* 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 */ 16 package com.android.car.vehiclehal.test; 17 18 import android.car.hardware.CarPropertyValue; 19 import android.os.ConditionVariable; 20 21 import java.util.ArrayList; 22 import 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 */ 30 class VhalEventVerifier { 31 private List<CarPropertyValue> mExpectedEvents; 32 // 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; 40 public final CarPropertyValue expectedEvent; 41 public final CarPropertyValue mismatchedEvent; 42 MismatchedEventPair(CarPropertyValue expectedEvent, CarPropertyValue mismatchedEvent, int idx)43 MismatchedEventPair(CarPropertyValue expectedEvent, CarPropertyValue mismatchedEvent, 44 int idx) { 45 this.idx = idx; 46 this.expectedEvent = expectedEvent; 47 this.mismatchedEvent = mismatchedEvent; 48 } 49 } 50 VhalEventVerifier(List<CarPropertyValue> expectedEvents)51 VhalEventVerifier(List<CarPropertyValue> expectedEvents) { 52 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 */ verify(CarPropertyValue nextEvent)65 public void verify(CarPropertyValue nextEvent) { 66 if (mIdx >= mExpectedEvents.size()) { 67 return; 68 } 69 CarPropertyValue expectedEvent = mExpectedEvents.get(mIdx); 70 if (!Utils.areCarPropertyValuesEqual(expectedEvent, nextEvent)) { 71 mMismatchedEvents.add(new MismatchedEventPair(expectedEvent, nextEvent, mIdx)); 72 } 73 if (++mIdx == mExpectedEvents.size()) { 74 mCond.open(); 75 } 76 } 77 getMismatchedEvents()78 public List<MismatchedEventPair> getMismatchedEvents() { 79 return mMismatchedEvents; 80 } 81 waitForEnd(long timeout)82 public void waitForEnd(long timeout) { 83 mCond.block(timeout); 84 } 85 getResultString()86 public String getResultString() { 87 StringBuilder resultBuilder = new StringBuilder(); 88 for (MismatchedEventPair pair : mMismatchedEvents) { 89 resultBuilder.append("Index " + pair.idx + ": Expected " 90 + pair.expectedEvent + ", Received " 91 + pair.mismatchedEvent + "\n"); 92 } 93 return resultBuilder.toString(); 94 } 95 } 96