summaryrefslogtreecommitdiffstats
path: root/robolectric_tests/src/com/android/launcher3/config/FlagOverrideRule.java
blob: 92bcc64348f87ff9dcadc937c7fe4a6b2a1bbb61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.android.launcher3.config;


import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.robolectric.RuntimeEnvironment;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Test rule that makes overriding flags in Robolectric tests easier. This rule clears all flags
 * before and after your test, avoiding one test method affecting subsequent methods.
 *
 * <p>Usage:
 * <pre>
 * {@literal @}Rule public final FlagOverrideRule flags = new FlagOverrideRule();
 *
 * {@literal @}FlagOverride(flag = "FOO", value=true)
 * {@literal @}Test public void myTest() {
 *     ...
 * }
 * </pre>
 */
public final class FlagOverrideRule implements TestRule {

    /**
     * Container annotation for handling multiple {@link FlagOverride} annotations.
     * <p>
     * <p>Don't use this directly, use repeated {@link FlagOverride} annotations instead.
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface FlagOverrides {
        FlagOverride[] value();
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    @Repeatable(FlagOverrides.class)
    public @interface FlagOverride {
        String key();

        boolean value();
    }

    private boolean ruleInProgress;

    @Override
    public Statement apply(Statement base, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                FeatureFlags.initialize(RuntimeEnvironment.application.getApplicationContext());
                ruleInProgress = true;
                try {
                    clearOverrides();
                    applyAnnotationOverrides(description);
                    base.evaluate();
                } finally {
                    ruleInProgress = false;
                    clearOverrides();
                }
            }
        };
    }

    private void override(BaseFlags.TogglableFlag flag, boolean newValue) {
        if (!ruleInProgress) {
            throw new IllegalStateException(
                    "Rule isn't in progress. Did you remember to mark it with @Rule?");
        }
        flag.setForTests(newValue);
    }

    private void applyAnnotationOverrides(Description description) {
        for (Annotation annotation : description.getAnnotations()) {
            if (annotation.annotationType() == FlagOverride.class) {
                applyAnnotation((FlagOverride) annotation);
            } else if (annotation.annotationType() == FlagOverrides.class) {
                // Note: this branch is hit if the annotation is repeated
                for (FlagOverride flagOverride : ((FlagOverrides) annotation).value()) {
                    applyAnnotation(flagOverride);
                }
            }
        }
    }

    private void applyAnnotation(FlagOverride flagOverride) {
        boolean found = false;
        for (BaseFlags.TogglableFlag flag : FeatureFlags.getTogglableFlags()) {
            if (flag.getKey().equals(flagOverride.key())) {
                override(flag, flagOverride.value());
                found = true;
                break;
            }
        }
        if (!found) {
            throw new IllegalStateException("Flag " + flagOverride.key() + " not found");
        }
    }

    /**
     * Resets all flags to their default values.
     */
    private void clearOverrides() {
        for (BaseFlags.TogglableFlag flag : FeatureFlags.getTogglableFlags()) {
            flag.setForTests(flag.getDefaultValue());
        }
    }
}