Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Unified Diff: base/test/android/javatests/src/org/chromium/base/test/util/CommandLineTestRule.java

Issue 2523983002: Create CommandLineTestRule (Closed)
Patch Set: implements SetUpTestRule Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 {
+ * &#064;Rule CommandLineTestRule mCommandLineRule = new CommandLineTestRule().setFlags(...);
+ * &#064;Rule ChromeActivityTestRule mActivityTestRule = new ChromeActivityTestRule();
+ *
+ * &#064;Before
+ * public void setUp() {
+ * mCommandLineRule.setUp();
+ * mActivityTestRule.launchActivity();
+ * }
+ *
+ * &#064;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);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698