1 /* 2 * Copyright (C) 2024 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 android.platform.test.ravenwood; 17 18 import static com.android.ravenwood.common.RavenwoodCommonUtils.RAVENWOOD_VERBOSE_LOGGING; 19 20 import android.platform.test.annotations.internal.InnerRunner; 21 import android.util.Log; 22 23 import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; 24 import org.junit.runner.Description; 25 import org.junit.runner.Runner; 26 import org.junit.runner.manipulation.Filter; 27 import org.junit.runner.manipulation.Filterable; 28 import org.junit.runner.manipulation.InvalidOrderingException; 29 import org.junit.runner.manipulation.NoTestsRemainException; 30 import org.junit.runner.manipulation.Orderable; 31 import org.junit.runner.manipulation.Orderer; 32 import org.junit.runner.manipulation.Sortable; 33 import org.junit.runner.manipulation.Sorter; 34 import org.junit.runner.notification.RunNotifier; 35 import org.junit.runners.BlockJUnit4ClassRunner; 36 import org.junit.runners.model.RunnerBuilder; 37 import org.junit.runners.model.TestClass; 38 39 abstract class RavenwoodAwareTestRunnerBase extends Runner implements Filterable, Orderable { 40 public static final String TAG = com.android.ravenwood.common.RavenwoodCommonUtils.TAG; 41 42 boolean mRealRunnerTakesRunnerBuilder = false; 43 getRealRunner()44 abstract Runner getRealRunner(); 45 instantiateRealRunner(TestClass testClass)46 final Runner instantiateRealRunner(TestClass testClass) { 47 // Find the real runner. 48 final Class<? extends Runner> runnerClass; 49 final InnerRunner innerRunnerAnnotation = testClass.getAnnotation(InnerRunner.class); 50 if (innerRunnerAnnotation != null) { 51 runnerClass = innerRunnerAnnotation.value(); 52 } else { 53 // Default runner. 54 runnerClass = BlockJUnit4ClassRunner.class; 55 } 56 57 try { 58 if (RAVENWOOD_VERBOSE_LOGGING) { 59 Log.v(TAG, "Initializing the inner runner: " + runnerClass); 60 } 61 try { 62 return runnerClass.getConstructor(Class.class) 63 .newInstance(testClass.getJavaClass()); 64 } catch (NoSuchMethodException e) { 65 var constructor = runnerClass.getConstructor(Class.class, RunnerBuilder.class); 66 mRealRunnerTakesRunnerBuilder = true; 67 return constructor.newInstance( 68 testClass.getJavaClass(), new AllDefaultPossibilitiesBuilder()); 69 } 70 } catch (ReflectiveOperationException e) { 71 throw logAndFail("Failed to instantiate " + runnerClass, e); 72 } 73 } 74 logAndFail(String message, Throwable exception)75 final Error logAndFail(String message, Throwable exception) { 76 Log.e(TAG, message, exception); 77 return new AssertionError(message, exception); 78 } 79 80 @Override getDescription()81 public final Description getDescription() { 82 return getRealRunner().getDescription(); 83 } 84 85 @Override filter(Filter filter)86 public final void filter(Filter filter) throws NoTestsRemainException { 87 if (getRealRunner() instanceof Filterable r) { 88 r.filter(filter); 89 } 90 } 91 92 @Override order(Orderer orderer)93 public final void order(Orderer orderer) throws InvalidOrderingException { 94 if (getRealRunner() instanceof Orderable r) { 95 r.order(orderer); 96 } 97 } 98 99 @Override sort(Sorter sorter)100 public final void sort(Sorter sorter) { 101 if (getRealRunner() instanceof Sortable r) { 102 r.sort(sorter); 103 } 104 } 105 } 106