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.util; |
| 6 |
| 7 import android.support.test.InstrumentationRegistry; |
| 8 |
| 9 import org.junit.rules.TestRule; |
| 10 import org.junit.runner.Description; |
| 11 import org.junit.runners.model.Statement; |
| 12 |
| 13 import org.chromium.base.test.SetUpTestRule; |
| 14 |
| 15 import java.util.Arrays; |
| 16 import java.util.HashSet; |
| 17 import java.util.Set; |
| 18 |
| 19 /** |
| 20 * TestRule to used by JUnit4 style instrumentation test to set CommandLine flag
s. |
| 21 * |
| 22 * If you need to add CommandLine flag to your junit4 style instrumentation test
, add this |
| 23 * TestRule with the flag string you need. Remember to call setUp() before you l
aunch |
| 24 * the activity. |
| 25 * |
| 26 * <pre><code> |
| 27 * public class ChromeActivityTest { |
| 28 * @Rule CommandLineTestRule mCommandLineRule = new CommandLineTestRule
().setFlags(...); |
| 29 * @Rule ChromeActivityTestRule mActivityTestRule = new ChromeActivityT
estRule(); |
| 30 * |
| 31 * @Before |
| 32 * public void setUp() { |
| 33 * mCommandLineRule.setUp(); |
| 34 * mActivityTestRule.launchActivity(); |
| 35 * } |
| 36 * |
| 37 * @Test |
| 38 * public void test() {...} |
| 39 * </code></pre> |
| 40 * |
| 41 */ |
| 42 public final class CommandLineTestRule implements TestRule, SetUpTestRule<Comman
dLineTestRule> { |
| 43 private Set<String> mFlagSet; |
| 44 private boolean mShouldSetUp = false; |
| 45 |
| 46 public CommandLineTestRule setFlags(String firstFlag, String... flags) { |
| 47 mFlagSet = new HashSet<>(); |
| 48 mFlagSet.add(firstFlag); |
| 49 mFlagSet.addAll(Arrays.asList(flags)); |
| 50 return this; |
| 51 } |
| 52 |
| 53 @Override |
| 54 public CommandLineTestRule shouldSetUp(boolean shouldSetUp) { |
| 55 mShouldSetUp = shouldSetUp; |
| 56 return this; |
| 57 } |
| 58 |
| 59 @Override |
| 60 public void setUp() { |
| 61 CommandLineFlags.setUp(InstrumentationRegistry.getTargetContext(), mFlag
Set); |
| 62 } |
| 63 |
| 64 @Override |
| 65 public Statement apply(final Statement base, Description desc) { |
| 66 return new SetUpStatement(base, this, mShouldSetUp); |
| 67 } |
| 68 } |
OLD | NEW |