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
3 // found in the LICENSE file.
5 #include "extensions/common/feature_switch.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/field_trial.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using extensions::FeatureSwitch
;
16 const char kSwitchName
[] = "test-switch";
17 const char kFieldTrialName
[] = "field-trial";
19 // Create and register a field trial that will always return the given
21 scoped_refptr
<base::FieldTrial
> CreateFieldTrial(
22 const std::string
& group_name
) {
23 const int kTotalProbability
= 10;
24 // 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
27 scoped_refptr
<base::FieldTrial
> trial
=
28 base::FieldTrialList::FactoryGetFieldTrial(
29 kFieldTrialName
, kTotalProbability
, "default", 5000, 1, 1,
30 base::FieldTrial::SESSION_RANDOMIZED
, nullptr);
31 trial
->AppendGroup(group_name
, kTotalProbability
);
35 template<FeatureSwitch::DefaultValue T
>
36 class FeatureSwitchTest
: public testing::Test
{
39 : command_line_(base::CommandLine::NO_PROGRAM
),
40 feature_(&command_line_
, kSwitchName
, T
) {}
42 base::CommandLine command_line_
;
43 FeatureSwitch feature_
;
46 typedef FeatureSwitchTest
<FeatureSwitch::DEFAULT_DISABLED
>
47 FeatureSwitchDisabledTest
;
48 typedef FeatureSwitchTest
<FeatureSwitch::DEFAULT_ENABLED
>
49 FeatureSwitchEnabledTest
;
53 TEST_F(FeatureSwitchDisabledTest
, NoSwitchValue
) {
54 EXPECT_FALSE(feature_
.IsEnabled());
57 TEST_F(FeatureSwitchDisabledTest
, FalseSwitchValue
) {
58 command_line_
.AppendSwitchASCII(kSwitchName
, "0");
59 EXPECT_FALSE(feature_
.IsEnabled());
62 TEST_F(FeatureSwitchDisabledTest
, GibberishSwitchValue
) {
63 command_line_
.AppendSwitchASCII(kSwitchName
, "monkey");
64 EXPECT_FALSE(feature_
.IsEnabled());
67 TEST_F(FeatureSwitchDisabledTest
, Override
) {
69 FeatureSwitch::ScopedOverride
override(&feature_
, false);
70 EXPECT_FALSE(feature_
.IsEnabled());
72 EXPECT_FALSE(feature_
.IsEnabled());
75 FeatureSwitch::ScopedOverride
override(&feature_
, true);
76 EXPECT_TRUE(feature_
.IsEnabled());
78 EXPECT_FALSE(feature_
.IsEnabled());
81 TEST_F(FeatureSwitchDisabledTest
, TrueSwitchValue
) {
82 command_line_
.AppendSwitchASCII(kSwitchName
, "1");
83 EXPECT_TRUE(feature_
.IsEnabled());
86 FeatureSwitch::ScopedOverride
override(&feature_
, false);
87 EXPECT_FALSE(feature_
.IsEnabled());
89 EXPECT_TRUE(feature_
.IsEnabled());
92 FeatureSwitch::ScopedOverride
override(&feature_
, true);
93 EXPECT_TRUE(feature_
.IsEnabled());
95 EXPECT_TRUE(feature_
.IsEnabled());
98 TEST_F(FeatureSwitchDisabledTest
, TrimSwitchValue
) {
99 command_line_
.AppendSwitchASCII(kSwitchName
, " \t 1\n ");
100 EXPECT_TRUE(feature_
.IsEnabled());
103 TEST_F(FeatureSwitchEnabledTest
, NoSwitchValue
) {
104 EXPECT_TRUE(feature_
.IsEnabled());
107 TEST_F(FeatureSwitchEnabledTest
, TrueSwitchValue
) {
108 command_line_
.AppendSwitchASCII(kSwitchName
, "1");
109 EXPECT_TRUE(feature_
.IsEnabled());
112 TEST_F(FeatureSwitchEnabledTest
, GibberishSwitchValue
) {
113 command_line_
.AppendSwitchASCII(kSwitchName
, "monkey");
114 EXPECT_TRUE(feature_
.IsEnabled());
117 TEST_F(FeatureSwitchEnabledTest
, Override
) {
119 FeatureSwitch::ScopedOverride
override(&feature_
, true);
120 EXPECT_TRUE(feature_
.IsEnabled());
122 EXPECT_TRUE(feature_
.IsEnabled());
125 FeatureSwitch::ScopedOverride
override(&feature_
, false);
126 EXPECT_FALSE(feature_
.IsEnabled());
128 EXPECT_TRUE(feature_
.IsEnabled());
131 TEST_F(FeatureSwitchEnabledTest
, FalseSwitchValue
) {
132 command_line_
.AppendSwitchASCII(kSwitchName
, "0");
133 EXPECT_FALSE(feature_
.IsEnabled());
136 FeatureSwitch::ScopedOverride
override(&feature_
, true);
137 EXPECT_TRUE(feature_
.IsEnabled());
139 EXPECT_FALSE(feature_
.IsEnabled());
142 FeatureSwitch::ScopedOverride
override(&feature_
, false);
143 EXPECT_FALSE(feature_
.IsEnabled());
145 EXPECT_FALSE(feature_
.IsEnabled());
148 TEST_F(FeatureSwitchEnabledTest
, TrimSwitchValue
) {
149 command_line_
.AppendSwitchASCII(kSwitchName
, "\t\t 0 \n");
150 EXPECT_FALSE(feature_
.IsEnabled());
153 TEST_F(FeatureSwitchEnabledTest
, TrueFieldTrialValue
) {
154 // Construct a fake field trial that defaults to the group "Enabled".
155 base::FieldTrialList
field_trials(nullptr);
156 scoped_refptr
<base::FieldTrial
> enabled_trial
= CreateFieldTrial("Enabled");
158 // A default-enabled switch should be enabled (naturally).
159 FeatureSwitch
default_enabled_switch(&command_line_
, kSwitchName
,
161 FeatureSwitch::DEFAULT_ENABLED
);
162 EXPECT_TRUE(default_enabled_switch
.IsEnabled());
163 // Scoped overrides override everything.
164 FeatureSwitch::ScopedOverride
scoped_override(&default_enabled_switch
,
166 EXPECT_FALSE(default_enabled_switch
.IsEnabled());
170 // A default-disabled switch should be enabled because of the field trial.
171 FeatureSwitch
default_disabled_switch(&command_line_
, kSwitchName
,
173 FeatureSwitch::DEFAULT_DISABLED
);
174 EXPECT_TRUE(default_disabled_switch
.IsEnabled());
175 // Scoped overrides override everything.
176 FeatureSwitch::ScopedOverride
scoped_override(&default_disabled_switch
,
178 EXPECT_FALSE(default_disabled_switch
.IsEnabled());
182 TEST_F(FeatureSwitchEnabledTest
, FalseFieldTrialValue
) {
183 // Construct a fake field trial that defaults to the group "Disabled".
184 base::FieldTrialList
field_trials(nullptr);
185 scoped_refptr
<base::FieldTrial
> disabled_trial
= CreateFieldTrial("Disabled");
187 // A default-enabled switch should be disabled because of the field trial.
188 FeatureSwitch
default_enabled_switch(&command_line_
, kSwitchName
,
190 FeatureSwitch::DEFAULT_ENABLED
);
191 EXPECT_FALSE(default_enabled_switch
.IsEnabled());
192 // Scoped overrides override everything.
193 FeatureSwitch::ScopedOverride
scoped_override(&default_enabled_switch
,
195 EXPECT_TRUE(default_enabled_switch
.IsEnabled());
199 // A default-disabled switch should remain disabled.
200 FeatureSwitch
default_disabled_switch(&command_line_
, kSwitchName
,
202 FeatureSwitch::DEFAULT_DISABLED
);
203 EXPECT_FALSE(default_disabled_switch
.IsEnabled());
204 // Scoped overrides override everything.
205 FeatureSwitch::ScopedOverride
scoped_override(&default_disabled_switch
,
207 EXPECT_TRUE(default_disabled_switch
.IsEnabled());