1/*
2 * Copyright (C) 2008 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
17package android.test.suitebuilder;
18
19import android.test.InstrumentationTestCase;
20import android.test.suitebuilder.annotation.Smoke;
21import android.test.suitebuilder.annotation.Suppress;
22import com.android.internal.util.Predicate;
23import java.lang.annotation.Annotation;
24
25/**
26 * {@hide} Not needed for 1.0 SDK.
27 */
28public class TestPredicates {
29
30    static final Predicate<TestMethod> REJECT_INSTRUMENTATION =
31            not(new AssignableFrom(InstrumentationTestCase.class));
32
33    static final Predicate<TestMethod> SELECT_SMOKE = hasAnnotation(Smoke.class);
34
35    static final Predicate<TestMethod> REJECT_SUPPRESSED = not(hasAnnotation(Suppress.class));
36
37    /**
38     * Return a predicate that checks to see if a {@link TestMethod} has an instance of the supplied
39     * annotation class, either on the method or on the containing class.
40     */
41    public static Predicate<TestMethod> hasAnnotation(Class<? extends Annotation> annotationClass) {
42        return new HasAnnotation(annotationClass);
43    }
44
45    private static class HasAnnotation implements Predicate<TestMethod> {
46
47        private final Class<? extends Annotation> annotationClass;
48
49        private HasAnnotation(Class<? extends Annotation> annotationClass) {
50            this.annotationClass = annotationClass;
51        }
52
53        @Override
54        public boolean apply(TestMethod testMethod) {
55            return testMethod.getAnnotation(annotationClass) != null ||
56                    testMethod.getEnclosingClass().getAnnotation(annotationClass) != null;
57        }
58    }
59
60    /**
61     * Returns a Predicate that evaluates to true iff the given Predicate
62     * evaluates to false.
63     */
64    public static <T> Predicate<T> not(Predicate<? super T> predicate) {
65        return new NotPredicate<T>(predicate);
66    }
67
68    private static class NotPredicate<T> implements Predicate<T> {
69        private final Predicate<? super T> predicate;
70
71        private NotPredicate(Predicate<? super T> predicate) {
72            this.predicate = predicate;
73        }
74
75        public boolean apply(T t) {
76            return !predicate.apply(t);
77        }
78    }
79}
80