blob: a425f70e836ce5aab15f46837c4eb26fad53c3f1 [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 Monk745d0a82017-04-17 11:34:22 -040021import android.testing.TestableLooper.LooperFrameworkMethod;
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
Jason Monk745d0a82017-04-17 11:34:22 -040033import java.util.ArrayList;
Jason Monke7507482017-02-02 13:00:05 -050034import java.util.List;
35
Jason Monk340b0e52017-03-08 14:57:56 -050036/**
37 * A runner with support for extra annotations provided by the Testables library.
38 */
39public class AndroidTestingRunner extends BlockJUnit4ClassRunner {
Jason Monke7507482017-02-02 13:00:05 -050040
41 private final long mTimeout;
42 private final Class<?> mKlass;
43
Jason Monk340b0e52017-03-08 14:57:56 -050044 public AndroidTestingRunner(Class<?> klass) throws InitializationError {
Jason Monke7507482017-02-02 13:00:05 -050045 super(klass);
46 mKlass = klass;
47 // Can't seem to get reference to timeout parameter from here, so set default to 10 mins.
48 mTimeout = 10 * 60 * 1000;
49 }
50
51 @Override
52 protected Statement methodInvoker(FrameworkMethod method, Object test) {
Jason Monk745d0a82017-04-17 11:34:22 -040053 method = looperWrap(method, test, method);
54 final Statement statement = super.methodInvoker(method, test);
55 return shouldRunOnUiThread(method) ? new UiThreadStatement(statement, true) : statement;
Jason Monke7507482017-02-02 13:00:05 -050056 }
57
58 protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
Jason Monk745d0a82017-04-17 11:34:22 -040059 List befores = looperWrap(method, target,
60 this.getTestClass().getAnnotatedMethods(Before.class));
Jason Monke7507482017-02-02 13:00:05 -050061 return befores.isEmpty() ? statement : new RunBefores(method, statement,
62 befores, target);
63 }
64
65 protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
Jason Monk745d0a82017-04-17 11:34:22 -040066 List afters = looperWrap(method, target,
67 this.getTestClass().getAnnotatedMethods(After.class));
Jason Monke7507482017-02-02 13:00:05 -050068 return afters.isEmpty() ? statement : new RunAfters(method, statement, afters,
69 target);
70 }
71
72 protected Statement withPotentialTimeout(FrameworkMethod method, Object test, Statement next) {
73 long timeout = this.getTimeout(method.getAnnotation(Test.class));
74 if (timeout <= 0L && mTimeout > 0L) {
75 timeout = mTimeout;
76 }
77
78 return timeout <= 0L ? next : new FailOnTimeout(next, timeout);
79 }
80
81 private long getTimeout(Test annotation) {
82 return annotation == null ? 0L : annotation.timeout();
83 }
Jason Monkba364322017-03-06 11:19:20 -050084
Jason Monk745d0a82017-04-17 11:34:22 -040085 protected List<FrameworkMethod> looperWrap(FrameworkMethod method, Object test,
86 List<FrameworkMethod> methods) {
87 RunWithLooper annotation = method.getAnnotation(RunWithLooper.class);
88 if (annotation == null) annotation = mKlass.getAnnotation(RunWithLooper.class);
89 if (annotation != null) {
90 methods = new ArrayList<>(methods);
91 for (int i = 0; i < methods.size(); i++) {
92 methods.set(i, LooperFrameworkMethod.get(methods.get(i),
93 annotation.setAsMainLooper(), test));
94 }
95 }
96 return methods;
97 }
98
99 protected FrameworkMethod looperWrap(FrameworkMethod method, Object test,
100 FrameworkMethod base) {
101 RunWithLooper annotation = method.getAnnotation(RunWithLooper.class);
102 if (annotation == null) annotation = mKlass.getAnnotation(RunWithLooper.class);
103 if (annotation != null) {
104 return LooperFrameworkMethod.get(base, annotation.setAsMainLooper(), test);
105 }
106 return base;
107 }
108
Jason Monkba364322017-03-06 11:19:20 -0500109 public boolean shouldRunOnUiThread(FrameworkMethod method) {
110 if (mKlass.getAnnotation(UiThreadTest.class) != null) {
111 return true;
112 } else {
113 return UiThreadStatement.shouldRunOnUiThread(method);
114 }
115 }
Jason Monke7507482017-02-02 13:00:05 -0500116}