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

Unified Diff: base/test/android/junit/src/org/chromium/base/test/CommandLineFlagsTest.java

Issue 2523983002: Create CommandLineTestRule (Closed)
Patch Set: change based on comments Created 3 years, 11 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/junit/src/org/chromium/base/test/CommandLineFlagsTest.java
diff --git a/base/test/android/junit/src/org/chromium/base/test/CommandLineFlagsTest.java b/base/test/android/junit/src/org/chromium/base/test/CommandLineFlagsTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..4f0058dc9b7285f562060ddf7e40160662f41046
--- /dev/null
+++ b/base/test/android/junit/src/org/chromium/base/test/CommandLineFlagsTest.java
@@ -0,0 +1,165 @@
+// Copyright 2016 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.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.Description;
+import org.junit.runner.RunWith;
+import org.junit.runners.BlockJUnit4ClassRunner;
+
+import org.chromium.base.CommandLine;
+import org.chromium.base.test.util.CommandLineFlags;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Set;
+
+/** Unit tests for CommandLineFlags. */
+@RunWith(BlockJUnit4ClassRunner.class)
+public class CommandLineFlagsTest {
+ @CommandLineFlags.Add("A")
+ class ParentClassAddFlagA {
+ @CommandLineFlags.Remove("A")
+ public void testMethodRemoveFlagA() {}
+
+ @CommandLineFlags.Add("B")
+ public void testMethodAddFlagB() {}
+ }
+
+ class ChildClass extends ParentClassAddFlagA {
+ public void testMethod() {}
+ }
+
+ @CommandLineFlags.Remove("A")
+ class ChildClassRemoveFlagA extends ParentClassAddFlagA {
+ public void testMethod() {}
+
+ @CommandLineFlags.Add("A")
+ public void testMethodAddFlagA() {}
+ }
+
+ class EmptyClass {
+ public void testEmpty() {}
+ }
+
+ @Before
+ public void setUp() {
+ CommandLine.init(new String[0]);
+ }
+
+ @Test
+ public void testGetFlagsParentRemoveA() throws Throwable {
+ Method method = ParentClassAddFlagA.class.getDeclaredMethod("testMethodRemoveFlagA");
+ Set<String> flags = CommandLineFlags.getFlags(method);
+ Assert.assertEquals("testMethodRemoveFlagA should have no flag", 0, flags.size());
+ }
+
+ @Test
+ public void testGetFlagsParentRemoveAWithDesc() throws Throwable {
+ Annotation[] annotations = ParentClassAddFlagA.class.getMethod("testMethodRemoveFlagA")
+ .getDeclaredAnnotations();
+ Description desc = Description.createTestDescription(
+ ParentClassAddFlagA.class, "testMethodRemoveFlagA", annotations);
+ Set<String> flags = CommandLineFlags.getFlags(desc);
+ Assert.assertEquals(String.format("testMethodRemoveFlagA should have no flag %s",
+ Arrays.toString(flags.toArray())),
+ 0, flags.size());
+ }
+
+ @Test
+ public void testGetFlagsParentAddB() throws Throwable {
+ Method method = ParentClassAddFlagA.class.getDeclaredMethod("testMethodAddFlagB");
+ Set<String> flags = CommandLineFlags.getFlags(method);
+ Assert.assertEquals("testMethodAddFlagB should have 2 flags", 2, flags.size());
+ Assert.assertTrue("testMethodAddFlagB should have both A and B flag", flags.contains("A"));
+ Assert.assertTrue("testMethodAddFlagB should have both A and B flag", flags.contains("B"));
+ }
+
+ @Test
+ public void testGetFlagsParentAddBWithDesc() throws Throwable {
+ Annotation[] annotations =
+ ParentClassAddFlagA.class.getMethod("testMethodAddFlagB").getDeclaredAnnotations();
+ Description desc = Description.createTestDescription(
+ ParentClassAddFlagA.class, "testMethodAddFlagB", annotations);
+ Set<String> flags = CommandLineFlags.getFlags(desc);
+ Assert.assertEquals("testMethodAddFlagB should have 2 flags", 2, flags.size());
+ Assert.assertTrue("testMethodAddFlagB should have both A and B flag", flags.contains("A"));
+ Assert.assertTrue("testMethodAddFlagB should have both A and B flag", flags.contains("B"));
+ }
+
+ @Test
+ public void testGetFlagsChild() throws Throwable {
+ Method method = ChildClass.class.getMethod("testMethod");
+ Set<String> flags = CommandLineFlags.getFlags(method);
+ Assert.assertEquals("testMethod should have 1 flag", 1, flags.size());
+ Assert.assertTrue("testMethod should have A flag", flags.contains("A"));
+ }
+
+ @Test
+ public void testGetFlagsChildWithDesc() throws Throwable {
+ Annotation[] annotations =
+ ChildClass.class.getMethod("testMethod").getDeclaredAnnotations();
+ Description desc =
+ Description.createTestDescription(ChildClass.class, "testMethod", annotations);
+ Set<String> flags = CommandLineFlags.getFlags(desc);
+ Assert.assertEquals("testMethod should have 1 flag", 1, flags.size());
+ Assert.assertTrue("testMethod should have A flag", flags.contains("A"));
+ }
+
+ @Test
+ public void testGetFlagsChildRemoveA() throws Throwable {
+ Method method = ChildClassRemoveFlagA.class.getMethod("testMethod");
+ Set<String> flags = CommandLineFlags.getFlags(method);
+ Assert.assertEquals("testMethod should have no flag", 0, flags.size());
+ }
+
+ @Test
+ public void testGetFlagsChildRemoveAWithDesc() throws Throwable {
+ Annotation[] annotations =
+ ChildClassRemoveFlagA.class.getMethod("testMethod").getDeclaredAnnotations();
+ Description desc = Description.createTestDescription(
+ ChildClassRemoveFlagA.class, "testMethod", annotations);
+ Set<String> flags = CommandLineFlags.getFlags(desc);
+ Assert.assertEquals("testMethod should have no flag", 0, flags.size());
+ }
+
+ @Test
+ public void testGetFlagsChildAddA() throws Throwable {
+ Method method = ChildClassRemoveFlagA.class.getMethod("testMethodAddFlagA");
+ Set<String> flags = CommandLineFlags.getFlags(method);
+ Assert.assertEquals("testMethodAddFlagB should have 1 flag", 1, flags.size());
+ Assert.assertTrue("testMethodAddFlagB should have A flag", flags.contains("A"));
+ }
+
+ @Test
+ public void testGetFlagsChildAddAWithDesc() throws Throwable {
+ Annotation[] annotations = ChildClassRemoveFlagA.class.getMethod("testMethodAddFlagA")
+ .getDeclaredAnnotations();
+ Description desc = Description.createTestDescription(
+ ChildClassRemoveFlagA.class, "testMethodAddFlagA", annotations);
+ Set<String> flags = CommandLineFlags.getFlags(desc);
+ Assert.assertEquals("testMethodAddFlagB should have 1 flag", 1, flags.size());
+ Assert.assertTrue("testMethodAddFlagB should have A flag", flags.contains("A"));
+ }
+
+ @Test
+ public void testEmptyClass() throws Throwable {
+ Method method = EmptyClass.class.getMethod("testEmpty");
+ Set<String> flags = CommandLineFlags.getFlags(method);
+ Assert.assertEquals("EmptyClass method should have 0 flag", 0, flags.size());
+ }
+
+ @Test
+ public void testEmptyClassWithDesc() throws Throwable {
+ Annotation[] annotations = EmptyClass.class.getMethod("testEmpty").getDeclaredAnnotations();
+ Description desc =
+ Description.createTestDescription(EmptyClass.class, "testEmpty", annotations);
+ Set<String> flags = CommandLineFlags.getFlags(desc);
+ Assert.assertEquals("EmptyClass method should have 0 flag", 0, flags.size());
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698