Index: base/test/android/javatests/src/org/chromium/base/test/BaseJUnit4ClassRunner.java |
diff --git a/base/test/android/javatests/src/org/chromium/base/test/BaseJUnit4ClassRunner.java b/base/test/android/javatests/src/org/chromium/base/test/BaseJUnit4ClassRunner.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b28a71b01de9373bde241ee4a39b125a0a1c8131 |
--- /dev/null |
+++ b/base/test/android/javatests/src/org/chromium/base/test/BaseJUnit4ClassRunner.java |
@@ -0,0 +1,84 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.base.test; |
+ |
+import android.content.Context; |
+import android.support.test.InstrumentationRegistry; |
+ |
+import org.chromium.base.test.BaseTestResult.PreTestHook; |
+import org.chromium.base.test.util.DisableIfSkipCheck; |
+import org.chromium.base.test.util.MinAndroidSdkLevelSkipCheck; |
+import org.chromium.base.test.util.RestrictionSkipCheck; |
+import org.chromium.base.test.util.SkipCheck; |
+import org.junit.runner.notification.RunNotifier; |
+import org.junit.runners.BlockJUnit4ClassRunner; |
+import org.junit.runners.model.FrameworkMethod; |
+import org.junit.runners.model.InitializationError; |
+ |
+import java.lang.reflect.Method; |
+import java.util.ArrayList; |
+import java.util.List; |
+ |
+/** |
+ * The custom runner for JUnit4 tests that checks requirements to conditionally ignore tests. |
+ */ |
+public class BaseJUnit4ClassRunner extends BlockJUnit4ClassRunner { |
+ private final List<SkipCheck> mSkipChecks = new ArrayList<>(); |
+ private final List<PreTestHook> mPreTestHooks = new ArrayList<>(); |
jbudorick
2016/09/15 22:52:29
How do pre-test hooks get added?
the real yoland
2016/09/20 21:26:43
They can be added by calling addPreTestHooks in th
|
+ |
+ /** |
+ * Create a BaseJUnit4ClassRunner to run {@code testClass} and set an ArrayList of |
+ * {@code SkipCheck}s |
+ * |
+ * @throws InitializationError if the test class malformed |
+ */ |
+ public BaseJUnit4ClassRunner(final Class<?> klass) throws InitializationError { |
+ super(klass); |
+ setSkipChecks(); |
+ } |
+ |
+ /** |
+ * Evaluate whether {@link FrameworkMethod}s are ignored based on {@code SkipCheck}s |
+ */ |
+ @Override |
+ protected boolean isIgnored(FrameworkMethod method) { |
+ return super.isIgnored(method) || shouldSkip(method); |
+ } |
+ |
+ @Override |
+ protected void runChild(FrameworkMethod method, RunNotifier notifier) { |
+ runPreTestHooks(method); |
+ super.runChild(method, notifier); |
+ } |
+ |
+ private void runPreTestHooks(FrameworkMethod frameworkMethod) { |
+ Method testMethod = frameworkMethod.getMethod(); |
+ Context targetContext = InstrumentationRegistry.getTargetContext(); |
+ |
+ for (PreTestHook hook : mPreTestHooks) { |
+ hook.run(targetContext, testMethod); |
+ } |
+ } |
+ |
+ protected void setSkipChecks() { |
jbudorick
2016/09/15 22:52:29
nit: addSkipChecks
the real yoland
2016/09/20 21:26:43
Done
|
+ mSkipChecks.add(new MinAndroidSdkLevelSkipCheck()); |
+ mSkipChecks.add(new RestrictionSkipCheck(InstrumentationRegistry.getTargetContext())); |
+ mSkipChecks.add(new DisableIfSkipCheck()); |
+ } |
+ |
+ /** |
+ * Loop through all the {@code SkipCheck}s to confirm whether a test should be ignored |
+ * |
+ * TODO(yolandyan): Do not expose the method, only pass in a list of annotations. |
+ */ |
+ protected boolean shouldSkip(FrameworkMethod method) { |
+ for (SkipCheck s : mSkipChecks) { |
+ if (s.shouldSkip(method)) { |
+ return true; |
+ } |
+ } |
+ return false; |
+ } |
+} |