1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.testing.local;
7 import org.junit.runner.Description;
8 import org.junit.runner.Result;
9 import org.junit.runner.notification.Failure;
10 import org.junit.runner.notification.RunListener;
12 import java.util.HashSet;
15 /** A JUnit RunListener that emulates GTest output to the extent that it can.
17 public class GtestListener extends RunListener {
19 private Set<Description> mFailedTests;
20 private final GtestLogger mLogger;
21 private long mRunStartTimeMillis;
22 private long mTestStartTimeMillis;
23 private int mTestsPassed;
24 private boolean mCurrentTestPassed;
26 public GtestListener(GtestLogger logger) {
30 /** Called before any tests run.
33 public void testRunStarted(Description d) throws Exception {
34 mLogger.testRunStarted(d.testCount());
35 mRunStartTimeMillis = System.currentTimeMillis();
37 mFailedTests = new HashSet<Description>();
38 mCurrentTestPassed = true;
41 /** Called after all tests run.
44 public void testRunFinished(Result r) throws Exception {
45 long elapsedTimeMillis = System.currentTimeMillis() - mRunStartTimeMillis;
46 mLogger.testRunFinished(mTestsPassed, mFailedTests, elapsedTimeMillis);
49 /** Called when a test is about to start.
52 public void testStarted(Description d) throws Exception {
53 mCurrentTestPassed = true;
54 mLogger.testStarted(d);
55 mTestStartTimeMillis = System.currentTimeMillis();
58 /** Called when a test has just finished.
61 public void testFinished(Description d) throws Exception {
62 long testElapsedTimeMillis = System.currentTimeMillis() - mTestStartTimeMillis;
63 mLogger.testFinished(d, mCurrentTestPassed, testElapsedTimeMillis);
64 if (mCurrentTestPassed) {
71 /** Called when a test fails.
74 public void testFailure(Failure f) throws Exception {
75 mCurrentTestPassed = false;
76 mLogger.testFailed(f);