AndroidJUnit3Builder.java revision 3604db5bcd124dfd1396fb51434d3f4337690cff
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.junit3; 17 18import android.app.Instrumentation; 19 20import junit.framework.TestCase; 21 22import org.junit.runner.Runner; 23import org.junit.runners.model.RunnerBuilder; 24 25/** 26 * A {@link RunnerBuilder} that will build customized runners needed for specialized Android 27 * {@link TestCase}s. 28 */ 29public class AndroidJUnit3Builder extends RunnerBuilder { 30 31 private Instrumentation mInstr; 32 private boolean mSkipExecution; 33 34 public AndroidJUnit3Builder(Instrumentation instr, boolean skipExecution) { 35 mInstr = instr; 36 mSkipExecution = skipExecution; 37 } 38 39 @Override 40 public Runner runnerForClass(Class<?> testClass) throws Throwable { 41 if (mSkipExecution && isJUnit3TestCase(testClass)) { 42 return new NonExecutingJUnit3ClassRunner(testClass); 43 } else if (isAndroidTestCase(testClass)) { 44 return new AndroidJUnit3ClassRunner(testClass, mInstr); 45 } else if (isInstrumentationTestCase(testClass)) { 46 return new AndroidJUnit3ClassRunner(testClass, mInstr); 47 } 48 return null; 49 } 50 51 boolean isJUnit3TestCase(Class<?> testClass) { 52 return junit.framework.TestCase.class.isAssignableFrom(testClass); 53 } 54 55 boolean isAndroidTestCase(Class<?> testClass) { 56 return android.test.AndroidTestCase.class.isAssignableFrom(testClass); 57 } 58 59 boolean isInstrumentationTestCase(Class<?> testClass) { 60 return android.test.InstrumentationTestCase.class.isAssignableFrom(testClass); 61 } 62} 63