Commit d5dd68d3 authored by jkrcal's avatar jkrcal Committed by Commit bot

Add generic functions for getting typed variation parameter values

This CL introduces generic functions for parsing variation parameters
into int, double, and bool (internally, variation parameters are
represented by a string).

The CL replaces some functions emulating this functionality in
ntp_snippets by the new ones in variations. More clean-up in
ntp_snippets is needed, left for further CLs.

BUG=671979

Review-Url: https://codereview.chromium.org/2558743003
Cr-Commit-Position: refs/heads/master@{#437491}
parent bd44869f
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "components/offline_pages/core/offline_page_model_query.h" #include "components/offline_pages/core/offline_page_model_query.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/variations/variations_associated_data.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
...@@ -51,7 +52,7 @@ const char* kMaxSuggestionsCountParamName = "downloads_max_count"; ...@@ -51,7 +52,7 @@ const char* kMaxSuggestionsCountParamName = "downloads_max_count";
int GetMaxSuggestionsCount() { int GetMaxSuggestionsCount() {
bool assets_enabled = bool assets_enabled =
base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature); base::FeatureList::IsEnabled(features::kAssetDownloadSuggestionsFeature);
return ntp_snippets::GetParamAsInt( return variations::GetVariationParamByFeatureAsInt(
assets_enabled ? features::kAssetDownloadSuggestionsFeature assets_enabled ? features::kAssetDownloadSuggestionsFeature
: features::kOfflinePageDownloadSuggestionsFeature, : features::kOfflinePageDownloadSuggestionsFeature,
kMaxSuggestionsCountParamName, kDefaultMaxSuggestionsCount); kMaxSuggestionsCountParamName, kDefaultMaxSuggestionsCount);
......
...@@ -49,20 +49,22 @@ const char kDeprecatedBookmarksFirstM54StartPref[] = ...@@ -49,20 +49,22 @@ const char kDeprecatedBookmarksFirstM54StartPref[] =
// Note that bookmarks can be shown that do not meet this threshold. // Note that bookmarks can be shown that do not meet this threshold.
base::Time GetThresholdTime() { base::Time GetThresholdTime() {
return base::Time::Now() - return base::Time::Now() -
base::TimeDelta::FromDays(GetParamAsInt( base::TimeDelta::FromDays(variations::GetVariationParamByFeatureAsInt(
ntp_snippets::kBookmarkSuggestionsFeature, ntp_snippets::kBookmarkSuggestionsFeature,
kMaxBookmarkAgeInDaysParamName, kMaxBookmarkAgeInDays)); kMaxBookmarkAgeInDaysParamName, kMaxBookmarkAgeInDays));
} }
// The maximum number of suggestions ever provided. // The maximum number of suggestions ever provided.
int GetMaxCount() { int GetMaxCount() {
return GetParamAsInt(ntp_snippets::kBookmarkSuggestionsFeature, return variations::GetVariationParamByFeatureAsInt(
kMaxBookmarksParamName, kMaxBookmarks); ntp_snippets::kBookmarkSuggestionsFeature, kMaxBookmarksParamName,
kMaxBookmarks);
} }
bool AreDesktopVisitsConsidered() { bool AreDesktopVisitsConsidered() {
return GetParamAsBool(ntp_snippets::kBookmarkSuggestionsFeature, return variations::GetVariationParamByFeatureAsBool(
kConsiderDesktopVisitsParamName, false); ntp_snippets::kBookmarkSuggestionsFeature,
kConsiderDesktopVisitsParamName, false);
} }
} // namespace } // namespace
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include "components/ntp_snippets/features.h" #include "components/ntp_snippets/features.h"
#include "base/strings/string_number_conversions.h"
#include "components/variations/variations_associated_data.h" #include "components/variations/variations_associated_data.h"
namespace ntp_snippets { namespace ntp_snippets {
...@@ -42,46 +41,4 @@ const base::Feature kForeignSessionsSuggestionsFeature{ ...@@ -42,46 +41,4 @@ const base::Feature kForeignSessionsSuggestionsFeature{
const base::Feature kFetchMoreFeature{"NTPSuggestionsFetchMore", const base::Feature kFetchMoreFeature{"NTPSuggestionsFetchMore",
base::FEATURE_ENABLED_BY_DEFAULT}; base::FEATURE_ENABLED_BY_DEFAULT};
int GetParamAsInt(const base::Feature& feature,
const std::string& param_name,
const int default_value) {
std::string value_as_string =
variations::GetVariationParamValueByFeature(feature, param_name);
int value_as_int = 0;
if (!base::StringToInt(value_as_string, &value_as_int)) {
if (!value_as_string.empty()) {
LOG(WARNING) << "Failed to parse variation param " << param_name
<< " with string value " << value_as_string
<< " under feature " << feature.name
<< " into an int. Falling back to default value of "
<< default_value;
}
value_as_int = default_value;
}
return value_as_int;
}
bool GetParamAsBool(const base::Feature& feature,
const std::string& param_name,
bool default_value) {
std::string value_as_string =
variations::GetVariationParamValueByFeature(feature, param_name);
if (value_as_string == "true") {
return true;
}
if (value_as_string == "false") {
return false;
}
if (!value_as_string.empty()) {
LOG(WARNING) << "Failed to parse variation param " << param_name
<< " with string value " << value_as_string
<< " under feature " << feature.name
<< " into a bool. Falling back to default value of "
<< default_value;
}
return default_value;
}
} // namespace ntp_snippets } // namespace ntp_snippets
...@@ -38,17 +38,6 @@ extern const base::Feature kIncreasedVisibility; ...@@ -38,17 +38,6 @@ extern const base::Feature kIncreasedVisibility;
// Feature to enable the Fetch More action // Feature to enable the Fetch More action
extern const base::Feature kFetchMoreFeature; extern const base::Feature kFetchMoreFeature;
// Returns a feature param as an int instead of a string.
int GetParamAsInt(const base::Feature& feature,
const std::string& param_name,
int default_value);
// Returns a feature param as a bool instead of a string.
// TODO(jkrcal): Use this function in other code in the ntp_snippets component.
bool GetParamAsBool(const base::Feature& feature,
const std::string& param_name,
bool default_value);
} // namespace ntp_snippets } // namespace ntp_snippets
#endif // COMPONENTS_NTP_SNIPPETS_FEATURES_H_ #endif // COMPONENTS_NTP_SNIPPETS_FEATURES_H_
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "components/offline_pages/core/offline_page_model_query.h" #include "components/offline_pages/core/offline_page_model_query.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/variations/variations_associated_data.h"
#include "grit/components_strings.h" #include "grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
...@@ -38,9 +39,9 @@ const int kDefaultMaxSuggestionsCount = 5; ...@@ -38,9 +39,9 @@ const int kDefaultMaxSuggestionsCount = 5;
const char* kMaxSuggestionsCountParamName = "recent_tabs_max_count"; const char* kMaxSuggestionsCountParamName = "recent_tabs_max_count";
int GetMaxSuggestionsCount() { int GetMaxSuggestionsCount() {
return GetParamAsInt(kRecentOfflineTabSuggestionsFeature, return variations::GetVariationParamByFeatureAsInt(
kMaxSuggestionsCountParamName, kRecentOfflineTabSuggestionsFeature, kMaxSuggestionsCountParamName,
kDefaultMaxSuggestionsCount); kDefaultMaxSuggestionsCount);
} }
struct OrderOfflinePagesByMostRecentlyCreatedFirst { struct OrderOfflinePagesByMostRecentlyCreatedFirst {
......
// Copyright 2016 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.
#ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_HARD_SCHEDULER_H_
#define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_HARD_SCHEDULER_H_
#include "base/macros.h"
#include "base/time/time.h"
namespace ntp_snippets {
// Interface to schedule "hard" periodic fetches of snippets. These "hard"
// fetches must get triggered according to their schedule independent of whether
// Chrome is running at that moment.
class RemoteSuggestionsHardScheduler {
public:
// Schedule periodic fetching of snippets, with different periods depending on
// network state. Once per period, the concrete implementation should call
// RemoteSuggestionsScheduler::Updater::HardUpdate() where the updater is
// obtained from ContentSuggestionsService. Any of the periods can be zero to
// indicate that the corresponding task should not be scheduled.
virtual bool Schedule(base::TimeDelta period_wifi,
base::TimeDelta period_fallback) = 0;
// Cancel any scheduled tasks.
virtual bool Unschedule() = 0;
protected:
RemoteSuggestionsHardScheduler() = default;
private:
DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsHardScheduler);
};
} // namespace ntp_snippets
#endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_HARD_SCHEDULER_H_
// Copyright 2016 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.
#ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_H_
#define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_H_
#include "base/macros.h"
#include "base/time/time.h"
namespace ntp_snippets {
// Class to take care of scheduling of periodic updates of snippets. There are
// two types of scheduled updates:
// - "hard" ones that should outlive current running instance of Chrome. These
// should get triggered according to their schedule even if Chrome is not
// running at the given moment. This is OS-dependent, may be unavilable on
// some platforms.
// - "soft" ones that get triggered only if Chrome stays running until the
// scheduled point.
class RemoteSuggestionsScheduler {
public:
// Interface to perform the scheduled update.
class Updater {
virtual void HardUpdate();
virtual void SoftUpdate();
};
// The passed in |updater| is called when an update is due according to the
// schedule. Note that hard fetches get access to the |updater| via the keyed
// ContentSuggestionService because the concrete instance passed to
// RemoteSuggestionsScheduler when the hard fetch was scheduled may not exist
// any more when the hard update is due.
explicit RemoteSuggestionsScheduler(Updater* updater);
// Schedules both "soft" and "hard" fetches. First removes existing schedule
// before scheduling new updates.
void Schedule();
// Removes any existing schedule.
void Unschedule();
// Schedule periodic fetching of snippets, with different periods depending on
// network state. Once per period, the concrete implementation should call
// RemoteSuggestionsUpdater::HardUpdate where RemoteSuggestionsUpdater is
// obtained from ContentSuggestionsService.
// Any of the periods can be zero to indicate that the corresponding task
// should not be scheduled.
virtual bool Schedule(base::TimeDelta period_wifi,
base::TimeDelta period_fallback) = 0;
// Cancel any scheduled tasks.
virtual bool Unschedule() = 0;
private:
DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsHardScheduler);
};
} // namespace ntp_snippets
#endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_HARD_SCHEDULER_H_
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/sessions/core/session_types.h" #include "components/sessions/core/session_types.h"
#include "components/sync_sessions/synced_session.h" #include "components/sync_sessions/synced_session.h"
#include "components/variations/variations_associated_data.h"
#include "grit/components_strings.h" #include "grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
...@@ -49,18 +50,19 @@ const char* kMaxForeignTabAgeInMinutesParamName = ...@@ -49,18 +50,19 @@ const char* kMaxForeignTabAgeInMinutesParamName =
"max_foreign_tabs_age_in_minutes"; "max_foreign_tabs_age_in_minutes";
int GetMaxForeignTabsTotal() { int GetMaxForeignTabsTotal() {
return GetParamAsInt(ntp_snippets::kForeignSessionsSuggestionsFeature, return variations::GetVariationParamByFeatureAsInt(
kMaxForeignTabsTotalParamName, kMaxForeignTabsTotal); ntp_snippets::kForeignSessionsSuggestionsFeature,
kMaxForeignTabsTotalParamName, kMaxForeignTabsTotal);
} }
int GetMaxForeignTabsPerDevice() { int GetMaxForeignTabsPerDevice() {
return GetParamAsInt(ntp_snippets::kForeignSessionsSuggestionsFeature, return variations::GetVariationParamByFeatureAsInt(
kMaxForeignTabsPerDeviceParamName, ntp_snippets::kForeignSessionsSuggestionsFeature,
kMaxForeignTabsPerDevice); kMaxForeignTabsPerDeviceParamName, kMaxForeignTabsPerDevice);
} }
TimeDelta GetMaxForeignTabAge() { TimeDelta GetMaxForeignTabAge() {
return TimeDelta::FromMinutes(GetParamAsInt( return TimeDelta::FromMinutes(variations::GetVariationParamByFeatureAsInt(
ntp_snippets::kForeignSessionsSuggestionsFeature, ntp_snippets::kForeignSessionsSuggestionsFeature,
kMaxForeignTabAgeInMinutesParamName, kMaxForeignTabAgeInMinutes)); kMaxForeignTabAgeInMinutesParamName, kMaxForeignTabAgeInMinutes));
} }
......
...@@ -91,22 +91,11 @@ static_assert(arraysize(kMetrics) == ...@@ -91,22 +91,11 @@ static_assert(arraysize(kMetrics) ==
static_cast<int>(UserClassifier::Metric::COUNT), static_cast<int>(UserClassifier::Metric::COUNT),
"Fill in info for all metrics."); "Fill in info for all metrics.");
double GetParamValue(const char* param_name, double default_value) {
std::string param_value_str = variations::GetVariationParamValueByFeature(
kArticleSuggestionsFeature, param_name);
double param_value = 0;
if (!base::StringToDouble(param_value_str, &param_value)) {
LOG_IF(WARNING, !param_value_str.empty())
<< "Invalid variation parameter for " << param_name;
return default_value;
}
return param_value;
}
// Computes the discount rate. // Computes the discount rate.
double GetDiscountRatePerHour() { double GetDiscountRatePerHour() {
double discount_rate_per_day = double discount_rate_per_day = variations::GetVariationParamByFeatureAsDouble(
GetParamValue(kDiscountRatePerDayParam, kDiscountRatePerDay); kArticleSuggestionsFeature, kDiscountRatePerDayParam,
kDiscountRatePerDay);
// Check for illegal values. // Check for illegal values.
if (discount_rate_per_day <= 0 || discount_rate_per_day >= 1) { if (discount_rate_per_day <= 0 || discount_rate_per_day >= 1) {
DLOG(WARNING) << "Illegal value " << discount_rate_per_day DLOG(WARNING) << "Illegal value " << discount_rate_per_day
...@@ -121,17 +110,20 @@ double GetDiscountRatePerHour() { ...@@ -121,17 +110,20 @@ double GetDiscountRatePerHour() {
} }
double GetInitialHoursBetweenEvents(UserClassifier::Metric metric) { double GetInitialHoursBetweenEvents(UserClassifier::Metric metric) {
return GetParamValue( return variations::GetVariationParamByFeatureAsDouble(
kArticleSuggestionsFeature,
kInitialHoursBetweenEventsParams[static_cast<int>(metric)], kInitialHoursBetweenEventsParams[static_cast<int>(metric)],
kInitialHoursBetweenEvents[static_cast<int>(metric)]); kInitialHoursBetweenEvents[static_cast<int>(metric)]);
} }
double GetMinHours() { double GetMinHours() {
return GetParamValue(kMinHoursParam, kMinHours); return variations::GetVariationParamByFeatureAsDouble(
kArticleSuggestionsFeature, kMinHoursParam, kMinHours);
} }
double GetMaxHours() { double GetMaxHours() {
return GetParamValue(kMaxHoursParam, kMaxHours); return variations::GetVariationParamByFeatureAsDouble(
kArticleSuggestionsFeature, kMaxHoursParam, kMaxHours);
} }
// Returns the new value of the metric using its |old_value|, assuming // Returns the new value of the metric using its |old_value|, assuming
...@@ -195,11 +187,15 @@ UserClassifier::UserClassifier(PrefService* pref_service) ...@@ -195,11 +187,15 @@ UserClassifier::UserClassifier(PrefService* pref_service)
min_hours_(GetMinHours()), min_hours_(GetMinHours()),
max_hours_(GetMaxHours()), max_hours_(GetMaxHours()),
active_consumer_scrolls_at_least_once_per_hours_( active_consumer_scrolls_at_least_once_per_hours_(
GetParamValue(kActiveConsumerScrollsAtLeastOncePerHoursParam, variations::GetVariationParamByFeatureAsDouble(
kActiveConsumerScrollsAtLeastOncePerHours)), kArticleSuggestionsFeature,
kActiveConsumerScrollsAtLeastOncePerHoursParam,
kActiveConsumerScrollsAtLeastOncePerHours)),
rare_user_opens_ntp_at_most_once_per_hours_( rare_user_opens_ntp_at_most_once_per_hours_(
GetParamValue(kRareUserOpensNTPAtMostOncePerHoursParam, variations::GetVariationParamByFeatureAsDouble(
kRareUserOpensNTPAtMostOncePerHours)) { kArticleSuggestionsFeature,
kRareUserOpensNTPAtMostOncePerHoursParam,
kRareUserOpensNTPAtMostOncePerHours)) {
// The pref_service_ can be null in tests. // The pref_service_ can be null in tests.
if (!pref_service_) { if (!pref_service_) {
return; return;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/metrics/field_trial.h" #include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_param_associator.h" #include "base/metrics/field_trial_param_associator.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "components/variations/variations_http_header_provider.h" #include "components/variations/variations_http_header_provider.h"
...@@ -190,6 +191,64 @@ std::string GetVariationParamValueByFeature(const base::Feature& feature, ...@@ -190,6 +191,64 @@ std::string GetVariationParamValueByFeature(const base::Feature& feature,
return GetVariationParamValue(trial->trial_name(), param_name); return GetVariationParamValue(trial->trial_name(), param_name);
} }
int GetVariationParamByFeatureAsInt(const base::Feature& feature,
const std::string& param_name,
int default_value) {
std::string value_as_string =
GetVariationParamValueByFeature(feature, param_name);
int value_as_int = 0;
if (!base::StringToInt(value_as_string, &value_as_int)) {
if (!value_as_string.empty()) {
DLOG(WARNING) << "Failed to parse variation param " << param_name
<< " with string value " << value_as_string
<< " under feature " << feature.name
<< " into an int. Falling back to default value of "
<< default_value;
}
value_as_int = default_value;
}
return value_as_int;
}
double GetVariationParamByFeatureAsDouble(const base::Feature& feature,
const std::string& param_name,
double default_value) {
std::string value_as_string =
GetVariationParamValueByFeature(feature, param_name);
double value_as_double = 0;
if (!base::StringToDouble(value_as_string, &value_as_double)) {
if (!value_as_string.empty()) {
DLOG(WARNING) << "Failed to parse variation param " << param_name
<< " with string value " << value_as_string
<< " under feature " << feature.name
<< " into a double. Falling back to default value of "
<< default_value;
}
value_as_double = default_value;
}
return value_as_double;
}
bool GetVariationParamByFeatureAsBool(const base::Feature& feature,
const std::string& param_name,
bool default_value) {
std::string value_as_string =
variations::GetVariationParamValueByFeature(feature, param_name);
if (value_as_string == "true")
return true;
if (value_as_string == "false")
return false;
if (!value_as_string.empty()) {
DLOG(WARNING) << "Failed to parse variation param " << param_name
<< " with string value " << value_as_string
<< " under feature " << feature.name
<< " into a bool. Falling back to default value of "
<< default_value;
}
return default_value;
}
// Functions below are exposed for testing explicitly behind this namespace. // Functions below are exposed for testing explicitly behind this namespace.
// They simply wrap existing functions in this file. // They simply wrap existing functions in this file.
namespace testing { namespace testing {
......
...@@ -152,13 +152,38 @@ std::string GetVariationParamValue(const std::string& trial_name, ...@@ -152,13 +152,38 @@ std::string GetVariationParamValue(const std::string& trial_name,
// with at most one variation, through the variation's associated field trial, // with at most one variation, through the variation's associated field trial,
// and selected group. See base/feature_list.h for more information on // and selected group. See base/feature_list.h for more information on
// features. If the feature is not enabled, or the specified parameter does not // features. If the feature is not enabled, or the specified parameter does not
// exist, returns an empty string.Calling this function will result in the // exist, returns an empty string. Calling this function will result in the
// associated field trial being marked as active if found (i.e. group() will be // associated field trial being marked as active if found (i.e. group() will be
// called on it), if it wasn't already. Currently, this information is only // called on it), if it wasn't already. Currently, this information is only
// available from the browser process. Thread safe. // available from the browser process. Thread safe.
std::string GetVariationParamValueByFeature(const base::Feature& feature, std::string GetVariationParamValueByFeature(const base::Feature& feature,
const std::string& param_name); const std::string& param_name);
// Same as GetVariationParamValueByFeature(). On top of that, it converts the
// string value into an int using base::StringToInt() and returns it, if
// successful. Otherwise, it returns |default_value|. If the string value is not
// empty and the conversion does not succeed, it produces a warning to LOG.
int GetVariationParamByFeatureAsInt(const base::Feature& feature,
const std::string& param_name,
int default_value);
// Same as GetVariationParamValueByFeature(). On top of that, it converts the
// string value into a double using base::StringToDouble() and returns it, if
// successful. Otherwise, it returns |default_value|. If the string value is not
// empty and the conversion does not succeed, it produces a warning to LOG.
double GetVariationParamByFeatureAsDouble(const base::Feature& feature,
const std::string& param_name,
double default_value);
// Same as GetVariationParamValueByFeature(). On top of that, it converts the
// string value into a boolean and returns it, if successful. Otherwise, it
// returns |default_value|. The only string representations accepted here are
// "true" and "false". If the string value is not empty and the conversion does
// not succeed, it produces a warning to LOG.
bool GetVariationParamByFeatureAsBool(const base::Feature& feature,
const std::string& param_name,
bool default_value);
// Expose some functions for testing. // Expose some functions for testing.
namespace testing { namespace testing {
......
...@@ -403,4 +403,89 @@ TEST_F(VariationsAssociatedDataTest, GetVariationParamValueByFeature_Disable) { ...@@ -403,4 +403,89 @@ TEST_F(VariationsAssociatedDataTest, GetVariationParamValueByFeature_Disable) {
EXPECT_EQ(std::string(), GetVariationParamValueByFeature(kFeature, "x")); EXPECT_EQ(std::string(), GetVariationParamValueByFeature(kFeature, "x"));
} }
TEST_F(VariationsAssociatedDataTest, GetVariationParamByFeatureAsInt) {
const std::string kTrialName = "GetVariationParamsByFeature";
const base::Feature kFeature{"TestFeature",
base::FEATURE_DISABLED_BY_DEFAULT};
std::map<std::string, std::string> params;
params["a"] = "1";
params["b"] = "1.5";
params["c"] = "foo";
params["d"] = "";
// "e" is not registered
variations::AssociateVariationParams(kTrialName, "A", params);
scoped_refptr<base::FieldTrial> trial(
CreateFieldTrial(kTrialName, 100, "A", NULL));
CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
trial.get());
std::map<std::string, std::string> actualParams;
EXPECT_EQ(1, GetVariationParamByFeatureAsInt(kFeature, "a", 0));
EXPECT_EQ(0, GetVariationParamByFeatureAsInt(kFeature, "b", 0)); // invalid
EXPECT_EQ(0, GetVariationParamByFeatureAsInt(kFeature, "c", 0)); // invalid
EXPECT_EQ(0, GetVariationParamByFeatureAsInt(kFeature, "d", 0)); // empty
EXPECT_EQ(0, GetVariationParamByFeatureAsInt(kFeature, "e", 0)); // empty
}
TEST_F(VariationsAssociatedDataTest, GetVariationParamByFeatureAsDouble) {
const std::string kTrialName = "GetVariationParamsByFeature";
const base::Feature kFeature{"TestFeature",
base::FEATURE_DISABLED_BY_DEFAULT};
std::map<std::string, std::string> params;
params["a"] = "1";
params["b"] = "1.5";
params["c"] = "1.0e-10";
params["d"] = "foo";
params["e"] = "";
// "f" is not registered
variations::AssociateVariationParams(kTrialName, "A", params);
scoped_refptr<base::FieldTrial> trial(
CreateFieldTrial(kTrialName, 100, "A", NULL));
CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
trial.get());
std::map<std::string, std::string> actualParams;
EXPECT_EQ(1, GetVariationParamByFeatureAsDouble(kFeature, "a", 0));
EXPECT_EQ(1.5, GetVariationParamByFeatureAsDouble(kFeature, "b", 0));
EXPECT_EQ(1.0e-10, GetVariationParamByFeatureAsDouble(kFeature, "c", 0));
EXPECT_EQ(0,
GetVariationParamByFeatureAsDouble(kFeature, "d", 0)); // invalid
EXPECT_EQ(0, GetVariationParamByFeatureAsDouble(kFeature, "e", 0)); // empty
EXPECT_EQ(0, GetVariationParamByFeatureAsDouble(kFeature, "f", 0)); // empty
}
TEST_F(VariationsAssociatedDataTest, GetVariationParamByFeatureAsBool) {
const std::string kTrialName = "GetVariationParamsByFeature";
const base::Feature kFeature{"TestFeature",
base::FEATURE_DISABLED_BY_DEFAULT};
std::map<std::string, std::string> params;
params["a"] = "true";
params["b"] = "false";
params["c"] = "1";
params["d"] = "False";
params["e"] = "";
// "f" is not registered
variations::AssociateVariationParams(kTrialName, "A", params);
scoped_refptr<base::FieldTrial> trial(
CreateFieldTrial(kTrialName, 100, "A", NULL));
CreateFeatureWithTrial(kFeature, base::FeatureList::OVERRIDE_ENABLE_FEATURE,
trial.get());
std::map<std::string, std::string> actualParams;
EXPECT_TRUE(GetVariationParamByFeatureAsBool(kFeature, "a", false));
EXPECT_FALSE(GetVariationParamByFeatureAsBool(kFeature, "b", true));
EXPECT_FALSE(
GetVariationParamByFeatureAsBool(kFeature, "c", false)); // invalid
EXPECT_TRUE(
GetVariationParamByFeatureAsBool(kFeature, "d", true)); // invalid
EXPECT_TRUE(GetVariationParamByFeatureAsBool(kFeature, "e", true)); // empty
EXPECT_TRUE(GetVariationParamByFeatureAsBool(kFeature, "f", true)); // empty
}
} // namespace variations } // namespace variations
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