Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / base / feature_list_unittest.cc
blobc9423ce776a7a00773dd0a2535e91a7603b95170
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"
9 namespace base {
11 namespace {
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
23 } // namespace
25 class FeatureListTest : public testing::Test {
26 public:
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_; }
43 private:
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) {
56 struct {
57 const char* enable_features;
58 const char* disable_features;
59 bool expected_feature_on_state;
60 bool expected_feature_off_state;
61 } test_cases[] = {
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))
82 << i;
83 EXPECT_EQ(test_case.expected_feature_off_state,
84 FeatureList::IsEnabled(kFeatureOffByDefault))
85 << i;
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));
108 } // namespace base