Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 1 | /* |
| 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 Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 15 | package android.testing; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 16 | |
| 17 | import android.support.test.internal.runner.junit4.statement.RunAfters; |
| 18 | import android.support.test.internal.runner.junit4.statement.RunBefores; |
| 19 | import android.support.test.internal.runner.junit4.statement.UiThreadStatement; |
| 20 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 21 | import android.testing.TestableLooper.LooperFrameworkMethod; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 22 | import android.testing.TestableLooper.RunWithLooper; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 23 | |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.internal.runners.statements.FailOnTimeout; |
| 28 | import org.junit.runners.BlockJUnit4ClassRunner; |
| 29 | import org.junit.runners.model.FrameworkMethod; |
| 30 | import org.junit.runners.model.InitializationError; |
| 31 | import org.junit.runners.model.Statement; |
| 32 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 33 | import java.util.ArrayList; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 34 | import java.util.List; |
| 35 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 36 | /** |
| 37 | * A runner with support for extra annotations provided by the Testables library. |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 38 | * @see UiThreadTest |
| 39 | * @see TestableLooper.RunWithLooper |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 40 | */ |
| 41 | public class AndroidTestingRunner extends BlockJUnit4ClassRunner { |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 42 | |
| 43 | private final long mTimeout; |
| 44 | private final Class<?> mKlass; |
| 45 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 46 | public AndroidTestingRunner(Class<?> klass) throws InitializationError { |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 47 | super(klass); |
| 48 | mKlass = klass; |
| 49 | // Can't seem to get reference to timeout parameter from here, so set default to 10 mins. |
| 50 | mTimeout = 10 * 60 * 1000; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected Statement methodInvoker(FrameworkMethod method, Object test) { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 55 | method = looperWrap(method, test, method); |
| 56 | final Statement statement = super.methodInvoker(method, test); |
| 57 | return shouldRunOnUiThread(method) ? new UiThreadStatement(statement, true) : statement; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 61 | List befores = looperWrap(method, target, |
| 62 | this.getTestClass().getAnnotatedMethods(Before.class)); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 63 | return befores.isEmpty() ? statement : new RunBefores(method, statement, |
| 64 | befores, target); |
| 65 | } |
| 66 | |
| 67 | protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 68 | List afters = looperWrap(method, target, |
| 69 | this.getTestClass().getAnnotatedMethods(After.class)); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 70 | return afters.isEmpty() ? statement : new RunAfters(method, statement, afters, |
| 71 | target); |
| 72 | } |
| 73 | |
| 74 | protected Statement withPotentialTimeout(FrameworkMethod method, Object test, Statement next) { |
| 75 | long timeout = this.getTimeout(method.getAnnotation(Test.class)); |
| 76 | if (timeout <= 0L && mTimeout > 0L) { |
| 77 | timeout = mTimeout; |
| 78 | } |
| 79 | |
| 80 | return timeout <= 0L ? next : new FailOnTimeout(next, timeout); |
| 81 | } |
| 82 | |
| 83 | private long getTimeout(Test annotation) { |
| 84 | return annotation == null ? 0L : annotation.timeout(); |
| 85 | } |
Jason Monk | ba36432 | 2017-03-06 11:19:20 -0500 | [diff] [blame] | 86 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 87 | protected List<FrameworkMethod> looperWrap(FrameworkMethod method, Object test, |
| 88 | List<FrameworkMethod> methods) { |
| 89 | RunWithLooper annotation = method.getAnnotation(RunWithLooper.class); |
| 90 | if (annotation == null) annotation = mKlass.getAnnotation(RunWithLooper.class); |
| 91 | if (annotation != null) { |
| 92 | methods = new ArrayList<>(methods); |
| 93 | for (int i = 0; i < methods.size(); i++) { |
| 94 | methods.set(i, LooperFrameworkMethod.get(methods.get(i), |
| 95 | annotation.setAsMainLooper(), test)); |
| 96 | } |
| 97 | } |
| 98 | return methods; |
| 99 | } |
| 100 | |
| 101 | protected FrameworkMethod looperWrap(FrameworkMethod method, Object test, |
| 102 | FrameworkMethod base) { |
| 103 | RunWithLooper annotation = method.getAnnotation(RunWithLooper.class); |
| 104 | if (annotation == null) annotation = mKlass.getAnnotation(RunWithLooper.class); |
| 105 | if (annotation != null) { |
| 106 | return LooperFrameworkMethod.get(base, annotation.setAsMainLooper(), test); |
| 107 | } |
| 108 | return base; |
| 109 | } |
| 110 | |
Jason Monk | ba36432 | 2017-03-06 11:19:20 -0500 | [diff] [blame] | 111 | public boolean shouldRunOnUiThread(FrameworkMethod method) { |
| 112 | if (mKlass.getAnnotation(UiThreadTest.class) != null) { |
| 113 | return true; |
| 114 | } else { |
| 115 | return UiThreadStatement.shouldRunOnUiThread(method); |
| 116 | } |
| 117 | } |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 118 | } |