1 // Copyright 2022 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.Computer; 8 import org.junit.runner.Description; 9 import org.junit.runner.Runner; 10 import org.junit.runner.manipulation.Filter; 11 import org.junit.runner.manipulation.Filterable; 12 import org.junit.runner.manipulation.NoTestsRemainException; 13 import org.junit.runner.notification.RunNotifier; 14 import org.junit.runners.model.InitializationError; 15 import org.junit.runners.model.RunnerBuilder; 16 17 import java.io.PrintStream; 18 19 class TestListComputer extends Computer { 20 private final PrintStream mOutputStream; 21 TestListComputer(PrintStream mOutputStream)22 TestListComputer(PrintStream mOutputStream) { 23 this.mOutputStream = mOutputStream; 24 } 25 26 private class TestListRunner extends Runner implements Filterable { 27 private final Runner mRunner; 28 TestListRunner(Runner contained)29 public TestListRunner(Runner contained) { 30 mRunner = contained; 31 } 32 33 @Override getDescription()34 public Description getDescription() { 35 return mRunner.getDescription(); 36 } 37 printTests(Description description)38 private void printTests(Description description) { 39 if (description.getMethodName() != null) { 40 mOutputStream.println( 41 "#TEST# " + description.getClassName() + '#' + description.getMethodName()); 42 } 43 for (Description child : description.getChildren()) { 44 printTests(child); 45 } 46 } 47 48 @Override run(RunNotifier notifier)49 public void run(RunNotifier notifier) { 50 printTests(mRunner.getDescription()); 51 } 52 53 @Override filter(Filter filter)54 public void filter(Filter filter) throws NoTestsRemainException { 55 if (mRunner instanceof Filterable) { 56 ((Filterable) mRunner).filter(filter); 57 } 58 } 59 } 60 61 @Override getSuite(final RunnerBuilder builder, Class<?>[] classes)62 public Runner getSuite(final RunnerBuilder builder, Class<?>[] classes) 63 throws InitializationError { 64 return super.getSuite(new RunnerBuilder() { 65 @Override 66 public Runner runnerForClass(Class<?> testClass) throws Throwable { 67 return new TestListRunner(builder.runnerForClass(testClass)); 68 } 69 }, classes); 70 } 71 } 72