Chromium Code Reviews| 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..c3b159bc10e344f8e08e8522ea0937a0bf4ee0aa |
| --- /dev/null |
| +++ b/base/test/android/junit/src/org/chromium/base/test/CommandLineFlagsTest.java |
| @@ -0,0 +1,142 @@ |
| +// 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() {} |
| + } |
| + |
| + @CommandLineFlags.Remove("A") |
| + class ChildClassRemoveFlagA extends ParentClassAddFlagA { |
| + public void testMethod() {} |
| + |
| + @CommandLineFlags.Add("A") |
| + public void testMethodAddFlagA() {} |
| + } |
| + |
|
jbudorick
2016/11/23 16:07:36
One more test case: a second class that extends Pa
the real yoland
2016/11/23 20:44:36
Done
|
| + 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 = ChildClassRemoveFlagA.class.getMethod("testMethod"); |
| + Set<String> flags = CommandLineFlags.getFlags(method); |
| + Assert.assertEquals("testMethod should have no flag", 0, flags.size()); |
| + } |
| + |
| + @Test |
| + public void testGetFlagsChildWithDesc() 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("testMethodRemoveFlagA 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()); |
| + } |
| +} |