OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser; | |
6 | |
7 import org.chromium.testing.local.AnnotationProcessor; | |
8 | |
9 import java.lang.annotation.ElementType; | |
10 import java.lang.annotation.Inherited; | |
11 import java.lang.annotation.Retention; | |
12 import java.lang.annotation.RetentionPolicy; | |
13 import java.lang.annotation.Target; | |
14 import java.util.Arrays; | |
15 import java.util.HashSet; | |
16 import java.util.Set; | |
17 | |
18 /** | |
19 * 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 * | |
22 * Sample code: | |
23 * | |
24 * <pre> | |
25 * public class Test { | |
26 * @Rule | |
27 * public EnableFeatures.Processor processor = new EnableFeatures.Processor()
; | |
28 * | |
29 * @EnableFeatures(ChromeFeatureList.NTP_SNIPPETS_OFFLINE_BADGE) | |
30 * public void testFoo() { ... } | |
31 * } | |
32 * </pre> | |
33 */ | |
34 @Inherited | |
35 @Retention(RetentionPolicy.RUNTIME) | |
36 @Target({ElementType.METHOD, ElementType.TYPE}) | |
37 public @interface EnableFeatures { | |
38 | |
39 String[] value(); | |
40 | |
41 /** | |
42 * Add this rule to tests to activate the {@link EnableFeatures} annotations
and choose flags | |
43 * to enable, or get rid of exceptions when the production code tries to che
ck for enabled | |
44 * features. | |
45 */ | |
46 public static class Processor extends AnnotationProcessor<EnableFeatures> { | |
47 public Processor() { | |
48 super(EnableFeatures.class); | |
49 } | |
50 | |
51 @Override | |
52 protected void before() throws Throwable { | |
53 Set<String> enabledFeatures = new HashSet<>(); | |
54 String[] values = getAnnotation().value(); | |
55 enabledFeatures.addAll(Arrays.asList(values)); | |
56 ChromeFeatureList.setTestEnabledFeatures(enabledFeatures); | |
57 } | |
58 | |
59 @Override | |
60 protected void after() { | |
61 ChromeFeatureList.setTestEnabledFeatures(null); | |
62 } | |
63 } | |
64 } | |
OLD | NEW |