OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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.chrome.browser; | 5 package org.chromium.chrome.browser; |
6 | 6 |
7 import org.chromium.testing.local.AnnotationProcessor; | 7 import org.chromium.testing.local.AnnotationProcessor; |
8 | 8 |
9 import java.lang.annotation.ElementType; | 9 import java.lang.annotation.ElementType; |
10 import java.lang.annotation.Inherited; | 10 import java.lang.annotation.Inherited; |
11 import java.lang.annotation.Retention; | 11 import java.lang.annotation.Retention; |
12 import java.lang.annotation.RetentionPolicy; | 12 import java.lang.annotation.RetentionPolicy; |
13 import java.lang.annotation.Target; | 13 import java.lang.annotation.Target; |
14 import java.util.Arrays; | 14 import java.util.Arrays; |
15 import java.util.Collections; | |
15 import java.util.HashSet; | 16 import java.util.HashSet; |
16 import java.util.Set; | 17 import java.util.Set; |
17 | 18 |
18 /** | 19 /** |
19 * Annotation used to set Feature flags during JUnit tests. To have an effect, t he associated | 20 * Annotation used to set Feature flags during JUnit tests. To have an effect, t he associated |
20 * {@link Processor} rule needs to be registered on the test class. | 21 * {@link Processor} rule needs to be registered on the test class. |
21 * | 22 * |
22 * Sample code: | 23 * Sample code: |
23 * | 24 * |
24 * <pre> | 25 * <pre> |
25 * public class Test { | 26 * public class Test { |
26 * @Rule | 27 * @Rule |
27 * public EnableFeatures.Processor processor = new EnableFeatures.Processor() ; | 28 * public EnableFeatures.Processor processor = new EnableFeatures.Processor() ; |
28 * | 29 * |
29 * @EnableFeatures(ChromeFeatureList.NTP_SNIPPETS_OFFLINE_BADGE) | 30 * @EnableFeatures(ChromeFeatureList.NTP_SNIPPETS_OFFLINE_BADGE) |
30 * public void testFoo() { ... } | 31 * public void testFoo() { ... } |
31 * } | 32 * } |
32 * </pre> | 33 * </pre> |
33 */ | 34 */ |
34 @Inherited | 35 @Inherited |
35 @Retention(RetentionPolicy.RUNTIME) | 36 @Retention(RetentionPolicy.RUNTIME) |
36 @Target({ElementType.METHOD, ElementType.TYPE}) | 37 @Target({ElementType.METHOD, ElementType.TYPE}) |
37 public @interface EnableFeatures { | 38 public @interface EnableFeatures { |
38 | 39 |
39 String[] value(); | 40 String[] value(); |
40 | 41 |
41 /** | 42 /** |
42 * Add this rule to tests to activate the {@link EnableFeatures} annotations and choose flags | 43 * Add this rule to tests get rid of exceptions when the production code tri es to check for |
43 * to enable, or get rid of exceptions when the production code tries to che ck for enabled | 44 * enabled features. It also activates the {@link EnableFeatures} annotation and allows to |
44 * features. | 45 * choose which features to enable. All are disabled by default. |
Bernhard Bauer
2017/03/03 11:54:38
Ooh, there is one thing I had been thinking of: wo
dgn
2017/03/03 13:48:30
Yes we could do that. But I think it becomes inter
Bernhard Bauer
2017/03/03 14:01:22
Well, what I'm a bit worried about is if we make a
dgn
2017/03/03 15:53:45
Okay got it. In that case, I reverted the change t
| |
45 */ | 46 */ |
46 public static class Processor extends AnnotationProcessor<EnableFeatures> { | 47 class Processor extends AnnotationProcessor<EnableFeatures> { |
47 public Processor() { | 48 public Processor() { |
48 super(EnableFeatures.class); | 49 super(EnableFeatures.class, true); |
49 } | 50 } |
50 | 51 |
51 @Override | 52 @Override |
52 protected void before() throws Throwable { | 53 protected void before() throws Throwable { |
53 Set<String> enabledFeatures = new HashSet<>(); | 54 Set<String> enabledFeatures; |
54 String[] values = getAnnotation().value(); | 55 |
55 enabledFeatures.addAll(Arrays.asList(values)); | 56 if (getAnnotation() != null) { |
57 String[] values = getAnnotation().value(); | |
58 enabledFeatures = new HashSet<>(Arrays.asList(values)); | |
59 } else { | |
60 enabledFeatures = Collections.emptySet(); | |
61 } | |
62 | |
56 ChromeFeatureList.setTestEnabledFeatures(enabledFeatures); | 63 ChromeFeatureList.setTestEnabledFeatures(enabledFeatures); |
57 } | 64 } |
58 | 65 |
59 @Override | 66 @Override |
60 protected void after() { | 67 protected void after() { |
61 ChromeFeatureList.setTestEnabledFeatures(null); | 68 ChromeFeatureList.setTestEnabledFeatures(null); |
62 } | 69 } |
63 } | 70 } |
64 } | 71 } |
OLD | NEW |