Commit d8bb00b7 authored by Sky Malice's avatar Sky Malice Committed by Commit Bot

Util to optionally check param.

The purpose being the method added in this CL is to allow experiments
to optionally specify params. If they are specified at the experiment
level, then they will always check and verify they are satisfied.
However if they are not specified at the experiment level, then the
check is a no-op.

Bug: 1130751
Change-Id: Ifa31189f9f1d92340a66311bdcacba75623b807b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2476713
Commit-Queue: Sky Malice <skym@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarWenyu Fu <wenyufu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820853}
parent 22762049
...@@ -11,6 +11,7 @@ android_library("java") { ...@@ -11,6 +11,7 @@ android_library("java") {
"android/java/src/org/chromium/chrome/browser/flags/CachedFieldTrialParameter.java", "android/java/src/org/chromium/chrome/browser/flags/CachedFieldTrialParameter.java",
"android/java/src/org/chromium/chrome/browser/flags/ChromeFeatureList.java", "android/java/src/org/chromium/chrome/browser/flags/ChromeFeatureList.java",
"android/java/src/org/chromium/chrome/browser/flags/DoubleCachedFieldTrialParameter.java", "android/java/src/org/chromium/chrome/browser/flags/DoubleCachedFieldTrialParameter.java",
"android/java/src/org/chromium/chrome/browser/flags/FeatureParamUtils.java",
"android/java/src/org/chromium/chrome/browser/flags/IntCachedFieldTrialParameter.java", "android/java/src/org/chromium/chrome/browser/flags/IntCachedFieldTrialParameter.java",
"android/java/src/org/chromium/chrome/browser/flags/StringCachedFieldTrialParameter.java", "android/java/src/org/chromium/chrome/browser/flags/StringCachedFieldTrialParameter.java",
] ]
...@@ -57,6 +58,7 @@ java_library("flags_junit_tests") { ...@@ -57,6 +58,7 @@ java_library("flags_junit_tests") {
"android/java/src/org/chromium/chrome/browser/flags/CachedFeatureFlagsUnitTest.java", "android/java/src/org/chromium/chrome/browser/flags/CachedFeatureFlagsUnitTest.java",
"android/java/src/org/chromium/chrome/browser/flags/ChromeFeatureListWithProcessorUnitTest.java", "android/java/src/org/chromium/chrome/browser/flags/ChromeFeatureListWithProcessorUnitTest.java",
"android/java/src/org/chromium/chrome/browser/flags/ChromeFeatureListWithoutProcessorUnitTest.java", "android/java/src/org/chromium/chrome/browser/flags/ChromeFeatureListWithoutProcessorUnitTest.java",
"android/java/src/org/chromium/chrome/browser/flags/FeatureParamUtilsUnitTest.java",
] ]
deps = [ deps = [
":java", ":java",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.flags;
import android.text.TextUtils;
/** Utility class for convenience functions for working feature params. */
public final class FeatureParamUtils {
/** Private constructor to avoid instantiation. */
private FeatureParamUtils() {}
/**
* Used to check a three state (true, false, not set) feature param and check if the current
* value at runtime is different from the specified param value.
* @param featureName Name the feature to fetch the param from.
* @param paramName The name of the param to fetch.
* @param currentValue The current value to match against the param value.
* @return Whether the param exists and does not matches the current value.
*/
public static boolean paramExistsAndDoesNotMatch(
String featureName, String paramName, boolean currentValue) {
Boolean paramValue = getFieldTrialParamByFeatureAsBooleanOrNull(featureName, paramName);
return paramValue != null && paramValue != currentValue;
}
/**
* Gets the given feature param as a nullable Boolean. If the feature/param is not set or does
* not exist, then null will be returned. This gives the caller 3 effect states.
* @param featureName Name the feature to fetch the param from.
* @param paramName The name of the param to fetch.
* @return Null if the param is not set, otherwise the value of the param parsed as a bool.
* Non-empty param values that do not look like a boolean will be treated as false.
*/
public static Boolean getFieldTrialParamByFeatureAsBooleanOrNull(
String featureName, String paramName) {
String paramValue = ChromeFeatureList.getFieldTrialParamByFeature(featureName, paramName);
return TextUtils.isEmpty(paramValue) ? null : Boolean.parseBoolean(paramValue);
}
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.flags;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.chromium.base.test.BaseRobolectricTestRunner;
/** Tests the behavior of {@link FeatureParamUtils}. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(shadows = {FeatureParamUtilsUnitTest.ShadowChromeFeatureList.class})
public class FeatureParamUtilsUnitTest {
private static final String FEATURE_NAME = "feature12345";
private static final String PARAM_NAME = "param12345";
@Implements(ChromeFeatureList.class)
static class ShadowChromeFeatureList {
static String sParamValue;
@Implementation
public static String getFieldTrialParamByFeature(String featureName, String paramName) {
Assert.assertEquals(FEATURE_NAME, featureName);
Assert.assertEquals(PARAM_NAME, paramName);
return sParamValue;
}
}
@Test
public void testParamExistsAndDoesNotMatch() {
ShadowChromeFeatureList.sParamValue = "";
Assert.assertFalse("Empty string param should be treated as not existing",
FeatureParamUtils.paramExistsAndDoesNotMatch(FEATURE_NAME, PARAM_NAME, false));
Assert.assertFalse("Empty string param should be treated as not existing",
FeatureParamUtils.paramExistsAndDoesNotMatch(FEATURE_NAME, PARAM_NAME, true));
ShadowChromeFeatureList.sParamValue = "true";
Assert.assertTrue("True and false do not match",
FeatureParamUtils.paramExistsAndDoesNotMatch(FEATURE_NAME, PARAM_NAME, false));
Assert.assertFalse("Both are true and should be treated as matching",
FeatureParamUtils.paramExistsAndDoesNotMatch(FEATURE_NAME, PARAM_NAME, true));
ShadowChromeFeatureList.sParamValue = "false";
Assert.assertFalse("Both are false and should be treated as matching",
FeatureParamUtils.paramExistsAndDoesNotMatch(FEATURE_NAME, PARAM_NAME, false));
Assert.assertTrue("True and false do not match",
FeatureParamUtils.paramExistsAndDoesNotMatch(FEATURE_NAME, PARAM_NAME, true));
}
@Test
public void testGetFieldTrialParamByFeatureAsBooleanOrNull() {
ShadowChromeFeatureList.sParamValue = "";
Assert.assertNull("Empty string should be treated as not existing",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
ShadowChromeFeatureList.sParamValue = null;
Assert.assertNull("Null should should be treated as not existing",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
ShadowChromeFeatureList.sParamValue = "true";
Assert.assertTrue("Should have parsed to true",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
ShadowChromeFeatureList.sParamValue = "True";
Assert.assertTrue("Should have parsed to true",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
ShadowChromeFeatureList.sParamValue = "false";
Assert.assertFalse("Should have parsed to false",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
ShadowChromeFeatureList.sParamValue = "False";
Assert.assertFalse("Should have parsed to false",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
ShadowChromeFeatureList.sParamValue = "null";
Assert.assertFalse("Should have parsed to false",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
ShadowChromeFeatureList.sParamValue = "bogus";
Assert.assertFalse("Should have parsed to false",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
ShadowChromeFeatureList.sParamValue = " ∑ \t\n";
Assert.assertFalse("Should have parsed to false",
FeatureParamUtils.getFieldTrialParamByFeatureAsBooleanOrNull(
FEATURE_NAME, PARAM_NAME));
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment