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 equalsIgnoreSubject(RecordEntry peerEntry)117 public boolean equalsIgnoreSubject(RecordEntry peerEntry) { 118 return mTestcase.equals(peerEntry.mTestcase) 119 && mAction.equals(peerEntry.mAction) 120 && mData.equals(peerEntry.mData); 121 } 122 equalsWithPowerPolicyData(RecordEntry peerEntry)123 public boolean equalsWithPowerPolicyData(RecordEntry peerEntry) { 124 PowerPolicyDef peerPolicy; 125 try { 126 peerPolicy = PowerPolicyDef.parse(/* policyDefStr= */ peerEntry.mData, 127 /* hasPolicyId= */ true, /* offset= */ 0); 128 } catch (Exception e) { 129 CLog.wtf("failed to parse policy string: " + peerEntry.mData, e); 130 return false; 131 } 132 return equalsWithPowerPolicyData(peerEntry.mTestcase, peerEntry.mAction, 133 peerEntry.mSubject, peerPolicy); 134 } 135 equalsWithPowerPolicyData(String testcase, String action, String subject, PowerPolicyDef policy)136 public boolean equalsWithPowerPolicyData(String testcase, String action, 137 String subject, PowerPolicyDef policy) { 138 if ((mSubject == null && null != subject) 139 || (mSubject != null && !mSubject.equals(subject)) 140 || !mTestcase.equals(testcase) 141 || !mAction.equals(action)) { 142 return false; 143 } 144 145 try { 146 PowerPolicyDef myPolicy = PowerPolicyDef.parse(/* policyDefStr= */ mData, 147 /* hasPolicyId= */ true, /* offset= */ 0); 148 if (!myPolicy.equals(policy)) { 149 return false; 150 } 151 } catch (Exception e) { 152 CLog.wtf("failed to parse policy string: " + mData, e); 153 return false; 154 } 155 156 return true; 157 } 158 } 159 } 160