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.
4 
5 #include "extensions/common/feature_switch.h"
6 
7 #include "base/command_line.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 using extensions::FeatureSwitch;
11 
12 namespace {
13 
14 const char kSwitchName[] = "test-switch";
15 
16 template <FeatureSwitch::DefaultValue T>
17 class FeatureSwitchTest : public testing::Test {
18  public:
FeatureSwitchTest()19   FeatureSwitchTest()
20       : command_line_(base::CommandLine::NO_PROGRAM),
21         feature_(&command_line_, kSwitchName, T) {}
22 
23  protected:
24   base::CommandLine command_line_;
25   FeatureSwitch feature_;
26 };
27 
28 typedef FeatureSwitchTest<FeatureSwitch::DEFAULT_DISABLED>
29     FeatureSwitchDisabledTest;
30 typedef FeatureSwitchTest<FeatureSwitch::DEFAULT_ENABLED>
31     FeatureSwitchEnabledTest;
32 
33 }  // namespace
34 
TEST_F(FeatureSwitchDisabledTest,NoSwitchValue)35 TEST_F(FeatureSwitchDisabledTest, NoSwitchValue) {
36   EXPECT_FALSE(feature_.IsEnabled());
37 }
38 
TEST_F(FeatureSwitchDisabledTest,FalseSwitchValue)39 TEST_F(FeatureSwitchDisabledTest, FalseSwitchValue) {
40   command_line_.AppendSwitchASCII(kSwitchName, "0");
41   EXPECT_FALSE(feature_.IsEnabled());
42 }
43 
TEST_F(FeatureSwitchDisabledTest,GibberishSwitchValue)44 TEST_F(FeatureSwitchDisabledTest, GibberishSwitchValue) {
45   command_line_.AppendSwitchASCII(kSwitchName, "monkey");
46   EXPECT_FALSE(feature_.IsEnabled());
47 }
48 
TEST_F(FeatureSwitchDisabledTest,Override)49 TEST_F(FeatureSwitchDisabledTest, Override) {
50   {
51     FeatureSwitch::ScopedOverride override(&feature_, false);
52     EXPECT_FALSE(feature_.IsEnabled());
53   }
54   EXPECT_FALSE(feature_.IsEnabled());
55 
56   {
57     FeatureSwitch::ScopedOverride override(&feature_, true);
58     EXPECT_TRUE(feature_.IsEnabled());
59   }
60   EXPECT_FALSE(feature_.IsEnabled());
61 }
62 
TEST_F(FeatureSwitchDisabledTest,TrueSwitchValue)63 TEST_F(FeatureSwitchDisabledTest, TrueSwitchValue) {
64   command_line_.AppendSwitchASCII(kSwitchName, "1");
65   EXPECT_TRUE(feature_.IsEnabled());
66 
67   {
68     FeatureSwitch::ScopedOverride override(&feature_, false);
69     EXPECT_FALSE(feature_.IsEnabled());
70   }
71   EXPECT_TRUE(feature_.IsEnabled());
72 
73   {
74     FeatureSwitch::ScopedOverride override(&feature_, true);
75     EXPECT_TRUE(feature_.IsEnabled());
76   }
77   EXPECT_TRUE(feature_.IsEnabled());
78 }
79 
TEST_F(FeatureSwitchDisabledTest,TrimSwitchValue)80 TEST_F(FeatureSwitchDisabledTest, TrimSwitchValue) {
81   command_line_.AppendSwitchASCII(kSwitchName, " \t  1\n  ");
82   EXPECT_TRUE(feature_.IsEnabled());
83 }
84 
TEST_F(FeatureSwitchEnabledTest,NoSwitchValue)85 TEST_F(FeatureSwitchEnabledTest, NoSwitchValue) {
86   EXPECT_TRUE(feature_.IsEnabled());
87 }
88 
TEST_F(FeatureSwitchEnabledTest,TrueSwitchValue)89 TEST_F(FeatureSwitchEnabledTest, TrueSwitchValue) {
90   command_line_.AppendSwitchASCII(kSwitchName, "1");
91   EXPECT_TRUE(feature_.IsEnabled());
92 }
93 
TEST_F(FeatureSwitchEnabledTest,GibberishSwitchValue)94 TEST_F(FeatureSwitchEnabledTest, GibberishSwitchValue) {
95   command_line_.AppendSwitchASCII(kSwitchName, "monkey");
96   EXPECT_TRUE(feature_.IsEnabled());
97 }
98 
TEST_F(FeatureSwitchEnabledTest,Override)99 TEST_F(FeatureSwitchEnabledTest, Override) {
100   {
101     FeatureSwitch::ScopedOverride override(&feature_, true);
102     EXPECT_TRUE(feature_.IsEnabled());
103   }
104   EXPECT_TRUE(feature_.IsEnabled());
105 
106   {
107     FeatureSwitch::ScopedOverride override(&feature_, false);
108     EXPECT_FALSE(feature_.IsEnabled());
109   }
110   EXPECT_TRUE(feature_.IsEnabled());
111 }
112 
TEST_F(FeatureSwitchEnabledTest,FalseSwitchValue)113 TEST_F(FeatureSwitchEnabledTest, FalseSwitchValue) {
114   command_line_.AppendSwitchASCII(kSwitchName, "0");
115   EXPECT_FALSE(feature_.IsEnabled());
116 
117   {
118     FeatureSwitch::ScopedOverride override(&feature_, true);
119     EXPECT_TRUE(feature_.IsEnabled());
120   }
121   EXPECT_FALSE(feature_.IsEnabled());
122 
123   {
124     FeatureSwitch::ScopedOverride override(&feature_, false);
125     EXPECT_FALSE(feature_.IsEnabled());
126   }
127   EXPECT_FALSE(feature_.IsEnabled());
128 }
129 
TEST_F(FeatureSwitchEnabledTest,TrimSwitchValue)130 TEST_F(FeatureSwitchEnabledTest, TrimSwitchValue) {
131   command_line_.AppendSwitchASCII(kSwitchName, "\t\t 0 \n");
132   EXPECT_FALSE(feature_.IsEnabled());
133 }
134