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

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

Issue 2834583002: Change ScopedFeatureList to overrides FeatureList not reset (Closed)
Patch Set: Alexei and Ilya comments addressed Created 3 years, 8 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
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<std::string> GetFeatureVector(
15 const std::initializer_list<base::Feature>& features) { 21 const std::initializer_list<base::Feature>& features) {
16 std::string output; 22 std::vector<std::string> 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<std::string> enabled_feature_list;
49 std::vector<std::string> disabled_feature_list;
50 };
51
52 // features is from current FeatureList as a string concat features with comma.
53 // override_state indicate features is enable list or disable list.
54 // This method add feature in features to merged_features if it is not exist in
55 // merged_features.
Ilya Sherman 2017/04/25 21:41:58 nit: Suggested rephrasing: // Merges previously-s
56 void OverrideFeatures(const std::string& features,
57 base::FeatureList::OverrideState override_state,
58 Features* merged_features) {
59 std::vector<StringPiece> features_list =
60 SplitStringPiece(features, ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
61
62 for (StringPiece feature : features_list) {
63 StringPiece feature_name = GetFeatureName(feature);
64
65 if (ContainsValue(merged_features->enabled_feature_list, feature_name) ||
66 ContainsValue(merged_features->disabled_feature_list, feature_name))
67 continue;
68
69 if (override_state == FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE) {
70 merged_features->enabled_feature_list.push_back(feature.as_string());
71 } else {
72 DCHECK(override_state ==
Ilya Sherman 2017/04/25 21:41:58 nit: DCHECK_EQ
73 FeatureList::OverrideState::OVERRIDE_DISABLE_FEATURE);
74 merged_features->disabled_feature_list.push_back(feature.as_string());
75 }
76 }
77 }
78
25 } // namespace 79 } // namespace
26 80
27 ScopedFeatureList::ScopedFeatureList() {} 81 ScopedFeatureList::ScopedFeatureList() {}
28 82
29 ScopedFeatureList::~ScopedFeatureList() { 83 ScopedFeatureList::~ScopedFeatureList() {
30 if (original_feature_list_) { 84 if (original_feature_list_) {
31 base::FeatureList::ClearInstanceForTesting(); 85 base::FeatureList::ClearInstanceForTesting();
32 base::FeatureList::RestoreInstanceForTesting( 86 base::FeatureList::RestoreInstanceForTesting(
33 std::move(original_feature_list_)); 87 std::move(original_feature_list_));
34 } 88 }
35 } 89 }
36 90
37 void ScopedFeatureList::Init() { 91 void ScopedFeatureList::Init() {
38 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); 92 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
39 feature_list->InitializeFromCommandLine(std::string(), std::string()); 93 feature_list->InitializeFromCommandLine(std::string(), std::string());
40 InitWithFeatureList(std::move(feature_list)); 94 InitWithFeatureList(std::move(feature_list));
41 } 95 }
42 96
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( 97 void ScopedFeatureList::InitWithFeatureList(
51 std::unique_ptr<FeatureList> feature_list) { 98 std::unique_ptr<FeatureList> feature_list) {
52 DCHECK(!original_feature_list_); 99 DCHECK(!original_feature_list_);
53 original_feature_list_ = base::FeatureList::ClearInstanceForTesting(); 100 original_feature_list_ = base::FeatureList::ClearInstanceForTesting();
54 base::FeatureList::SetInstance(std::move(feature_list)); 101 base::FeatureList::SetInstance(std::move(feature_list));
55 } 102 }
56 103
57 void ScopedFeatureList::InitFromCommandLine( 104 void ScopedFeatureList::InitFromCommandLine(
58 const std::string& enable_features, 105 const std::string& enable_features,
59 const std::string& disable_features) { 106 const std::string& disable_features) {
60 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); 107 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
61 feature_list->InitializeFromCommandLine(enable_features, disable_features); 108 feature_list->InitializeFromCommandLine(enable_features, disable_features);
62 InitWithFeatureList(std::move(feature_list)); 109 InitWithFeatureList(std::move(feature_list));
63 } 110 }
64 111
112 void ScopedFeatureList::InitWithFeatures(
113 const std::initializer_list<base::Feature>& enabled_features,
114 const std::initializer_list<base::Feature>& disabled_features) {
115 Features merged_features;
116 merged_features.enabled_feature_list = GetFeatureVector(enabled_features);
117 merged_features.disabled_feature_list = GetFeatureVector(disabled_features);
118
119 base::FeatureList* feature_list = base::FeatureList::GetInstance();
120
121 if (feature_list) {
122 std::string enables;
123 std::string disables;
chaopeng 2017/04/25 21:21:07 Re Alexei: I need to move this 2 line out of if-sc
Ilya Sherman 2017/04/25 21:41:58 nit: Please restore the previous variable names of
Ilya Sherman 2017/04/25 21:41:58 Hmm, why do you need to move these in order to mak
chaopeng 2017/04/26 01:04:39 these names conflict with the function arguments.
chaopeng 2017/04/26 01:04:39 StringPiece is not alloc new memory but save a poi
Ilya Sherman 2017/04/26 07:19:20 Ah, yes, that makes sense. Thanks for the explana
Alexei Svitkine (slow) 2017/04/27 14:58:08 Moving out of the if SGTM to me, with a correspond
124
125 base::FeatureList::GetInstance()->GetFeatureOverrides(&enables, &disables);
126 OverrideFeatures(enables,
127 FeatureList::OverrideState::OVERRIDE_ENABLE_FEATURE,
128 &merged_features);
129 OverrideFeatures(disables,
130 FeatureList::OverrideState::OVERRIDE_DISABLE_FEATURE,
131 &merged_features);
132 }
133
134 std::string enabled = JoinString(merged_features.enabled_feature_list, ",");
135 std::string disabled = JoinString(merged_features.disabled_feature_list, ",");
136 InitFromCommandLine(enabled, disabled);
137 }
138
65 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) { 139 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) {
66 InitFromCommandLine(feature.name, std::string()); 140 InitWithFeatures({feature}, {});
67 } 141 }
68 142
69 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) { 143 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) {
70 InitFromCommandLine(std::string(), feature.name); 144 InitWithFeatures({}, {feature});
71 } 145 }
72 146
73 } // namespace test 147 } // namespace test
74 } // namespace base 148 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698