Home | History | Annotate | Download | only in runner
      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  */
     16 package com.android.test.runner;
     17 
     18 import android.app.Instrumentation;
     19 import android.os.Bundle;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 import android.test.suitebuilder.annotation.Suppress;
     22 
     23 import com.android.test.InjectBundle;
     24 import com.android.test.InjectInstrumentation;
     25 
     26 import junit.framework.TestCase;
     27 
     28 import org.junit.Assert;
     29 import org.junit.Test;
     30 import org.junit.runner.Description;
     31 import org.junit.runner.JUnitCore;
     32 import org.junit.runner.Result;
     33 import org.junit.runner.notification.RunListener;
     34 
     35 import java.io.ByteArrayOutputStream;
     36 import java.io.PrintStream;
     37 
     38 /**
     39  * Unit tests for {@link TestRequestBuilder}.
     40  */
     41 public class TestRequestBuilderTest {
     42 
     43     public static class SampleTest {
     44 
     45         @SmallTest
     46         @Test
     47         public void testSmall() {
     48         }
     49 
     50         @Test
     51         public void testOther() {
     52         }
     53     }
     54 
     55     @SmallTest
     56     public static class SampleClassSize {
     57 
     58         @Test
     59         public void testSmall() {
     60         }
     61 
     62         @Test
     63         public void testSmallToo() {
     64         }
     65     }
     66 
     67     public static class SampleNoSize extends TestCase {
     68 
     69         public void testOther() {
     70         }
     71 
     72         public void testOther2() {
     73         }
     74 
     75     }
     76 
     77     public static class SampleJUnit3Test extends TestCase {
     78 
     79         @SmallTest
     80         public void testSmall() {
     81         }
     82 
     83         @SmallTest
     84         public void testSmall2() {
     85         }
     86 
     87         public void testOther() {
     88         }
     89     }
     90 
     91     @SmallTest
     92     public static class SampleJUnit3ClassSize extends TestCase {
     93 
     94         public void testSmall() {
     95         }
     96 
     97         public void testSmall2() {
     98         }
     99 
    100     }
    101 
    102     public static class SampleJUnit3Suppressed extends TestCase {
    103 
    104         public void testRun() {
    105         }
    106 
    107         public void testRun2() {
    108         }
    109 
    110         @Suppress
    111         public void testSuppressed() {
    112         }
    113 
    114     }
    115 
    116     @InjectInstrumentation
    117     public Instrumentation mInstr;
    118 
    119     @InjectBundle
    120     public Bundle mBundle;
    121 
    122     /**
    123      * Test initial condition for size filtering - that all tests run when no filter is attached
    124      */
    125     @Test
    126     public void testNoSize() {
    127         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    128         b.addTestClass(SampleTest.class.getName());
    129         TestRequest request = b.build(mInstr, mBundle);
    130         JUnitCore testRunner = new JUnitCore();
    131         Result result = testRunner.run(request.getRequest());
    132         Assert.assertEquals(2, result.getRunCount());
    133     }
    134 
    135     /**
    136      * Test that size annotation filtering works
    137      */
    138     @Test
    139     public void testSize() {
    140         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    141         b.addTestClass(SampleTest.class.getName());
    142         b.addTestSizeFilter("small");
    143         TestRequest request = b.build(mInstr, mBundle);
    144         JUnitCore testRunner = new JUnitCore();
    145         Result result = testRunner.run(request.getRequest());
    146         Assert.assertEquals(1, result.getRunCount());
    147     }
    148 
    149     /**
    150      * Test that size annotation filtering by class works
    151      */
    152     @Test
    153     public void testSize_class() {
    154         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    155         b.addTestClass(SampleTest.class.getName());
    156         b.addTestClass(SampleClassSize.class.getName());
    157         b.addTestSizeFilter("small");
    158         TestRequest request = b.build(mInstr, mBundle);
    159         JUnitCore testRunner = new JUnitCore();
    160         Result result = testRunner.run(request.getRequest());
    161         Assert.assertEquals(3, result.getRunCount());
    162     }
    163 
    164     /**
    165      * Test case where entire JUnit3 test class has been filtered out
    166      */
    167     @Test
    168     public void testSize_classFiltered() {
    169         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    170         b.addTestClass(SampleTest.class.getName());
    171         b.addTestClass(SampleNoSize.class.getName());
    172         b.addTestSizeFilter("small");
    173         TestRequest request = b.build(mInstr, mBundle);
    174         MyRunListener l = new MyRunListener();
    175         JUnitCore testRunner = new JUnitCore();
    176         testRunner.addListener(l);
    177         testRunner.run(request.getRequest());
    178         Assert.assertEquals(1, l.mTestCount);
    179     }
    180 
    181     private static class MyRunListener extends RunListener {
    182         private int mTestCount = -1;
    183 
    184         public void testRunStarted(Description description) throws Exception {
    185             mTestCount = description.testCount();
    186         }
    187     }
    188 
    189     /**
    190      * Test size annotations with JUnit3 test methods
    191      */
    192     @Test
    193     public void testSize_junit3Method() {
    194         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    195         b.addTestClass(SampleJUnit3Test.class.getName());
    196         b.addTestClass(SampleNoSize.class.getName());
    197         b.addTestSizeFilter("small");
    198         TestRequest request = b.build(mInstr, mBundle);
    199         JUnitCore testRunner = new JUnitCore();
    200         Result r = testRunner.run(request.getRequest());
    201         Assert.assertEquals(2, r.getRunCount());
    202     }
    203 
    204     /**
    205      * Test @Suppress with JUnit3 tests
    206      */
    207     @Test
    208     public void testSuppress_junit3Method() {
    209         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    210         b.addTestClass(SampleJUnit3Suppressed.class.getName());
    211         TestRequest request = b.build(mInstr, mBundle);
    212         JUnitCore testRunner = new JUnitCore();
    213         Result r = testRunner.run(request.getRequest());
    214         Assert.assertEquals(2, r.getRunCount());
    215     }
    216 
    217     /**
    218      * Test that annotation filtering by class works
    219      */
    220     @Test
    221     public void testAddAnnotationInclusionFilter() {
    222         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    223         b.addAnnotationInclusionFilter(SmallTest.class.getName());
    224         b.addTestClass(SampleTest.class.getName());
    225         b.addTestClass(SampleClassSize.class.getName());
    226         TestRequest request = b.build(mInstr, mBundle);
    227         JUnitCore testRunner = new JUnitCore();
    228         Result result = testRunner.run(request.getRequest());
    229         Assert.assertEquals(3, result.getRunCount());
    230     }
    231 
    232     /**
    233      * Test that annotation filtering by class works
    234      */
    235     @Test
    236     public void testAddAnnotationExclusionFilter() {
    237         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    238         b.addAnnotationExclusionFilter(SmallTest.class.getName());
    239         b.addTestClass(SampleTest.class.getName());
    240         b.addTestClass(SampleClassSize.class.getName());
    241         TestRequest request = b.build(mInstr, mBundle);
    242         JUnitCore testRunner = new JUnitCore();
    243         Result result = testRunner.run(request.getRequest());
    244         Assert.assertEquals(1, result.getRunCount());
    245     }
    246 }
    247