Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 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 | |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 18 | import android.car.hardware.CarPropertyValue; |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 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 { |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 31 | private List<CarPropertyValue> mExpectedEvents; |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 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; |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 40 | public final CarPropertyValue expectedEvent; |
| 41 | public final CarPropertyValue mismatchedEvent; |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 42 | |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 43 | MismatchedEventPair(CarPropertyValue expectedEvent, CarPropertyValue mismatchedEvent, |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 44 | int idx) { |
| 45 | this.idx = idx; |
| 46 | this.expectedEvent = expectedEvent; |
| 47 | this.mismatchedEvent = mismatchedEvent; |
| 48 | } |
| 49 | } |
| 50 | |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 51 | VhalEventVerifier(List<CarPropertyValue> expectedEvents) { |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 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 | */ |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 65 | public void verify(CarPropertyValue nextEvent) { |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 66 | if (mIdx >= mExpectedEvents.size()) { |
| 67 | return; |
| 68 | } |
Chao Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 69 | CarPropertyValue expectedEvent = mExpectedEvents.get(mIdx); |
| 70 | if (!Utils.areCarPropertyValuesEqual(expectedEvent, nextEvent)) { |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 71 | 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 Yan | a916068 | 2018-06-14 11:05:45 -0700 | [diff] [blame] | 90 | + pair.expectedEvent + ", Received " |
| 91 | + pair.mismatchedEvent + "\n"); |
Chao Yan | f2c109c | 2018-05-11 12:16:14 -0700 | [diff] [blame] | 92 | } |
| 93 | return resultBuilder.toString(); |
| 94 | } |
| 95 | } |