1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.util;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertFalse;
21import static junit.framework.Assert.assertNotNull;
22import static junit.framework.Assert.assertTrue;
23
24import android.content.Context;
25import android.os.SystemProperties;
26import android.provider.Settings;
27import android.support.test.InstrumentationRegistry;
28import android.support.test.runner.AndroidJUnit4;
29import android.test.suitebuilder.annotation.SmallTest;
30
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36@RunWith(AndroidJUnit4.class)
37@SmallTest
38public class FeatureFlagUtilsTest {
39
40    private static final String TEST_FEATURE_NAME = "feature_foobar";
41
42    private Context mContext;
43
44    @Before
45    public void setUp() {
46        mContext = InstrumentationRegistry.getTargetContext();
47        cleanup();
48    }
49
50    @After
51    public void tearDown() {
52        cleanup();
53    }
54
55    private void cleanup() {
56        Settings.Global.putString(mContext.getContentResolver(), TEST_FEATURE_NAME, "");
57        SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, "");
58        SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "");
59    }
60
61    @Test
62    public void testGetFlag_enabled_shouldReturnTrue() {
63        FeatureFlagUtils.getAllFeatureFlags().put(TEST_FEATURE_NAME, "true");
64
65        assertTrue(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME));
66    }
67
68    @Test
69    public void testGetFlag_adb_override_shouldReturnTrue() {
70        SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, "false");
71        SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "true");
72
73        assertTrue(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME));
74    }
75
76    @Test
77    public void testGetFlag_settings_override_shouldReturnTrue() {
78        SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, "false");
79        SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "false");
80
81        Settings.Global.putString(mContext.getContentResolver(), TEST_FEATURE_NAME, "true");
82
83        assertTrue(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME));
84    }
85
86    @Test
87    public void testSetEnabled_shouldSetOverrideFlag() {
88        assertFalse(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME));
89
90        FeatureFlagUtils.setEnabled(null /* context */, TEST_FEATURE_NAME, true);
91
92        assertEquals(SystemProperties.get(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, null),
93                "");
94        assertTrue(Boolean.parseBoolean(SystemProperties.get(
95                FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "")));
96    }
97
98    @Test
99    public void testGetFlag_notSet_shouldReturnFalse() {
100        assertFalse(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME + "does_not_exist"));
101    }
102
103    @Test
104    public void getAllFeatureFlags_shouldNotBeNull() {
105        assertNotNull(FeatureFlagUtils.getAllFeatureFlags());
106    }
107}
108