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