summaryrefslogtreecommitdiff
path: root/packages/SystemUI/tests/src/com/android/AAAPlusPlusVerifySysuiRequiredTestPropertiesTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'packages/SystemUI/tests/src/com/android/AAAPlusPlusVerifySysuiRequiredTestPropertiesTest.java')
-rw-r--r--packages/SystemUI/tests/src/com/android/AAAPlusPlusVerifySysuiRequiredTestPropertiesTest.java92
1 files changed, 87 insertions, 5 deletions
diff --git a/packages/SystemUI/tests/src/com/android/AAAPlusPlusVerifySysuiRequiredTestPropertiesTest.java b/packages/SystemUI/tests/src/com/android/AAAPlusPlusVerifySysuiRequiredTestPropertiesTest.java
index b597868fb22b..fe235410cc26 100644
--- a/packages/SystemUI/tests/src/com/android/AAAPlusPlusVerifySysuiRequiredTestPropertiesTest.java
+++ b/packages/SystemUI/tests/src/com/android/AAAPlusPlusVerifySysuiRequiredTestPropertiesTest.java
@@ -22,7 +22,6 @@ import android.support.test.filters.SmallTest;
import android.support.test.internal.runner.ClassPathScanner;
import android.support.test.internal.runner.ClassPathScanner.ChainedClassNameFilter;
import android.support.test.internal.runner.ClassPathScanner.ExternalClassNameFilter;
-import android.support.test.internal.runner.TestLoader;
import android.testing.AndroidTestingRunner;
import android.text.TextUtils;
import android.util.Log;
@@ -32,8 +31,11 @@ import com.android.systemui.SysuiTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -69,12 +71,11 @@ public class AAAPlusPlusVerifySysuiRequiredTestPropertiesTest extends SysuiTestC
};
@Test
- public void testAllClassInheritance() {
+ public void testAllClassInheritance() throws Throwable {
boolean anyClassWrong = false;
- TestLoader loader = new TestLoader();
for (String className : getClassNamesFromClassPath()) {
- Class<?> cls = loader.loadIfTest(className);
- if (cls == null) continue;
+ Class<?> cls = Class.forName(className, false, this.getClass().getClassLoader());
+ if (!isTestClass(cls)) continue;
boolean hasParent = false;
for (Class<?> parent : BASE_CLS_WHITELIST) {
@@ -125,4 +126,85 @@ public class AAAPlusPlusVerifySysuiRequiredTestPropertiesTest extends SysuiTestC
return TextUtils.join(",", Arrays.asList(BASE_CLS_WHITELIST)
.stream().map(cls -> cls.getSimpleName()).toArray());
}
+
+ /**
+ * Determines if given class is a valid test class.
+ *
+ * @param loadedClass
+ * @return <code>true</code> if loadedClass is a test
+ */
+ private boolean isTestClass(Class<?> loadedClass) {
+ try {
+ if (Modifier.isAbstract(loadedClass.getModifiers())) {
+ logDebug(String.format("Skipping abstract class %s: not a test",
+ loadedClass.getName()));
+ return false;
+ }
+ // TODO: try to find upstream junit calls to replace these checks
+ if (junit.framework.Test.class.isAssignableFrom(loadedClass)) {
+ // ensure that if a TestCase, it has at least one test method otherwise
+ // TestSuite will throw error
+ if (junit.framework.TestCase.class.isAssignableFrom(loadedClass)) {
+ return hasJUnit3TestMethod(loadedClass);
+ }
+ return true;
+ }
+ // TODO: look for a 'suite' method?
+ if (loadedClass.isAnnotationPresent(org.junit.runner.RunWith.class)) {
+ return true;
+ }
+ for (Method testMethod : loadedClass.getMethods()) {
+ if (testMethod.isAnnotationPresent(org.junit.Test.class)) {
+ return true;
+ }
+ }
+ logDebug(String.format("Skipping class %s: not a test", loadedClass.getName()));
+ return false;
+ } catch (Exception e) {
+ // Defensively catch exceptions - Will throw runtime exception if it cannot load methods.
+ // For earlier versions of Android (Pre-ICS), Dalvik might try to initialize a class
+ // during getMethods(), fail to do so, hide the error and throw a NoSuchMethodException.
+ // Since the java.lang.Class.getMethods does not declare such an exception, resort to a
+ // generic catch all.
+ // For ICS+, Dalvik will throw a NoClassDefFoundException.
+ Log.w(TAG, String.format("%s in isTestClass for %s", e.toString(),
+ loadedClass.getName()));
+ return false;
+ } catch (Error e) {
+ // defensively catch Errors too
+ Log.w(TAG, String.format("%s in isTestClass for %s", e.toString(),
+ loadedClass.getName()));
+ return false;
+ }
+ }
+
+ private boolean hasJUnit3TestMethod(Class<?> loadedClass) {
+ for (Method testMethod : loadedClass.getMethods()) {
+ if (isPublicTestMethod(testMethod)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ // copied from junit.framework.TestSuite
+ private boolean isPublicTestMethod(Method m) {
+ return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
+ }
+
+ // copied from junit.framework.TestSuite
+ private boolean isTestMethod(Method m) {
+ return m.getParameterTypes().length == 0 && m.getName().startsWith("test")
+ && m.getReturnType().equals(Void.TYPE);
+ }
+
+ /**
+ * Utility method for logging debug messages. Only actually logs a message if TAG is marked
+ * as loggable to limit log spam during normal use.
+ */
+ private void logDebug(String msg) {
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, msg);
+ }
+ }
}