1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android; 16 17 import static org.hamcrest.Matchers.empty; 18 import static org.hamcrest.Matchers.is; 19 import static org.junit.Assert.assertThat; 20 21 import android.testing.AndroidTestingRunner; 22 import android.text.TextUtils; 23 import android.util.Log; 24 25 import androidx.test.filters.LargeTest; 26 import androidx.test.filters.MediumTest; 27 import androidx.test.filters.SmallTest; 28 import androidx.test.internal.runner.ClassPathScanner; 29 import androidx.test.internal.runner.ClassPathScanner.ChainedClassNameFilter; 30 import androidx.test.internal.runner.ClassPathScanner.ExternalClassNameFilter; 31 32 import com.android.systemui.SysuiBaseFragmentTest; 33 import com.android.systemui.SysuiTestCase; 34 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 import java.io.IOException; 39 import java.lang.reflect.Method; 40 import java.lang.reflect.Modifier; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.Collection; 44 import java.util.Collections; 45 46 /** 47 * This is named AAAPlusPlusVerifySysuiRequiredTestPropertiesTest for two reasons. 48 * a) Its so awesome it deserves an AAA++ 49 * b) It should run first to draw attention to itself. 50 * 51 * For trues though: this test verifies that all the sysui tests extend the right classes. 52 * This matters because including tests with different context implementations in the same 53 * test suite causes errors, such as the incorrect settings provider being cached. 54 * For an example, see {@link com.android.systemui.DependencyTest}. 55 */ 56 @RunWith(AndroidTestingRunner.class) 57 @SmallTest 58 public class AAAPlusPlusVerifySysuiRequiredTestPropertiesTest extends SysuiTestCase { 59 60 private static final String TAG = "AAA++VerifyTest"; 61 62 private static final Class[] BASE_CLS_WHITELIST = { 63 SysuiTestCase.class, 64 SysuiBaseFragmentTest.class, 65 }; 66 67 private static final Class[] SUPPORTED_SIZES = { 68 SmallTest.class, 69 MediumTest.class, 70 LargeTest.class, 71 android.test.suitebuilder.annotation.SmallTest.class, 72 android.test.suitebuilder.annotation.MediumTest.class, 73 android.test.suitebuilder.annotation.LargeTest.class, 74 }; 75 76 @Test testAllClassInheritance()77 public void testAllClassInheritance() throws Throwable { 78 ArrayList<String> fails = new ArrayList<>(); 79 for (String className : getClassNamesFromClassPath()) { 80 Class<?> cls = Class.forName(className, false, this.getClass().getClassLoader()); 81 if (!isTestClass(cls)) continue; 82 83 boolean hasParent = false; 84 for (Class<?> parent : BASE_CLS_WHITELIST) { 85 if (parent.isAssignableFrom(cls)) { 86 hasParent = true; 87 break; 88 } 89 } 90 boolean hasSize = hasSize(cls); 91 if (!hasSize) { 92 fails.add(cls.getName() + " does not have size annotation, such as @SmallTest"); 93 } 94 if (!hasParent) { 95 fails.add(cls.getName() + " does not extend any of " + getClsStr()); 96 } 97 } 98 99 assertThat("All sysui test classes must have size and extend one of " + getClsStr(), 100 fails, is(empty())); 101 } 102 hasSize(Class<?> cls)103 private boolean hasSize(Class<?> cls) { 104 for (int i = 0; i < SUPPORTED_SIZES.length; i++) { 105 if (cls.getDeclaredAnnotation(SUPPORTED_SIZES[i]) != null) return true; 106 } 107 return false; 108 } 109 getClassNamesFromClassPath()110 private Collection<String> getClassNamesFromClassPath() { 111 ClassPathScanner scanner = new ClassPathScanner(mContext.getPackageCodePath()); 112 113 ChainedClassNameFilter filter = new ChainedClassNameFilter(); 114 115 filter.add(new ExternalClassNameFilter()); 116 filter.add(s -> s.startsWith("com.android.systemui") 117 || s.startsWith("com.android.keyguard")); 118 119 try { 120 return scanner.getClassPathEntries(filter); 121 } catch (IOException e) { 122 Log.e(TAG, "Failed to scan classes", e); 123 } 124 return Collections.emptyList(); 125 } 126 getClsStr()127 private String getClsStr() { 128 return TextUtils.join(",", Arrays.asList(BASE_CLS_WHITELIST) 129 .stream().map(cls -> cls.getSimpleName()).toArray()); 130 } 131 132 /** 133 * Determines if given class is a valid test class. 134 * 135 * @param loadedClass 136 * @return <code>true</code> if loadedClass is a test 137 */ isTestClass(Class<?> loadedClass)138 private boolean isTestClass(Class<?> loadedClass) { 139 try { 140 if (Modifier.isAbstract(loadedClass.getModifiers())) { 141 logDebug(String.format("Skipping abstract class %s: not a test", 142 loadedClass.getName())); 143 return false; 144 } 145 // TODO: try to find upstream junit calls to replace these checks 146 if (junit.framework.Test.class.isAssignableFrom(loadedClass)) { 147 // ensure that if a TestCase, it has at least one test method otherwise 148 // TestSuite will throw error 149 if (junit.framework.TestCase.class.isAssignableFrom(loadedClass)) { 150 return hasJUnit3TestMethod(loadedClass); 151 } 152 return true; 153 } 154 // TODO: look for a 'suite' method? 155 if (loadedClass.isAnnotationPresent(org.junit.runner.RunWith.class)) { 156 return true; 157 } 158 for (Method testMethod : loadedClass.getMethods()) { 159 if (testMethod.isAnnotationPresent(org.junit.Test.class)) { 160 return true; 161 } 162 } 163 logDebug(String.format("Skipping class %s: not a test", loadedClass.getName())); 164 return false; 165 } catch (Exception e) { 166 // Defensively catch exceptions - Will throw runtime exception if it cannot load methods. 167 // For earlier versions of Android (Pre-ICS), Dalvik might try to initialize a class 168 // during getMethods(), fail to do so, hide the error and throw a NoSuchMethodException. 169 // Since the java.lang.Class.getMethods does not declare such an exception, resort to a 170 // generic catch all. 171 // For ICS+, Dalvik will throw a NoClassDefFoundException. 172 Log.w(TAG, String.format("%s in isTestClass for %s", e.toString(), 173 loadedClass.getName())); 174 return false; 175 } catch (Error e) { 176 // defensively catch Errors too 177 Log.w(TAG, String.format("%s in isTestClass for %s", e.toString(), 178 loadedClass.getName())); 179 return false; 180 } 181 } 182 hasJUnit3TestMethod(Class<?> loadedClass)183 private boolean hasJUnit3TestMethod(Class<?> loadedClass) { 184 for (Method testMethod : loadedClass.getMethods()) { 185 if (isPublicTestMethod(testMethod)) { 186 return true; 187 } 188 } 189 return false; 190 } 191 192 // copied from junit.framework.TestSuite isPublicTestMethod(Method m)193 private boolean isPublicTestMethod(Method m) { 194 return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); 195 } 196 197 // copied from junit.framework.TestSuite isTestMethod(Method m)198 private boolean isTestMethod(Method m) { 199 return m.getParameterTypes().length == 0 && m.getName().startsWith("test") 200 && m.getReturnType().equals(Void.TYPE); 201 } 202 203 /** 204 * Utility method for logging debug messages. Only actually logs a message if TAG is marked 205 * as loggable to limit log spam during normal use. 206 */ logDebug(String msg)207 private void logDebug(String msg) { 208 if (Log.isLoggable(TAG, Log.DEBUG)) { 209 Log.d(TAG, msg); 210 } 211 } 212 } 213