diff options
| -rw-r--r-- | core/java/android/util/FeatureFlagUtils.java | 58 | ||||
| -rw-r--r-- | core/tests/featureflagtests/Android.mk | 19 | ||||
| -rw-r--r-- | core/tests/featureflagtests/AndroidManifest.xml | 33 | ||||
| -rw-r--r-- | core/tests/featureflagtests/AndroidTest.xml | 28 | ||||
| -rw-r--r-- | core/tests/featureflagtests/src/android/util/FeatureFlagUtilsTest.java | 72 |
5 files changed, 210 insertions, 0 deletions
diff --git a/core/java/android/util/FeatureFlagUtils.java b/core/java/android/util/FeatureFlagUtils.java new file mode 100644 index 000000000000..5838f9590c9d --- /dev/null +++ b/core/java/android/util/FeatureFlagUtils.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.util; + +import android.os.SystemProperties; +import android.text.TextUtils; + +import java.util.Map; + +/** + * Util class to get feature flag information. + * + * @hide + */ +public class FeatureFlagUtils { + + public static final String FFLAG_PREFIX = "sys.fflag."; + public static final String FFLAG_OVERRIDE_PREFIX = FFLAG_PREFIX + "override."; + + /** + * Whether or not a flag is enabled. + * + * @param feature the flag name + * @return true if the flag is enabled (either by default in system, or override by user) + */ + public static boolean isEnabled(String feature) { + // Tries to get feature flag from system property. + // Step 1: check if feature flag has any override. Flag name: sys.fflag.override.<feature> + String value = SystemProperties.get(FFLAG_OVERRIDE_PREFIX + feature); + if (!TextUtils.isEmpty(value)) { + return Boolean.parseBoolean(value); + } + // Step 2: check if feature flag has any default value. Flag name: sys.fflag.<feature> + value = SystemProperties.get(FFLAG_PREFIX + feature); + return Boolean.parseBoolean(value); + } + + /** + * Returns all feature flags in their raw form. + */ + public static Map<String, String> getAllFeatureFlags() { + return null; + } +} diff --git a/core/tests/featureflagtests/Android.mk b/core/tests/featureflagtests/Android.mk new file mode 100644 index 000000000000..f2d205885c20 --- /dev/null +++ b/core/tests/featureflagtests/Android.mk @@ -0,0 +1,19 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +# We only want this apk build for tests. +LOCAL_MODULE_TAGS := tests + +# Include all test java files. +LOCAL_SRC_FILES := \ + $(call all-java-files-under, src) + +LOCAL_DX_FLAGS := --core-library +LOCAL_STATIC_JAVA_LIBRARIES := android-common frameworks-core-util-lib android-support-test +LOCAL_JAVA_LIBRARIES := android.test.runner +LOCAL_PACKAGE_NAME := FrameworksCoreFeatureFlagTests + +LOCAL_CERTIFICATE := platform +LOCAL_COMPATIBILITY_SUITE := device-tests + +include $(BUILD_PACKAGE) diff --git a/core/tests/featureflagtests/AndroidManifest.xml b/core/tests/featureflagtests/AndroidManifest.xml new file mode 100644 index 000000000000..b8ffacbe5aa8 --- /dev/null +++ b/core/tests/featureflagtests/AndroidManifest.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2017 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> + +<manifest + xmlns:android="http://schemas.android.com/apk/res/android" + android:installLocation="internalOnly" + package="com.android.frameworks.coretests.featureflagtests" + android:sharedUserId="android.uid.system"> + + <application> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation + android:name="android.support.test.runner.AndroidJUnitRunner" + android:targetPackage="com.android.frameworks.coretests.featureflagtests" + android:label="Frameworks FeatureFlagUtils Tests" /> + +</manifest> diff --git a/core/tests/featureflagtests/AndroidTest.xml b/core/tests/featureflagtests/AndroidTest.xml new file mode 100644 index 000000000000..44f9c3e37853 --- /dev/null +++ b/core/tests/featureflagtests/AndroidTest.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2017 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> +<configuration description="Runs Frameworks Utility Tests."> + <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup"> + <option name="test-file-name" value="FrameworksCoreFeatureFlagTests.apk" /> + </target_preparer> + + <option name="test-suite-tag" value="apct" /> + <option name="test-tag" value="FrameworksCoreFeatureFlagTests" /> + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="com.android.frameworks.coretests.featureflagtests" /> + <option name="runner" value="android.support.test.runner.AndroidJUnitRunner" /> + </test> +</configuration> diff --git a/core/tests/featureflagtests/src/android/util/FeatureFlagUtilsTest.java b/core/tests/featureflagtests/src/android/util/FeatureFlagUtilsTest.java new file mode 100644 index 000000000000..8fee1d11a954 --- /dev/null +++ b/core/tests/featureflagtests/src/android/util/FeatureFlagUtilsTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.util; + +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + +import android.os.SystemProperties; +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class FeatureFlagUtilsTest { + + private static final String TEST_FEATURE_NAME = "feature_foobar"; + + @Before + public void setUp() { + cleanup(); + } + + @After + public void tearDown() { + cleanup(); + } + + private void cleanup() { + SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, ""); + SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, ""); + } + + @Test + public void testGetFlag_enabled_shouldReturnTrue() { + SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, "true"); + + assertTrue(FeatureFlagUtils.isEnabled(TEST_FEATURE_NAME)); + } + + @Test + public void testGetFlag_override_shouldReturnTrue() { + SystemProperties.set(FeatureFlagUtils.FFLAG_PREFIX + TEST_FEATURE_NAME, "false"); + SystemProperties.set(FeatureFlagUtils.FFLAG_OVERRIDE_PREFIX + TEST_FEATURE_NAME, "true"); + + assertTrue(FeatureFlagUtils.isEnabled(TEST_FEATURE_NAME)); + } + + @Test + public void testGetFlag_notSet_shouldReturnFalse() { + assertFalse(FeatureFlagUtils.isEnabled(TEST_FEATURE_NAME)); + } + +} |