OLD | NEW |
---|---|
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/strings/string_split.h" | |
12 #include "base/strings/string_util.h" | |
8 | 13 |
9 namespace base { | 14 namespace base { |
10 namespace test { | 15 namespace test { |
11 | 16 |
12 namespace { | 17 namespace { |
13 | 18 |
14 static std::string GetFeatureString( | 19 static std::vector<std::string> GetFeatureString( |
Ilya Sherman
2017/04/21 22:43:33
nit: s/String/Vector
Ilya Sherman
2017/04/21 22:43:34
nit: No need for "static" within an anonymous name
| |
15 const std::initializer_list<base::Feature>& features) { | 20 const std::initializer_list<base::Feature>& features) { |
16 std::string output; | 21 std::vector<std::string> output; |
17 for (const base::Feature& feature : features) { | 22 for (const base::Feature& feature : features) |
Ilya Sherman
2017/04/21 22:43:33
nit: Please add curly braces. I'm not sure whethe
| |
18 if (!output.empty()) | 23 output.push_back(feature.name); |
19 output += ","; | 24 |
20 output += feature.name; | 25 return output; |
26 } | |
27 | |
28 static StringPiece GetFeatureName(const StringPiece& feature) { | |
Ilya Sherman
2017/04/21 22:43:34
Please add documentation for this function.
Ilya Sherman
2017/04/21 22:43:34
Per the StringPiece documentation, it's preferable
| |
29 StringPiece feature_name = feature; | |
30 | |
31 // Remove started *. | |
Ilya Sherman
2017/04/21 22:43:33
This just repeats what the code does. Please inst
| |
32 if (feature_name.starts_with("*")) | |
33 feature_name.substr(1); | |
Ilya Sherman
2017/04/21 22:43:34
Did you mean to assign this back to feature_name?
| |
34 | |
35 // Remove field_trial info. | |
36 std::size_t index = feature_name.find("<"); | |
37 if (index != std::string::npos) | |
38 feature_name = feature_name.substr(0, index); | |
39 | |
40 return feature_name; | |
41 } | |
42 | |
43 // Given features from FeatureList::GetFeatureOverrides, add them to overrides | |
44 // if it is not existing in already_overridden_feature_names or overrides. | |
Ilya Sherman
2017/04/21 22:43:33
If you take my advice about passing a unioned set
| |
45 static void OverrideFeatures( | |
46 const std::string& features, | |
47 std::vector<std::string>* already_overridden_feature_names, | |
48 std::vector<std::string>* overrides) { | |
49 std::vector<StringPiece> features_list = | |
50 SplitStringPiece(features, ",", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); | |
51 | |
52 for (StringPiece& feature : features_list) { | |
Ilya Sherman
2017/04/21 22:43:34
nit: Chromium strongly frowns upon non-const refer
| |
53 StringPiece feature_name = GetFeatureName(feature); | |
54 | |
55 if (std::count(already_overridden_feature_names->begin(), | |
56 already_overridden_feature_names->end(), feature_name) > 0) | |
57 continue; | |
58 | |
59 if (std::count(overrides->begin(), overrides->end(), feature_name) > 0) | |
60 continue; | |
61 | |
62 overrides->push_back(feature.as_string()); | |
21 } | 63 } |
22 return output; | |
23 } | 64 } |
24 | 65 |
25 } // namespace | 66 } // namespace |
26 | 67 |
27 ScopedFeatureList::ScopedFeatureList() {} | 68 ScopedFeatureList::ScopedFeatureList() {} |
28 | 69 |
29 ScopedFeatureList::~ScopedFeatureList() { | 70 ScopedFeatureList::~ScopedFeatureList() { |
30 if (original_feature_list_) { | 71 if (original_feature_list_) { |
31 base::FeatureList::ClearInstanceForTesting(); | 72 base::FeatureList::ClearInstanceForTesting(); |
32 base::FeatureList::RestoreInstanceForTesting( | 73 base::FeatureList::RestoreInstanceForTesting( |
33 std::move(original_feature_list_)); | 74 std::move(original_feature_list_)); |
34 } | 75 } |
35 } | 76 } |
36 | 77 |
37 void ScopedFeatureList::Init() { | 78 void ScopedFeatureList::Init() { |
38 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 79 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
39 feature_list->InitializeFromCommandLine(std::string(), std::string()); | 80 feature_list->InitializeFromCommandLine(std::string(), std::string()); |
40 InitWithFeatureList(std::move(feature_list)); | 81 InitWithFeatureList(std::move(feature_list)); |
41 } | 82 } |
42 | 83 |
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( | 84 void ScopedFeatureList::InitWithFeatureList( |
51 std::unique_ptr<FeatureList> feature_list) { | 85 std::unique_ptr<FeatureList> feature_list) { |
52 DCHECK(!original_feature_list_); | 86 DCHECK(!original_feature_list_); |
53 original_feature_list_ = base::FeatureList::ClearInstanceForTesting(); | 87 original_feature_list_ = base::FeatureList::ClearInstanceForTesting(); |
54 base::FeatureList::SetInstance(std::move(feature_list)); | 88 base::FeatureList::SetInstance(std::move(feature_list)); |
55 } | 89 } |
56 | 90 |
57 void ScopedFeatureList::InitFromCommandLine( | 91 void ScopedFeatureList::InitFromCommandLine( |
58 const std::string& enable_features, | 92 const std::string& enable_features, |
59 const std::string& disable_features) { | 93 const std::string& disable_features) { |
60 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 94 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
61 feature_list->InitializeFromCommandLine(enable_features, disable_features); | 95 feature_list->InitializeFromCommandLine(enable_features, disable_features); |
62 InitWithFeatureList(std::move(feature_list)); | 96 InitWithFeatureList(std::move(feature_list)); |
63 } | 97 } |
64 | 98 |
65 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) { | 99 void ScopedFeatureList::InitAndEnableFeature(const base::Feature& feature) { |
66 InitFromCommandLine(feature.name, std::string()); | 100 InitWithFeatures({feature}, {}); |
67 } | 101 } |
68 | 102 |
69 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) { | 103 void ScopedFeatureList::InitAndDisableFeature(const base::Feature& feature) { |
70 InitFromCommandLine(std::string(), feature.name); | 104 InitWithFeatures({}, {feature}); |
105 } | |
106 | |
107 void ScopedFeatureList::InitWithFeatures( | |
Ilya Sherman
2017/04/21 22:43:34
Please position this within the .cc file in the sa
| |
108 const std::initializer_list<base::Feature>& enabled_features, | |
109 const std::initializer_list<base::Feature>& disabled_features) { | |
110 std::vector<std::string> merged_enabled_features = | |
111 GetFeatureString(enabled_features); | |
112 std::vector<std::string> merged_disabled_features = | |
113 GetFeatureString(disabled_features); | |
Ilya Sherman
2017/04/21 22:43:34
Please construct a set containing the union of the
| |
114 | |
115 base::FeatureList* feature_list = base::FeatureList::GetInstance(); | |
116 if (feature_list) { | |
117 std::string enabled_features; | |
118 std::string disabled_features; | |
119 base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features, | |
120 &disabled_features); | |
121 OverrideFeatures(enabled_features, &merged_disabled_features, | |
Ilya Sherman
2017/04/21 22:43:34
The second param is not an outparam, so please pas
| |
122 &merged_enabled_features); | |
123 OverrideFeatures(disabled_features, &merged_enabled_features, | |
124 &merged_disabled_features); | |
125 } | |
126 | |
127 std::string enabled = JoinString(merged_enabled_features, ","); | |
128 std::string disabled = JoinString(merged_disabled_features, ","); | |
129 InitFromCommandLine(enabled, disabled); | |
71 } | 130 } |
72 | 131 |
73 } // namespace test | 132 } // namespace test |
74 } // namespace base | 133 } // namespace base |
OLD | NEW |