1 /* 2 * Copyright (C) 2022 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.car.test; 17 18 import static com.google.common.truth.Truth.assertWithMessage; 19 20 import android.util.Log; 21 22 import org.junit.runner.Description; 23 import org.junit.runners.model.Statement; 24 25 import java.lang.annotation.Annotation; 26 27 /** 28 * Provides common {@code JUnit} artifacts for the tests. 29 * 30 */ 31 final class JUnitHelper { 32 33 private static final String TAG = JUnitHelper.class.getSimpleName(); 34 35 // Not a real test (i.e., it doesn't exist on this class), but it's passed to Description 36 private static final String TEST_METHOD_BEING_EXECUTED = "testAmI..OrNot"; 37 newTestMethod(Annotation... annotations)38 public static Description newTestMethod(Annotation... annotations) { 39 return newTestMethod(TEST_METHOD_BEING_EXECUTED, annotations); 40 } 41 newTestMethod(String methodName, Annotation... annotations)42 public static Description newTestMethod(String methodName, Annotation... annotations) { 43 return Description.createTestDescription(PermissionsCheckerRuleTest.class, 44 methodName, annotations); 45 } 46 47 public static final class SimpleStatement<T extends Exception> extends Statement { 48 49 private boolean mEvaluated; 50 private Throwable mThrowable; 51 52 @Override evaluate()53 public void evaluate() throws Throwable { 54 Log.d(TAG, "evaluate() called"); 55 mEvaluated = true; 56 if (mThrowable != null) { 57 Log.d(TAG, "Throwing " + mThrowable); 58 throw mThrowable; 59 } 60 } 61 failWith(Throwable t)62 public void failWith(Throwable t) { 63 mThrowable = t; 64 } 65 assertEvaluated()66 public void assertEvaluated() { 67 assertWithMessage("test method called").that(mEvaluated).isTrue(); 68 } 69 } 70 JUnitHelper()71 private JUnitHelper() { 72 throw new UnsupportedOperationException("contains only static members"); 73 } 74 } 75