| 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 | fd8f615 | 2017-03-24 12:44:34 +0000 | [diff] [blame] | 21 | import android.testing.TestableLooper.LooperStatement; |
| 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 | |
| 33 | import java.util.List; |
| 34 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 35 | /** |
| 36 | * A runner with support for extra annotations provided by the Testables library. |
| 37 | */ |
| 38 | public class AndroidTestingRunner extends BlockJUnit4ClassRunner { |
| Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 39 | |
| 40 | private final long mTimeout; |
| 41 | private final Class<?> mKlass; |
| 42 | |
| Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 43 | public AndroidTestingRunner(Class<?> klass) throws InitializationError { |
| Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 44 | 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 Monk | fd8f615 | 2017-03-24 12:44:34 +0000 | [diff] [blame] | 52 | 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 Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) { |
| Jason Monk | fd8f615 | 2017-03-24 12:44:34 +0000 | [diff] [blame] | 67 | List befores = this.getTestClass().getAnnotatedMethods(Before.class); |
| Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 68 | return befores.isEmpty() ? statement : new RunBefores(method, statement, |
| 69 | befores, target); |
| 70 | } |
| 71 | |
| 72 | protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) { |
| Jason Monk | fd8f615 | 2017-03-24 12:44:34 +0000 | [diff] [blame] | 73 | List afters = this.getTestClass().getAnnotatedMethods(After.class); |
| Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 74 | 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 Monk | ba36432 | 2017-03-06 11:19:20 -0500 | [diff] [blame] | 90 | |
| 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 Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 98 | } |