Index: base/test/android/javatests/src/org/chromium/base/test/util/CommandLineTestRule.java |
diff --git a/base/test/android/javatests/src/org/chromium/base/test/util/CommandLineTestRule.java b/base/test/android/javatests/src/org/chromium/base/test/util/CommandLineTestRule.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..700cf1c18050c29b3634207c359b7d99d68fd5ac |
--- /dev/null |
+++ b/base/test/android/javatests/src/org/chromium/base/test/util/CommandLineTestRule.java |
@@ -0,0 +1,68 @@ |
+// 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.util; |
+ |
+import android.support.test.InstrumentationRegistry; |
+ |
+import org.junit.rules.TestRule; |
+import org.junit.runner.Description; |
+import org.junit.runners.model.Statement; |
+ |
+import org.chromium.base.test.SetUpTestRule; |
+ |
+import java.util.Arrays; |
+import java.util.HashSet; |
+import java.util.Set; |
+ |
+/** |
+ * TestRule to used by JUnit4 style instrumentation test to set CommandLine flags. |
+ * |
+ * If you need to add CommandLine flag to your junit4 style instrumentation test, add this |
+ * TestRule with the flag string you need. Remember to call setUp() before you launch |
+ * the activity. |
+ * |
+ * <pre><code> |
+ * public class ChromeActivityTest { |
+ * @Rule CommandLineTestRule mCommandLineRule = new CommandLineTestRule().setFlags(...); |
+ * @Rule ChromeActivityTestRule mActivityTestRule = new ChromeActivityTestRule(); |
+ * |
+ * @Before |
+ * public void setUp() { |
+ * mCommandLineRule.setUp(); |
+ * mActivityTestRule.launchActivity(); |
+ * } |
+ * |
+ * @Test |
+ * public void test() {...} |
+ * </code></pre> |
+ * |
+ */ |
+public final class CommandLineTestRule implements TestRule, SetUpTestRule<CommandLineTestRule> { |
+ private Set<String> mFlagSet; |
+ private boolean mShouldSetUp = false; |
+ |
+ public CommandLineTestRule setFlags(String firstFlag, String... flags) { |
+ mFlagSet = new HashSet<>(); |
+ mFlagSet.add(firstFlag); |
+ mFlagSet.addAll(Arrays.asList(flags)); |
+ return this; |
+ } |
+ |
+ @Override |
+ public CommandLineTestRule shouldSetUp(boolean shouldSetUp) { |
+ mShouldSetUp = shouldSetUp; |
+ return this; |
+ } |
+ |
+ @Override |
+ public void setUp() { |
+ CommandLineFlags.setUp(InstrumentationRegistry.getTargetContext(), mFlagSet); |
+ } |
+ |
+ @Override |
+ public Statement apply(final Statement base, Description desc) { |
+ return new SetUpStatement(base, this, mShouldSetUp); |
+ } |
+} |