Skip to content

Commit

Permalink
Add simpler API for changing parameters in tests.
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
jkrcal authored and Commit bot committed Jul 18, 2016
1 parent dbc04d6 commit 86e3d75
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions components/variations/variations_associated_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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();
}
Expand Down
25 changes: 25 additions & 0 deletions components/variations/variations_associated_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 86e3d75

Please sign in to comment.