Merge "Clean up Predicate related code in android.test" into oc-dev-plus-aosp am: 50826c5f2e

am: 4f9d89a380

Change-Id: I24b978bb63875ed7e1af7041f8ecbbfc482e0614
This commit is contained in:
Paul Duffin
2017-06-26 13:29:23 +00:00
committed by android-build-merger
14 changed files with 87 additions and 483 deletions

View File

@@ -18,7 +18,6 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
android-support-test \
frameworks-base-testutils \
mockito-target-minus-junit4 \
legacy-android-tests
LOCAL_JAVA_LIBRARIES := android.test.runner

View File

@@ -48,19 +48,6 @@ LOCAL_JAVA_LIBRARIES := core-oj core-libart framework junit
include $(BUILD_STATIC_JAVA_LIBRARY)
# Build the legacy-android-tests library
# ======================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
$(call all-java-files-under, tests)
LOCAL_MODULE := legacy-android-tests
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JAVA_LIBRARIES := core-oj core-libart framework junit
LOCAL_STATIC_JAVA_LIBRARIES := legacy-android-test
include $(BUILD_STATIC_JAVA_LIBRARY)
ifeq ($(HOST_OS),linux)
# Build the legacy-performance-test-hostdex library
# =================================================

View File

@@ -1,127 +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 com.android.internal.util;
import java.util.Arrays;
/**
* Predicates contains static methods for creating the standard set of
* {@code Predicate} objects.
*
* @hide
*/
public class Predicates {
private Predicates() {
}
/**
* Returns a Predicate that evaluates to true iff each of its components
* evaluates to true. The components are evaluated in order, and evaluation
* will be "short-circuited" as soon as the answer is determined.
*/
public static <T> Predicate<T> and(Predicate<? super T>... components) {
return Predicates.<T>and(Arrays.asList(components));
}
/**
* Returns a Predicate that evaluates to true iff each of its components
* evaluates to true. The components are evaluated in order, and evaluation
* will be "short-circuited" as soon as the answer is determined. Does not
* defensively copy the iterable passed in, so future changes to it will alter
* the behavior of this Predicate. If components is empty, the returned
* Predicate will always evaluate to true.
*/
public static <T> Predicate<T> and(Iterable<? extends Predicate<? super T>> components) {
return new AndPredicate(components);
}
/**
* Returns a Predicate that evaluates to true iff any one of its components
* evaluates to true. The components are evaluated in order, and evaluation
* will be "short-circuited" as soon as the answer is determined.
*/
public static <T> Predicate<T> or(Predicate<? super T>... components) {
return Predicates.<T>or(Arrays.asList(components));
}
/**
* Returns a Predicate that evaluates to true iff any one of its components
* evaluates to true. The components are evaluated in order, and evaluation
* will be "short-circuited" as soon as the answer is determined. Does not
* defensively copy the iterable passed in, so future changes to it will alter
* the behavior of this Predicate. If components is empty, the returned
* Predicate will always evaluate to false.
*/
public static <T> Predicate<T> or(Iterable<? extends Predicate<? super T>> components) {
return new OrPredicate(components);
}
/**
* 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 AndPredicate<T> implements Predicate<T> {
private final Iterable<? extends Predicate<? super T>> components;
private AndPredicate(Iterable<? extends Predicate<? super T>> components) {
this.components = components;
}
public boolean apply(T t) {
for (Predicate<? super T> predicate : components) {
if (!predicate.apply(t)) {
return false;
}
}
return true;
}
}
private static class OrPredicate<T> implements Predicate<T> {
private final Iterable<? extends Predicate<? super T>> components;
private OrPredicate(Iterable<? extends Predicate<? super T>> components) {
this.components = components;
}
public boolean apply(T t) {
for (Predicate<? super T> predicate : components) {
if (predicate.apply(t)) {
return true;
}
}
return false;
}
}
private static class NotPredicate<T> implements Predicate<T> {
private final Predicate<? super T> predicate;
private NotPredicate(Predicate<? super T> predicate) {
this.predicate = predicate;
}
public boolean apply(T t) {
return !predicate.apply(t);
}
}
}

View File

@@ -1,74 +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 com.android.internal.util;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.Collections;
public class PredicatesTest extends TestCase {
private static final Predicate<Object> TRUE = new Predicate<Object>() {
public boolean apply(Object o) {
return true;
}
};
private static final Predicate<Object> FALSE = new Predicate<Object>() {
public boolean apply(Object o) {
return false;
}
};
public void testAndPredicate_AllConditionsTrue() throws Exception {
assertTrue(Predicates.and(newArrayList(TRUE)).apply(null));
assertTrue(Predicates.and(newArrayList(TRUE, TRUE)).apply(null));
}
public void testAndPredicate_AtLeastOneConditionIsFalse() throws Exception {
assertFalse(Predicates.and(newArrayList(FALSE, TRUE, TRUE)).apply(null));
assertFalse(Predicates.and(newArrayList(TRUE, FALSE, TRUE)).apply(null));
assertFalse(Predicates.and(newArrayList(TRUE, TRUE, FALSE)).apply(null));
}
public void testOrPredicate_AllConditionsTrue() throws Exception {
assertTrue(Predicates.or(newArrayList(TRUE, TRUE, TRUE)).apply(null));
}
public void testOrPredicate_AllConditionsFalse() throws Exception {
assertFalse(Predicates.or(newArrayList(FALSE, FALSE, FALSE)).apply(null));
}
public void testOrPredicate_AtLeastOneConditionIsTrue() throws Exception {
assertTrue(Predicates.or(newArrayList(TRUE, FALSE, FALSE)).apply(null));
assertTrue(Predicates.or(newArrayList(FALSE, TRUE, FALSE)).apply(null));
assertTrue(Predicates.or(newArrayList(FALSE, FALSE, TRUE)).apply(null));
}
public void testNotPredicate() throws Exception {
assertTrue(Predicates.not(FALSE).apply(null));
assertFalse(Predicates.not(TRUE).apply(null));
}
private static <E> ArrayList<E> newArrayList(E... elements) {
ArrayList<E> list = new ArrayList<E>();
Collections.addAll(list, elements);
return list;
}
}

View File

@@ -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;
@@ -27,7 +28,6 @@ import android.os.Looper;
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;
@@ -49,6 +49,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).
@@ -193,6 +195,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.
@@ -460,11 +468,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;
}
@@ -479,7 +487,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;
}
@@ -493,7 +501,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;
}

View File

@@ -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;
}

View File

@@ -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));
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));
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;
private NotPredicate(Predicate<? super T> predicate) {
this.predicate = predicate;
}
public boolean apply(T t) {
return !predicate.apply(t);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -14,9 +14,9 @@
* limitations under the License.
*/
package android.test.suitebuilder.annotation;
package android.test.suitebuilder;
import android.test.suitebuilder.TestMethod;
import com.android.internal.util.Predicate;
import junit.framework.TestCase;
import java.lang.annotation.ElementType;
@@ -25,7 +25,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
public class HasAnnotationTest extends TestCase {
public class TestPredicatesTest extends TestCase {
public void testThatMethodWithAnnotationIsReportedAsBeingAnnotated() throws Exception {
assertTrue(hasExampleAnnotation(ClassWithAnnotation.class, "testWithAnnotation"));
@@ -45,7 +45,7 @@ public class HasAnnotationTest extends TestCase {
throws NoSuchMethodException {
Method method = aClass.getMethod(methodName);
TestMethod testMethod = new TestMethod(method, aClass);
return new HasAnnotation(Example.class).apply(testMethod);
return TestPredicates.hasAnnotation(Example.class).apply(testMethod);
}
@Retention(RetentionPolicy.RUNTIME)
@@ -73,4 +73,21 @@ public class HasAnnotationTest extends TestCase {
public void testWithoutAnnotation() {
}
}
private static final Predicate<Object> TRUE = new Predicate<Object>() {
public boolean apply(Object o) {
return true;
}
};
private static final Predicate<Object> FALSE = new Predicate<Object>() {
public boolean apply(Object o) {
return false;
}
};
public void testNotPredicate() throws Exception {
assertTrue(TestPredicates.not(FALSE).apply(null));
assertFalse(TestPredicates.not(TRUE).apply(null));
}
}

View File

@@ -1,57 +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 android.test.suitebuilder.TestMethod;
import junit.framework.TestCase;
import java.lang.reflect.Method;
public class HasClassAnnotationTest extends TestCase {
public void testShouldTellIfParentClassHasSpecifiedClassification()
throws NoSuchMethodException {
assertTrue(classHasAnnotation(SmokeTestExample.class, Smoke.class));
}
public void testShouldTellIfParentClassDoesNotHaveSpecifiedClassification()
throws NoSuchMethodException {
assertFalse(classHasAnnotation(NonSmokeTestExample.class, Smoke.class));
}
private boolean classHasAnnotation(
Class<? extends TestCase> aClass,
Class<Smoke> expectedClassification) throws NoSuchMethodException {
Method method = aClass.getMethod("testSomeTest");
TestMethod testMethod = new TestMethod(method, aClass);
return new HasClassAnnotation(expectedClassification).apply(testMethod);
}
@Smoke
static class SmokeTestExample extends TestCase {
public void testSomeTest() {
}
}
static class NonSmokeTestExample extends TestCase {
public void testSomeTest() {
}
}
}

View File

@@ -1,56 +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 android.test.suitebuilder.TestMethod;
import junit.framework.TestCase;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class HasMethodAnnotationTest extends TestCase {
public void testMethodWithSpecifiedAttribute() throws Exception {
assertTrue(methodHasAnnotation(AnnotatedMethodExample.class,
"testThatIsAnnotated", Smoke.class));
}
public void testMethodWithoutSpecifiedAttribute() throws Exception {
assertFalse(methodHasAnnotation(AnnotatedMethodExample.class,
"testThatIsNotAnnotated", Smoke.class));
}
private boolean methodHasAnnotation(Class<? extends TestCase> aClass,
String methodName,
Class<? extends Annotation> expectedClassification
) throws NoSuchMethodException {
Method method = aClass.getMethod(methodName);
TestMethod testMethod = new TestMethod(method, aClass);
return new HasMethodAnnotation(expectedClassification).apply(testMethod);
}
static class AnnotatedMethodExample extends TestCase {
@Smoke
public void testThatIsAnnotated() {
}
public void testThatIsNotAnnotated() {
}
}
}