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; |
| 8 |
| 9 import junit.framework.Assert; |
| 10 |
| 11 import org.chromium.base.BaseChromiumApplication; |
| 12 import org.chromium.base.CommandLine; |
| 13 |
7 import java.lang.annotation.ElementType; | 14 import java.lang.annotation.ElementType; |
8 import java.lang.annotation.Inherited; | 15 import java.lang.annotation.Inherited; |
9 import java.lang.annotation.Retention; | 16 import java.lang.annotation.Retention; |
10 import java.lang.annotation.RetentionPolicy; | 17 import java.lang.annotation.RetentionPolicy; |
11 import java.lang.annotation.Target; | 18 import java.lang.annotation.Target; |
| 19 import java.lang.reflect.AnnotatedElement; |
| 20 import java.lang.reflect.Method; |
| 21 import java.util.Arrays; |
| 22 import java.util.HashSet; |
| 23 import java.util.List; |
| 24 import java.util.Set; |
12 | 25 |
13 /** | 26 /** |
14 * Provides annotations related to command-line flag handling. | 27 * Provides annotations related to command-line flag handling. |
15 * | 28 * |
16 * Uses of these annotations on a derived class will take precedence over uses o
n its base classes, | 29 * Uses of these annotations on a derived class will take precedence over uses o
n its base classes, |
17 * so a derived class can add a command-line flag that a base class has removed
(or vice versa). | 30 * so a derived class can add a command-line flag that a base class has removed
(or vice versa). |
18 * Similarly, uses of these annotations on a test method will take precedence ov
er uses on the | 31 * Similarly, uses of these annotations on a test method will take precedence ov
er uses on the |
19 * containing class. | 32 * containing class. |
20 * | 33 * |
21 * Note that this class should never be instantiated. | 34 * Note that this class should never be instantiated. |
(...skipping 15 matching lines...) Expand all Loading... |
37 * | 50 * |
38 * Note that this can only remove flags added via {@link Add} above. | 51 * Note that this can only remove flags added via {@link Add} above. |
39 */ | 52 */ |
40 @Inherited | 53 @Inherited |
41 @Retention(RetentionPolicy.RUNTIME) | 54 @Retention(RetentionPolicy.RUNTIME) |
42 @Target({ElementType.METHOD, ElementType.TYPE}) | 55 @Target({ElementType.METHOD, ElementType.TYPE}) |
43 public @interface Remove { | 56 public @interface Remove { |
44 String[] value(); | 57 String[] value(); |
45 } | 58 } |
46 | 59 |
| 60 /** |
| 61 * Sets up the CommandLine with the appropriate flags. |
| 62 * |
| 63 * This will add the difference of the sets of flags specified by {@link Com
mandLineFlags.Add} |
| 64 * and {@link CommandLineFlags.Remove} to the {@link org.chromium.base.Comma
ndLine}. Note that |
| 65 * trying to remove a flag set externally, i.e. by the command-line flags fi
le, will not work. |
| 66 */ |
| 67 public static void setUp(Context targetContext, AnnotatedElement element) { |
| 68 Assert.assertNotNull("Unable to get a non-null target context.", targetC
ontext); |
| 69 CommandLine.reset(); |
| 70 BaseChromiumApplication.initCommandLine(targetContext); |
| 71 Set<String> flags = getFlags(element); |
| 72 for (String flag : flags) { |
| 73 CommandLine.getInstance().appendSwitch(flag); |
| 74 } |
| 75 } |
| 76 |
| 77 private static Set<String> getFlags(AnnotatedElement element) { |
| 78 AnnotatedElement parent = (element instanceof Method) |
| 79 ? ((Method) element).getDeclaringClass() |
| 80 : ((Class) element).getSuperclass(); |
| 81 Set<String> flags = (parent == null) ? new HashSet<String>() : getFlags(
parent); |
| 82 |
| 83 if (element.isAnnotationPresent(CommandLineFlags.Add.class)) { |
| 84 flags.addAll(Arrays.asList(element.getAnnotation(CommandLineFlags.Ad
d.class).value())); |
| 85 } |
| 86 |
| 87 if (element.isAnnotationPresent(CommandLineFlags.Remove.class)) { |
| 88 List<String> flagsToRemove = |
| 89 Arrays.asList(element.getAnnotation(CommandLineFlags.Remove.
class).value()); |
| 90 for (String flagToRemove : flagsToRemove) { |
| 91 // If your test fails here, you have tried to remove a command-l
ine flag via |
| 92 // CommandLineFlags.Remove that was loaded into CommandLine via
something other |
| 93 // than CommandLineFlags.Add (probably the command-line flag fil
e). |
| 94 Assert.assertFalse("Unable to remove command-line flag \"" + fla
gToRemove + "\".", |
| 95 CommandLine.getInstance().hasSwitch(flagToRemove)); |
| 96 } |
| 97 flags.removeAll(flagsToRemove); |
| 98 } |
| 99 |
| 100 return flags; |
| 101 } |
| 102 |
47 private CommandLineFlags() {} | 103 private CommandLineFlags() {} |
48 } | 104 } |
OLD | NEW |