1 /*
2  * Copyright (C) 2020 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 package com.android.tradefed.result;
17 
18 import com.android.ddmlib.testrunner.TestResult.TestStatus;
19 
20 import java.util.Collection;
21 import java.util.Map;
22 import java.util.concurrent.ConcurrentHashMap;
23 
24 /** A {@link TestResultListener} that tracks the total number of tests by {@link TestStatus} */
25 public class CountingTestResultListener extends TestResultListener {
26 
27     /** Keep a map of results, to accurately track final results after reruns etc */
28     private Map<TestDescription, TestStatus> mResults = new ConcurrentHashMap<>();
29 
30     @Override
testResult(TestDescription test, TestResult result)31     public void testResult(TestDescription test, TestResult result) {
32         mResults.put(test, result.getStatus());
33     }
34 
35     /**
36      * Return the number of PASSED, INCOMPLETE, IGNORED etc tests.
37      *
38      * @return an array, indexed by TestStatus.ordinal(), that stores the number of tests with each
39      *     status
40      */
getResultCounts()41     public int[] getResultCounts() {
42         // calculate the counts as an array indexed by ordinal because its easier/faster
43         int[] results = new int[TestStatus.values().length];
44         Collection<TestStatus> data = mResults.values();
45         for (TestStatus status : data) {
46             results[status.ordinal()]++;
47         }
48         return results;
49     }
50 
51     /** Return the total number of tests executed. */
getTotalTests()52     public int getTotalTests() {
53         return mResults.size();
54     }
55 
56     /**
57      * Helper method to determine if there any failing (one of Incomplete, AssumptionFailure,
58      * Failure) results.
59      */
hasFailedTests()60     public boolean hasFailedTests() {
61         int[] results = getResultCounts();
62         return results[TestStatus.INCOMPLETE.ordinal()] > 0
63                 || results[TestStatus.ASSUMPTION_FAILURE.ordinal()] > 0
64                 || results[TestStatus.FAILURE.ordinal()] > 0;
65     }
66 }
67