1 /*
2  * Copyright (C) 2011 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 package com.android.cts.tradefed.testtype;
17 
18 import com.android.ddmlib.testrunner.TestIdentifier;
19 
20 import junit.framework.TestCase;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Iterator;
25 import java.util.List;
26 
27 /**
28  * Unit tests for {@link TestFilter}.
29  */
30 public class TestFilterTest extends TestCase {
31 
32     private TestFilter mFilter;
33     private List<TestIdentifier> mTestList;
34 
35     private static final TestIdentifier TEST1 = new TestIdentifier("FooTest", "testFoo");
36     private static final TestIdentifier TEST2 = new TestIdentifier("FooTest", "testFoo2");
37     private static final TestIdentifier TEST3 = new TestIdentifier("FooTest2", "testFoo3");
38 
39     @Override
setUp()40     protected void setUp() throws Exception {
41         mFilter = new TestFilter();
42         mTestList = new ArrayList<TestIdentifier>();
43         mTestList.add(TEST1);
44         mTestList.add(TEST2);
45         mTestList.add(TEST3);
46     }
47 
48     /**
49      * Test {@link TestFilter#filter(java.util.Collection)} with no rules defined
50      */
testFilter_empty()51     public void testFilter_empty() {
52         assertEquals(mTestList.size(), mFilter.filter(mTestList).size());
53     }
54 
55     /**
56      * Test {@link TestFilter#filter(java.util.Collection)} with an excluded test filter
57      */
testFilter_excludeTest()58     public void testFilter_excludeTest() {
59         mFilter.addExcludedTest(TEST1);
60         Collection<TestIdentifier> filteredList = mFilter.filter(mTestList);
61         assertEquals(2, filteredList.size());
62         Iterator<TestIdentifier> iter = filteredList.iterator();
63         assertEquals(TEST2, iter.next());
64         assertEquals(TEST3, iter.next());
65     }
66 
67     /**
68      * Test {@link TestFilter#filter(java.util.Collection)} with an excluded test filter
69      */
testFilter_excludeClass()70     public void testFilter_excludeClass() {
71         mFilter.addExcludedClass(TEST1.getClassName());
72         Collection<TestIdentifier> filteredList = mFilter.filter(mTestList);
73         assertEquals(1, filteredList.size());
74         assertEquals(TEST3, filteredList.iterator().next());
75     }
76 
77     /**
78      * Test {@link TestFilter#filter(java.util.Collection)} with a class inclusion rule
79      */
testFilter_includeClass()80     public void testFilter_includeClass() {
81         mFilter.setTestInclusion(TEST1.getClassName(), null);
82         Collection<TestIdentifier> filteredList = mFilter.filter(mTestList);
83         assertEquals(2, filteredList.size());
84         Iterator<TestIdentifier> iter = filteredList.iterator();
85         assertEquals(TEST1, iter.next());
86         assertEquals(TEST2, iter.next());
87     }
88 
89     /**
90      * Test {@link TestFilter#filter(java.util.Collection)} with a class
91      */
testFilter_includeTest()92     public void testFilter_includeTest() {
93         mFilter.setTestInclusion(TEST1.getClassName(), TEST1.getTestName());
94         Collection<TestIdentifier> filteredList = mFilter.filter(mTestList);
95         assertEquals(1, filteredList.size());
96         Iterator<TestIdentifier> iter = filteredList.iterator();
97         assertEquals(TEST1, iter.next());
98     }
99 
100     /**
101      * Test {@link TestFilter#filter(java.util.Collection)} with an included test filter
102      */
testFilter_includeClasses()103     public void testFilter_includeClasses() {
104         mFilter.addIncludedClass(TEST3.getClassName());
105         Collection<TestIdentifier> filteredList = mFilter.filter(mTestList);
106         assertEquals(1, filteredList.size());
107         assertEquals(TEST3, filteredList.iterator().next());
108     }
109 
110     /**
111      * Test {@link TestFilter#filter(java.util.Collection)} with an included test filter
112      */
testFilter_includeTests()113     public void testFilter_includeTests() {
114         mFilter.addIncludedTest(TEST1);
115         mFilter.addIncludedTest(TEST3);
116         Collection<TestIdentifier> filteredList = mFilter.filter(mTestList);
117         assertEquals(2, filteredList.size());
118         Iterator<TestIdentifier> iter = filteredList.iterator();
119         assertEquals(TEST1, iter.next());
120         assertEquals(TEST3, iter.next());
121     }
122 
123     /**
124      * Test {@link TestFilter#filter(java.util.Collection)}. Exclusion overrides
125      * inclusion.
126      */
testFilter_includeAndExcludeClasses()127     public void testFilter_includeAndExcludeClasses() {
128         String className = TEST3.getClassName();
129         mFilter.addExcludedClass(className);
130         mFilter.addIncludedClass(className);
131         assertTrue(mFilter.filter(mTestList).isEmpty());
132     }
133 
134     /**
135      * Test {@link TestFilter#filter(java.util.Collection)} Exclusion overrides
136      * inclusion.
137      */
testFilter_includeAndExcludeTests()138     public void testFilter_includeAndExcludeTests() {
139         mFilter.addExcludedTest(TEST1);
140         mFilter.addIncludedTest(TEST1);
141         assertTrue(mFilter.filter(mTestList).isEmpty());
142     }
143 
144     /**
145      * Test {@link TestFilter#filter(java.util.Collection)} Exclusion overrides
146      * inclusion.
147      */
testFilter_includeTestAndExcludeClass()148     public void testFilter_includeTestAndExcludeClass() {
149         mFilter.addExcludedClass(TEST1.getClassName());
150         mFilter.addIncludedTest(TEST1);
151         assertTrue(mFilter.filter(mTestList).isEmpty());
152     }
153 
154     /**
155      * Test {@link TestFilter#filter(java.util.Collection)} Exclusion overrides
156      * inclusion.
157      */
testFilter_includeClassAndExcludeTest()158     public void testFilter_includeClassAndExcludeTest() {
159         mFilter.addExcludedTest(TEST1);
160         mFilter.addIncludedClass(TEST1.getClassName());
161         Collection<TestIdentifier> filteredList = mFilter.filter(mTestList);
162         assertEquals(1, filteredList.size());
163         Iterator<TestIdentifier> iter = filteredList.iterator();
164         assertEquals(TEST2, iter.next());
165     }
166 
167     /**
168      * Test {@link TestFilter#filter(java.util.Collection)} Exclusion overrides
169      * inclusion.
170      */
testFilter_includeClassAndExcludeTests()171     public void testFilter_includeClassAndExcludeTests() {
172         mFilter.addExcludedTest(TEST1);
173         mFilter.addExcludedTest(TEST2);
174         mFilter.addIncludedClass(TEST1.getClassName());
175         assertTrue(mFilter.filter(mTestList).isEmpty());
176     }
177 }
178