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 static android.test.suitebuilder.ListTestCaseNames.getTestCaseNames;
20import android.test.suitebuilder.examples.OuterTest;
21import android.test.suitebuilder.examples.instrumentation.InstrumentationTest;
22
23import junit.framework.AssertionFailedError;
24import junit.framework.Test;
25import junit.framework.TestCase;
26import junit.framework.TestListener;
27import junit.framework.TestResult;
28import junit.framework.TestSuite;
29
30import java.util.HashSet;
31import java.util.List;
32import java.util.Set;
33
34public class InstrumentationTestSuiteBuilderTest extends TestCase {
35
36    private InstrumentationTestSuiteBuilder instrumentationTestSuiteBuilder;
37
38    protected void setUp() throws Exception {
39        super.setUp();
40        instrumentationTestSuiteBuilder = new InstrumentationTestSuiteBuilder(getClass());
41    }
42
43    public void testShouldIncludeIntrumentationTests() throws Exception {
44        instrumentationTestSuiteBuilder.includePackages(packageFor(InstrumentationTest.class));
45
46        SuiteExecutionRecorder recorder = runSuite(instrumentationTestSuiteBuilder);
47
48        assertEquals(1, recorder.testsSeen.size());
49        assertTrue(recorder.saw("InstrumentationTest.testInstrumentation"));
50    }
51
52    public void testShouldOnlyIncludeIntrumentationTests() throws Exception {
53        TestSuite testSuite = new OuterTest()
54                .buildTestsUnderHereWith(instrumentationTestSuiteBuilder);
55        List<String> testCaseNames = getTestCaseNames(testSuite);
56        assertEquals(1, testCaseNames.size());
57        assertEquals("testInstrumentation", testCaseNames.get(0));
58    }
59
60    private static String packageFor(Class clazz) {
61        String className = clazz.getName();
62        return className.substring(0, className.lastIndexOf('.'));
63    }
64
65    private SuiteExecutionRecorder runSuite(TestSuiteBuilder builder) {
66        TestSuite suite = builder.build();
67        SuiteExecutionRecorder recorder = new SuiteExecutionRecorder();
68        TestResult result = new TestResult();
69        result.addListener(recorder);
70        suite.run(result);
71        return recorder;
72    }
73
74    private class SuiteExecutionRecorder implements TestListener {
75
76        private Set<String> failures = new HashSet<String>();
77        private Set<String> errors = new HashSet<String>();
78        private Set<String> testsSeen = new HashSet<String>();
79
80        public void addError(Test test, Throwable t) {
81            errors.add(testName(test));
82        }
83
84        public void addFailure(Test test, AssertionFailedError t) {
85            failures.add(testName(test));
86        }
87
88        public void endTest(Test test) {
89        }
90
91        public void startTest(Test test) {
92            testsSeen.add(testName(test));
93        }
94
95        public boolean saw(String testName) {
96            return testsSeen.contains(testName);
97        }
98
99        public boolean failed(String testName) {
100            return failures.contains(testName);
101        }
102
103        public boolean errored(String testName) {
104            return errors.contains(testName);
105        }
106
107        public boolean passed(String testName) {
108            return saw(testName) && !failed(testName) && !errored(testName);
109        }
110
111        private String testName(Test test) {
112            TestCase testCase = (TestCase) test;
113            return testCase.getClass().getSimpleName() + "." + testCase.getName();
114        }
115    }
116}
117