1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.testing.local; 6 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; 11 12 /** A json RunListener that creates a Json file with test run information. 13 */ 14 public class JsonListener extends RunListener { 15 16 private final JsonLogger mJsonLogger; 17 private long mTestStartTimeMillis; 18 private boolean mCurrentTestPassed; 19 JsonListener(JsonLogger jsonLogger)20 public JsonListener(JsonLogger jsonLogger) { 21 mJsonLogger = jsonLogger; 22 } 23 24 /** Called after all tests run. 25 */ 26 @Override testRunFinished(Result r)27 public void testRunFinished(Result r) throws Exception { 28 mJsonLogger.writeJsonToFile(); 29 } 30 31 /** Called when a test is about to start. 32 */ 33 @Override testStarted(Description d)34 public void testStarted(Description d) throws Exception { 35 mCurrentTestPassed = true; 36 mTestStartTimeMillis = System.currentTimeMillis(); 37 } 38 39 /** Called when a test has just finished. 40 */ 41 @Override testFinished(Description d)42 public void testFinished(Description d) throws Exception { 43 long testElapsedTimeMillis = System.currentTimeMillis() - mTestStartTimeMillis; 44 mJsonLogger.addTestResultInfo(d, mCurrentTestPassed, testElapsedTimeMillis); 45 } 46 47 /** Called when a test fails. 48 */ 49 @Override testFailure(Failure f)50 public void testFailure(Failure f) throws Exception { 51 mCurrentTestPassed = false; 52 } 53 } 54 55