Debian Codegraph @

core/tests/featureflagtests/src/android/util/FeatureFlagUtilsTest.java

raw symbols SCIP
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
17 package android.util;
18
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertFalse;
21 import static junit.framework.Assert.assertNotNull;
22 import static junit.framework.Assert.assertTrue;
23
24 import android.content.Context;
25 import android.os.SystemProperties;
26 import android.provider.Settings;
27 import android.test.suitebuilder.annotation.SmallTest;
28
29 import androidx.test.InstrumentationRegistry;
30 import androidx.test.runner.AndroidJUnit4;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36
37 @RunWith(AndroidJUnit4.class)
38 @SmallTest
39 public class FeatureFlagUtilsTest {
40
41 private static final String TEST_FEATURE_NAME = "feature_foobar";
42
43 private Context mContext;
44
45 @Before
46 public void setUp() {
47 mContext = InstrumentationRegistry.getTargetContext();
48 cleanup();
49 }
50
51 @After
52 public void tearDown() {
53 cleanup();
54 }
55
56 private void cleanup() {
57 Settings.Global.putString(mContext.getContentResolver(), TEST_FEATURE_NAME, "");
58 SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, "");
59 SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "");
60 }
61
62 @Test
63 public void testGetFlag_enabled_shouldReturnTrue() {
64 FeatureFlagUtils.getAllFeatureFlags().put(TEST_FEATURE_NAME, "true");
65
66 assertTrue(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME));
67 }
68
69 @Test
70 public void testGetFlag_adb_override_shouldReturnTrue() {
71 SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, "false");
72 SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "true");
73
74 assertTrue(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME));
75 }
76
77 @Test
78 public void testGetFlag_settings_override_shouldReturnTrue() {
79 SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, "false");
80 SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "false");
81
82 Settings.Global.putString(mContext.getContentResolver(), TEST_FEATURE_NAME, "true");
83
84 assertTrue(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME));
85 }
86
87 @Test
88 public void testSetEnabled_shouldSetOverrideFlag() {
89 assertFalse(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME));
90
91 FeatureFlagUtils.setEnabled(null /* context */, TEST_FEATURE_NAME, true);
92
93 assertEquals(SystemProperties.get(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, null),
94 "");
95 assertTrue(Boolean.parseBoolean(SystemProperties.get(
96 FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "")));
97 }
98
99 @Test
100 public void testGetFlag_notSet_shouldReturnFalse() {
101 assertFalse(FeatureFlagUtils.isEnabled(mContext, TEST_FEATURE_NAME + "does_not_exist"));
102 }
103
104 @Test
105 public void getAllFeatureFlags_shouldNotBeNull() {
106 assertNotNull(FeatureFlagUtils.getAllFeatureFlags());
107 }
108 }