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 
     22 import com.android.test.InjectBundle;
     23 import com.android.test.InjectInstrumentation;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import org.junit.Assert;
     28 import org.junit.Test;
     29 import org.junit.runner.Description;
     30 import org.junit.runner.JUnitCore;
     31 import org.junit.runner.Result;
     32 import org.junit.runner.notification.RunListener;
     33 
     34 import java.io.ByteArrayOutputStream;
     35 import java.io.PrintStream;
     36 
     37 /**
     38  * Unit tests for {@link TestRequestBuilder}.
     39  */
     40 public class TestRequestBuilderTest {
     41 
     42     public static class SampleTest {
     43 
     44         @SmallTest
     45         @Test
     46         public void testSmall() {
     47         }
     48 
     49         @Test
     50         public void testOther() {
     51         }
     52     }
     53 
     54     @SmallTest
     55     public static class SampleClassSize {
     56 
     57         @Test
     58         public void testSmall() {
     59         }
     60 
     61         @Test
     62         public void testSmallToo() {
     63         }
     64     }
     65 
     66     public static class SampleNoSize extends TestCase {
     67 
     68         public void testOther() {
     69         }
     70 
     71         public void testOther2() {
     72         }
     73 
     74     }
     75 
     76     @InjectInstrumentation
     77     public Instrumentation mInstr;
     78 
     79     @InjectBundle
     80     public Bundle mBundle;
     81 
     82     /**
     83      * Test initial condition for size filtering - that all tests run when no filter is attached
     84      */
     85     @Test
     86     public void testNoSize() {
     87         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
     88         b.addTestClass(SampleTest.class.getName());
     89         TestRequest request = b.build(mInstr, mBundle);
     90         JUnitCore testRunner = new JUnitCore();
     91         Result result = testRunner.run(request.getRequest());
     92         Assert.assertEquals(2, result.getRunCount());
     93     }
     94 
     95     /**
     96      * Test that size annotation filtering works
     97      */
     98     @Test
     99     public void testSize() {
    100         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    101         b.addTestClass(SampleTest.class.getName());
    102         b.addTestSizeFilter("small");
    103         TestRequest request = b.build(mInstr, mBundle);
    104         JUnitCore testRunner = new JUnitCore();
    105         Result result = testRunner.run(request.getRequest());
    106         Assert.assertEquals(1, result.getRunCount());
    107     }
    108 
    109     /**
    110      * Test that size annotation filtering by class works
    111      */
    112     @Test
    113     public void testSize_class() {
    114         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    115         b.addTestClass(SampleTest.class.getName());
    116         b.addTestClass(SampleClassSize.class.getName());
    117         b.addTestSizeFilter("small");
    118         TestRequest request = b.build(mInstr, mBundle);
    119         JUnitCore testRunner = new JUnitCore();
    120         Result result = testRunner.run(request.getRequest());
    121         Assert.assertEquals(3, result.getRunCount());
    122     }
    123 
    124     /**
    125      * Test case where entire JUnit3 test class has been filtered out
    126      */
    127     @Test
    128     public void testSize_classFiltered() {
    129         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    130         b.addTestClass(SampleTest.class.getName());
    131         b.addTestClass(SampleNoSize.class.getName());
    132         b.addTestSizeFilter("small");
    133         TestRequest request = b.build(mInstr, mBundle);
    134         MyRunListener l = new MyRunListener();
    135         JUnitCore testRunner = new JUnitCore();
    136         testRunner.addListener(l);
    137         testRunner.run(request.getRequest());
    138         Assert.assertEquals(1, l.mTestCount);
    139     }
    140 
    141     private static class MyRunListener extends RunListener {
    142         private int mTestCount = -1;
    143 
    144         public void testRunStarted(Description description) throws Exception {
    145             mTestCount = description.testCount();
    146         }
    147     }
    148 
    149     /**
    150      * Test that annotation filtering by class works
    151      */
    152     @Test
    153     public void testAddAnnotationInclusionFilter() {
    154         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    155         b.addAnnotationInclusionFilter(SmallTest.class.getName());
    156         b.addTestClass(SampleTest.class.getName());
    157         b.addTestClass(SampleClassSize.class.getName());
    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 that annotation filtering by class works
    166      */
    167     @Test
    168     public void testAddAnnotationExclusionFilter() {
    169         TestRequestBuilder b = new TestRequestBuilder(new PrintStream(new ByteArrayOutputStream()));
    170         b.addAnnotationExclusionFilter(SmallTest.class.getName());
    171         b.addTestClass(SampleTest.class.getName());
    172         b.addTestClass(SampleClassSize.class.getName());
    173         TestRequest request = b.build(mInstr, mBundle);
    174         JUnitCore testRunner = new JUnitCore();
    175         Result result = testRunner.run(request.getRequest());
    176         Assert.assertEquals(1, result.getRunCount());
    177     }
    178 }
    179