blob: be71186b466e1129d4d47e184b822c8d84299a35 [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"
fdorayba121422016-12-23 19:51:4811#include "base/memory/ptr_util.h"
jkrcalce21e97f2016-12-05 22:36:4512#include "base/metrics/field_trial.h"
13#include "base/test/scoped_feature_list.h"
Lukasz Anforowicz1d46ecd2017-07-14 18:21:0814#include "components/variations/field_trial_config/field_trial_util.h"
jkrcalce21e97f2016-12-05 22:36:4515#include "components/variations/variations_associated_data.h"
Lukasz Anforowicz1d46ecd2017-07-14 18:21:0816#include "components/variations/variations_switches.h"
jkrcalce21e97f2016-12-05 22:36:4517
18namespace variations {
19namespace testing {
jkrcalce21e97f2016-12-05 22:36:4520namespace {
21
22// The fixed testing group created in the provided trail when setting up params.
23const char kGroupTesting[] = "Testing";
24
25base::FieldTrial* CreateFieldTrialWithParams(
26 const std::string& trial_name,
27 const std::map<std::string, std::string>& param_values) {
ishermanb1917aabe2017-06-13 23:35:1428 AssociateVariationParams(trial_name, kGroupTesting, param_values);
jkrcalce21e97f2016-12-05 22:36:4529 return base::FieldTrialList::CreateFieldTrial(trial_name, kGroupTesting);
30}
31
32} // namespace
33
34VariationParamsManager::VariationParamsManager()
35 : field_trial_list_(new base::FieldTrialList(nullptr)),
36 scoped_feature_list_(new base::test::ScopedFeatureList()) {}
37
38VariationParamsManager::VariationParamsManager(
39 const std::string& trial_name,
40 const std::map<std::string, std::string>& param_values)
41 : VariationParamsManager() {
42 SetVariationParams(trial_name, param_values);
43}
44
45VariationParamsManager::VariationParamsManager(
46 const std::string& trial_name,
47 const std::map<std::string, std::string>& param_values,
48 const std::set<std::string>& associated_features)
49 : VariationParamsManager() {
50 SetVariationParamsWithFeatureAssociations(trial_name, param_values,
51 associated_features);
52}
53
54VariationParamsManager::~VariationParamsManager() {
55 ClearAllVariationIDs();
56 ClearAllVariationParams();
57}
58
59void VariationParamsManager::SetVariationParams(
60 const std::string& trial_name,
61 const std::map<std::string, std::string>& param_values) {
62 CreateFieldTrialWithParams(trial_name, param_values);
63}
64
65void VariationParamsManager::SetVariationParamsWithFeatureAssociations(
66 const std::string& trial_name,
67 const std::map<std::string, std::string>& param_values,
68 const std::set<std::string>& associated_features) {
69 base::FieldTrial* field_trial =
70 CreateFieldTrialWithParams(trial_name, param_values);
71
72 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
73 for (const std::string& feature_name : associated_features) {
74 feature_list->RegisterFieldTrialOverride(
75 feature_name,
76 base::FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE,
77 field_trial);
78 }
79
80 scoped_feature_list_->InitWithFeatureList(std::move(feature_list));
81}
82
83void VariationParamsManager::ClearAllVariationIDs() {
ishermanb1917aabe2017-06-13 23:35:1484 testing::ClearAllVariationIDs();
jkrcalce21e97f2016-12-05 22:36:4585}
86
87void VariationParamsManager::ClearAllVariationParams() {
ishermanb1917aabe2017-06-13 23:35:1488 testing::ClearAllVariationParams();
jkrcalce21e97f2016-12-05 22:36:4589 // When the scoped feature list is destroyed, it puts back the original
90 // feature list that was there when InitWithFeatureList() was called.
91 scoped_feature_list_.reset(new base::test::ScopedFeatureList());
fhorschige0bf61282016-12-20 16:53:0692 // Ensure the destructor is called properly, so it can be freshly recreated.
93 field_trial_list_.reset();
94 field_trial_list_ = base::MakeUnique<base::FieldTrialList>(nullptr);
jkrcalce21e97f2016-12-05 22:36:4595}
96
Lukasz Anforowicz1d46ecd2017-07-14 18:21:0897// static
98void VariationParamsManager::AppendVariationParams(
99 const std::string& trial_name,
100 const std::string& trial_group_name,
101 const std::map<std::string, std::string>& param_values,
102 base::CommandLine* command_line) {
103 // Register the trial group.
104 command_line->AppendSwitchASCII(
105 ::switches::kForceFieldTrials,
106 EscapeValue(trial_name) + "/" + EscapeValue(trial_group_name));
107
108 // Associate |param_values| with the trial group.
109 std::string params_arg =
110 EscapeValue(trial_name) + "." + EscapeValue(trial_group_name) + ":";
111 bool first = true;
112 for (const auto& param : param_values) {
113 // Separate each |param|.
114 if (!first)
115 params_arg += "/";
116 first = false;
117
118 // Append each |param|.
119 const std::string& name = param.first;
120 const std::string& value = param.second;
121 params_arg += EscapeValue(name) + "/" + EscapeValue(value);
122 }
123 command_line->AppendSwitchASCII(variations::switches::kForceFieldTrialParams,
124 params_arg);
125}
126
jkrcalce21e97f2016-12-05 22:36:45127} // namespace testing
128} // namespace variations