Chromium Code Reviews| Index: base/test/android/javatests/src/org/chromium/base/test/SetUpTestRule.java |
| diff --git a/base/test/android/javatests/src/org/chromium/base/test/SetUpTestRule.java b/base/test/android/javatests/src/org/chromium/base/test/SetUpTestRule.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ee1feee19c33a2d94c6e105b69d70c6092d7a315 |
| --- /dev/null |
| +++ b/base/test/android/javatests/src/org/chromium/base/test/SetUpTestRule.java |
| @@ -0,0 +1,58 @@ |
| +// Copyright 2017 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 org.junit.rules.TestRule; |
| +import org.junit.runners.model.Statement; |
| + |
| +/** |
| + * Interface to ensure TestRule that implements it specifies if it would run a set-up process |
| + * before @Before. |
| + * |
| + * @param <T> TestRule type that implements this SetUpTestRule |
| + */ |
| +public interface SetUpTestRule<T extends TestRule> { |
| + /** |
| + * Set whether the TestRule should run setUp automatically and return the TestRule. |
|
mikecase (-- gone --)
2017/03/27 18:40:59
This could probably use clearer wording. Maybe...
the real yoland
2017/03/27 20:52:36
Done
|
| + * |
| + * So TestRule can be declared in test like this: |
| + * <code> |
| + * @Rule TestRule mRule = new MySetUpTestRule().shouldSetUp(true); |
| + * </code> |
| + */ |
| + T shouldSetUp(boolean runSetUp); |
| + |
| + /** |
| + * Specify setUp action in this method |
| + */ |
| + void setUp(); |
| + |
| + /** |
| + * Custom Statment for SetUpTestRules. |
| + * |
| + * The statment calls {@link SetUpTestRule#setUp} before test run based on |
| + * {@link SetUpTestRule#shouldSetUp} argument |
| + */ |
| + public static class SetUpStatement extends Statement { |
| + private final Statement mBase; |
| + private final SetUpTestRule<? extends TestRule> mSetUpTestRule; |
| + private final boolean mShouldSetUp; |
| + |
| + public SetUpStatement(final Statement base, SetUpTestRule<? extends TestRule> callback, |
| + boolean shouldSetUp) { |
| + mBase = base; |
| + mSetUpTestRule = callback; |
| + mShouldSetUp = shouldSetUp; |
| + } |
| + |
| + @Override |
| + public void evaluate() throws Throwable { |
| + if (mShouldSetUp) { |
| + mSetUpTestRule.setUp(); |
| + } |
| + mBase.evaluate(); |
| + } |
| + } |
| +} |