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.tradefed.metrics.proto.MetricMeasurement.Metric; 19 import com.android.tradefed.result.skipped.SkipReason; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /** 25 * A simplification of ITestLifecycleListener for implementers that only care about individual test 26 * results. 27 * 28 * <p>It filters the various lifecycle events down to a testResult method. 29 * 30 * <p>It is NOT thread safe - and in particular assumes that the ITestLifecycleListener events are 31 * received in order. 32 */ 33 public abstract class TestResultListener implements ITestLifeCycleReceiver { 34 35 private TestDescription mCurrentTest; 36 private TestResult mCurrentResult; 37 testResult(TestDescription test, TestResult result)38 public abstract void testResult(TestDescription test, TestResult result); 39 40 @Override testStarted(TestDescription test, long startTime)41 public final void testStarted(TestDescription test, long startTime) { 42 if (mCurrentTest != null) { 43 // oh noes, previous test do not complete, forward an incomplete event 44 mCurrentResult.setEndTime(System.currentTimeMillis()); 45 mCurrentResult.setStatus(TestStatus.FAILURE); 46 mCurrentResult.setStackTrace("did not complete"); 47 testResult(mCurrentTest, mCurrentResult); 48 mCurrentResult = null; 49 mCurrentTest = null; 50 } 51 mCurrentTest = test; 52 mCurrentResult = new TestResult(); 53 mCurrentResult.setStartTime(startTime); 54 } 55 56 @Override testStarted(TestDescription test)57 public final void testStarted(TestDescription test) { 58 testStarted(test, System.currentTimeMillis()); 59 } 60 61 @Override testFailed(TestDescription test, String trace)62 public final void testFailed(TestDescription test, String trace) { 63 mCurrentResult.setStatus(TestStatus.FAILURE); 64 mCurrentResult.setStackTrace(trace); 65 } 66 67 @Override testAssumptionFailure(TestDescription test, String trace)68 public final void testAssumptionFailure(TestDescription test, String trace) { 69 mCurrentResult.setStatus(TestStatus.ASSUMPTION_FAILURE); 70 mCurrentResult.setStackTrace(trace); 71 } 72 73 @Override testSkipped(TestDescription test, SkipReason reason)74 public void testSkipped(TestDescription test, SkipReason reason) { 75 mCurrentResult.setStatus(TestStatus.SKIPPED); 76 mCurrentResult.setSkipReason(reason); 77 } 78 79 @Override testIgnored(TestDescription test)80 public final void testIgnored(TestDescription test) { 81 mCurrentResult.setStatus(TestStatus.IGNORED); 82 } 83 84 @Override testEnded(TestDescription test, Map<String, String> testMetrics)85 public final void testEnded(TestDescription test, Map<String, String> testMetrics) { 86 mCurrentResult.setMetrics(testMetrics); 87 reportTestFinish(test); 88 } 89 90 @Override testEnded(TestDescription test, HashMap<String, Metric> testMetrics)91 public final void testEnded(TestDescription test, HashMap<String, Metric> testMetrics) { 92 mCurrentResult.setProtoMetrics(testMetrics); 93 reportTestFinish(test); 94 } 95 96 @Override testEnded( TestDescription test, long endTime, Map<String, String> testMetrics)97 public final void testEnded( 98 TestDescription test, long endTime, Map<String, String> testMetrics) { 99 mCurrentResult.setMetrics(testMetrics); 100 reportTestFinish(test, endTime); 101 } 102 103 @Override testEnded( TestDescription test, long endTime, HashMap<String, Metric> testMetrics)104 public final void testEnded( 105 TestDescription test, long endTime, HashMap<String, Metric> testMetrics) { 106 mCurrentResult.setProtoMetrics(testMetrics); 107 reportTestFinish(test, endTime); 108 } 109 110 @Override testRunEnded(long elapsedTimeMillis, HashMap<String, Metric> runMetrics)111 public void testRunEnded(long elapsedTimeMillis, HashMap<String, Metric> runMetrics) { 112 if (mCurrentTest != null) { 113 // last test did not finish! report incomplete 114 mCurrentResult.setEndTime(System.currentTimeMillis()); 115 mCurrentResult.setStatus(TestStatus.FAILURE); 116 mCurrentResult.setStackTrace("did not complete"); 117 testResult(mCurrentTest, mCurrentResult); 118 mCurrentTest = null; 119 mCurrentResult = null; 120 } 121 } 122 reportTestFinish(TestDescription test)123 private void reportTestFinish(TestDescription test) { 124 reportTestFinish(test, System.currentTimeMillis()); 125 } 126 reportTestFinish(TestDescription test, long endTime)127 private void reportTestFinish(TestDescription test, long endTime) { 128 if (mCurrentTest != null 129 && mCurrentTest.equals(test) 130 && TestStatus.INCOMPLETE.equals(mCurrentResult.getResultStatus())) { 131 // If we reach here with no status changed, that means passed otherwise the status 132 // would have been updated to an error 133 mCurrentResult.setStatus(TestStatus.PASSED); 134 } 135 mCurrentResult.setEndTime(endTime); 136 testResult(mCurrentTest, mCurrentResult); 137 mCurrentTest = null; 138 mCurrentResult = null; 139 } 140 } 141