blob: 23e54081fb89d0a9d85e49c8daa98cc14ec1f6cc [file] [log] [blame]
jkrcalce21e97f2016-12-05 22:36:451// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/variations/variations_params_manager.h"
6
7#include <utility>
8
Lukasz Anforowicz1d46ecd2017-07-14 18:21:089#include "base/base_switches.h"
jkrcalce21e97f2016-12-05 22:36:4510#include "base/feature_list.h"
11#include "base/metrics/field_trial.h"
12#include "base/test/scoped_feature_list.h"
Lukasz Anforowicz1d46ecd2017-07-14 18:21:0813#include "components/variations/field_trial_config/field_trial_util.h"
jkrcalce21e97f2016-12-05 22:36:4514#include "components/variations/variations_associated_data.h"
Lukasz Anforowicz1d46ecd2017-07-14 18:21:0815#include "components/variations/variations_switches.h"
jkrcalce21e97f2016-12-05 22:36:4516
17namespace variations {
18namespace testing {
jkrcalce21e97f2016-12-05 22:36:4519namespace {
20
21// The fixed testing group created in the provided trail when setting up params.
22const char kGroupTesting[] = "Testing";
23
24base::FieldTrial* CreateFieldTrialWithParams(
25 const std::string& trial_name,
26 const std::map<std::string, std::string>& param_values) {
ishermanb1917aabe2017-06-13 23:35:1427 AssociateVariationParams(trial_name, kGroupTesting, param_values);
jkrcalce21e97f2016-12-05 22:36:4528 return base::FieldTrialList::CreateFieldTrial(trial_name, kGroupTesting);
29}
30
31} // namespace
32
33VariationParamsManager::VariationParamsManager()
34 : field_trial_list_(new base::FieldTrialList(nullptr)),
35 scoped_feature_list_(new base::test::ScopedFeatureList()) {}
36
37VariationParamsManager::VariationParamsManager(
38 const std::string& trial_name,
39 const std::map<std::string, std::string>& param_values)
40 : VariationParamsManager() {
41 SetVariationParams(trial_name, param_values);
42}
43
44VariationParamsManager::VariationParamsManager(
45 const std::string& trial_name,
46 const std::map<std::string, std::string>& param_values,
47 const std::set<std::string>& associated_features)
48 : VariationParamsManager() {
49 SetVariationParamsWithFeatureAssociations(trial_name, param_values,
50 associated_features);
51}
52
53VariationParamsManager::~VariationParamsManager() {
54 ClearAllVariationIDs();
55 ClearAllVariationParams();
56}
57
58void VariationParamsManager::SetVariationParams(
59 const std::string& trial_name,
60 const std::map<std::string, std::string>& param_values) {
61 CreateFieldTrialWithParams(trial_name, param_values);
62}
63
64void VariationParamsManager::SetVariationParamsWithFeatureAssociations(
65 const std::string& trial_name,
66 const std::map<std::string, std::string>& param_values,
67 const std::set<std::string>& associated_features) {
68 base::FieldTrial* field_trial =
69 CreateFieldTrialWithParams(trial_name, param_values);
70
71 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
72 for (const std::string& feature_name : associated_features) {
73 feature_list->RegisterFieldTrialOverride(
74 feature_name,
75 base::FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE,
76 field_trial);
77 }
78
79 scoped_feature_list_->InitWithFeatureList(std::move(feature_list));
80}
81
82void VariationParamsManager::ClearAllVariationIDs() {
ishermanb1917aabe2017-06-13 23:35:1483 testing::ClearAllVariationIDs();
jkrcalce21e97f2016-12-05 22:36:4584}
85
86void VariationParamsManager::ClearAllVariationParams() {
ishermanb1917aabe2017-06-13 23:35:1487 testing::ClearAllVariationParams();
jkrcalce21e97f2016-12-05 22:36:4588 // When the scoped feature list is destroyed, it puts back the original
89 // feature list that was there when InitWithFeatureList() was called.
90 scoped_feature_list_.reset(new base::test::ScopedFeatureList());
fhorschige0bf61282016-12-20 16:53:0691 // Ensure the destructor is called properly, so it can be freshly recreated.
92 field_trial_list_.reset();
Jinho Bangc3bcb5c2018-01-15 16:13:0093 field_trial_list_ = std::make_unique<base::FieldTrialList>(nullptr);
jkrcalce21e97f2016-12-05 22:36:4594}
95
Lukasz Anforowicz1d46ecd2017-07-14 18:21:0896// static
97void VariationParamsManager::AppendVariationParams(
98 const std::string& trial_name,
99 const std::string& trial_group_name,
100 const std::map<std::string, std::string>& param_values,
101 base::CommandLine* command_line) {
102 // Register the trial group.
103 command_line->AppendSwitchASCII(
104 ::switches::kForceFieldTrials,
105 EscapeValue(trial_name) + "/" + EscapeValue(trial_group_name));
106
107 // Associate |param_values| with the trial group.
108 std::string params_arg =
109 EscapeValue(trial_name) + "." + EscapeValue(trial_group_name) + ":";
110 bool first = true;
111 for (const auto& param : param_values) {
112 // Separate each |param|.
113 if (!first)
114 params_arg += "/";
115 first = false;
116
117 // Append each |param|.
118 const std::string& name = param.first;
119 const std::string& value = param.second;
120 params_arg += EscapeValue(name) + "/" + EscapeValue(value);
121 }
122 command_line->AppendSwitchASCII(variations::switches::kForceFieldTrialParams,
123 params_arg);
124}
125
jkrcalce21e97f2016-12-05 22:36:45126} // namespace testing
127} // namespace variations