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