summaryrefslogtreecommitdiff
path: root/test-runner/src
diff options
context:
space:
mode:
authorPaul Duffin <paulduffin@google.com>2017-06-22 10:47:25 +0100
committerPaul Duffin <paulduffin@google.com>2017-06-22 15:48:44 +0100
commitfedb4b745693afba9627f8971346d5151101fb88 (patch)
tree73d4ab78b4a0877817f19ff34b60dbff9ddc9e0d /test-runner/src
parente211119fea8e01e573ecde57ced2e9bf8dfbf65e (diff)
Clean up Predicate related code in android.test
Part of the work of removing JUnit and dependent android.test classes from the Android API involves providing a static library that developers can include in their test applications to ease migration. That library will be built directly from the source (as opposed to android.jar which is built from stubs) and so developers will be able to see classes and methods that are not present in the stubs. This change is one of a number of similar changes that cleanup the existing non-API code in order to minimize the additional methods and classes exposed externally. The basic approach is to remove unused classes and methods, use least visible access modifier possible and generally minimize the amount of publicly visible code. The HasClassAnnotation and HasMethodAnnotation were never used separately and only used by HasAnnotation. This merges the functionality into a single nested class in TestPredicates, hidden behind the new TestPredicates.hasAnnotation(). The HasAnnotationTest was renamed as TestPredicatesTest and the HasClassAnnotationTest and HasMethodAnnotationTest classes were removed as their tests provide no additional coverage. The removal of the Has*Annotation.java files means that the test-runner/src/android/test/suitebuilder/annotation/ directory is empty apart from the package.html file so that was moved to legacy-test to sit alongside the actual annotation classes. The Predicates class, while part of the legacy-test module was only ever used by the test-runner module and only its not() method was actually used. So, the not() method and associated nested class were moved to TestPredicates, the tests for not() were moved to TestPredicatesTest and the Predicates* classes were removed. That allowed for the removal of the legacy-android-tests as that is now empty. TestPredicates has a number of constants that were public. They were hidden by moving them to the class that actually used them. A minor generic issue was fixed in AssignableFrom. Bug: 30188076 Test: make checkbuild and run FrameworkTestRunnerTests Change-Id: I861da388a4146bb28e1e480d1b7ba9137b7b270e
Diffstat (limited to 'test-runner/src')
-rw-r--r--test-runner/src/android/test/InstrumentationTestRunner.java22
-rw-r--r--test-runner/src/android/test/suitebuilder/AssignableFrom.java4
-rw-r--r--test-runner/src/android/test/suitebuilder/TestPredicates.java65
-rw-r--r--test-runner/src/android/test/suitebuilder/annotation/HasAnnotation.java44
-rw-r--r--test-runner/src/android/test/suitebuilder/annotation/HasClassAnnotation.java41
-rw-r--r--test-runner/src/android/test/suitebuilder/annotation/HasMethodAnnotation.java41
-rw-r--r--test-runner/src/android/test/suitebuilder/annotation/package.html5
7 files changed, 66 insertions, 156 deletions
diff --git a/test-runner/src/android/test/InstrumentationTestRunner.java b/test-runner/src/android/test/InstrumentationTestRunner.java
index 6e5492bd751c..3f8b7a7137e8 100644
--- a/test-runner/src/android/test/InstrumentationTestRunner.java
+++ b/test-runner/src/android/test/InstrumentationTestRunner.java
@@ -16,8 +16,9 @@
package android.test;
+import android.test.suitebuilder.annotation.MediumTest;
+import android.test.suitebuilder.annotation.SmallTest;
import com.android.internal.util.Predicate;
-import com.android.internal.util.Predicates;
import android.app.Activity;
import android.app.Instrumentation;
@@ -30,7 +31,6 @@ import android.os.PerformanceCollector.PerformanceResultsWriter;
import android.test.suitebuilder.TestMethod;
import android.test.suitebuilder.TestPredicates;
import android.test.suitebuilder.TestSuiteBuilder;
-import android.test.suitebuilder.annotation.HasAnnotation;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
@@ -52,6 +52,8 @@ import junit.framework.TestSuite;
import junit.runner.BaseTestRunner;
import junit.textui.ResultPrinter;
+import static android.test.suitebuilder.TestPredicates.hasAnnotation;
+
/**
* An {@link Instrumentation} that runs various types of {@link junit.framework.TestCase}s against
* an Android package (application).
@@ -196,6 +198,12 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu
/** @hide */
static final String ARGUMENT_NOT_ANNOTATION = "notAnnotation";
+ private static final Predicate<TestMethod> SELECT_SMALL = hasAnnotation(SmallTest.class);
+
+ private static final Predicate<TestMethod> SELECT_MEDIUM = hasAnnotation(MediumTest.class);
+
+ private static final Predicate<TestMethod> SELECT_LARGE = hasAnnotation(LargeTest.class);
+
/**
* This constant defines the maximum allowed runtime (in ms) for a test included in the "small"
* suite. It is used to make an educated guess at what suite an unlabeled test belongs.
@@ -464,11 +472,11 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu
private Predicate<TestMethod> getSizePredicateFromArg(String sizeArg) {
if (SMALL_SUITE.equals(sizeArg)) {
- return TestPredicates.SELECT_SMALL;
+ return SELECT_SMALL;
} else if (MEDIUM_SUITE.equals(sizeArg)) {
- return TestPredicates.SELECT_MEDIUM;
+ return SELECT_MEDIUM;
} else if (LARGE_SUITE.equals(sizeArg)) {
- return TestPredicates.SELECT_LARGE;
+ return SELECT_LARGE;
} else {
return null;
}
@@ -483,7 +491,7 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu
private Predicate<TestMethod> getAnnotationPredicate(String annotationClassName) {
Class<? extends Annotation> annotationClass = getAnnotationClass(annotationClassName);
if (annotationClass != null) {
- return new HasAnnotation(annotationClass);
+ return hasAnnotation(annotationClass);
}
return null;
}
@@ -497,7 +505,7 @@ public class InstrumentationTestRunner extends Instrumentation implements TestSu
private Predicate<TestMethod> getNotAnnotationPredicate(String annotationClassName) {
Class<? extends Annotation> annotationClass = getAnnotationClass(annotationClassName);
if (annotationClass != null) {
- return Predicates.not(new HasAnnotation(annotationClass));
+ return TestPredicates.not(hasAnnotation(annotationClass));
}
return null;
}
diff --git a/test-runner/src/android/test/suitebuilder/AssignableFrom.java b/test-runner/src/android/test/suitebuilder/AssignableFrom.java
index 38b4ee3f8c6e..84db06669cf4 100644
--- a/test-runner/src/android/test/suitebuilder/AssignableFrom.java
+++ b/test-runner/src/android/test/suitebuilder/AssignableFrom.java
@@ -20,9 +20,9 @@ import com.android.internal.util.Predicate;
class AssignableFrom implements Predicate<TestMethod> {
- private final Class root;
+ private final Class<?> root;
- AssignableFrom(Class root) {
+ AssignableFrom(Class<?> root) {
this.root = root;
}
diff --git a/test-runner/src/android/test/suitebuilder/TestPredicates.java b/test-runner/src/android/test/suitebuilder/TestPredicates.java
index 47aca55f9972..616d1a972612 100644
--- a/test-runner/src/android/test/suitebuilder/TestPredicates.java
+++ b/test-runner/src/android/test/suitebuilder/TestPredicates.java
@@ -17,30 +17,63 @@
package android.test.suitebuilder;
import android.test.InstrumentationTestCase;
-import android.test.suitebuilder.annotation.HasAnnotation;
-import android.test.suitebuilder.annotation.Suppress;
-import android.test.suitebuilder.annotation.LargeTest;
-import android.test.suitebuilder.annotation.MediumTest;
-import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.Smoke;
+import android.test.suitebuilder.annotation.Suppress;
import com.android.internal.util.Predicate;
-import com.android.internal.util.Predicates;
+import java.lang.annotation.Annotation;
/**
* {@hide} Not needed for 1.0 SDK.
*/
public class TestPredicates {
- public static final Predicate<TestMethod> SELECT_INSTRUMENTATION =
- new AssignableFrom(InstrumentationTestCase.class);
- public static final Predicate<TestMethod> REJECT_INSTRUMENTATION =
- Predicates.not(SELECT_INSTRUMENTATION);
+ static final Predicate<TestMethod> REJECT_INSTRUMENTATION =
+ not(new AssignableFrom(InstrumentationTestCase.class));
+
+ static final Predicate<TestMethod> SELECT_SMOKE = hasAnnotation(Smoke.class);
+
+ static final Predicate<TestMethod> REJECT_SUPPRESSED = not(hasAnnotation(Suppress.class));
+
+ /**
+ * Return a predicate that checks to see if a {@link TestMethod} has an instance of the supplied
+ * annotation class, either on the method or on the containing class.
+ */
+ public static Predicate<TestMethod> hasAnnotation(Class<? extends Annotation> annotationClass) {
+ return new HasAnnotation(annotationClass);
+ }
+
+ private static class HasAnnotation implements Predicate<TestMethod> {
+
+ private final Class<? extends Annotation> annotationClass;
+
+ private HasAnnotation(Class<? extends Annotation> annotationClass) {
+ this.annotationClass = annotationClass;
+ }
+
+ @Override
+ public boolean apply(TestMethod testMethod) {
+ return testMethod.getAnnotation(annotationClass) != null ||
+ testMethod.getEnclosingClass().getAnnotation(annotationClass) != null;
+ }
+ }
+
+ /**
+ * Returns a Predicate that evaluates to true iff the given Predicate
+ * evaluates to false.
+ */
+ public static <T> Predicate<T> not(Predicate<? super T> predicate) {
+ return new NotPredicate<T>(predicate);
+ }
+
+ private static class NotPredicate<T> implements Predicate<T> {
+ private final Predicate<? super T> predicate;
- public static final Predicate<TestMethod> SELECT_SMOKE = new HasAnnotation(Smoke.class);
- public static final Predicate<TestMethod> SELECT_SMALL = new HasAnnotation(SmallTest.class);
- public static final Predicate<TestMethod> SELECT_MEDIUM = new HasAnnotation(MediumTest.class);
- public static final Predicate<TestMethod> SELECT_LARGE = new HasAnnotation(LargeTest.class);
- public static final Predicate<TestMethod> REJECT_SUPPRESSED =
- Predicates.not(new HasAnnotation(Suppress.class));
+ private NotPredicate(Predicate<? super T> predicate) {
+ this.predicate = predicate;
+ }
+ public boolean apply(T t) {
+ return !predicate.apply(t);
+ }
+ }
}
diff --git a/test-runner/src/android/test/suitebuilder/annotation/HasAnnotation.java b/test-runner/src/android/test/suitebuilder/annotation/HasAnnotation.java
deleted file mode 100644
index a2868fc9e0fd..000000000000
--- a/test-runner/src/android/test/suitebuilder/annotation/HasAnnotation.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.test.suitebuilder.annotation;
-
-import static com.android.internal.util.Predicates.or;
-import com.android.internal.util.Predicate;
-import android.test.suitebuilder.TestMethod;
-
-import java.lang.annotation.Annotation;
-
-/**
- * A predicate that checks to see if a {@link TestMethod} has a specific annotation, either on the
- * method or on the containing class.
- *
- * {@hide} Not needed for 1.0 SDK.
- */
-public class HasAnnotation implements Predicate<TestMethod> {
-
- private Predicate<TestMethod> hasMethodOrClassAnnotation;
-
- public HasAnnotation(Class<? extends Annotation> annotationClass) {
- this.hasMethodOrClassAnnotation = or(
- new HasMethodAnnotation(annotationClass),
- new HasClassAnnotation(annotationClass));
- }
-
- public boolean apply(TestMethod testMethod) {
- return hasMethodOrClassAnnotation.apply(testMethod);
- }
-}
diff --git a/test-runner/src/android/test/suitebuilder/annotation/HasClassAnnotation.java b/test-runner/src/android/test/suitebuilder/annotation/HasClassAnnotation.java
deleted file mode 100644
index ac76f4cb46fe..000000000000
--- a/test-runner/src/android/test/suitebuilder/annotation/HasClassAnnotation.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.test.suitebuilder.annotation;
-
-import java.lang.annotation.Annotation;
-
-import android.test.suitebuilder.TestMethod;
-import com.android.internal.util.Predicate;
-
-/**
- * A predicate that checks to see if a {@link android.test.suitebuilder.TestMethod} has a specific annotation on the
- * containing class. Consider using the public {@link HasAnnotation} class instead of this class.
- *
- * {@hide} Not needed for 1.0 SDK.
- */
-class HasClassAnnotation implements Predicate<TestMethod> {
-
- private Class<? extends Annotation> annotationClass;
-
- public HasClassAnnotation(Class<? extends Annotation> annotationClass) {
- this.annotationClass = annotationClass;
- }
-
- public boolean apply(TestMethod testMethod) {
- return testMethod.getEnclosingClass().getAnnotation(annotationClass) != null;
- }
-}
diff --git a/test-runner/src/android/test/suitebuilder/annotation/HasMethodAnnotation.java b/test-runner/src/android/test/suitebuilder/annotation/HasMethodAnnotation.java
deleted file mode 100644
index 96bd922721ea..000000000000
--- a/test-runner/src/android/test/suitebuilder/annotation/HasMethodAnnotation.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.test.suitebuilder.annotation;
-
-import com.android.internal.util.Predicate;
-import android.test.suitebuilder.TestMethod;
-
-import java.lang.annotation.Annotation;
-
-/**
- * A predicate that checks to see if a the method represented by {@link TestMethod} has a certain
- * annotation on it. Consider using the public {@link HasAnnotation} class instead of this class.
- *
- * {@hide} Not needed for 1.0 SDK.
- */
-class HasMethodAnnotation implements Predicate<TestMethod> {
-
- private final Class<? extends Annotation> annotationClass;
-
- public HasMethodAnnotation(Class<? extends Annotation> annotationClass) {
- this.annotationClass = annotationClass;
- }
-
- public boolean apply(TestMethod testMethod) {
- return testMethod.getAnnotation(annotationClass) != null;
- }
-}
diff --git a/test-runner/src/android/test/suitebuilder/annotation/package.html b/test-runner/src/android/test/suitebuilder/annotation/package.html
deleted file mode 100644
index ffba2e9bf980..000000000000
--- a/test-runner/src/android/test/suitebuilder/annotation/package.html
+++ /dev/null
@@ -1,5 +0,0 @@
-<HTML>
-<BODY>
-Utility classes supporting the test runner classes.
-</BODY>
-</HTML>