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

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

Issue 2523983002: Create CommandLineTestRule (Closed)
Patch Set: Created 4 years 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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;
6
7 import org.junit.Assert;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.Description;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.BlockJUnit4ClassRunner;
13
14 import org.chromium.base.CommandLine;
15 import org.chromium.base.test.util.CommandLineFlags;
16
17 import java.lang.annotation.Annotation;
18 import java.lang.reflect.Method;
19 import java.util.Arrays;
20 import java.util.Set;
21
22 /** Unit tests for CommandLineFlags. */
23 @RunWith(BlockJUnit4ClassRunner.class)
24 public class CommandLineFlagsTest {
25 @CommandLineFlags.Add("A")
26 class ParentClassAddFlagA {
27 @CommandLineFlags.Remove("A")
28 public void testMethodRemoveFlagA() {}
29
30 @CommandLineFlags.Add("B")
31 public void testMethodAddFlagB() {}
32 }
33
34 @CommandLineFlags.Remove("A")
35 class ChildClassRemoveFlagA extends ParentClassAddFlagA {
36 public void testMethod() {}
37
38 @CommandLineFlags.Add("A")
39 public void testMethodAddFlagA() {}
40 }
41
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
42 class EmptyClass {
43 public void testEmpty() {}
44 }
45
46 @Before
47 public void setUp() {
48 CommandLine.init(new String[0]);
49 }
50
51 @Test
52 public void testGetFlagsParentRemoveA() throws Throwable {
53 Method method = ParentClassAddFlagA.class.getDeclaredMethod("testMethodR emoveFlagA");
54 Set<String> flags = CommandLineFlags.getFlags(method);
55 Assert.assertEquals("testMethodRemoveFlagA should have no flag", 0, flag s.size());
56 }
57
58 @Test
59 public void testGetFlagsParentRemoveAWithDesc() throws Throwable {
60 Annotation[] annotations = ParentClassAddFlagA.class.getMethod("testMeth odRemoveFlagA")
61 .getDeclaredAnnotations();
62 Description desc = Description.createTestDescription(
63 ParentClassAddFlagA.class, "testMethodRemoveFlagA", annotations) ;
64 Set<String> flags = CommandLineFlags.getFlags(desc);
65 Assert.assertEquals(String.format("testMethodRemoveFlagA should have no flag %s",
66 Arrays.toString(flags.toArray())),
67 0, flags.size());
68 }
69
70 @Test
71 public void testGetFlagsParentAddB() throws Throwable {
72 Method method = ParentClassAddFlagA.class.getDeclaredMethod("testMethodA ddFlagB");
73 Set<String> flags = CommandLineFlags.getFlags(method);
74 Assert.assertEquals("testMethodAddFlagB should have 2 flags", 2, flags.s ize());
75 Assert.assertTrue("testMethodAddFlagB should have both A and B flag", fl ags.contains("A"));
76 Assert.assertTrue("testMethodAddFlagB should have both A and B flag", fl ags.contains("B"));
77 }
78
79 @Test
80 public void testGetFlagsParentAddBWithDesc() throws Throwable {
81 Annotation[] annotations =
82 ParentClassAddFlagA.class.getMethod("testMethodAddFlagB").getDec laredAnnotations();
83 Description desc = Description.createTestDescription(
84 ParentClassAddFlagA.class, "testMethodAddFlagB", annotations);
85 Set<String> flags = CommandLineFlags.getFlags(desc);
86 Assert.assertEquals("testMethodAddFlagB should have 2 flags", 2, flags.s ize());
87 Assert.assertTrue("testMethodAddFlagB should have both A and B flag", fl ags.contains("A"));
88 Assert.assertTrue("testMethodAddFlagB should have both A and B flag", fl ags.contains("B"));
89 }
90
91 @Test
92 public void testGetFlagsChild() throws Throwable {
93 Method method = ChildClassRemoveFlagA.class.getMethod("testMethod");
94 Set<String> flags = CommandLineFlags.getFlags(method);
95 Assert.assertEquals("testMethod should have no flag", 0, flags.size());
96 }
97
98 @Test
99 public void testGetFlagsChildWithDesc() throws Throwable {
100 Annotation[] annotations =
101 ChildClassRemoveFlagA.class.getMethod("testMethod").getDeclaredA nnotations();
102 Description desc = Description.createTestDescription(
103 ChildClassRemoveFlagA.class, "testMethod", annotations);
104 Set<String> flags = CommandLineFlags.getFlags(desc);
105 Assert.assertEquals("testMethodRemoveFlagA should have no flag", 0, flag s.size());
106 }
107
108 @Test
109 public void testGetFlagsChildAddA() throws Throwable {
110 Method method = ChildClassRemoveFlagA.class.getMethod("testMethodAddFlag A");
111 Set<String> flags = CommandLineFlags.getFlags(method);
112 Assert.assertEquals("testMethodAddFlagB should have 1 flag", 1, flags.si ze());
113 Assert.assertTrue("testMethodAddFlagB should have A flag", flags.contain s("A"));
114 }
115
116 @Test
117 public void testGetFlagsChildAddAWithDesc() throws Throwable {
118 Annotation[] annotations = ChildClassRemoveFlagA.class.getMethod("testMe thodAddFlagA")
119 .getDeclaredAnnotations();
120 Description desc = Description.createTestDescription(
121 ChildClassRemoveFlagA.class, "testMethodAddFlagA", annotations);
122 Set<String> flags = CommandLineFlags.getFlags(desc);
123 Assert.assertEquals("testMethodAddFlagB should have 1 flag", 1, flags.si ze());
124 Assert.assertTrue("testMethodAddFlagB should have A flag", flags.contain s("A"));
125 }
126
127 @Test
128 public void testEmptyClass() throws Throwable {
129 Method method = EmptyClass.class.getMethod("testEmpty");
130 Set<String> flags = CommandLineFlags.getFlags(method);
131 Assert.assertEquals("EmptyClass method should have 0 flag", 0, flags.siz e());
132 }
133
134 @Test
135 public void testEmptyClassWithDesc() throws Throwable {
136 Annotation[] annotations = EmptyClass.class.getMethod("testEmpty").getDe claredAnnotations();
137 Description desc =
138 Description.createTestDescription(EmptyClass.class, "testEmpty", annotations);
139 Set<String> flags = CommandLineFlags.getFlags(desc);
140 Assert.assertEquals("EmptyClass method should have 0 flag", 0, flags.siz e());
141 }
142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698