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