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 android.car.cts.PowerPolicyHostTest;
20 
21 public final class PowerPolicyTestAnalyzer {
22     private static final String TEST_RESULT_PREFIX = "PowerPolicyTestResult";
23     private final PowerPolicyHostTest mHostTest;
24 
PowerPolicyTestAnalyzer(PowerPolicyHostTest hostTest)25     public PowerPolicyTestAnalyzer(PowerPolicyHostTest hostTest) {
26         mHostTest = hostTest;
27     }
28 
29     /**
30      * Compares results.
31      */
checkIfTestResultMatch(TestResultTable result1, TestResultTable result2, boolean withPowerPolicyData)32     public static boolean checkIfTestResultMatch(TestResultTable result1,
33             TestResultTable result2, boolean withPowerPolicyData) {
34         if ((result1 == null || result2 == null)) {
35             return result1 == result2;
36         }
37 
38         int size = result1.size();
39         if (size != result2.size()) {
40             return false;
41         }
42         for (int i = 0; i < size; i++) {
43             TestResultTable.RecordEntry entry1 = result1.get(i);
44             TestResultTable.RecordEntry entry2 = result2.get(i);
45             boolean status = withPowerPolicyData
46                     ? entry1.equalsWithPowerPolicyData(entry2) : entry1.equals(entry2);
47             if (!status) {
48                 return false;
49             }
50         }
51         return true;
52     }
53 
snapshotTestResult()54     public TestResultTable snapshotTestResult() throws Exception {
55         String shellOutput = mHostTest.fetchActivityDumpsys();
56         return getTestResults(shellOutput, false);
57     }
58 
getLatestTestResultEntry()59     public TestResultTable getLatestTestResultEntry() throws Exception {
60         String shellOutput = mHostTest.fetchActivityDumpsys();
61         return getTestResults(shellOutput, true);
62     }
63 
getTestResults(String shellOutput, boolean onlyLastEntry)64     private TestResultTable getTestResults(String shellOutput, boolean onlyLastEntry)
65             throws Exception {
66         TestResultTable snapshot = new TestResultTable();
67         String[] lines = shellOutput.split("\n");
68 
69         int i = 0;
70         if (onlyLastEntry) {
71             i = lines.length == 0 ? 0 : lines.length - 1;
72         }
73         for (; i < lines.length; i++) {
74             parseAndAddTestEntry(snapshot, lines[i]);
75         }
76 
77         return snapshot;
78     }
79 
parseAndAddTestEntry(TestResultTable results, String line)80     private void parseAndAddTestEntry(TestResultTable results, String line) throws Exception {
81         if (!line.trim().startsWith(TEST_RESULT_PREFIX)) {
82             return;
83         }
84 
85         String[] tokens = line.split("##");
86         if (tokens.length != 2) {
87             throw new IllegalArgumentException("malformatted result entry: " + line);
88         }
89         String header = tokens[0].trim();
90         String data = tokens[1].trim();
91         String[] hdrTokens = header.split(":\\s*");
92         if (hdrTokens.length != 3 && hdrTokens.length != 4) {
93             throw new IllegalArgumentException("malformatted result header: " + line);
94         }
95 
96         String subject = hdrTokens.length == 3 ? null : hdrTokens[3];
97         results.add(hdrTokens[1], hdrTokens[2], subject, data);
98     }
99 
100     /**
101      * Subtract the common front TestResultEntry items.
102      */
getDiff(TestResultTable result1, TestResultTable result2)103     public static TestResultTable getDiff(TestResultTable result1, TestResultTable result2) {
104         TestResultTable diff;
105 
106         if (result1 != null && result2 != null) {
107             TestResultTable longResult = result1;
108             TestResultTable shortResult = result2;
109             if (longResult.size() < shortResult.size()) {
110                 longResult = result2;
111                 shortResult = result1;
112             }
113             int shortSize = shortResult.size();
114             int longSize = longResult.size();
115             int idx = 0;
116             diff = new TestResultTable();
117             for (; idx < shortSize; idx++) {
118                 if (!shortResult.get(idx).equals(longResult.get(idx))) {
119                     break;
120                 }
121             }
122             for (; idx < longSize; idx++) {
123                 diff.add(longResult.get(idx));
124             }
125         } else if (result1 == null) {
126             diff = result2;
127         } else {
128             diff = result1;
129         }
130         return diff;
131     }
132 
getTailDiff(TestResultTable result1, TestResultTable result2)133     public TestResultTable getTailDiff(TestResultTable result1, TestResultTable result2) {
134         TestResultTable diff;
135 
136         if (result1 != null && result2 != null) {
137             TestResultTable longResult = result1;
138             TestResultTable shortResult = result2;
139             if (longResult.size() < shortResult.size()) {
140                 longResult = result2;
141                 shortResult = result1;
142             }
143             int shortSize = shortResult.size();
144             int longSize = longResult.size();
145             diff = new TestResultTable();
146             for (int idx = shortSize; idx < longSize; idx++) {
147                 diff.add(longResult.get(idx));
148             }
149         } else if (result1 == null) {
150             diff = result2;
151         } else {
152             diff = result1;
153         }
154         return diff;
155     }
156 }
157