jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 1 | // 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 Anforowicz | 1d46ecd | 2017-07-14 18:21:08 | [diff] [blame] | 9 | #include "base/base_switches.h" |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 10 | #include "base/feature_list.h" |
| 11 | #include "base/metrics/field_trial.h" |
| 12 | #include "base/test/scoped_feature_list.h" |
Lukasz Anforowicz | 1d46ecd | 2017-07-14 18:21:08 | [diff] [blame] | 13 | #include "components/variations/field_trial_config/field_trial_util.h" |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 14 | #include "components/variations/variations_associated_data.h" |
Lukasz Anforowicz | 1d46ecd | 2017-07-14 18:21:08 | [diff] [blame] | 15 | #include "components/variations/variations_switches.h" |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 16 | |
| 17 | namespace variations { |
| 18 | namespace testing { |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | // The fixed testing group created in the provided trail when setting up params. |
| 22 | const char kGroupTesting[] = "Testing"; |
| 23 | |
| 24 | base::FieldTrial* CreateFieldTrialWithParams( |
| 25 | const std::string& trial_name, |
| 26 | const std::map<std::string, std::string>& param_values) { |
isherman | b1917aabe | 2017-06-13 23:35:14 | [diff] [blame] | 27 | AssociateVariationParams(trial_name, kGroupTesting, param_values); |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 28 | return base::FieldTrialList::CreateFieldTrial(trial_name, kGroupTesting); |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | VariationParamsManager::VariationParamsManager() |
| 34 | : field_trial_list_(new base::FieldTrialList(nullptr)), |
| 35 | scoped_feature_list_(new base::test::ScopedFeatureList()) {} |
| 36 | |
| 37 | VariationParamsManager::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 | |
| 44 | VariationParamsManager::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 | |
| 53 | VariationParamsManager::~VariationParamsManager() { |
| 54 | ClearAllVariationIDs(); |
| 55 | ClearAllVariationParams(); |
| 56 | } |
| 57 | |
| 58 | void 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 | |
| 64 | void 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 | |
| 82 | void VariationParamsManager::ClearAllVariationIDs() { |
isherman | b1917aabe | 2017-06-13 23:35:14 | [diff] [blame] | 83 | testing::ClearAllVariationIDs(); |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void VariationParamsManager::ClearAllVariationParams() { |
isherman | b1917aabe | 2017-06-13 23:35:14 | [diff] [blame] | 87 | testing::ClearAllVariationParams(); |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 88 | // 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()); |
fhorschig | e0bf6128 | 2016-12-20 16:53:06 | [diff] [blame] | 91 | // Ensure the destructor is called properly, so it can be freshly recreated. |
| 92 | field_trial_list_.reset(); |
Jinho Bang | c3bcb5c | 2018-01-15 16:13:00 | [diff] [blame] | 93 | field_trial_list_ = std::make_unique<base::FieldTrialList>(nullptr); |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 94 | } |
| 95 | |
Lukasz Anforowicz | 1d46ecd | 2017-07-14 18:21:08 | [diff] [blame] | 96 | // static |
| 97 | void 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 | |
jkrcal | ce21e97f | 2016-12-05 22:36:45 | [diff] [blame] | 126 | } // namespace testing |
| 127 | } // namespace variations |