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 android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.platform.test.annotations.DisabledOnRavenwood; 21 import android.platform.test.annotations.EnabledOnRavenwood; 22 23 import org.junit.runner.Description; 24 25 /** 26 * Calculates which tests need to be executed on Ravenwood. 27 */ 28 public class RavenwoodEnablementChecker { RavenwoodEnablementChecker()29 private RavenwoodEnablementChecker() { 30 } 31 32 /** 33 * Determine if the given {@link Description} should be enabled when running on the 34 * Ravenwood test environment. 35 * 36 * A more specific method-level annotation always takes precedence over any class-level 37 * annotation, and an {@link EnabledOnRavenwood} annotation always takes precedence over 38 * an {@link DisabledOnRavenwood} annotation. 39 */ shouldEnableOnRavenwood(Description description, boolean takeIntoAccountRunDisabledTestsFlag)40 public static boolean shouldEnableOnRavenwood(Description description, 41 boolean takeIntoAccountRunDisabledTestsFlag) { 42 // First, consult any method-level annotations 43 if (description.isTest()) { 44 Boolean result = null; 45 46 // Stopgap for http://g/ravenwood/EPAD-N5ntxM 47 if (description.getMethodName().endsWith("$noRavenwood")) { 48 result = false; 49 } else if (description.getAnnotation(EnabledOnRavenwood.class) != null) { 50 result = true; 51 } else if (description.getAnnotation(DisabledOnRavenwood.class) != null) { 52 result = false; 53 } 54 if (result != null) { 55 if (takeIntoAccountRunDisabledTestsFlag 56 && RavenwoodRule.private$ravenwood().isRunningDisabledTests()) { 57 result = !shouldStillIgnoreInProbeIgnoreMode( 58 description.getTestClass(), description.getMethodName()); 59 } 60 } 61 if (result != null) { 62 return result; 63 } 64 } 65 66 // Otherwise, consult any class-level annotations 67 return shouldRunClassOnRavenwood(description.getTestClass(), 68 takeIntoAccountRunDisabledTestsFlag); 69 } 70 shouldRunClassOnRavenwood(@onNull Class<?> testClass, boolean takeIntoAccountRunDisabledTestsFlag)71 public static boolean shouldRunClassOnRavenwood(@NonNull Class<?> testClass, 72 boolean takeIntoAccountRunDisabledTestsFlag) { 73 boolean result = true; 74 if (testClass.getAnnotation(EnabledOnRavenwood.class) != null) { 75 result = true; 76 } else if (testClass.getAnnotation(DisabledOnRavenwood.class) != null) { 77 result = false; 78 } 79 if (!result) { 80 if (takeIntoAccountRunDisabledTestsFlag 81 && RavenwoodRule.private$ravenwood().isRunningDisabledTests()) { 82 result = !shouldStillIgnoreInProbeIgnoreMode(testClass, null); 83 } 84 } 85 return result; 86 } 87 88 /** 89 * Check if a test should _still_ disabled even if {@code RUN_DISABLED_TESTS} 90 * is true, using {@code REALLY_DISABLED_PATTERN}. 91 * 92 * This only works on tests, not on classes. 93 */ shouldStillIgnoreInProbeIgnoreMode( @onNull Class<?> testClass, @Nullable String methodName)94 static boolean shouldStillIgnoreInProbeIgnoreMode( 95 @NonNull Class<?> testClass, @Nullable String methodName) { 96 if (RavenwoodRule.private$ravenwood().getReallyDisabledPattern().pattern().isEmpty()) { 97 return false; 98 } 99 100 final var fullname = testClass.getName() + (methodName != null ? "#" + methodName : ""); 101 102 System.out.println("XXX=" + fullname); 103 104 if (RavenwoodRule.private$ravenwood().getReallyDisabledPattern().matcher(fullname).find()) { 105 System.out.println("Still ignoring " + fullname); 106 return true; 107 } 108 return false; 109 } 110 } 111