| 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, kFieldTrialName, |
| 161 FeatureSwitch::DEFAULT_ENABLED); | 169 std::vector<const char*>(), FeatureSwitch::DEFAULT_ENABLED); |
| 162 EXPECT_TRUE(default_enabled_switch.IsEnabled()); | 170 EXPECT_TRUE(default_enabled_switch.IsEnabled()); |
| 163 // Scoped overrides override everything. | 171 // Scoped overrides override everything. |
| 164 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, | 172 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, |
| 165 false); | 173 false); |
| 166 EXPECT_FALSE(default_enabled_switch.IsEnabled()); | 174 EXPECT_FALSE(default_enabled_switch.IsEnabled()); |
| 167 } | 175 } |
| 168 | 176 |
| 169 { | 177 { |
| 170 // A default-disabled switch should be enabled because of the field trial. | 178 // A default-disabled switch should be enabled because of the field trial. |
| 171 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, | 179 FeatureSwitch default_disabled_switch( |
| 172 kFieldTrialName, | 180 &command_line_, kSwitchName, kFieldTrialName, |
| 173 FeatureSwitch::DEFAULT_DISABLED); | 181 std::vector<const char*>(), FeatureSwitch::DEFAULT_DISABLED); |
| 174 EXPECT_TRUE(default_disabled_switch.IsEnabled()); | 182 EXPECT_TRUE(default_disabled_switch.IsEnabled()); |
| 175 // Scoped overrides override everything. | 183 // Scoped overrides override everything. |
| 176 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, | 184 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, |
| 177 false); | 185 false); |
| 178 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 186 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
| 179 } | 187 } |
| 180 } | 188 } |
| 181 | 189 |
| 182 TEST_F(FeatureSwitchEnabledTest, FalseFieldTrialValue) { | 190 TEST_F(FeatureSwitchEnabledTest, FalseFieldTrialValue) { |
| 183 // Construct a fake field trial that defaults to the group "Disabled". | 191 // Construct a fake field trial that defaults to the group "Disabled". |
| 184 base::FieldTrialList field_trials(nullptr); | 192 base::FieldTrialList field_trials(nullptr); |
| 185 scoped_refptr<base::FieldTrial> disabled_trial = CreateFieldTrial("Disabled"); | 193 scoped_refptr<base::FieldTrial> disabled_trial = CreateFieldTrial("Disabled"); |
| 186 { | 194 { |
| 187 // A default-enabled switch should be disabled because of the field trial. | 195 // A default-enabled switch should be disabled because of the field trial. |
| 188 FeatureSwitch default_enabled_switch(&command_line_, kSwitchName, | 196 FeatureSwitch default_enabled_switch( |
| 189 kFieldTrialName, | 197 &command_line_, kSwitchName, kFieldTrialName, |
| 190 FeatureSwitch::DEFAULT_ENABLED); | 198 std::vector<const char*>(), FeatureSwitch::DEFAULT_ENABLED); |
| 191 EXPECT_FALSE(default_enabled_switch.IsEnabled()); | 199 EXPECT_FALSE(default_enabled_switch.IsEnabled()); |
| 192 // Scoped overrides override everything. | 200 // Scoped overrides override everything. |
| 193 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, | 201 FeatureSwitch::ScopedOverride scoped_override(&default_enabled_switch, |
| 194 true); | 202 true); |
| 195 EXPECT_TRUE(default_enabled_switch.IsEnabled()); | 203 EXPECT_TRUE(default_enabled_switch.IsEnabled()); |
| 196 } | 204 } |
| 197 | 205 |
| 198 { | 206 { |
| 199 // A default-disabled switch should remain disabled. | 207 // A default-disabled switch should remain disabled. |
| 200 FeatureSwitch default_disabled_switch(&command_line_, kSwitchName, | 208 FeatureSwitch default_disabled_switch( |
| 201 kFieldTrialName, | 209 &command_line_, kSwitchName, kFieldTrialName, |
| 202 FeatureSwitch::DEFAULT_DISABLED); | 210 std::vector<const char*>(), FeatureSwitch::DEFAULT_DISABLED); |
| 203 EXPECT_FALSE(default_disabled_switch.IsEnabled()); | 211 EXPECT_FALSE(default_disabled_switch.IsEnabled()); |
| 204 // Scoped overrides override everything. | 212 // Scoped overrides override everything. |
| 205 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, | 213 FeatureSwitch::ScopedOverride scoped_override(&default_disabled_switch, |
| 206 true); | 214 true); |
| 207 EXPECT_TRUE(default_disabled_switch.IsEnabled()); | 215 EXPECT_TRUE(default_disabled_switch.IsEnabled()); |
| 208 } | 216 } |
| 209 } | 217 } |
| 218 |
| 219 TEST_F(FeatureSwitchEnabledTest, |
| 220 TrueFieldTrialValueAndTrueDependentFieldTrialValue) { |
| 221 const char* dependent_trial_name = "dependent-trial"; |
| 222 base::FieldTrialList field_trials(nullptr); |
| 223 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); |
| 224 scoped_refptr<base::FieldTrial> enabled_dependent_trial = |
| 225 CreateFieldTrialWithName(dependent_trial_name, "Enabled"); |
| 226 std::vector<const char*> dependent_trials; |
| 227 dependent_trials.push_back(dependent_trial_name); |
| 228 FeatureSwitch trial_enabled_switch(&command_line_, kSwitchName, |
| 229 kFieldTrialName, dependent_trials, |
| 230 FeatureSwitch::DEFAULT_DISABLED); |
| 231 EXPECT_TRUE(trial_enabled_switch.IsEnabled()); |
| 232 } |
| 233 |
| 234 TEST_F(FeatureSwitchEnabledTest, |
| 235 TrueFieldTrialValueAndFalseDependentFieldTrialValue) { |
| 236 const char* dependent_trial_name = "dependent-trial"; |
| 237 base::FieldTrialList field_trials(nullptr); |
| 238 scoped_refptr<base::FieldTrial> enabled_trial = CreateFieldTrial("Enabled"); |
| 239 scoped_refptr<base::FieldTrial> enabled_dependent_trial = |
| 240 CreateFieldTrialWithName(dependent_trial_name, "Disabled"); |
| 241 std::vector<const char*> dependent_trials; |
| 242 dependent_trials.push_back(dependent_trial_name); |
| 243 FeatureSwitch trial_enabled_switch(&command_line_, kSwitchName, |
| 244 kFieldTrialName, dependent_trials, |
| 245 FeatureSwitch::DEFAULT_DISABLED); |
| 246 EXPECT_FALSE(trial_enabled_switch.IsEnabled()); |
| 247 } |
| OLD | NEW |