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

Side by Side Diff: base/test/android/javatests/src/org/chromium/base/test/util/CommandLineFlags.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
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;
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;
13 import org.chromium.base.test.BaseTestResult.PreTestHook; 17 import org.chromium.base.test.BaseTestResult.PreTestHook;
14 import org.chromium.base.test.util.parameter.BaseParameter; 18 import org.chromium.base.test.util.parameter.BaseParameter;
15 19
16 import java.lang.annotation.ElementType; 20 import java.lang.annotation.ElementType;
17 import java.lang.annotation.Inherited; 21 import java.lang.annotation.Inherited;
18 import java.lang.annotation.Retention; 22 import java.lang.annotation.Retention;
19 import java.lang.annotation.RetentionPolicy; 23 import java.lang.annotation.RetentionPolicy;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 } 64 }
61 65
62 /** 66 /**
63 * Sets up the CommandLine with the appropriate flags. 67 * Sets up the CommandLine with the appropriate flags.
64 * 68 *
65 * This will add the difference of the sets of flags specified by {@link Com mandLineFlags.Add} 69 * 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 70 * 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. 71 * trying to remove a flag set externally, i.e. by the command-line flags fi le, will not work.
68 */ 72 */
69 public static void setUp(Context targetContext, AnnotatedElement element) { 73 public static void setUp(Context targetContext, AnnotatedElement element) {
74 Set<String> flags = getFlags(element);
75 setUp(targetContext, flags);
76 }
77
78 public static void setUp(Context targetContext, Set<String> flags) {
jbudorick 2016/11/23 16:07:36 Public methods ought to have javadoc comments.
the real yoland 2016/11/23 20:44:36 Done
70 Assert.assertNotNull("Unable to get a non-null target context.", targetC ontext); 79 Assert.assertNotNull("Unable to get a non-null target context.", targetC ontext);
71 CommandLine.reset();
72 BaseChromiumApplication.initCommandLine(targetContext); 80 BaseChromiumApplication.initCommandLine(targetContext);
73 Set<String> flags = getFlags(element);
74 for (String flag : flags) { 81 for (String flag : flags) {
75 CommandLine.getInstance().appendSwitch(flag); 82 CommandLine.getInstance().appendSwitch(flag);
76 } 83 }
77 } 84 }
78 85
79 private static Set<String> getFlags(AnnotatedElement element) { 86 public static Set<String> getFlags(AnnotatedElement element) {
80 AnnotatedElement parent = (element instanceof Method) 87 AnnotatedElement parent = (element instanceof Method)
81 ? ((Method) element).getDeclaringClass() 88 ? ((Method) element).getDeclaringClass()
82 : ((Class) element).getSuperclass(); 89 : ((Class<?>) element).getSuperclass();
83 Set<String> flags = (parent == null) ? new HashSet<String>() : getFlags( parent); 90 Set<String> flags = (parent == null) ? new HashSet<String>() : getFlags( parent);
91 flags = addAndRemoveFlags(flags, element.getAnnotation(CommandLineFlags. Add.class),
jbudorick 2016/11/23 16:07:36 nit: The indentation here, while maybe right, look
the real yoland 2016/11/23 20:44:36 Hmm, it does looks weird, but it was formatted by
92 element.getAnnotation(CommandLineFlags.Remove.class));
93 return flags;
94 }
84 95
85 if (element.isAnnotationPresent(CommandLineFlags.Add.class)) { 96 public static Set<String> getFlags(Description desc) {
86 flags.addAll( 97 Set<String> flags = getFlags(desc.getTestClass());
87 Arrays.asList(element.getAnnotation(CommandLineFlags.Add.cla ss).value())); 98 flags = addAndRemoveFlags(flags, desc.getAnnotation(CommandLineFlags.Add .class),
99 desc.getAnnotation(CommandLineFlags.Remove.class));
100 return flags;
101 }
102
103 public static Set<String> addAndRemoveFlags(Set<String> flags,
mikecase (-- gone --) 2016/11/23 16:14:34 nit: updateFlags might be a better name
the real yoland 2016/11/23 20:44:36 Done
104 CommandLineFlags.Add addAnnotation, CommandLineFlags.Remove removeAn notation) {
105 Assert.assertNotNull("flags set can not be null", flags);
106 if (addAnnotation != null) {
107 flags.addAll(Arrays.asList(addAnnotation.value()));
88 } 108 }
89 109 if (removeAnnotation != null) {
90 if (element.isAnnotationPresent(CommandLineFlags.Remove.class)) { 110 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) { 111 for (String flagToRemove : flagsToRemove) {
94 // If your test fails here, you have tried to remove a command-l ine flag via 112 // 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 113 // CommandLineFlags.Remove that was loaded into CommandLine via something other
96 // than CommandLineFlags.Add (probably the command-line flag fil e). 114 // than CommandLineFlags.Add (probably the command-line flag fil e).
97 Assert.assertFalse("Unable to remove command-line flag \"" + fla gToRemove + "\".", 115 Assert.assertFalse("Unable to remove command-line flag \"" + fla gToRemove + "\".",
98 CommandLine.getInstance().hasSwitch(flagToRemove)); 116 CommandLine.getInstance().hasSwitch(flagToRemove));
99 } 117 }
100 flags.removeAll(flagsToRemove); 118 flags.removeAll(flagsToRemove);
101 } 119 }
102
103 return flags; 120 return flags;
104 } 121 }
105 122
106 private CommandLineFlags() { 123 private CommandLineFlags() {
107 throw new AssertionError("CommandLineFlags is a non-instantiable class") ; 124 throw new AssertionError("CommandLineFlags is a non-instantiable class") ;
108 } 125 }
109 126
110 public static PreTestHook getRegistrationHook() { 127 public static PreTestHook getRegistrationHook() {
111 return new PreTestHook() { 128 return new PreTestHook() {
112 @Override 129 @Override
(...skipping 30 matching lines...) Expand all
143 */ 160 */
144 public static class Parameter extends BaseParameter { 161 public static class Parameter extends BaseParameter {
145 public static final String PARAMETER_TAG = "cmdlinearg-parameter"; 162 public static final String PARAMETER_TAG = "cmdlinearg-parameter";
146 public static final String ADD_ARG = "add"; 163 public static final String ADD_ARG = "add";
147 public static final String REMOVE_ARG = "remove"; 164 public static final String REMOVE_ARG = "remove";
148 165
149 public Parameter(org.chromium.base.test.util.parameter.Parameter.Reader parameterReader) { 166 public Parameter(org.chromium.base.test.util.parameter.Parameter.Reader parameterReader) {
150 super(PARAMETER_TAG, parameterReader); 167 super(PARAMETER_TAG, parameterReader);
151 } 168 }
152 } 169 }
170
171 /**
172 * TestRule to used by JUnit4 style instrumentation test to set CommandLine flags.
173 *
174 * If you need to add CommandLine flag to your junit4 style instrumentation test,
175 * use CommandLineFlag.Add and CommandLineFlags.Remove annotation on your te st method or test
176 * class. Then add CommandLineTestRule in your test. For example
177 *
178 * <code>
179 * @CommandLineFlags.Add(...)
180 * public class ChromeActivityTest {
181 * @Rule CommandLineTestRule mCommandLineRule = new CommandLineTestRule( );
jbudorick 2016/11/23 16:07:36 So what happens if someone uses @CommandLineFlags.
mikecase (-- gone --) 2016/11/23 16:14:34 Is there any way to warn people if they added the
the real yoland 2016/11/23 20:44:36 I can throw error in BaseJUnit4ClassRunner when a
182 *
183 * @CommandLineFlags.Remove(...)
184 * @Test
185 * public void test() {...}
186 * </code>
187 */
188 public static class CommandLineTestRule implements TestRule {
189 private class CommandLineTestRuleStatement extends Statement {
190 private Statement mBase;
191 private Set<String> mFlags;
192
193 public CommandLineTestRuleStatement(Statement base, Set<String> flag s) {
194 mBase = base;
195 mFlags = flags;
196 }
197
198 @Override
199 public void evaluate() throws Throwable {
200 Context targetContext = InstrumentationRegistry.getContext();
201 CommandLineFlags.setUp(targetContext, mFlags);
202 mBase.evaluate();
203 }
204 }
205
206 @Override
207 public Statement apply(final Statement base, Description desc) {
208 return new CommandLineTestRuleStatement(base, getFlags(desc));
209 }
210 }
153 } 211 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698