OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.base.test; | |
6 | |
7 import org.junit.rules.TestRule; | |
8 import org.junit.runners.model.Statement; | |
9 | |
10 /** | |
11 * Interface to ensure TestRule that implements it specifies if it would run a s et-up process | |
12 * before @Before. | |
13 * | |
14 * @param <T> TestRule type that implements this SetUpTestRule | |
15 */ | |
16 public interface SetUpTestRule<T extends TestRule> { | |
17 /** | |
18 * Set whether the TestRule should run setUp automatically. | |
19 * | |
20 * So TestRule can be declared in test like this: | |
21 * <code> | |
22 * @Rule TestRule mRule = new MySetUpTestRule().shouldSetUp(true); | |
23 * </code> | |
24 * | |
25 * @return itself to chain up the calls for convenience | |
26 */ | |
27 T shouldSetUp(boolean runSetUp); | |
28 | |
29 /** | |
30 * Specify setUp action in this method | |
31 */ | |
32 void setUp(); | |
33 | |
34 /** | |
35 * Custom Statment for SetUpTestRules. | |
jbudorick
2017/03/28 01:13:28
nit: Statment -> Statement
the real yoland
2017/03/28 01:32:50
Done
| |
36 * | |
37 * The statment calls {@link SetUpTestRule#setUp} before test run based on | |
38 * {@link SetUpTestRule#shouldSetUp} argument | |
39 */ | |
40 public static class SetUpStatement extends Statement { | |
41 private final Statement mBase; | |
42 private final SetUpTestRule<? extends TestRule> mSetUpTestRule; | |
43 private final boolean mShouldSetUp; | |
44 | |
45 public SetUpStatement(final Statement base, SetUpTestRule<? extends Test Rule> callback, | |
46 boolean shouldSetUp) { | |
47 mBase = base; | |
48 mSetUpTestRule = callback; | |
49 mShouldSetUp = shouldSetUp; | |
50 } | |
51 | |
52 @Override | |
53 public void evaluate() throws Throwable { | |
54 if (mShouldSetUp) { | |
55 mSetUpTestRule.setUp(); | |
56 } | |
57 mBase.evaluate(); | |
58 } | |
59 } | |
60 } | |
OLD | NEW |