| 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/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 using extensions::FeatureSwitch; | 11 using extensions::FeatureSwitch; |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 const char kSwitchName[] = "test-switch"; | 15 const char kSwitchName[] = "test-switch"; |
| 16 const char kFieldTrialName[] = "field-trial"; | 16 const char kFieldTrialName[] = "field-trial"; |
| 17 | 17 |
| 18 // Create and register a field trial named |field_trial_name| that will always | 18 // Create and register a field trial that will always return the given |
| 19 // return the given |group_name|. | 19 // |group_name|. |
| 20 scoped_refptr<base::FieldTrial> CreateFieldTrialWithName( | 20 scoped_refptr<base::FieldTrial> CreateFieldTrial( |
| 21 const std::string& field_trial_name, | |
| 22 const std::string& group_name) { | 21 const std::string& group_name) { |
| 23 const int kTotalProbability = 10; | 22 const int kTotalProbability = 10; |
| 24 // Note: This code will probably fail in the year 5000. But all the cycles we | 23 // 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 | 24 // save in the next 3000 years by not determining the current year will be |
| 26 // worth it. | 25 // worth it. |
| 27 scoped_refptr<base::FieldTrial> trial = | 26 scoped_refptr<base::FieldTrial> trial = |
| 28 base::FieldTrialList::FactoryGetFieldTrial( | 27 base::FieldTrialList::FactoryGetFieldTrial( |
| 29 field_trial_name, kTotalProbability, "default", 5000, 1, 1, | 28 kFieldTrialName, kTotalProbability, "default", 5000, 1, 1, |
| 30 base::FieldTrial::SESSION_RANDOMIZED, nullptr); | 29 base::FieldTrial::SESSION_RANDOMIZED, nullptr); |
| 31 trial->AppendGroup(group_name, kTotalProbability); | 30 trial->AppendGroup(group_name, kTotalProbability); |
| 32 return trial; | 31 return trial; |
| 33 } | 32 } |
| 34 | 33 |
| 35 // Create and register a field trial that will always return the given | |
| 36 // |group_name|. | |
| 37 scoped_refptr<base::FieldTrial> CreateFieldTrial( | |
| 38 const std::string& group_name) { | |
| 39 return CreateFieldTrialWithName(kFieldTrialName, group_name); | |
| 40 } | |
| 41 | |
| 42 template<FeatureSwitch::DefaultValue T> | 34 template<FeatureSwitch::DefaultValue T> |
| 43 class FeatureSwitchTest : public testing::Test { | 35 class FeatureSwitchTest : public testing::Test { |
| 44 public: | 36 public: |
| 45 FeatureSwitchTest() | 37 FeatureSwitchTest() |
| 46 : command_line_(base::CommandLine::NO_PROGRAM), | 38 : command_line_(base::CommandLine::NO_PROGRAM), |
| 47 feature_(&command_line_, kSwitchName, T) {} | 39 feature_(&command_line_, kSwitchName, T) {} |
| 48 protected: | 40 protected: |
| 49 base::CommandLine command_line_; | 41 base::CommandLine command_line_; |
| 50 FeatureSwitch feature_; | 42 FeatureSwitch feature_; |
| 51 }; | 43 }; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 command_line_.AppendSwitchASCII(kSwitchName, "\t\t 0 \n"); | 148 command_line_.AppendSwitchASCII(kSwitchName, "\t\t 0 \n"); |
| 157 EXPECT_FALSE(feature_.IsEnabled()); | 149 EXPECT_FALSE(feature_.IsEnabled()); |
| 158 } | 150 } |
| 159 | 151 |
| 160 TEST_F(FeatureSwitchEnabledTest, TrueFieldTrialValue) { | 152 TEST_F(FeatureSwitchEnabledTest, TrueFieldTrialValue) { |
| 161 // Construct a fake field trial that defaults to the group "Enabled". | 153 // Construct a fake field trial that defaults to the group "Enabled". |
| 162 base::FieldTrialList field_trials(nullptr); | 154 base::FieldTrialList field_trials(nullptr); |
| 163 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); | 155 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); |
| 164 { | 156 { |
| 165 // A default-enabled switch should be enabled (naturally). | 157 // A default-enabled switch should be enabled (naturally). |
| 166 FeatureSwitch default_enabled_switch( | 158 FeatureSwitch default_enabled_switch(&command_line_, kSwitchName, |
| 167 &command_line_, kSwitchName, | 159 kFieldTrialName, |
| 168 std::vector<std::string>(1, kFieldTrialName), | 160 FeatureSwitch::DEFAULT_ENABLED); |
| 169 FeatureSwitch::DEFAULT_ENABLED); | |
| 170 EXPECT_TRUE(default_enabled_switch.IsEnabled()); | 161 EXPECT_TRUE(default_enabled_switch.IsEnabled()); |
| 171 // Scoped overrides override everything. | 162 // Scoped overrides override everything. |
| 172 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, | 163 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, |
| 173 false); | 164 false); |
| 174 EXPECT_FALSE(default_enabled_switch.IsEnabled()); | 165 EXPECT_FALSE(default_enabled_switch.IsEnabled()); |
| 175 } | 166 } |
| 176 | 167 |
| 177 { | 168 { |
| 178 // A default-disabled switch should be enabled because of the field trial. | 169 // A default-disabled switch should be enabled because of the field trial. |
| 179 FeatureSwitch default_disabled_switch( | 170 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, |
| 180 &command_line_, kSwitchName, | 171 kFieldTrialName, |
| 181 std::vector<std::string>(1, kFieldTrialName), | 172 FeatureSwitch::DEFAULT_DISABLED); |
| 182 FeatureSwitch::DEFAULT_DISABLED); | |
| 183 EXPECT_TRUE(default_disabled_switch.IsEnabled()); | 173 EXPECT_TRUE(default_disabled_switch.IsEnabled()); |
| 184 // Scoped overrides override everything. | 174 // Scoped overrides override everything. |
| 185 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, | 175 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, |
| 186 false); | 176 false); |
| 187 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 177 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
| 188 } | 178 } |
| 189 } | 179 } |
| 190 | 180 |
| 191 TEST_F(FeatureSwitchEnabledTest, TrueFieldTrialDogfoodValue) { | 181 TEST_F(FeatureSwitchEnabledTest, TrueFieldTrialDogfoodValue) { |
| 192 // Construct a fake field trial that defaults to the group "Enabled_Dogfood". | 182 // Construct a fake field trial that defaults to the group "Enabled_Dogfood". |
| 193 base::FieldTrialList field_trials(nullptr); | 183 base::FieldTrialList field_trials(nullptr); |
| 194 scoped_refptr<base::FieldTrial> enabled_trial = | 184 scoped_refptr<base::FieldTrial> enabled_trial = |
| 195 CreateFieldTrial("Enabled_Dogfood"); | 185 CreateFieldTrial("Enabled_Dogfood"); |
| 196 { | 186 { |
| 197 // A default-enabled switch should be enabled (naturally). | 187 // A default-enabled switch should be enabled (naturally). |
| 198 FeatureSwitch default_enabled_switch( | 188 FeatureSwitch default_enabled_switch(&command_line_, kSwitchName, |
| 199 &command_line_, kSwitchName, | 189 kFieldTrialName, |
| 200 std::vector<std::string>(1, kFieldTrialName), | 190 FeatureSwitch::DEFAULT_ENABLED); |
| 201 FeatureSwitch::DEFAULT_ENABLED); | |
| 202 EXPECT_TRUE(default_enabled_switch.IsEnabled()); | 191 EXPECT_TRUE(default_enabled_switch.IsEnabled()); |
| 203 // Scoped overrides override everything. | 192 // Scoped overrides override everything. |
| 204 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, | 193 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, |
| 205 false); | 194 false); |
| 206 EXPECT_FALSE(default_enabled_switch.IsEnabled()); | 195 EXPECT_FALSE(default_enabled_switch.IsEnabled()); |
| 207 } | 196 } |
| 208 | 197 |
| 209 { | 198 { |
| 210 // A default-disabled switch should be enabled because of the field trial. | 199 // A default-disabled switch should be enabled because of the field trial. |
| 211 FeatureSwitch default_disabled_switch( | 200 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, |
| 212 &command_line_, kSwitchName, | 201 kFieldTrialName, |
| 213 std::vector<std::string>(1, kFieldTrialName), | 202 FeatureSwitch::DEFAULT_DISABLED); |
| 214 FeatureSwitch::DEFAULT_DISABLED); | |
| 215 EXPECT_TRUE(default_disabled_switch.IsEnabled()); | 203 EXPECT_TRUE(default_disabled_switch.IsEnabled()); |
| 216 // Scoped overrides override everything. | 204 // Scoped overrides override everything. |
| 217 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, | 205 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, |
| 218 false); | 206 false); |
| 219 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 207 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
| 220 } | 208 } |
| 221 } | 209 } |
| 222 | 210 |
| 223 TEST_F(FeatureSwitchEnabledTest, FalseFieldTrialValue) { | 211 TEST_F(FeatureSwitchEnabledTest, FalseFieldTrialValue) { |
| 224 // Construct a fake field trial that defaults to the group "Disabled". | 212 // Construct a fake field trial that defaults to the group "Disabled". |
| 225 base::FieldTrialList field_trials(nullptr); | 213 base::FieldTrialList field_trials(nullptr); |
| 226 scoped_refptr<base::FieldTrial> disabled_trial = CreateFieldTrial("Disabled"); | 214 scoped_refptr<base::FieldTrial> disabled_trial = CreateFieldTrial("Disabled"); |
| 227 { | 215 { |
| 228 // A default-enabled switch should be disabled because of the field trial. | 216 // A default-enabled switch should be disabled because of the field trial. |
| 229 FeatureSwitch default_enabled_switch( | 217 FeatureSwitch default_enabled_switch(&command_line_, kSwitchName, |
| 230 &command_line_, kSwitchName, | 218 kFieldTrialName, |
| 231 std::vector<std::string>(1, kFieldTrialName), | 219 FeatureSwitch::DEFAULT_ENABLED); |
| 232 FeatureSwitch::DEFAULT_ENABLED); | |
| 233 EXPECT_FALSE(default_enabled_switch.IsEnabled()); | 220 EXPECT_FALSE(default_enabled_switch.IsEnabled()); |
| 234 // Scoped overrides override everything. | 221 // Scoped overrides override everything. |
| 235 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, | 222 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, |
| 236 true); | 223 true); |
| 237 EXPECT_TRUE(default_enabled_switch.IsEnabled()); | 224 EXPECT_TRUE(default_enabled_switch.IsEnabled()); |
| 238 } | 225 } |
| 239 | 226 |
| 240 { | 227 { |
| 241 // A default-disabled switch should remain disabled. | 228 // A default-disabled switch should remain disabled. |
| 242 FeatureSwitch default_disabled_switch( | 229 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, |
| 243 &command_line_, kSwitchName, | 230 kFieldTrialName, |
| 244 std::vector<std::string>(1, kFieldTrialName), | 231 FeatureSwitch::DEFAULT_DISABLED); |
| 245 FeatureSwitch::DEFAULT_DISABLED); | |
| 246 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 232 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
| 247 // Scoped overrides override everything. | 233 // Scoped overrides override everything. |
| 248 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, | 234 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, |
| 249 true); | 235 true); |
| 250 EXPECT_TRUE(default_disabled_switch.IsEnabled()); | 236 EXPECT_TRUE(default_disabled_switch.IsEnabled()); |
| 251 } | 237 } |
| 252 } | 238 } |
| 253 | 239 |
| 254 TEST_F(FeatureSwitchEnabledTest, FalseFieldTrialDogfoodValue) { | 240 TEST_F(FeatureSwitchEnabledTest, FalseFieldTrialDogfoodValue) { |
| 255 // Construct a fake field trial that defaults to the group "Disabled_Dogfood". | 241 // Construct a fake field trial that defaults to the group "Disabled_Dogfood". |
| 256 base::FieldTrialList field_trials(nullptr); | 242 base::FieldTrialList field_trials(nullptr); |
| 257 scoped_refptr<base::FieldTrial> disabled_trial = | 243 scoped_refptr<base::FieldTrial> disabled_trial = |
| 258 CreateFieldTrial("Disabled_Dogfood"); | 244 CreateFieldTrial("Disabled_Dogfood"); |
| 259 { | 245 { |
| 260 // A default-enabled switch should be disabled because of the field trial. | 246 // A default-enabled switch should be disabled because of the field trial. |
| 261 FeatureSwitch default_enabled_switch( | 247 FeatureSwitch default_enabled_switch(&command_line_, kSwitchName, |
| 262 &command_line_, kSwitchName, | 248 kFieldTrialName, |
| 263 std::vector<std::string>(1, kFieldTrialName), | 249 FeatureSwitch::DEFAULT_ENABLED); |
| 264 FeatureSwitch::DEFAULT_ENABLED); | |
| 265 EXPECT_FALSE(default_enabled_switch.IsEnabled()); | 250 EXPECT_FALSE(default_enabled_switch.IsEnabled()); |
| 266 } | 251 } |
| 267 | 252 |
| 268 { | 253 { |
| 269 // A default-disabled switch should remain disabled. | 254 // A default-disabled switch should remain disabled. |
| 270 FeatureSwitch default_disabled_switch( | 255 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, |
| 271 &command_line_, kSwitchName, | 256 kFieldTrialName, |
| 272 std::vector<std::string>(1, kFieldTrialName), | 257 FeatureSwitch::DEFAULT_DISABLED); |
| 273 FeatureSwitch::DEFAULT_DISABLED); | |
| 274 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 258 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
| 275 } | 259 } |
| 276 } | 260 } |
| 277 | 261 |
| 278 TEST_F(FeatureSwitchEnabledTest, InvalidGroupFieldTrial) { | 262 TEST_F(FeatureSwitchEnabledTest, InvalidGroupFieldTrial) { |
| 279 // Construct a fake field trial that defaults to the group "InvalidGroup". | 263 // Construct a fake field trial that defaults to the group "InvalidGroup". |
| 280 base::FieldTrialList field_trials(nullptr); | 264 base::FieldTrialList field_trials(nullptr); |
| 281 scoped_refptr<base::FieldTrial> disabled_trial = | 265 scoped_refptr<base::FieldTrial> disabled_trial = |
| 282 CreateFieldTrial("InvalidGroup"); | 266 CreateFieldTrial("InvalidGroup"); |
| 283 { | 267 { |
| 284 // A default-enabled switch should be enabled (the group has no effect). | 268 // A default-enabled switch should be enabled (the group has no effect). |
| 285 FeatureSwitch default_enabled_switch( | 269 FeatureSwitch default_enabled_switch(&command_line_, kSwitchName, |
| 286 &command_line_, kSwitchName, | 270 kFieldTrialName, |
| 287 std::vector<std::string>(1, kFieldTrialName), | 271 FeatureSwitch::DEFAULT_ENABLED); |
| 288 FeatureSwitch::DEFAULT_ENABLED); | |
| 289 EXPECT_TRUE(default_enabled_switch.IsEnabled()); | 272 EXPECT_TRUE(default_enabled_switch.IsEnabled()); |
| 290 } | 273 } |
| 291 | 274 |
| 292 { | 275 { |
| 293 // A default-disabled switch should remain disabled. | 276 // A default-disabled switch should remain disabled. |
| 294 FeatureSwitch default_disabled_switch( | 277 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, |
| 295 &command_line_, kSwitchName, | 278 kFieldTrialName, |
| 296 std::vector<std::string>(1, kFieldTrialName), | 279 FeatureSwitch::DEFAULT_DISABLED); |
| 297 FeatureSwitch::DEFAULT_DISABLED); | |
| 298 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 280 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
| 299 } | 281 } |
| 300 } | 282 } |
| 301 | |
| 302 TEST_F(FeatureSwitchEnabledTest, | |
| 303 TrueFieldTrialValueAndTrueRequiredFieldTrialValue) { | |
| 304 std::vector<std::string> required_trials; | |
| 305 base::FieldTrialList field_trials(nullptr); | |
| 306 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); | |
| 307 required_trials.push_back(kFieldTrialName); | |
| 308 const char* required_trial_name = "required-trial"; | |
| 309 scoped_refptr<base::FieldTrial> enabled_required_trial = | |
| 310 CreateFieldTrialWithName(required_trial_name, "Enabled"); | |
| 311 required_trials.push_back(required_trial_name); | |
| 312 FeatureSwitch trial_enabled_switch(&command_line_, kSwitchName, | |
| 313 required_trials, | |
| 314 FeatureSwitch::DEFAULT_DISABLED); | |
| 315 EXPECT_TRUE(trial_enabled_switch.IsEnabled()); | |
| 316 } | |
| 317 | |
| 318 TEST_F(FeatureSwitchEnabledTest, | |
| 319 TrueFieldTrialValueAndFalseRequiredFieldTrialValue) { | |
| 320 std::vector<std::string> required_trials; | |
| 321 base::FieldTrialList field_trials(nullptr); | |
| 322 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); | |
| 323 required_trials.push_back(kFieldTrialName); | |
| 324 const char* required_trial_name = "required-trial"; | |
| 325 scoped_refptr<base::FieldTrial> enabled_required_trial = | |
| 326 CreateFieldTrialWithName(required_trial_name, "Disabled"); | |
| 327 required_trials.push_back(required_trial_name); | |
| 328 FeatureSwitch trial_enabled_switch(&command_line_, kSwitchName, | |
| 329 required_trials, | |
| 330 FeatureSwitch::DEFAULT_DISABLED); | |
| 331 EXPECT_FALSE(trial_enabled_switch.IsEnabled()); | |
| 332 } | |
| OLD | NEW |