blob: c1f93537edece58528568b4a768eb32654b44987 [file] [log] [blame]
Brett Chabot51a4e3d2010-03-09 11:08:44 -08001/*
2 * Copyright (C) 2010 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 */
16package com.android.tradefed.result;
17
Brett Chabot5ec52922010-09-07 18:08:30 -070018import com.android.ddmlib.testrunner.TestIdentifier;
Brett Chabot873741a2010-09-15 12:24:08 -070019import com.android.tradefed.result.TestResult.TestStatus;
Brett Chabot5ec52922010-09-07 18:08:30 -070020import com.android.tradefed.targetsetup.IBuildInfo;
21
Brett Chabotfeeb2142010-04-16 16:22:52 -070022import java.io.InputStream;
Brett Chabot873741a2010-09-15 12:24:08 -070023import java.util.Collection;
Brett Chabotfccfc612010-03-29 16:08:49 -070024import java.util.Collections;
25import java.util.LinkedHashMap;
Brett Chabot51a4e3d2010-03-09 11:08:44 -080026import java.util.Map;
Brett Chabot51a4e3d2010-03-09 11:08:44 -080027
28/**
Brett Chabot873741a2010-09-15 12:24:08 -070029 * A {@link ITestInvocationListener} that will collect all test results.
30 * <p/>
31 * Although the data structures used in this object are thread-safe, the
32 * {@link ITestInvocationListener} callbacks must be called in the correct order.
Brett Chabot51a4e3d2010-03-09 11:08:44 -080033 */
Brett Chabotfeeb2142010-04-16 16:22:52 -070034public class CollectingTestListener implements ITestInvocationListener {
Brett Chabot51a4e3d2010-03-09 11:08:44 -080035
Brett Chabotfccfc612010-03-29 16:08:49 -070036 // Stores the test results
37 // Uses a synchronized map to make thread safe.
38 // Uses a LinkedHashmap to have predictable iteration order
Brett Chabot873741a2010-09-15 12:24:08 -070039 private Map<String, TestRunResult> mRunResultsMap =
40 Collections.synchronizedMap(new LinkedHashMap<String, TestRunResult>());
41 private TestRunResult mCurrentResults = null;
42
43 // cached test constants
44 private Integer mNumTotalTests = null;
45 private Integer mNumPassedTests = null;
46 private Integer mNumFailedTests = null;
47 private Integer mNumErrorTests = null;
48
49 /**
50 * {@inheritDoc}
51 */
52 public void invocationStarted(IBuildInfo buildInfo) {
53 // ignore
54 }
55
56 /**
57 * {@inheritDoc}
58 */
59 public void testRunStarted(String name, int numTests) {
60 if (mRunResultsMap.containsKey(name)) {
61 // rerun of previous run. Add test results to it
62 mCurrentResults = mRunResultsMap.get(name);
63 } else {
64 // new run
65 mCurrentResults = new TestRunResult(name);
66 mRunResultsMap.put(name, mCurrentResults);
67 }
68 mCurrentResults.setRunComplete(false);
69 mCurrentResults.setRunFailed(false);
70 }
Brett Chabot51a4e3d2010-03-09 11:08:44 -080071
72 /**
73 * {@inheritDoc}
74 */
75 public void testStarted(TestIdentifier test) {
76 // ignore
77 }
78
79 /**
80 * {@inheritDoc}
81 */
Omari Stephens81e6ce02010-08-05 21:58:14 -070082 public void testEnded(TestIdentifier test, Map<String, String> testMetrics) {
Brett Chabot873741a2010-09-15 12:24:08 -070083 if (mCurrentResults == null) {
84 throw new IllegalStateException("testEnded called before testRunStarted");
85 }
Brett Chabot51a4e3d2010-03-09 11:08:44 -080086 // only record test pass if failure not already recorded
Brett Chabot873741a2010-09-15 12:24:08 -070087 if (!mCurrentResults.getTestResults().containsKey(test)) {
88 mCurrentResults.getTestResults().put(test, new TestResult(TestStatus.PASSED));
Brett Chabot51a4e3d2010-03-09 11:08:44 -080089 }
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 public void testFailed(TestFailure status, TestIdentifier test, String trace) {
Brett Chabot873741a2010-09-15 12:24:08 -070096 if (mCurrentResults == null) {
97 throw new IllegalStateException("testFailed called before testRunStarted");
98 }
Brett Chabotfccfc612010-03-29 16:08:49 -070099 if (status.equals(TestFailure.ERROR)) {
Brett Chabot873741a2010-09-15 12:24:08 -0700100 mCurrentResults.getTestResults().put(test, new TestResult(TestStatus.ERROR, trace));
Brett Chabotfccfc612010-03-29 16:08:49 -0700101 } else {
Brett Chabot873741a2010-09-15 12:24:08 -0700102 mCurrentResults.getTestResults().put(test, new TestResult(TestStatus.FAILURE, trace));
Brett Chabotfccfc612010-03-29 16:08:49 -0700103 }
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800104 }
105
106 /**
107 * {@inheritDoc}
108 */
Brett Chabot873741a2010-09-15 12:24:08 -0700109 public void testRunEnded(long elapsedTime, Map<String, String> runMetrics) {
110 if (mCurrentResults == null) {
111 throw new IllegalStateException("testRunEnded called before testRunStarted");
112 }
113 mCurrentResults.setRunComplete(true);
114 mCurrentResults.setMetrics(runMetrics);
115 mCurrentResults.addElapsedTime(elapsedTime);
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800116 }
117
118 /**
119 * {@inheritDoc}
120 */
Brett Chabot873741a2010-09-15 12:24:08 -0700121 public void testRunFailed(String errorMessage) {
122 if (mCurrentResults == null) {
123 throw new IllegalStateException("testRunFailed called before testRunStarted");
124 }
125 mCurrentResults.setRunComplete(true);
126 mCurrentResults.setRunFailed(true);
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800127 }
128
129 /**
130 * {@inheritDoc}
131 */
132 public void testRunStopped(long elapsedTime) {
Brett Chabot873741a2010-09-15 12:24:08 -0700133 if (mCurrentResults == null) {
134 throw new IllegalStateException("testRunStopped called before testRunStarted");
135 }
136 mCurrentResults.setRunComplete(true);
137 mCurrentResults.addElapsedTime(elapsedTime);
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800138 }
139
140 /**
Brett Chabot873741a2010-09-15 12:24:08 -0700141 * Gets the results for the current test run.
Brett Chabot4d081a92010-09-29 16:44:19 -0700142 * <p/>
143 * Its intended to be called once test run is complete (ie {@link #testRunEnded(long, Map)} has
144 * been called). Calling this method before the run has even started
145 * (ie before {@link #testRunStarted(String, int)} has been called) is invalid, and will
146 * produce a {@link IllegalStateException}.
147 *
148 * @return the {@link TestRunResult} representing data collected during last test run
149 * @throws IllegalStateException if no test run data has been collected. This can occur if this
150 * method is called before {@link #testRunStarted(String, int))} has been called.
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800151 */
Brett Chabot873741a2010-09-15 12:24:08 -0700152 public TestRunResult getCurrentRunResults() {
Brett Chabot4d081a92010-09-29 16:44:19 -0700153 if (mCurrentResults == null) {
154 throw new IllegalStateException("no current results");
155 }
Brett Chabot873741a2010-09-15 12:24:08 -0700156 return mCurrentResults;
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800157 }
158
159 /**
Brett Chabot873741a2010-09-15 12:24:08 -0700160 * Gets the results for all test runs.
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800161 */
Brett Chabot873741a2010-09-15 12:24:08 -0700162 public Collection<TestRunResult> getRunResults() {
163 return mRunResultsMap.values();
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800164 }
165
166 /**
Brett Chabot873741a2010-09-15 12:24:08 -0700167 * Gets the total number of tests for all runs.
Guang Zhu42fb2822010-06-17 17:55:28 -0700168 */
Brett Chabot873741a2010-09-15 12:24:08 -0700169 public int getNumTotalTests() {
170 if (!areTestCountsCalculated()) {
171 calculateTestCounts();
172 }
173 return mNumTotalTests;
Guang Zhu42fb2822010-06-17 17:55:28 -0700174 }
175
176 /**
Brett Chabot873741a2010-09-15 12:24:08 -0700177 * Gets the total number of failed tests for all runs.
Brett Chabotfccfc612010-03-29 16:08:49 -0700178 */
179 public int getNumFailedTests() {
Brett Chabot873741a2010-09-15 12:24:08 -0700180 if (!areTestCountsCalculated()) {
181 calculateTestCounts();
182 }
183 return mNumFailedTests;
Brett Chabotfccfc612010-03-29 16:08:49 -0700184 }
185
186 /**
Brett Chabot873741a2010-09-15 12:24:08 -0700187 * Gets the total number of error tests for all runs.
Brett Chabotfccfc612010-03-29 16:08:49 -0700188 */
189 public int getNumErrorTests() {
Brett Chabot873741a2010-09-15 12:24:08 -0700190 if (!areTestCountsCalculated()) {
191 calculateTestCounts();
192 }
193 return mNumErrorTests;
Brett Chabotfccfc612010-03-29 16:08:49 -0700194 }
195
Brett Chabot873741a2010-09-15 12:24:08 -0700196 /**
197 * Gets the total number of passed tests for all runs.
198 */
199 public int getNumPassedTests() {
200 if (!areTestCountsCalculated()) {
201 calculateTestCounts();
202 }
203 return mNumPassedTests;
204 }
205
206 /**
207 * @returns true if invocation had any failed or error tests.
208 */
Brett Chabot057e16e2010-06-27 21:53:48 -0700209 public boolean hasFailedTests() {
210 return getNumErrorTests() > 0 || getNumFailedTests() > 0;
211 }
212
Brett Chabot873741a2010-09-15 12:24:08 -0700213 private synchronized boolean areTestCountsCalculated() {
214 return mNumTotalTests != null;
215 }
216
217 private synchronized void calculateTestCounts() {
218 mNumTotalTests = 0;
219 mNumPassedTests = 0;
220 mNumFailedTests = 0;
221 mNumErrorTests = 0;
222 for (TestRunResult runResult : getRunResults()) {
223 mNumTotalTests += runResult.getNumTests();
224 mNumPassedTests += runResult.getNumPassedTests();
225 mNumFailedTests += runResult.getNumFailedTests();
226 mNumErrorTests += runResult.getNumErrorTests();
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800227 }
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800228 }
Brett Chabotfeeb2142010-04-16 16:22:52 -0700229
230 /**
231 * {@inheritDoc}
232 */
Brett Chabot3759c852010-07-13 17:36:31 -0700233 public void invocationEnded(long elapsedTime) {
Brett Chabotfeeb2142010-04-16 16:22:52 -0700234 // ignore
235 }
236
237 /**
238 * {@inheritDoc}
239 */
Omari Stephens203296c2010-09-02 18:20:45 -0700240 public void invocationFailed(Throwable cause) {
Brett Chabotfeeb2142010-04-16 16:22:52 -0700241 // ignore
242 }
243
244 /**
245 * {@inheritDoc}
246 */
Omari Stephensa01abfe2010-09-07 15:48:44 -0700247 public TestSummary getSummary() {
Brett Chabotfe88a152010-08-02 17:52:59 -0700248 // ignore
Omari Stephens203296c2010-09-02 18:20:45 -0700249 return null;
Brett Chabotfe88a152010-08-02 17:52:59 -0700250 }
251
252 /**
253 * {@inheritDoc}
254 */
Brett Chabot057e16e2010-06-27 21:53:48 -0700255 public void testLog(String dataName, LogDataType dataType, InputStream dataStream) {
Brett Chabotfeeb2142010-04-16 16:22:52 -0700256 // ignore
257 }
Brett Chabot51a4e3d2010-03-09 11:08:44 -0800258}