OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "extensions/common/feature_switch.h" | 5 #include "extensions/common/feature_switch.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 using extensions::FeatureSwitch; | 12 using extensions::FeatureSwitch; |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 const char kSwitchName[] = "test-switch"; | 16 const char kSwitchName[] = "test-switch"; |
17 const char kFieldTrialName[] = "field-trial"; | 17 const char kFieldTrialName[] = "field-trial"; |
18 | 18 |
19 // Create and register a field trial that will always return the given | 19 // Create and register a field trial named |field_trial_name| that will always |
20 // |group_name|. | 20 // return the given |group_name|. |
21 scoped_refptr<base::FieldTrial> CreateFieldTrial( | 21 scoped_refptr<base::FieldTrial> CreateFieldTrialWithName( |
| 22 const std::string& field_trial_name, |
22 const std::string& group_name) { | 23 const std::string& group_name) { |
23 const int kTotalProbability = 10; | 24 const int kTotalProbability = 10; |
24 // Note: This code will probably fail in the year 5000. But all the cycles we | 25 // Note: This code will probably fail in the year 5000. But all the cycles we |
25 // save in the next 3000 years by not determining the current year will be | 26 // save in the next 3000 years by not determining the current year will be |
26 // worth it. | 27 // worth it. |
27 scoped_refptr<base::FieldTrial> trial = | 28 scoped_refptr<base::FieldTrial> trial = |
28 base::FieldTrialList::FactoryGetFieldTrial( | 29 base::FieldTrialList::FactoryGetFieldTrial( |
29 kFieldTrialName, kTotalProbability, "default", 5000, 1, 1, | 30 field_trial_name, kTotalProbability, "default", 5000, 1, 1, |
30 base::FieldTrial::SESSION_RANDOMIZED, nullptr); | 31 base::FieldTrial::SESSION_RANDOMIZED, nullptr); |
31 trial->AppendGroup(group_name, kTotalProbability); | 32 trial->AppendGroup(group_name, kTotalProbability); |
32 return trial; | 33 return trial; |
33 } | 34 } |
34 | 35 |
| 36 // Create and register a field trial that will always return the given |
| 37 // |group_name|. |
| 38 scoped_refptr<base::FieldTrial> CreateFieldTrial( |
| 39 const std::string& group_name) { |
| 40 return CreateFieldTrialWithName(kFieldTrialName, group_name); |
| 41 } |
| 42 |
35 template<FeatureSwitch::DefaultValue T> | 43 template<FeatureSwitch::DefaultValue T> |
36 class FeatureSwitchTest : public testing::Test { | 44 class FeatureSwitchTest : public testing::Test { |
37 public: | 45 public: |
38 FeatureSwitchTest() | 46 FeatureSwitchTest() |
39 : command_line_(base::CommandLine::NO_PROGRAM), | 47 : command_line_(base::CommandLine::NO_PROGRAM), |
40 feature_(&command_line_, kSwitchName, T) {} | 48 feature_(&command_line_, kSwitchName, T) {} |
41 protected: | 49 protected: |
42 base::CommandLine command_line_; | 50 base::CommandLine command_line_; |
43 FeatureSwitch feature_; | 51 FeatureSwitch feature_; |
44 }; | 52 }; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 command_line_.AppendSwitchASCII(kSwitchName, "\t\t 0 \n"); | 157 command_line_.AppendSwitchASCII(kSwitchName, "\t\t 0 \n"); |
150 EXPECT_FALSE(feature_.IsEnabled()); | 158 EXPECT_FALSE(feature_.IsEnabled()); |
151 } | 159 } |
152 | 160 |
153 TEST_F(FeatureSwitchEnabledTest, TrueFieldTrialValue) { | 161 TEST_F(FeatureSwitchEnabledTest, TrueFieldTrialValue) { |
154 // Construct a fake field trial that defaults to the group "Enabled". | 162 // Construct a fake field trial that defaults to the group "Enabled". |
155 base::FieldTrialList field_trials(nullptr); | 163 base::FieldTrialList field_trials(nullptr); |
156 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); | 164 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); |
157 { | 165 { |
158 // A default-enabled switch should be enabled (naturally). | 166 // A default-enabled switch should be enabled (naturally). |
159 FeatureSwitch default_enabled_switch(&command_line_, kSwitchName, | 167 FeatureSwitch default_enabled_switch( |
160 kFieldTrialName, | 168 &command_line_, kSwitchName, |
161 FeatureSwitch::DEFAULT_ENABLED); | 169 std::vector<std::string>(1, kFieldTrialName), |
| 170 FeatureSwitch::DEFAULT_ENABLED); |
162 EXPECT_TRUE(default_enabled_switch.IsEnabled()); | 171 EXPECT_TRUE(default_enabled_switch.IsEnabled()); |
163 // Scoped overrides override everything. | 172 // Scoped overrides override everything. |
164 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, | 173 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, |
165 false); | 174 false); |
166 EXPECT_FALSE(default_enabled_switch.IsEnabled()); | 175 EXPECT_FALSE(default_enabled_switch.IsEnabled()); |
167 } | 176 } |
168 | 177 |
169 { | 178 { |
170 // A default-disabled switch should be enabled because of the field trial. | 179 // A default-disabled switch should be enabled because of the field trial. |
171 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, | 180 FeatureSwitch default_disabled_switch( |
172 kFieldTrialName, | 181 &command_line_, kSwitchName, |
173 FeatureSwitch::DEFAULT_DISABLED); | 182 std::vector<std::string>(1, kFieldTrialName), |
| 183 FeatureSwitch::DEFAULT_DISABLED); |
174 EXPECT_TRUE(default_disabled_switch.IsEnabled()); | 184 EXPECT_TRUE(default_disabled_switch.IsEnabled()); |
175 // Scoped overrides override everything. | 185 // Scoped overrides override everything. |
176 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, | 186 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, |
177 false); | 187 false); |
178 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 188 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
179 } | 189 } |
180 } | 190 } |
181 | 191 |
182 TEST_F(FeatureSwitchEnabledTest, FalseFieldTrialValue) { | 192 TEST_F(FeatureSwitchEnabledTest, FalseFieldTrialValue) { |
183 // Construct a fake field trial that defaults to the group "Disabled". | 193 // Construct a fake field trial that defaults to the group "Disabled". |
184 base::FieldTrialList field_trials(nullptr); | 194 base::FieldTrialList field_trials(nullptr); |
185 scoped_refptr<base::FieldTrial> disabled_trial = CreateFieldTrial("Disabled"); | 195 scoped_refptr<base::FieldTrial> disabled_trial = CreateFieldTrial("Disabled"); |
186 { | 196 { |
187 // A default-enabled switch should be disabled because of the field trial. | 197 // A default-enabled switch should be disabled because of the field trial. |
188 FeatureSwitch default_enabled_switch(&command_line_, kSwitchName, | 198 FeatureSwitch default_enabled_switch( |
189 kFieldTrialName, | 199 &command_line_, kSwitchName, |
190 FeatureSwitch::DEFAULT_ENABLED); | 200 std::vector<std::string>(1, kFieldTrialName), |
| 201 FeatureSwitch::DEFAULT_ENABLED); |
191 EXPECT_FALSE(default_enabled_switch.IsEnabled()); | 202 EXPECT_FALSE(default_enabled_switch.IsEnabled()); |
192 // Scoped overrides override everything. | 203 // Scoped overrides override everything. |
193 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, | 204 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, |
194 true); | 205 true); |
195 EXPECT_TRUE(default_enabled_switch.IsEnabled()); | 206 EXPECT_TRUE(default_enabled_switch.IsEnabled()); |
196 } | 207 } |
197 | 208 |
198 { | 209 { |
199 // A default-disabled switch should remain disabled. | 210 // A default-disabled switch should remain disabled. |
200 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, | 211 FeatureSwitch default_disabled_switch( |
201 kFieldTrialName, | 212 &command_line_, kSwitchName, |
202 FeatureSwitch::DEFAULT_DISABLED); | 213 std::vector<std::string>(1, kFieldTrialName), |
| 214 FeatureSwitch::DEFAULT_DISABLED); |
203 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 215 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
204 // Scoped overrides override everything. | 216 // Scoped overrides override everything. |
205 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, | 217 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, |
206 true); | 218 true); |
207 EXPECT_TRUE(default_disabled_switch.IsEnabled()); | 219 EXPECT_TRUE(default_disabled_switch.IsEnabled()); |
208 } | 220 } |
209 } | 221 } |
| 222 |
| 223 TEST_F(FeatureSwitchEnabledTest, |
| 224 TrueFieldTrialValueAndTrueRequiredFieldTrialValue) { |
| 225 std::vector<std::string> required_trials; |
| 226 base::FieldTrialList field_trials(nullptr); |
| 227 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); |
| 228 required_trials.push_back(kFieldTrialName); |
| 229 const char* required_trial_name = "required-trial"; |
| 230 scoped_refptr<base::FieldTrial> enabled_required_trial = |
| 231 CreateFieldTrialWithName(required_trial_name, "Enabled"); |
| 232 required_trials.push_back(required_trial_name); |
| 233 FeatureSwitch trial_enabled_switch(&command_line_, kSwitchName, |
| 234 required_trials, |
| 235 FeatureSwitch::DEFAULT_DISABLED); |
| 236 EXPECT_TRUE(trial_enabled_switch.IsEnabled()); |
| 237 } |
| 238 |
| 239 TEST_F(FeatureSwitchEnabledTest, |
| 240 TrueFieldTrialValueAndFalseRequiredFieldTrialValue) { |
| 241 std::vector<std::string> required_trials; |
| 242 base::FieldTrialList field_trials(nullptr); |
| 243 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); |
| 244 required_trials.push_back(kFieldTrialName); |
| 245 const char* required_trial_name = "required-trial"; |
| 246 scoped_refptr<base::FieldTrial> enabled_required_trial = |
| 247 CreateFieldTrialWithName(required_trial_name, "Disabled"); |
| 248 required_trials.push_back(required_trial_name); |
| 249 FeatureSwitch trial_enabled_switch(&command_line_, kSwitchName, |
| 250 required_trials, |
| 251 FeatureSwitch::DEFAULT_DISABLED); |
| 252 EXPECT_FALSE(trial_enabled_switch.IsEnabled()); |
| 253 } |
OLD | NEW |