OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/feature_list.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace base { |
| 10 |
| 11 namespace { |
| 12 |
| 13 const char kFeatureOnByDefaultName[] = "OnByDefault"; |
| 14 struct Feature kFeatureOnByDefault { |
| 15 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT |
| 16 }; |
| 17 |
| 18 const char kFeatureOffByDefaultName[] = "OffByDefault"; |
| 19 struct Feature kFeatureOffByDefault { |
| 20 kFeatureOffByDefaultName, FEATURE_DISABLED_BY_DEFAULT |
| 21 }; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 class FeatureListTest : public testing::Test { |
| 26 public: |
| 27 FeatureListTest() : feature_list_(nullptr) { |
| 28 RegisterFeatureListInstance(make_scoped_ptr(new FeatureList)); |
| 29 } |
| 30 ~FeatureListTest() override { ClearFeatureListInstance(); } |
| 31 |
| 32 void RegisterFeatureListInstance(scoped_ptr<FeatureList> feature_list) { |
| 33 feature_list_ = feature_list.get(); |
| 34 FeatureList::SetInstance(feature_list.Pass()); |
| 35 } |
| 36 void ClearFeatureListInstance() { |
| 37 FeatureList::ClearInstanceForTesting(); |
| 38 feature_list_ = nullptr; |
| 39 } |
| 40 |
| 41 FeatureList* feature_list() { return feature_list_; } |
| 42 |
| 43 private: |
| 44 // Weak. Owned by the FeatureList::SetInstance(). |
| 45 FeatureList* feature_list_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(FeatureListTest); |
| 48 }; |
| 49 |
| 50 TEST_F(FeatureListTest, DefaultStates) { |
| 51 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOnByDefault)); |
| 52 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 53 } |
| 54 |
| 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}, |
| 69 }; |
| 70 |
| 71 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| 72 const auto& test_case = test_cases[i]; |
| 73 |
| 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()); |
| 79 |
| 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; |
| 86 } |
| 87 } |
| 88 |
| 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. |
| 92 |
| 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)); |
| 99 |
| 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 |
| 104 }; |
| 105 EXPECT_FALSE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault2)); |
| 106 } |
| 107 |
| 108 } // namespace base |
OLD | NEW |