• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 android.test;
18 
19 import android.app.Instrumentation;
20 
21 import junit.framework.TestSuite;
22 import junit.framework.Test;
23 import junit.framework.TestResult;
24 
25 /**
26  * A {@link junit.framework.TestSuite} that injects {@link android.app.Instrumentation} into
27  * {@link InstrumentationTestCase} before running them.
28  */
29 public class InstrumentationTestSuite extends TestSuite {
30 
31     private final Instrumentation mInstrumentation;
32 
33     /**
34      * @param instr The instrumentation that will be injected into each
35      *   test before running it.
36      */
InstrumentationTestSuite(Instrumentation instr)37     public InstrumentationTestSuite(Instrumentation instr) {
38         mInstrumentation = instr;
39     }
40 
41 
InstrumentationTestSuite(String name, Instrumentation instr)42     public InstrumentationTestSuite(String name, Instrumentation instr) {
43         super(name);
44         mInstrumentation = instr;
45     }
46 
47     /**
48      * @param theClass Inspected for methods starting with 'test'
49      * @param instr The instrumentation to inject into each test before
50      *   running.
51      */
InstrumentationTestSuite(final Class theClass, Instrumentation instr)52     public InstrumentationTestSuite(final Class theClass, Instrumentation instr) {
53         super(theClass);
54         mInstrumentation = instr;
55     }
56 
57 
58     @Override
addTestSuite(Class testClass)59     public void addTestSuite(Class testClass) {
60         addTest(new InstrumentationTestSuite(testClass, mInstrumentation));
61     }
62 
63 
64     @Override
runTest(Test test, TestResult result)65     public void runTest(Test test, TestResult result) {
66 
67         if (test instanceof InstrumentationTestCase) {
68             ((InstrumentationTestCase) test).injectInstrumentation(mInstrumentation);
69         }
70 
71         // run the test as usual
72         super.runTest(test, result);
73     }
74 }
75