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 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 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 reportTestFinish(null); 45 } 46 mCurrentTest = test; 47 mCurrentResult = new TestResult(); 48 mCurrentResult.setStartTime(startTime); 49 } 50 51 @Override testStarted(TestDescription test)52 public final void testStarted(TestDescription test) { 53 testStarted(test, System.currentTimeMillis()); 54 } 55 56 @Override testFailed(TestDescription test, String trace)57 public final void testFailed(TestDescription test, String trace) { 58 mCurrentResult.setStatus(TestStatus.FAILURE); 59 mCurrentResult.setStackTrace(trace); 60 } 61 62 @Override testAssumptionFailure(TestDescription test, String trace)63 public final void testAssumptionFailure(TestDescription test, String trace) { 64 mCurrentResult.setStatus(TestStatus.ASSUMPTION_FAILURE); 65 mCurrentResult.setStackTrace(trace); 66 } 67 68 @Override testIgnored(TestDescription test)69 public final void testIgnored(TestDescription test) { 70 mCurrentResult.setStatus(com.android.ddmlib.testrunner.TestResult.TestStatus.IGNORED); 71 } 72 73 @Override testEnded(TestDescription test, Map<String, String> testMetrics)74 public final void testEnded(TestDescription test, Map<String, String> testMetrics) { 75 mCurrentResult.setMetrics(testMetrics); 76 reportTestFinish(test); 77 } 78 79 @Override testEnded(TestDescription test, HashMap<String, Metric> testMetrics)80 public final void testEnded(TestDescription test, HashMap<String, Metric> testMetrics) { 81 mCurrentResult.setProtoMetrics(testMetrics); 82 reportTestFinish(test); 83 } 84 85 @Override testEnded( TestDescription test, long endTime, Map<String, String> testMetrics)86 public final void testEnded( 87 TestDescription test, long endTime, Map<String, String> testMetrics) { 88 mCurrentResult.setMetrics(testMetrics); 89 reportTestFinish(test, endTime); 90 } 91 92 @Override testEnded( TestDescription test, long endTime, HashMap<String, Metric> testMetrics)93 public final void testEnded( 94 TestDescription test, long endTime, HashMap<String, Metric> testMetrics) { 95 mCurrentResult.setProtoMetrics(testMetrics); 96 reportTestFinish(test, endTime); 97 } 98 99 @Override testRunEnded(long elapsedTimeMillis, HashMap<String, Metric> runMetrics)100 public void testRunEnded(long elapsedTimeMillis, HashMap<String, Metric> runMetrics) { 101 if (mCurrentTest != null) { 102 // last test did not finish! report incomplete 103 mCurrentResult.setEndTime(System.currentTimeMillis()); 104 testResult(mCurrentTest, mCurrentResult); 105 mCurrentTest = null; 106 mCurrentResult = null; 107 } 108 } 109 reportTestFinish(TestDescription test)110 private void reportTestFinish(TestDescription test) { 111 reportTestFinish(test, System.currentTimeMillis()); 112 } 113 reportTestFinish(TestDescription test, long endTime)114 private void reportTestFinish(TestDescription test, long endTime) { 115 if (mCurrentTest != null 116 && mCurrentTest.equals(test) 117 && mCurrentResult.getStatus() == TestStatus.INCOMPLETE) { 118 mCurrentResult.setStatus(TestStatus.PASSED); 119 } 120 mCurrentResult.setEndTime(endTime); 121 testResult(mCurrentTest, mCurrentResult); 122 mCurrentTest = null; 123 mCurrentResult = null; 124 } 125 } 126