1 /*
2  * Copyright (C) 2019 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 com.android.test.filters;
18 
19 import static com.android.test.filters.SelectTest.OPTION_SELECT_TEST;
20 import static com.android.test.filters.SelectTest.OPTION_SELECT_TEST_VERBOSE;
21 
22 import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.assertTrue;
28 
29 import android.os.Bundle;
30 import android.util.ArraySet;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.Description;
35 import org.junit.runner.manipulation.Filter;
36 
37 import java.util.Collections;
38 import java.util.LinkedHashSet;
39 import java.util.Set;
40 import java.util.StringJoiner;
41 
42 public class SelectTestTests {
43 
44     private static final String PACKAGE_A = "packageA.";
45     private static final String PACKAGE_B = "packageB.";
46     private static final String PACKAGE_C = "packageC.";
47     private static final String CLASS_A1 = PACKAGE_A + "Class1";
48     private static final String CLASS_A2 = PACKAGE_A + "Class2";
49     private static final String CLASS_B3 = PACKAGE_B + "Class3";
50     private static final String CLASS_B4 = PACKAGE_B + "Class4";
51     private static final String CLASS_C5 = PACKAGE_C + "Class5";
52     private static final String CLASS_C6 = PACKAGE_C + "Class6";
53     private static final String METHOD_A1K = CLASS_A1 + "#methodK";
54     private static final String METHOD_A1L = CLASS_A1 + "#methodL";
55     private static final String METHOD_A2M = CLASS_A2 + "#methodM";
56     private static final String METHOD_A2N = CLASS_A2 + "#methodN";
57     private static final String METHOD_B3P = CLASS_B3 + "#methodP";
58     private static final String METHOD_B3Q = CLASS_B3 + "#methodQ";
59     private static final String METHOD_B4R = CLASS_B4 + "#methodR";
60     private static final String METHOD_B4S = CLASS_B4 + "#methodS";
61     private static final String METHOD_C5W = CLASS_C5 + "#methodW";
62     private static final String METHOD_C5X = CLASS_C5 + "#methodX";
63     private static final String METHOD_C6Y = CLASS_C6 + "#methodY";
64     private static final String METHOD_C6Z = CLASS_C6 + "#methodZ";
65 
66     private static final Set<Description> TEST_METHOD_A1K = methodTest(METHOD_A1K);
67     private static final Set<Description> TEST_METHOD_A1L = methodTest(METHOD_A1L);
68     private static final Set<Description> TEST_METHOD_A2M = methodTest(METHOD_A2M);
69     private static final Set<Description> TEST_METHOD_A2N = methodTest(METHOD_A2N);
70     private static final Set<Description> TEST_METHOD_B3P = methodTest(METHOD_B3P);
71     private static final Set<Description> TEST_METHOD_B3Q = methodTest(METHOD_B3Q);
72     private static final Set<Description> TEST_METHOD_B4R = methodTest(METHOD_B4R);
73     private static final Set<Description> TEST_METHOD_B4S = methodTest(METHOD_B4S);
74     private static final Set<Description> TEST_METHOD_C5W = methodTest(METHOD_C5W);
75     private static final Set<Description> TEST_METHOD_C5X = methodTest(METHOD_C5X);
76     private static final Set<Description> TEST_METHOD_C6Y = methodTest(METHOD_C6Y);
77     private static final Set<Description> TEST_METHOD_C6Z = methodTest(METHOD_C6Z);
78     private static final Set<Description> TEST_CLASS_A1 = merge(TEST_METHOD_A1K, TEST_METHOD_A1L);
79     private static final Set<Description> TEST_CLASS_A2 = merge(TEST_METHOD_A2M, TEST_METHOD_A2N);
80     private static final Set<Description> TEST_CLASS_B3 = merge(TEST_METHOD_B3P, TEST_METHOD_B3Q);
81     private static final Set<Description> TEST_CLASS_B4 = merge(TEST_METHOD_B4R, TEST_METHOD_B4S);
82     private static final Set<Description> TEST_CLASS_C5 = merge(TEST_METHOD_C5W, TEST_METHOD_C5X);
83     private static final Set<Description> TEST_CLASS_C6 = merge(TEST_METHOD_C6Y, TEST_METHOD_C6Z);
84     private static final Set<Description> TEST_PACKAGE_A = merge(TEST_CLASS_A1, TEST_CLASS_A2);
85     private static final Set<Description> TEST_PACKAGE_B = merge(TEST_CLASS_B3, TEST_CLASS_B4);
86     private static final Set<Description> TEST_PACKAGE_C = merge(TEST_CLASS_C5, TEST_CLASS_C6);
87     private static final Set<Description> TEST_ALL =
88             merge(TEST_PACKAGE_A, TEST_PACKAGE_B, TEST_PACKAGE_C);
89 
90     private SelectTestBuilder mBuilder;
91 
92     @Before
setUp()93     public void setUp() {
94         mBuilder = new SelectTestBuilder();
95     }
96 
97     private static class SelectTestBuilder {
98         private final Bundle mTestArgs = new Bundle();
99 
build()100         Filter build() {
101             mTestArgs.putString(OPTION_SELECT_TEST_VERBOSE, Boolean.TRUE.toString());
102             return new SelectTest(mTestArgs);
103         }
104 
withSelectTest(String... selectTestArgs)105         SelectTestBuilder withSelectTest(String... selectTestArgs) {
106             putTestOption(OPTION_SELECT_TEST, selectTestArgs);
107             return this;
108         }
109 
putTestOption(String option, String... args)110         private void putTestOption(String option, String... args) {
111             if (args.length > 0) {
112                 StringJoiner joiner = new StringJoiner(",");
113                 for (String arg : args) {
114                     joiner.add(arg);
115                 }
116                 mTestArgs.putString(option, joiner.toString());
117             }
118         }
119     }
120 
methodTest(String testName)121     private static Set<Description> methodTest(String testName) {
122         int methodSep = testName.indexOf("#");
123         String className = testName.substring(0, methodSep);
124         String methodName = testName.substring(methodSep + 1);
125         final Set<Description> tests = new ArraySet<>();
126         tests.add(Description.createSuiteDescription(className));
127         tests.add(Description.createTestDescription(className, methodName));
128         return Collections.unmodifiableSet(tests);
129     }
130 
131     @SafeVarargs
merge(Set<Description>.... testSpecs)132     private static Set<Description> merge(Set<Description>... testSpecs) {
133         final Set<Description> merged = new LinkedHashSet<>();
134         for (Set<Description> testSet : testSpecs) {
135             merged.addAll(testSet);
136         }
137         return Collections.unmodifiableSet(merged);
138     }
139 
140     @SafeVarargs
acceptTests(Filter filter, Set<Description>... testSpecs)141     private static void acceptTests(Filter filter, Set<Description>... testSpecs) {
142         final Set<Description> accepts = merge(testSpecs);
143         for (Description test : TEST_ALL) {
144             if (accepts.contains(test)) {
145                 assertTrue("accept " + test, filter.shouldRun(test));
146             } else {
147                 assertFalse("reject " + test, filter.shouldRun(test));
148             }
149         }
150     }
151 
152     @Test
testAddSelectTest()153     public void testAddSelectTest() {
154         final Bundle testArgs = new Bundle();
155 
156         final Bundle modifiedTestArgs =
157                 SelectTest.addSelectTest(testArgs, PACKAGE_A, CLASS_B3, METHOD_C5X);
158         assertSame(testArgs, modifiedTestArgs);
159 
160         final String selectTestArgs = modifiedTestArgs.getString(OPTION_SELECT_TEST);
161         assertNotNull(selectTestArgs);
162         final String[] selectedTests = selectTestArgs.split(",");
163         assertThat(selectedTests, hasItemInArray(PACKAGE_A));
164         assertThat(selectedTests, hasItemInArray(CLASS_B3));
165         assertThat(selectedTests, hasItemInArray(METHOD_C5X));
166     }
167 
168     @Test
testAddSelectTest_prependExistingTestArg()169     public void testAddSelectTest_prependExistingTestArg() {
170         final Bundle testArgs = new Bundle();
171         testArgs.putString(OPTION_SELECT_TEST, new StringJoiner(",")
172                 .add(PACKAGE_A)
173                 .add(CLASS_B3)
174                 .add(METHOD_C5X)
175                 .toString());
176 
177         final Bundle modifiedTestArgs =
178                 SelectTest.addSelectTest(testArgs, PACKAGE_B, CLASS_B4, METHOD_C6Y);
179 
180         final String selectTestArgs = modifiedTestArgs.getString(OPTION_SELECT_TEST);
181         assertNotNull(selectTestArgs);
182         final String[] selectedTests = selectTestArgs.split(",");
183         assertThat(selectedTests, hasItemInArray(PACKAGE_A));
184         assertThat(selectedTests, hasItemInArray(CLASS_B3));
185         assertThat(selectedTests, hasItemInArray(METHOD_C5X));
186         assertThat(selectedTests, hasItemInArray(PACKAGE_B));
187         assertThat(selectedTests, hasItemInArray(CLASS_B4));
188         assertThat(selectedTests, hasItemInArray(METHOD_C6Y));
189     }
190 
191     @Test
testFilterDisabled()192     public void testFilterDisabled() {
193         final Filter filter = mBuilder.build();
194         acceptTests(filter, TEST_ALL);
195     }
196 
197     @Test
testSelectPackage()198     public void testSelectPackage() {
199         final Filter filter = mBuilder.withSelectTest(PACKAGE_A, PACKAGE_B).build();
200         acceptTests(filter, TEST_PACKAGE_A, TEST_PACKAGE_B);
201     }
202 
203     @Test
testSelectClass()204     public void testSelectClass() {
205         final Filter filter = mBuilder.withSelectTest(CLASS_A1, CLASS_A2, CLASS_B3).build();
206         acceptTests(filter, TEST_CLASS_A1, TEST_CLASS_A2, TEST_CLASS_B3);
207     }
208 
209     @Test
testSelectMethod()210     public void testSelectMethod() {
211         final Filter filter = mBuilder
212                 .withSelectTest(METHOD_A1K, METHOD_A2M, METHOD_A2N, METHOD_B3P).build();
213         acceptTests(filter, TEST_METHOD_A1K, TEST_METHOD_A2M, TEST_METHOD_A2N, TEST_METHOD_B3P);
214     }
215 
216     @Test
testSelectClassAndPackage()217     public void testSelectClassAndPackage() {
218         final Filter filter = mBuilder.withSelectTest(CLASS_A1, PACKAGE_B, CLASS_C5).build();
219         acceptTests(filter, TEST_CLASS_A1, TEST_PACKAGE_B, TEST_CLASS_C5);
220     }
221 
222     @Test
testSelectMethodAndPackage()223     public void testSelectMethodAndPackage() {
224         final Filter filter = mBuilder.withSelectTest(METHOD_A1K, PACKAGE_B, METHOD_C5W).build();
225         acceptTests(filter, TEST_METHOD_A1K, TEST_PACKAGE_B, TEST_METHOD_C5W);
226     }
227 
228     @Test
testSelectMethodAndClass()229     public void testSelectMethodAndClass() {
230         final Filter filter = mBuilder.withSelectTest(METHOD_A1K, CLASS_C5, METHOD_B3P).build();
231         acceptTests(filter, TEST_METHOD_A1K, TEST_CLASS_C5, TEST_METHOD_B3P);
232     }
233 
234     @Test
testSelectClassAndSamePackage()235     public void testSelectClassAndSamePackage() {
236         final Filter filter = mBuilder.withSelectTest(
237                 CLASS_A1, PACKAGE_A, CLASS_B3, PACKAGE_C, CLASS_C5).build();
238         acceptTests(filter, TEST_PACKAGE_A, TEST_CLASS_B3, TEST_PACKAGE_C);
239     }
240 
241     @Test
testSelectMethodAndSameClass()242     public void testSelectMethodAndSameClass() {
243         final Filter filter = mBuilder.withSelectTest(
244                 METHOD_A1K, METHOD_A2M, CLASS_A1, CLASS_B3, METHOD_B3P, METHOD_B4R).build();
245         acceptTests(filter, TEST_CLASS_A1, TEST_METHOD_A2M, TEST_CLASS_B3, TEST_METHOD_B4R);
246     }
247 
248     @Test
testSelectMethodAndSamePackage()249     public void testSelectMethodAndSamePackage() {
250         final Filter filter = mBuilder.withSelectTest(
251                 METHOD_A1K, METHOD_A1L, METHOD_A2M, PACKAGE_A,
252                 PACKAGE_C, METHOD_C5W, METHOD_C5X, METHOD_C6Y).build();
253         acceptTests(filter, TEST_PACKAGE_A, TEST_PACKAGE_C);
254     }
255 
256     @Test
testSelectMethodAndClassAndPackage()257     public void testSelectMethodAndClassAndPackage() {
258         final Filter filter = mBuilder.withSelectTest(
259                 METHOD_A1K, CLASS_A1, METHOD_A1L, METHOD_A2M, PACKAGE_A,
260                 PACKAGE_B, METHOD_B3Q, CLASS_B3, METHOD_B4R, METHOD_B3P).build();
261         acceptTests(filter, TEST_PACKAGE_A, TEST_PACKAGE_B);
262     }
263 }
264