1 // Copyright 2015 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 "base/feature_list.h"
7 #include "testing/gtest/include/gtest/gtest.h"
13 const char kFeatureOnByDefaultName
[] = "OnByDefault";
14 struct Feature kFeatureOnByDefault
{
15 kFeatureOnByDefaultName
, FEATURE_ENABLED_BY_DEFAULT
18 const char kFeatureOffByDefaultName
[] = "OffByDefault";
19 struct Feature kFeatureOffByDefault
{
20 kFeatureOffByDefaultName
, FEATURE_DISABLED_BY_DEFAULT
25 class FeatureListTest
: public testing::Test
{
27 FeatureListTest() : feature_list_(nullptr) {
28 RegisterFeatureListInstance(make_scoped_ptr(new FeatureList
));
30 ~FeatureListTest() override
{ ClearFeatureListInstance(); }
32 void RegisterFeatureListInstance(scoped_ptr
<FeatureList
> feature_list
) {
33 feature_list_
= feature_list
.get();
34 FeatureList::SetInstance(feature_list
.Pass());
36 void ClearFeatureListInstance() {
37 FeatureList::ClearInstanceForTesting();
38 feature_list_
= nullptr;
41 FeatureList
* feature_list() { return feature_list_
; }
44 // Weak. Owned by the FeatureList::SetInstance().
45 FeatureList
* feature_list_
;
47 DISALLOW_COPY_AND_ASSIGN(FeatureListTest
);
50 TEST_F(FeatureListTest
, DefaultStates
) {
51 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOnByDefault
));
52 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault
));
55 TEST_F(FeatureListTest
, InitializeFromCommandLine
) {
57 const char* enable_features
;
58 const char* disable_features
;
59 bool expected_feature_on_state
;
60 bool expected_feature_off_state
;
62 {"", "", true, false},
63 {"OffByDefault", "", true, true},
64 {"OffByDefault", "OnByDefault", false, true},
65 {"OnByDefault,OffByDefault", "", true, true},
66 {"", "OnByDefault,OffByDefault", false, false},
67 // In the case an entry is both, disable takes precedence.
68 {"OnByDefault", "OnByDefault,OffByDefault", false, false},
71 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
72 const auto& test_case
= test_cases
[i
];
74 ClearFeatureListInstance();
75 scoped_ptr
<FeatureList
> feature_list(new FeatureList
);
76 feature_list
->InitializeFromCommandLine(test_case
.enable_features
,
77 test_case
.disable_features
);
78 RegisterFeatureListInstance(feature_list
.Pass());
80 EXPECT_EQ(test_case
.expected_feature_on_state
,
81 FeatureList::IsEnabled(kFeatureOnByDefault
))
83 EXPECT_EQ(test_case
.expected_feature_off_state
,
84 FeatureList::IsEnabled(kFeatureOffByDefault
))
89 TEST_F(FeatureListTest
, CheckFeatureIdentity
) {
90 // Tests that CheckFeatureIdentity() correctly detects when two different
91 // structs with the same feature name are passed to it.
93 // Call it twice for each feature at the top of the file, since the first call
94 // makes it remember the entry and the second call will verify it.
95 EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault
));
96 EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault
));
97 EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kFeatureOffByDefault
));
98 EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kFeatureOffByDefault
));
100 // Now, call it with a distinct struct for |kFeatureOnByDefaultName|, which
101 // should return false.
102 struct Feature kFeatureOnByDefault2
{
103 kFeatureOnByDefaultName
, FEATURE_ENABLED_BY_DEFAULT
105 EXPECT_FALSE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault2
));