1 /*
2  * Copyright (C) 2021 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 android.car.cts.powerpolicy;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 public final class PowerPolicyTestResult {
22     private final PowerPolicyTestAnalyzer mTestAnalyzer;
23     private final TestResultTable mExpected = new TestResultTable();
24     private TestResultTable mStartSnapshot;
25     private TestResultTable mEndSnapshot;
26 
PowerPolicyTestResult(PowerPolicyTestAnalyzer testAnalyzer)27     public PowerPolicyTestResult(PowerPolicyTestAnalyzer testAnalyzer) {
28         mTestAnalyzer = testAnalyzer;
29     }
30 
31     /**
32      * Adds test passing criteria.
33      *
34      * <p> For multiple criteria, the order of adding them into this object matters.
35      */
addCriteria(String testcase, String action, String subject, String data)36     public void addCriteria(String testcase, String action, String subject, String data)
37             throws Exception {
38         if (testcase == null || action == null) {
39             throw new IllegalArgumentException("testcase and action should not be null");
40         }
41         if (data == null) {
42             data = "null";
43         }
44         mExpected.add(testcase, action, subject, data);
45     }
46 
takeStartSnapshot()47     public void takeStartSnapshot() throws Exception {
48         if (mStartSnapshot != null) {
49             return;
50         }
51         mStartSnapshot = mTestAnalyzer.snapshotTestResult();
52     }
53 
takeEndSnapshot()54     public void takeEndSnapshot() throws Exception {
55         if (mEndSnapshot != null) {
56             return;
57         }
58         mEndSnapshot = mTestAnalyzer.snapshotTestResult();
59     }
60 
checkFullTestResult()61     public void checkFullTestResult() throws Exception {
62         TestResultTable testResult;
63         if (mStartSnapshot == null || mEndSnapshot == null) {
64             throw new IllegalArgumentException("start snapshot or end snapshot is null");
65         }
66         testResult = mTestAnalyzer.getTailDiff(mStartSnapshot, mEndSnapshot);
67         if (testResult == null) {
68             throw new IllegalArgumentException("empty test result");
69         }
70         assertWithMessage("checkFullTestresult")
71                 .that(mTestAnalyzer.checkIfTestResultMatch(mExpected, testResult, false))
72                 .isTrue();
73     }
74 
checkLastTestResultEntry(String testcase, String action, String subject, PowerPolicyDef policy)75     public void checkLastTestResultEntry(String testcase, String action,
76             String subject, PowerPolicyDef policy) throws Exception {
77         TestResultTable.RecordEntry lastEntry = mTestAnalyzer.snapshotTestResult().getLastEntry();
78         assertWithMessage("checkLastTestEntry with policy data")
79                 .that(lastEntry.equalsWithPowerPolicyData(testcase, action, subject, policy))
80                 .isTrue();
81     }
82 
checkLastTestResultEntry(String testcase, String action, String subject, String data)83     public void checkLastTestResultEntry(String testcase, String action,
84             String subject, String data) throws Exception {
85         TestResultTable expected = new TestResultTable();
86         expected.add(testcase, action, subject, data);
87         TestResultTable.RecordEntry lastEntry1 = expected.getLastEntry();
88         TestResultTable.RecordEntry lastEntry2 = mTestAnalyzer.snapshotTestResult().getLastEntry();
89         assertWithMessage("checkLastTestEntry with string data")
90                 .that(lastEntry1.equals(lastEntry2)).isTrue();
91     }
92 
checkLastTestResultEntryData(String testcase, String action, String subject, String data)93     public boolean checkLastTestResultEntryData(String testcase, String action,
94             String subject, String data) throws Exception {
95         TestResultTable expected = new TestResultTable();
96         expected.add(testcase, action, subject, data);
97         TestResultTable.RecordEntry lastEntry1 = expected.getLastEntry();
98         TestResultTable.RecordEntry lastEntry2 = mTestAnalyzer.snapshotTestResult().getLastEntry();
99         return lastEntry1.equalsIgnoreSubject(lastEntry2);
100     }
101 }
102