1/*
2 * Copyright (C) 2012 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 */
16package com.android.test.runner;
17
18import android.app.Instrumentation;
19import android.test.suitebuilder.annotation.SmallTest;
20
21import com.android.test.InjectInstrumentation;
22
23import org.junit.Assert;
24import org.junit.Test;
25import org.junit.runner.JUnitCore;
26import org.junit.runner.Result;
27
28import java.io.ByteArrayOutputStream;
29import java.io.PrintStream;
30
31/**
32 * Unit tests for {@link TestRequestBuilder}.
33 */
34public class TestRequestBuilderTest {
35
36    public static class SampleTest {
37
38        @SmallTest
39        @Test
40        public void testSmall() {
41        }
42
43        @Test
44        public void testOther() {
45        }
46    }
47
48    @SmallTest
49    public static class SampleClassSize {
50
51        @Test
52        public void testSmall() {
53        }
54
55        @Test
56        public void testSmallToo() {
57        }
58    }
59
60    @InjectInstrumentation
61    public Instrumentation mInstr;
62
63    /**
64     * Test initial condition for size filtering - that all tests run when no filter is attached
65     */
66    @Test
67    public void testNoSize() {
68        TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
69        b.addTestClass(SampleTest.class.getName());
70        TestRequest request = b.build(mInstr);
71        JUnitCore testRunner = new JUnitCore();
72        Result result = testRunner.run(request.getRequest());
73        Assert.assertEquals(2, result.getRunCount());
74    }
75
76    /**
77     * Test that size annotation filtering works
78     */
79    @Test
80    public void testSize() {
81        TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
82        b.addTestClass(SampleTest.class.getName());
83        b.addTestSizeFilter("small");
84        TestRequest request = b.build(mInstr);
85        JUnitCore testRunner = new JUnitCore();
86        Result result = testRunner.run(request.getRequest());
87        Assert.assertEquals(1, result.getRunCount());
88    }
89
90    /**
91     * Test that size annotation filtering by class works
92     */
93    @Test
94    public void testSize_class() {
95        TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
96        b.addTestClass(SampleTest.class.getName());
97        b.addTestClass(SampleClassSize.class.getName());
98        b.addTestSizeFilter("small");
99        TestRequest request = b.build(mInstr);
100        JUnitCore testRunner = new JUnitCore();
101        Result result = testRunner.run(request.getRequest());
102        Assert.assertEquals(3, result.getRunCount());
103    }
104
105    /**
106     * Test that annotation filtering by class works
107     */
108    @Test
109    public void testAddAnnotationInclusionFilter() {
110        TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
111        b.addAnnotationInclusionFilter(SmallTest.class.getName());
112        b.addTestClass(SampleTest.class.getName());
113        b.addTestClass(SampleClassSize.class.getName());
114        TestRequest request = b.build(mInstr);
115        JUnitCore testRunner = new JUnitCore();
116        Result result = testRunner.run(request.getRequest());
117        Assert.assertEquals(3, result.getRunCount());
118    }
119
120    /**
121     * Test that annotation filtering by class works
122     */
123    @Test
124    public void testAddAnnotationExclusionFilter() {
125        TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
126        b.addAnnotationExclusionFilter(SmallTest.class.getName());
127        b.addTestClass(SampleTest.class.getName());
128        b.addTestClass(SampleClassSize.class.getName());
129        TestRequest request = b.build(mInstr);
130        JUnitCore testRunner = new JUnitCore();
131        Result result = testRunner.run(request.getRequest());
132        Assert.assertEquals(1, result.getRunCount());
133    }
134}
135