1 package com.android.cts.verifier;
2 
3 import com.android.compatibility.common.util.TestResultHistory;
4 
5 import java.io.Serializable;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Set;
10 
11 public class TestResultHistoryCollection implements Serializable {
12 
13     private static final long serialVersionUID = 0L;
14     private final Set<TestResultHistory> mHistoryCollection = new HashSet<>();
15 
16     /**
17      * Covert object to set.
18      *
19      * @return A set of test result history.
20      */
asSet()21     public Set<TestResultHistory> asSet() {
22         return mHistoryCollection;
23     }
24 
25     /**
26      * Add a test result history with test name, start time, end time and isAutomated.
27      *
28      * @param test a string of test name.
29      * @param start start time of a test.
30      * @param end end time of a test.
31      * @param isAutomated whether test case was executed through automation.
32      */
add(String test, long start, long end, boolean isAutomated)33     public void add(String test, long start, long end, boolean isAutomated) {
34         Set<TestResultHistory.ExecutionRecord> executionRecords
35                 = new HashSet<TestResultHistory.ExecutionRecord> ();
36         executionRecords.add(new TestResultHistory.ExecutionRecord(start, end, isAutomated));
37         mHistoryCollection.add(new TestResultHistory(test, executionRecords));
38     }
39 
40     /**
41      * Add test result histories for tests containing test name and a set of ExecutionRecords
42      *
43      * @param test test name.
44      * @param executionRecords set of ExecutionRecords.
45      */
addAll(String test, Set<TestResultHistory.ExecutionRecord> executionRecords)46     public void addAll(String test, Set<TestResultHistory.ExecutionRecord> executionRecords) {
47         TestResultHistory history = new TestResultHistory(test, executionRecords);
48         boolean match = false;
49         for (TestResultHistory resultHistory: mHistoryCollection) {
50             if (resultHistory.getTestName().equals(test)) {
51                 resultHistory.getExecutionRecords().addAll(executionRecords);
52                 match = true;
53                 break;
54             }
55         }
56         if (match == false) {
57             mHistoryCollection.add(history);
58         }
59     }
60 
61     /**
62      * Merge test with its sub-tests result histories.
63      *
64      * @param prefix optional test name prefix to apply.
65      * @param resultHistoryCollection a set of test result histories.
66      */
merge(String prefix, TestResultHistoryCollection resultHistoryCollection)67     public void merge(String prefix, TestResultHistoryCollection resultHistoryCollection) {
68         if (resultHistoryCollection != null) {
69             resultHistoryCollection.asSet().forEach(t-> addAll(
70                 prefix != null
71                         ? prefix + ":" + t.getTestName()
72                         : t.getTestName(), t.getExecutionRecords()));
73         }
74     }
75 
76     /**
77      * Merge test with its sub-tests result histories.
78      *
79      * @param prefix optional test name prefix to apply.
80      * @param resultHistories a list of test result history collection.
81      */
merge(String prefix, List<TestResultHistoryCollection> resultHistories)82     public void merge(String prefix, List<TestResultHistoryCollection> resultHistories) {
83         resultHistories.forEach(resultHistoryCollection -> merge(prefix, resultHistoryCollection));
84     }
85 }
86