blob: 816ed033a3e258d884cc0f9d1a1390cc1b92853a [file] [log] [blame]
Jason Monke7507482017-02-02 13:00:05 -05001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
Jason Monk340b0e52017-03-08 14:57:56 -050015package android.testing;
Jason Monke7507482017-02-02 13:00:05 -050016
17import android.support.test.internal.runner.junit4.statement.RunAfters;
18import android.support.test.internal.runner.junit4.statement.RunBefores;
19import android.support.test.internal.runner.junit4.statement.UiThreadStatement;
20
Jason Monkfd8f6152017-03-24 12:44:34 +000021import android.testing.TestableLooper.LooperStatement;
Jason Monk340b0e52017-03-08 14:57:56 -050022import android.testing.TestableLooper.RunWithLooper;
Jason Monke7507482017-02-02 13:00:05 -050023
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.junit.internal.runners.statements.FailOnTimeout;
28import org.junit.runners.BlockJUnit4ClassRunner;
29import org.junit.runners.model.FrameworkMethod;
30import org.junit.runners.model.InitializationError;
31import org.junit.runners.model.Statement;
32
33import java.util.List;
34
Jason Monk340b0e52017-03-08 14:57:56 -050035/**
36 * A runner with support for extra annotations provided by the Testables library.
37 */
38public class AndroidTestingRunner extends BlockJUnit4ClassRunner {
Jason Monke7507482017-02-02 13:00:05 -050039
40 private final long mTimeout;
41 private final Class<?> mKlass;
42
Jason Monk340b0e52017-03-08 14:57:56 -050043 public AndroidTestingRunner(Class<?> klass) throws InitializationError {
Jason Monke7507482017-02-02 13:00:05 -050044 super(klass);
45 mKlass = klass;
46 // Can't seem to get reference to timeout parameter from here, so set default to 10 mins.
47 mTimeout = 10 * 60 * 1000;
48 }
49
50 @Override
51 protected Statement methodInvoker(FrameworkMethod method, Object test) {
Jason Monkfd8f6152017-03-24 12:44:34 +000052 return shouldRunOnUiThread(method) ? new UiThreadStatement(
53 methodInvokerInt(method, test), true) : methodInvokerInt(method, test);
54 }
55
56 protected Statement methodInvokerInt(FrameworkMethod method, Object test) {
57 RunWithLooper annotation = method.getAnnotation(RunWithLooper.class);
58 if (annotation == null) annotation = mKlass.getAnnotation(RunWithLooper.class);
59 if (annotation != null) {
60 return new LooperStatement(super.methodInvoker(method, test),
61 annotation.setAsMainLooper(), test);
62 }
63 return super.methodInvoker(method, test);
Jason Monke7507482017-02-02 13:00:05 -050064 }
65
66 protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
Jason Monkfd8f6152017-03-24 12:44:34 +000067 List befores = this.getTestClass().getAnnotatedMethods(Before.class);
Jason Monke7507482017-02-02 13:00:05 -050068 return befores.isEmpty() ? statement : new RunBefores(method, statement,
69 befores, target);
70 }
71
72 protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
Jason Monkfd8f6152017-03-24 12:44:34 +000073 List afters = this.getTestClass().getAnnotatedMethods(After.class);
Jason Monke7507482017-02-02 13:00:05 -050074 return afters.isEmpty() ? statement : new RunAfters(method, statement, afters,
75 target);
76 }
77
78 protected Statement withPotentialTimeout(FrameworkMethod method, Object test, Statement next) {
79 long timeout = this.getTimeout(method.getAnnotation(Test.class));
80 if (timeout <= 0L && mTimeout > 0L) {
81 timeout = mTimeout;
82 }
83
84 return timeout <= 0L ? next : new FailOnTimeout(next, timeout);
85 }
86
87 private long getTimeout(Test annotation) {
88 return annotation == null ? 0L : annotation.timeout();
89 }
Jason Monkba364322017-03-06 11:19:20 -050090
91 public boolean shouldRunOnUiThread(FrameworkMethod method) {
92 if (mKlass.getAnnotation(UiThreadTest.class) != null) {
93 return true;
94 } else {
95 return UiThreadStatement.shouldRunOnUiThread(method);
96 }
97 }
Jason Monke7507482017-02-02 13:00:05 -050098}