Commit 86e3d75a authored by jkrcal's avatar jkrcal Committed by Commit bot

Add simpler API for changing parameters in tests.

When in need of changing variation parameters in unittests, one has to
dig unneccessarily deep into the inner workings of variations::.

This CL adds a simple class to use in unittests.
Simplifying existing unittest code is a matter of further CLs.

BUG=629013

Review-Url: https://codereview.chromium.org/2145963002
Cr-Commit-Position: refs/heads/master@{#405992}
parent dbc04d68
......@@ -16,6 +16,8 @@ namespace variations {
namespace {
const char kGroupTesting[] = "Testing";
// The internal singleton accessor for the map, used to keep it thread-safe.
class GroupMapAccessor {
public:
......@@ -252,6 +254,26 @@ std::string GetVariationParamValueByFeature(const base::Feature& feature,
// They simply wrap existing functions in this file.
namespace testing {
VariationParamsManager::VariationParamsManager(
const std::string& trial_name,
const std::map<std::string, std::string>& params) {
SetVariationParams(trial_name, params);
}
VariationParamsManager::~VariationParamsManager() {
ClearAllVariationIDs();
ClearAllVariationParams();
field_trial_list_.reset();
}
void VariationParamsManager::SetVariationParams(
const std::string& trial_name,
const std::map<std::string, std::string>& params) {
field_trial_list_.reset(new base::FieldTrialList(nullptr));
variations::AssociateVariationParams(trial_name, kGroupTesting, params);
base::FieldTrialList::CreateFieldTrial(trial_name, kGroupTesting);
}
void ClearAllVariationIDs() {
GroupMapAccessor::GetInstance()->ClearAllMapsForTesting();
}
......
......@@ -164,6 +164,31 @@ std::string GetVariationParamValueByFeature(const base::Feature& feature,
// Expose some functions for testing.
namespace testing {
// Use this class as a member in your test class to set variation params for
// your tests. You can directly set the parameters in the constructor (if they
// are used by other members upon construction). You can change them later
// arbitrarily many times using the SetVariationParams function. Internally, it
// creates a FieldTrialList as a member. It works well for multiple tests of a
// given test class, as it clears the parameters when this class is destructed.
// Note that it clears all parameters (not just those registered here).
class VariationParamsManager {
public:
VariationParamsManager(const std::string& trial_name,
const std::map<std::string, std::string>& params);
~VariationParamsManager();
// Associates |params| with the given |trial_name|. It creates a new group,
// used only for testing. Between two calls of this function,
// ClearAllVariationParams() has to be called.
void SetVariationParams(const std::string& trial_name,
const std::map<std::string, std::string>& params);
private:
std::unique_ptr<base::FieldTrialList> field_trial_list_;
DISALLOW_COPY_AND_ASSIGN(VariationParamsManager);
};
// Clears all of the mapped associations.
void ClearAllVariationIDs();
......
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