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