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 com.android.tradefed.log.LogUtil.CLog;
20 
21 import java.util.ArrayList;
22 
23 /**
24  * TestResultTable consists of a list of RecordEntry records
25  *
26  * <p>Each record represents one entry line in the device data file,
27  * {@code /storage/emulated/obb/PowerPolicyData.txt}, which records the power
28  * state and policy behavior.
29  */
30 public final class TestResultTable {
31     private final ArrayList<RecordEntry> mTestResults = new ArrayList<RecordEntry>();
32 
size()33     public int size() {
34         return mTestResults.size();
35     }
36 
get(int i)37     public RecordEntry get(int i) throws IndexOutOfBoundsException {
38         return mTestResults.get(i);
39     }
40 
getLastEntry()41     public RecordEntry getLastEntry() {
42         if (mTestResults.isEmpty()) {
43             return null;
44         }
45         return mTestResults.get(mTestResults.size() - 1);
46     }
47 
add(RecordEntry entry)48     public void add(RecordEntry entry) {
49         mTestResults.add(entry);
50     }
51 
add(String testcase, String action, String subject, String data)52     public void add(String testcase, String action, String subject, String data)
53             throws Exception {
54         if (testcase == null || action == null || data == null) {
55             throw new IllegalArgumentException("testcase, action or data can not be null");
56         }
57 
58         add(new RecordEntry(testcase, action, subject, data));
59     }
60 
61     @Override
toString()62     public String toString() {
63         StringBuilder strBuilder = new StringBuilder();
64         mTestResults.forEach(l -> strBuilder.append(l).append('\n'));
65         return strBuilder.toString();
66     }
67 
68     static final class RecordEntry {
69         private final String mTestcase;
70         private final String mAction;
71         private final String mSubject;
72         private final String mData;
73 
RecordEntry(String testcase, String action, String subject, String data)74         private RecordEntry(String testcase, String action, String subject, String data) {
75             mTestcase = testcase;
76             mAction = action;
77             mSubject = subject;
78             mData = data;
79         }
80 
81         @Override
equals(Object obj)82         public boolean equals(Object obj) {
83             RecordEntry peerEntry;
84             if (!(obj instanceof RecordEntry)) {
85                 return false;
86             }
87             peerEntry = (RecordEntry) obj;
88             if ((mSubject == null && null != peerEntry.mSubject)
89                     || (mSubject != null && !mSubject.equals(peerEntry.mSubject))) {
90                 return false;
91             }
92             if (!mTestcase.equals(peerEntry.mTestcase)
93                     || !mAction.equals(peerEntry.mAction)
94                     || !mData.equals(peerEntry.mData)) {
95                 return false;
96             }
97             return true;
98         }
99 
100         @Override
hashCode()101         public int hashCode() {
102             int code = mTestcase.hashCode() + mAction.hashCode() + mData.hashCode();
103             code += mSubject != null ? mSubject.hashCode() : 0;
104             return code;
105         }
106 
107         @Override
toString()108         public String toString() {
109             StringBuilder strBuilder = (new StringBuilder())
110                     .append(mTestcase).append(": ").append(mAction).append(": ");
111             if (mSubject != null) {
112                 strBuilder.append(mSubject).append(": ");
113             }
114             return strBuilder.append(mData).toString();
115         }
116 
equalsWithPowerPolicyData(RecordEntry peerEntry)117         public boolean equalsWithPowerPolicyData(RecordEntry peerEntry) {
118             PowerPolicyDef peerPolicy;
119             try {
120                 peerPolicy = PowerPolicyDef.parse(/* policyDefStr= */ peerEntry.mData,
121                         /* hasPolicyId= */ true, /* offset= */ 0);
122             } catch (Exception e) {
123                 CLog.wtf("failed to parse policy string: " + peerEntry.mData, e);
124                 return false;
125             }
126             return equalsWithPowerPolicyData(peerEntry.mTestcase, peerEntry.mAction,
127                     peerEntry.mSubject, peerPolicy);
128         }
129 
equalsWithPowerPolicyData(String testcase, String action, String subject, PowerPolicyDef policy)130         public boolean equalsWithPowerPolicyData(String testcase, String action,
131                 String subject, PowerPolicyDef policy) {
132             if ((mSubject == null && null != subject)
133                     || (mSubject != null && !mSubject.equals(subject))
134                     || !mTestcase.equals(testcase)
135                     || !mAction.equals(action)) {
136                 return false;
137             }
138 
139             try {
140                 PowerPolicyDef myPolicy = PowerPolicyDef.parse(/* policyDefStr= */ mData,
141                         /* hasPolicyId= */ true, /* offset= */ 0);
142                 if (!myPolicy.equals(policy)) {
143                     return false;
144                 }
145             } catch (Exception e) {
146                 CLog.wtf("failed to parse policy string: " + mData, e);
147                 return false;
148             }
149 
150             return true;
151         }
152     }
153 }
154