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

Side by Side Diff: base/test/android/javatests/src/org/chromium/base/test/util/CommandLineFlags.java

Issue 2523983002: Create CommandLineTestRule (Closed)
Patch Set: Remove PreTestHook in BaseJUnit4ClassRunner 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.base.test.util; 5 package org.chromium.base.test.util;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.support.test.InstrumentationRegistry;
8 9
9 import junit.framework.Assert; 10 import org.junit.Assert;
jbudorick 2016/12/01 00:30:14 Does this work with the existing JUnit3-based code
the real yoland 2017/01/11 19:57:50 yes, it does
11 import org.junit.rules.TestRule;
12 import org.junit.runner.Description;
13 import org.junit.runners.model.Statement;
10 14
11 import org.chromium.base.BaseChromiumApplication; 15 import org.chromium.base.BaseChromiumApplication;
12 import org.chromium.base.CommandLine; 16 import org.chromium.base.CommandLine;
17 import org.chromium.base.VisibleForTesting;
jbudorick 2016/12/01 00:30:14 This shouldn't be necessary...
the real yoland 2017/01/11 19:57:50 Deleted
13 import org.chromium.base.test.BaseTestResult.PreTestHook; 18 import org.chromium.base.test.BaseTestResult.PreTestHook;
14 import org.chromium.base.test.util.parameter.BaseParameter; 19 import org.chromium.base.test.util.parameter.BaseParameter;
15 20
16 import java.lang.annotation.ElementType; 21 import java.lang.annotation.ElementType;
17 import java.lang.annotation.Inherited; 22 import java.lang.annotation.Inherited;
18 import java.lang.annotation.Retention; 23 import java.lang.annotation.Retention;
19 import java.lang.annotation.RetentionPolicy; 24 import java.lang.annotation.RetentionPolicy;
20 import java.lang.annotation.Target; 25 import java.lang.annotation.Target;
21 import java.lang.reflect.AnnotatedElement; 26 import java.lang.reflect.AnnotatedElement;
22 import java.lang.reflect.Method; 27 import java.lang.reflect.Method;
(...skipping 30 matching lines...) Expand all
53 * Note that this can only remove flags added via {@link Add} above. 58 * Note that this can only remove flags added via {@link Add} above.
54 */ 59 */
55 @Inherited 60 @Inherited
56 @Retention(RetentionPolicy.RUNTIME) 61 @Retention(RetentionPolicy.RUNTIME)
57 @Target({ElementType.METHOD, ElementType.TYPE}) 62 @Target({ElementType.METHOD, ElementType.TYPE})
58 public @interface Remove { 63 public @interface Remove {
59 String[] value(); 64 String[] value();
60 } 65 }
61 66
62 /** 67 /**
63 * Sets up the CommandLine with the appropriate flags. 68 * Sets up the CommandLine flags using test method or class.
64 * 69 *
65 * This will add the difference of the sets of flags specified by {@link Com mandLineFlags.Add} 70 * This will add the difference of the sets of flags specified by {@link Com mandLineFlags.Add}
66 * and {@link CommandLineFlags.Remove} to the {@link org.chromium.base.Comma ndLine}. Note that 71 * and {@link CommandLineFlags.Remove} to the {@link org.chromium.base.Comma ndLine}. Note that
67 * trying to remove a flag set externally, i.e. by the command-line flags fi le, will not work. 72 * trying to remove a flag set externally, i.e. by the command-line flags fi le, will not work.
68 */ 73 */
69 public static void setUp(Context targetContext, AnnotatedElement element) { 74 public static void setUp(Context targetContext, AnnotatedElement element) {
75 Set<String> flags = getFlags(element);
76 setUp(targetContext, flags);
77 }
78
79 /**
80 * Sets up the CommandLine flags using a set of flag strings.
81 *
82 * This will add the difference of the sets of flags specified by {@link Com mandLineFlags.Add}
83 * and {@link CommandLineFlags.Remove} to the {@link org.chromium.base.Comma ndLine}. Note that
84 * trying to remove a flag set externally, i.e. by the command-line flags fi le, will not work.
85
86 */
87 public static void setUp(Context targetContext, Set<String> flags) {
70 Assert.assertNotNull("Unable to get a non-null target context.", targetC ontext); 88 Assert.assertNotNull("Unable to get a non-null target context.", targetC ontext);
71 CommandLine.reset();
72 BaseChromiumApplication.initCommandLine(targetContext); 89 BaseChromiumApplication.initCommandLine(targetContext);
73 Set<String> flags = getFlags(element);
74 for (String flag : flags) { 90 for (String flag : flags) {
75 CommandLine.getInstance().appendSwitch(flag); 91 CommandLine.getInstance().appendSwitch(flag);
76 } 92 }
77 } 93 }
78 94
79 private static Set<String> getFlags(AnnotatedElement element) { 95 /**
96 * @return a set of strings to represent the commandline flags
97 */
98 @VisibleForTesting
99 public static Set<String> getFlags(AnnotatedElement element) {
80 AnnotatedElement parent = (element instanceof Method) 100 AnnotatedElement parent = (element instanceof Method)
81 ? ((Method) element).getDeclaringClass() 101 ? ((Method) element).getDeclaringClass()
82 : ((Class) element).getSuperclass(); 102 : ((Class<?>) element).getSuperclass();
83 Set<String> flags = (parent == null) ? new HashSet<String>() : getFlags( parent); 103 Set<String> flags = (parent == null) ? new HashSet<String>() : getFlags( parent);
104 flags = updateFlags(flags, element.getAnnotation(CommandLineFlags.Add.cl ass),
jbudorick 2016/12/01 00:30:14 nit: return updateFlags(...)
the real yoland 2017/01/11 19:57:50 Done
105 element.getAnnotation(CommandLineFlags.Remove.class));
106 return flags;
107 }
84 108
85 if (element.isAnnotationPresent(CommandLineFlags.Add.class)) { 109 /**
86 flags.addAll( 110 * @return a set of strings to represent the commandline flags
87 Arrays.asList(element.getAnnotation(CommandLineFlags.Add.cla ss).value())); 111 */
112 @VisibleForTesting
113 public static Set<String> getFlags(Description desc) {
114 Set<String> flags = getFlags(desc.getTestClass());
115 flags = updateFlags(flags, desc.getAnnotation(CommandLineFlags.Add.class ),
jbudorick 2016/12/01 00:30:14 nit: return updateFlags(...)
the real yoland 2017/01/11 19:57:50 Done
116 desc.getAnnotation(CommandLineFlags.Remove.class));
117 return flags;
118 }
119
120 private static Set<String> updateFlags(Set<String> flags, CommandLineFlags.A dd addAnnotation,
121 CommandLineFlags.Remove removeAnnotation) {
122 Assert.assertNotNull("flags set can not be null", flags);
123 if (addAnnotation != null) {
124 flags.addAll(Arrays.asList(addAnnotation.value()));
88 } 125 }
89 126 if (removeAnnotation != null) {
90 if (element.isAnnotationPresent(CommandLineFlags.Remove.class)) { 127 List<String> flagsToRemove = Arrays.asList(removeAnnotation.value()) ;
91 List<String> flagsToRemove =
92 Arrays.asList(element.getAnnotation(CommandLineFlags.Remove. class).value());
93 for (String flagToRemove : flagsToRemove) { 128 for (String flagToRemove : flagsToRemove) {
94 // If your test fails here, you have tried to remove a command-l ine flag via 129 // If your test fails here, you have tried to remove a command-l ine flag via
95 // CommandLineFlags.Remove that was loaded into CommandLine via something other 130 // CommandLineFlags.Remove that was loaded into CommandLine via something other
96 // than CommandLineFlags.Add (probably the command-line flag fil e). 131 // than CommandLineFlags.Add (probably the command-line flag fil e).
97 Assert.assertFalse("Unable to remove command-line flag \"" + fla gToRemove + "\".", 132 Assert.assertFalse("Unable to remove command-line flag \"" + fla gToRemove + "\".",
98 CommandLine.getInstance().hasSwitch(flagToRemove)); 133 CommandLine.getInstance().hasSwitch(flagToRemove));
99 } 134 }
100 flags.removeAll(flagsToRemove); 135 flags.removeAll(flagsToRemove);
101 } 136 }
102
103 return flags; 137 return flags;
104 } 138 }
105 139
106 private CommandLineFlags() { 140 private CommandLineFlags() {
107 throw new AssertionError("CommandLineFlags is a non-instantiable class") ; 141 throw new AssertionError("CommandLineFlags is a non-instantiable class") ;
108 } 142 }
109 143
110 public static PreTestHook getRegistrationHook() { 144 public static PreTestHook getRegistrationHook() {
111 return new PreTestHook() { 145 return new PreTestHook() {
112 @Override 146 @Override
(...skipping 30 matching lines...) Expand all
143 */ 177 */
144 public static class Parameter extends BaseParameter { 178 public static class Parameter extends BaseParameter {
145 public static final String PARAMETER_TAG = "cmdlinearg-parameter"; 179 public static final String PARAMETER_TAG = "cmdlinearg-parameter";
146 public static final String ADD_ARG = "add"; 180 public static final String ADD_ARG = "add";
147 public static final String REMOVE_ARG = "remove"; 181 public static final String REMOVE_ARG = "remove";
148 182
149 public Parameter(org.chromium.base.test.util.parameter.Parameter.Reader parameterReader) { 183 public Parameter(org.chromium.base.test.util.parameter.Parameter.Reader parameterReader) {
150 super(PARAMETER_TAG, parameterReader); 184 super(PARAMETER_TAG, parameterReader);
151 } 185 }
152 } 186 }
187
188 /**
189 * TestRule to used by JUnit4 style instrumentation test to set CommandLine flags.
190 *
191 * If you need to add CommandLine flag to your junit4 style instrumentation test,
192 * use CommandLineFlag.Add and CommandLineFlags.Remove annotation on your te st method or test
193 * class. Then add CommandLineTestRule in your test. For example
194 *
195 * <code>
196 * @CommandLineFlags.Add(...)
197 * public class ChromeActivityTest {
198 * @Rule CommandLineTestRule mCommandLineRule = new CommandLineTestRule( );
199 *
200 * @CommandLineFlags.Remove(...)
201 * @Test
202 * public void test() {...}
203 * </code>
204 *
205 * TODO(yolandyan): Add error message in BaseJUnit4ClassRunner for tests wit h CommandLineFlag
206 * annotations but did not have a CommandLineTestRule field
207 */
208 public static class CommandLineTestRule implements TestRule {
209 private class CommandLineTestRuleStatement extends Statement {
210 private Statement mBase;
211 private Set<String> mFlags;
212
213 public CommandLineTestRuleStatement(Statement base, Set<String> flag s) {
214 mBase = base;
215 mFlags = flags;
216 }
217
218 @Override
219 public void evaluate() throws Throwable {
220 Context targetContext = InstrumentationRegistry.getContext();
221 CommandLineFlags.setUp(targetContext, mFlags);
222 mBase.evaluate();
mikecase (-- gone --) 2016/12/01 00:27:54 what does mBase.evaluate() do?
the real yoland 2017/01/11 19:57:50 This is the the onion structure Rule have to wrap
223 }
224 }
225
226 @Override
227 public Statement apply(final Statement base, Description desc) {
228 return new CommandLineTestRuleStatement(base, getFlags(desc));
229 }
230 }
153 } 231 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698