Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 * Note that this can only remove flags added via {@link Add} above. | 57 * Note that this can only remove flags added via {@link Add} above. |
| 54 */ | 58 */ |
| 55 @Inherited | 59 @Inherited |
| 56 @Retention(RetentionPolicy.RUNTIME) | 60 @Retention(RetentionPolicy.RUNTIME) |
| 57 @Target({ElementType.METHOD, ElementType.TYPE}) | 61 @Target({ElementType.METHOD, ElementType.TYPE}) |
| 58 public @interface Remove { | 62 public @interface Remove { |
| 59 String[] value(); | 63 String[] value(); |
| 60 } | 64 } |
| 61 | 65 |
| 62 /** | 66 /** |
| 63 * Sets up the CommandLine with the appropriate flags. | 67 * Sets up the CommandLine flags using test method or class. |
|
jbudorick
2017/01/11 20:21:33
nit: "... using the given test method or class."
the real yoland
2017/03/16 22:52:59
No longer relevant
| |
| 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); | |
|
jbudorick
2017/01/11 20:21:33
nit: just do
setUp(targetContext, getFlags(elem
the real yoland
2017/03/16 22:52:59
No longer relevant
| |
| 76 } | |
| 77 | |
| 78 /** | |
| 79 * Sets up the CommandLine flags using a set of flag strings. | |
| 80 * | |
| 81 * This will add the difference of the sets of flags specified by {@link Com mandLineFlags.Add} | |
|
jbudorick
2017/01/11 20:21:33
Update this sentence:
"This will add {@code fla
the real yoland
2017/03/16 22:52:59
Done
| |
| 82 * and {@link CommandLineFlags.Remove} to the {@link org.chromium.base.Comma ndLine}. Note that | |
| 83 * trying to remove a flag set externally, i.e. by the command-line flags fi le, will not work. | |
| 84 | |
| 85 */ | |
| 86 public static void setUp(Context targetContext, Set<String> flags) { | |
| 70 Assert.assertNotNull("Unable to get a non-null target context.", targetC ontext); | 87 Assert.assertNotNull("Unable to get a non-null target context.", targetC ontext); |
| 71 CommandLine.reset(); | |
| 72 BaseChromiumApplication.initCommandLine(targetContext); | 88 BaseChromiumApplication.initCommandLine(targetContext); |
| 73 Set<String> flags = getFlags(element); | |
| 74 for (String flag : flags) { | 89 for (String flag : flags) { |
| 75 CommandLine.getInstance().appendSwitch(flag); | 90 CommandLine.getInstance().appendSwitch(flag); |
| 76 } | 91 } |
| 77 } | 92 } |
| 78 | 93 |
| 79 private static Set<String> getFlags(AnnotatedElement element) { | 94 /** |
| 95 * @return a set of strings to represent the commandline flags | |
|
jbudorick
2017/01/11 20:21:33
This should say something like
/** Determines t
the real yoland
2017/03/16 22:52:59
No longer relevant
| |
| 96 */ | |
| 97 @VisibleForTesting | |
|
jbudorick
2017/01/11 20:21:33
This shouldn't be necessary.
the real yoland
2017/03/16 22:52:59
No longer relevant
| |
| 98 public static Set<String> getFlags(AnnotatedElement element) { | |
| 80 AnnotatedElement parent = (element instanceof Method) | 99 AnnotatedElement parent = (element instanceof Method) |
| 81 ? ((Method) element).getDeclaringClass() | 100 ? ((Method) element).getDeclaringClass() |
| 82 : ((Class) element).getSuperclass(); | 101 : ((Class<?>) element).getSuperclass(); |
| 83 Set<String> flags = (parent == null) ? new HashSet<String>() : getFlags( parent); | 102 Set<String> flags = (parent == null) ? new HashSet<String>() : getFlags( parent); |
| 103 return updateFlags(flags, element.getAnnotation(CommandLineFlags.Add.cla ss), | |
| 104 element.getAnnotation(CommandLineFlags.Remove.class)); | |
| 105 } | |
| 84 | 106 |
| 85 if (element.isAnnotationPresent(CommandLineFlags.Add.class)) { | 107 /** |
| 86 flags.addAll( | 108 * @return a set of strings to represent the commandline flags |
|
jbudorick
2017/01/11 20:21:33
Same as above.
the real yoland
2017/03/16 22:52:59
No longer relevant
| |
| 87 Arrays.asList(element.getAnnotation(CommandLineFlags.Add.cla ss).value())); | 109 */ |
| 110 @VisibleForTesting | |
|
jbudorick
2017/01/11 20:21:33
This shouldn't be necessary.
the real yoland
2017/03/16 22:52:59
No longer relevant
| |
| 111 public static Set<String> getFlags(Description desc) { | |
|
jbudorick
2017/01/11 20:21:33
Do we need to provide both versions of getFlags?
the real yoland
2017/03/16 22:52:59
No longer relevant
| |
| 112 Set<String> flags = getFlags(desc.getTestClass()); | |
| 113 return updateFlags(flags, desc.getAnnotation(CommandLineFlags.Add.class) , | |
|
jbudorick
2017/01/11 20:21:32
Why does this call updateFlags a second time?
the real yoland
2017/03/16 22:52:59
No longer relevant
| |
| 114 desc.getAnnotation(CommandLineFlags.Remove.class)); | |
| 115 } | |
| 116 | |
| 117 private static Set<String> updateFlags(Set<String> flags, CommandLineFlags.A dd addAnnotation, | |
| 118 CommandLineFlags.Remove removeAnnotation) { | |
| 119 Assert.assertNotNull("flags set can not be null", flags); | |
| 120 if (addAnnotation != null) { | |
| 121 flags.addAll(Arrays.asList(addAnnotation.value())); | |
| 88 } | 122 } |
| 89 | 123 if (removeAnnotation != null) { |
| 90 if (element.isAnnotationPresent(CommandLineFlags.Remove.class)) { | 124 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) { | 125 for (String flagToRemove : flagsToRemove) { |
| 94 // If your test fails here, you have tried to remove a command-l ine flag via | 126 // 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 | 127 // CommandLineFlags.Remove that was loaded into CommandLine via something other |
| 96 // than CommandLineFlags.Add (probably the command-line flag fil e). | 128 // than CommandLineFlags.Add (probably the command-line flag fil e). |
| 97 Assert.assertFalse("Unable to remove command-line flag \"" + fla gToRemove + "\".", | 129 Assert.assertFalse("Unable to remove command-line flag \"" + fla gToRemove + "\".", |
| 98 CommandLine.getInstance().hasSwitch(flagToRemove)); | 130 CommandLine.getInstance().hasSwitch(flagToRemove)); |
| 99 } | 131 } |
| 100 flags.removeAll(flagsToRemove); | 132 flags.removeAll(flagsToRemove); |
| 101 } | 133 } |
| 102 | |
| 103 return flags; | 134 return flags; |
| 104 } | 135 } |
| 105 | 136 |
| 106 private CommandLineFlags() { | 137 private CommandLineFlags() { |
| 107 throw new AssertionError("CommandLineFlags is a non-instantiable class") ; | 138 throw new AssertionError("CommandLineFlags is a non-instantiable class") ; |
| 108 } | 139 } |
| 109 | 140 |
| 110 public static PreTestHook getRegistrationHook() { | 141 public static PreTestHook getRegistrationHook() { |
| 111 return new PreTestHook() { | 142 return new PreTestHook() { |
| 112 @Override | 143 @Override |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 143 */ | 174 */ |
| 144 public static class Parameter extends BaseParameter { | 175 public static class Parameter extends BaseParameter { |
| 145 public static final String PARAMETER_TAG = "cmdlinearg-parameter"; | 176 public static final String PARAMETER_TAG = "cmdlinearg-parameter"; |
| 146 public static final String ADD_ARG = "add"; | 177 public static final String ADD_ARG = "add"; |
| 147 public static final String REMOVE_ARG = "remove"; | 178 public static final String REMOVE_ARG = "remove"; |
| 148 | 179 |
| 149 public Parameter(org.chromium.base.test.util.parameter.Parameter.Reader parameterReader) { | 180 public Parameter(org.chromium.base.test.util.parameter.Parameter.Reader parameterReader) { |
| 150 super(PARAMETER_TAG, parameterReader); | 181 super(PARAMETER_TAG, parameterReader); |
| 151 } | 182 } |
| 152 } | 183 } |
| 184 | |
| 185 /** | |
| 186 * TestRule to used by JUnit4 style instrumentation test to set CommandLine flags. | |
| 187 * | |
| 188 * If you need to add CommandLine flag to your junit4 style instrumentation test, | |
| 189 * use CommandLineFlag.Add and CommandLineFlags.Remove annotation on your te st method or test | |
| 190 * class. Then add CommandLineTestRule in your test. For example | |
| 191 * | |
| 192 * <code> | |
| 193 * @CommandLineFlags.Add(...) | |
| 194 * public class ChromeActivityTest { | |
| 195 * @Rule CommandLineTestRule mCommandLineRule = new CommandLineTestRule( ); | |
| 196 * | |
| 197 * @CommandLineFlags.Remove(...) | |
| 198 * @Test | |
| 199 * public void test() {...} | |
| 200 * </code> | |
| 201 * | |
| 202 * TODO(yolandyan): Add error message in BaseJUnit4ClassRunner for tests wit h CommandLineFlag | |
| 203 * annotations but did not have a CommandLineTestRule field | |
| 204 */ | |
| 205 public static class CommandLineTestRule implements TestRule { | |
|
jbudorick
2017/01/11 20:21:32
A client would use this as
@Rule CommandLineFla
the real yoland
2017/03/16 22:52:59
Moved CommandLineTestRule to its own file
| |
| 206 private class CommandLineTestRuleStatement extends Statement { | |
| 207 private Statement mBase; | |
| 208 private Set<String> mFlags; | |
| 209 | |
| 210 public CommandLineTestRuleStatement(Statement base, Set<String> flag s) { | |
| 211 mBase = base; | |
| 212 mFlags = flags; | |
| 213 } | |
| 214 | |
| 215 @Override | |
| 216 public void evaluate() throws Throwable { | |
| 217 Context targetContext = InstrumentationRegistry.getContext(); | |
| 218 CommandLineFlags.setUp(targetContext, mFlags); | |
| 219 mBase.evaluate(); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 @Override | |
| 224 public Statement apply(final Statement base, Description desc) { | |
| 225 return new CommandLineTestRuleStatement(base, getFlags(desc)); | |
| 226 } | |
| 227 } | |
| 153 } | 228 } |
| OLD | NEW |