Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: base/test/scoped_feature_list.cc

Issue 2834583002: Change ScopedFeatureList to overrides FeatureList not reset (Closed)
Patch Set: Alexei comment addressed Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/test/scoped_feature_list.h ('k') | base/test/scoped_feature_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/test/scoped_feature_list.h" 5 #include "base/test/scoped_feature_list.h"
6 6
7 #include <algorithm>
7 #include <string> 8 #include <string>
9 #include <vector>
10
11 #include "base/stl_util.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
8 14
9 namespace base { 15 namespace base {
10 namespace test { 16 namespace test {
11 17
12 namespace { 18 namespace {
13 19
14 static std::string GetFeatureString( 20 std::vector<StringPiece> GetFeatureVector(
15 const std::initializer_list<base::Feature>& features) { 21 const std::initializer_list<base::Feature>& features) {
16 std::string output; 22 std::vector<StringPiece> output;
17 for (const base::Feature& feature : features) { 23 for (const base::Feature& feature : features) {
18 if (!output.empty()) 24 output.push_back(feature.name);
19 output += ",";
20 output += feature.name;
21 } 25 }
26
22 return output; 27 return output;
23 } 28 }
24 29
30 // Extracts a feature name from a feature state string. For example, given
31 // the input "*MyLovelyFeature<SomeFieldTrial", returns "MyLovelyFeature".
32 StringPiece GetFeatureName(StringPiece feature) {
33 StringPiece feature_name = feature;
34
35 // Remove default info.
36 if (feature_name.starts_with("*"))
37 feature_name = feature_name.substr(1);
38
39 // Remove field_trial info.
40 std::size_t index = feature_name.find("<");
41 if (index != std::string::npos)
42 feature_name = feature_name.substr(0, index);
43
44 return feature_name;
45 }
46
47 struct Features {
48 std::vector<StringPiece> enabled_feature_list;
49 std::vector<StringPiece> disabled_feature_list;
50 };
51
52 // Merges previously-specified feature overrides with those passed into one of
53 // the Init() methods. |features| should be a list of features previously
54 // overridden to be in the |override_state|. |merged_features| should contain
55 // the enabled and disabled features passed into the Init() method, plus any
56 // overrides merged as a result of previous calls to this function.
57 void OverrideFeatures(const std::string& features,
58 base::FeatureList::OverrideState override_state,
59 Features* merged_features) {
60 std::vector<StringPiece> features_list =
61 SplitStringPiece(features, ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
62
63 for (StringPiece feature : features_list) {
64 StringPiece feature_name = GetFeatureName(feature);
65
66 if (ContainsValue(merged_features->enabled_feature_list, feature_name) ||
67 ContainsValue(merged_features->disabled_feature_list, feature_name))
68 continue;
69
70 if (override_state == FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE) {
71 merged_features->enabled_feature_list.push_back(feature);
72 } else {
73 DCHECK_EQ(override_state,
74 FeatureList::OverrideState::OVERRIDE_DISABLE_FEATURE);
75 merged_features->disabled_feature_list.push_back(feature);
76 }
77 }
78 }
79
25 } // namespace 80 } // namespace
26 81
27 ScopedFeatureList::ScopedFeatureList() {} 82 ScopedFeatureList::ScopedFeatureList() {}
28 83
29 ScopedFeatureList::~ScopedFeatureList() { 84 ScopedFeatureList::~ScopedFeatureList() {
30 if (original_feature_list_) { 85 if (original_feature_list_) {
31 base::FeatureList::ClearInstanceForTesting(); 86 base::FeatureList::ClearInstanceForTesting();
32 base::FeatureList::RestoreInstanceForTesting( 87 base::FeatureList::RestoreInstanceForTesting(
33 std::move(original_feature_list_)); 88 std::move(original_feature_list_));
34 } 89 }
35 } 90 }
36 91
37 void ScopedFeatureList::Init() { 92 void ScopedFeatureList::Init() {
38 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); 93 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
39 feature_list->InitializeFromCommandLine(std::string(), std::string()); 94 feature_list->InitializeFromCommandLine(std::string(), std::string());
40 InitWithFeatureList(std::move(feature_list)); 95 InitWithFeatureList(std::move(feature_list));
41 } 96 }
42 97
43 void ScopedFeatureList::InitWithFeatures(
44 const std::initializer_list<base::Feature>& enabled_features,
45 const std::initializer_list<base::Feature>& disabled_features) {
46 InitFromCommandLine(GetFeatureString(enabled_features),
47 GetFeatureString(disabled_features));
48 }
49
50 void ScopedFeatureList::InitWithFeatureList( 98 void ScopedFeatureList::InitWithFeatureList(
51 std::unique_ptr<FeatureList> feature_list) { 99 std::unique_ptr<FeatureList> feature_list) {
52 DCHECK(!original_feature_list_); 100 DCHECK(!original_feature_list_);
53 original_feature_list_ = base::FeatureList::ClearInstanceForTesting(); 101 original_feature_list_ = base::FeatureList::ClearInstanceForTesting();
54 base::FeatureList::SetInstance(std::move(feature_list)); 102 base::FeatureList::SetInstance(std::move(feature_list));
55 } 103 }
56 104
57 void ScopedFeatureList::InitFromCommandLine( 105 void ScopedFeatureList::InitFromCommandLine(
58 const std::string& enable_features, 106 const std::string& enable_features,
59 const std::string& disable_features) { 107 const std::string& disable_features) {
60 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); 108 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
61 feature_list->InitializeFromCommandLine(enable_features, disable_features); 109 feature_list->InitializeFromCommandLine(enable_features, disable_features);
62 InitWithFeatureList(std::move(feature_list)); 110 InitWithFeatureList(std::move(feature_list));
63 } 111 }
64 112
113 void ScopedFeatureList::InitWithFeatures(
114 const std::initializer_list<base::Feature>& enabled_features,
115 const std::initializer_list<base::Feature>& disabled_features) {
116 Features merged_features;
117 merged_features.enabled_feature_list = GetFeatureVector(enabled_features);
118 merged_features.disabled_feature_list = GetFeatureVector(disabled_features);
119
120 base::FeatureList* feature_list = base::FeatureList::GetInstance();
121
122 // |current_enabled_features| and |current_disabled_features| must declare out
123 // of if scope to avoid them out of scope before JoinString calls because
124 // |merged_features| may contains StringPiece which holding pointer points to
125 // |current_enabled_features| and |current_disabled_features|.
126 std::string current_enabled_features;
127 std::string current_disabled_features;
128 if (feature_list) {
129 base::FeatureList::GetInstance()->GetFeatureOverrides(
130 &current_enabled_features, &current_disabled_features);
131 OverrideFeatures(current_enabled_features,
132 FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE,
133 &merged_features);
134 OverrideFeatures(current_disabled_features,
135 FeatureList::OverrideState::OVERRIDE_DISABLE_FEATURE,
136 &merged_features);
137 }
138
139 std::string enabled = JoinString(merged_features.enabled_feature_list, ",");
140 std::string disabled = JoinString(merged_features.disabled_feature_list, ",");
141 InitFromCommandLine(enabled, disabled);
142 }
143
65 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) { 144 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) {
66 InitFromCommandLine(feature.name, std::string()); 145 InitWithFeatures({feature}, {});
67 } 146 }
68 147
69 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) { 148 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) {
70 InitFromCommandLine(std::string(), feature.name); 149 InitWithFeatures({}, {feature});
71 } 150 }
72 151
73 } // namespace test 152 } // namespace test
74 } // namespace base 153 } // namespace base
OLDNEW
« no previous file with comments | « base/test/scoped_feature_list.h ('k') | base/test/scoped_feature_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698