| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/feature_list.h" | 5 #include "base/feature_list.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 8 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 13 |
| 12 namespace base { | 14 namespace base { |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 const char kFeatureOnByDefaultName[] = "OnByDefault"; | 18 const char kFeatureOnByDefaultName[] = "OnByDefault"; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 28 class FeatureListTest : public testing::Test { | 30 class FeatureListTest : public testing::Test { |
| 29 public: | 31 public: |
| 30 FeatureListTest() : feature_list_(nullptr) { | 32 FeatureListTest() : feature_list_(nullptr) { |
| 31 RegisterFeatureListInstance(make_scoped_ptr(new FeatureList)); | 33 RegisterFeatureListInstance(make_scoped_ptr(new FeatureList)); |
| 32 } | 34 } |
| 33 ~FeatureListTest() override { ClearFeatureListInstance(); } | 35 ~FeatureListTest() override { ClearFeatureListInstance(); } |
| 34 | 36 |
| 35 void RegisterFeatureListInstance(scoped_ptr<FeatureList> feature_list) { | 37 void RegisterFeatureListInstance(scoped_ptr<FeatureList> feature_list) { |
| 36 FeatureList::ClearInstanceForTesting(); | 38 FeatureList::ClearInstanceForTesting(); |
| 37 feature_list_ = feature_list.get(); | 39 feature_list_ = feature_list.get(); |
| 38 FeatureList::SetInstance(feature_list.Pass()); | 40 FeatureList::SetInstance(std::move(feature_list)); |
| 39 } | 41 } |
| 40 void ClearFeatureListInstance() { | 42 void ClearFeatureListInstance() { |
| 41 FeatureList::ClearInstanceForTesting(); | 43 FeatureList::ClearInstanceForTesting(); |
| 42 feature_list_ = nullptr; | 44 feature_list_ = nullptr; |
| 43 } | 45 } |
| 44 | 46 |
| 45 FeatureList* feature_list() { return feature_list_; } | 47 FeatureList* feature_list() { return feature_list_; } |
| 46 | 48 |
| 47 private: | 49 private: |
| 48 // Weak. Owned by the FeatureList::SetInstance(). | 50 // Weak. Owned by the FeatureList::SetInstance(). |
| (...skipping 26 matching lines...) Expand all Loading... |
| 75 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 77 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| 76 const auto& test_case = test_cases[i]; | 78 const auto& test_case = test_cases[i]; |
| 77 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: [%s] [%s]", i, | 79 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: [%s] [%s]", i, |
| 78 test_case.enable_features, | 80 test_case.enable_features, |
| 79 test_case.disable_features)); | 81 test_case.disable_features)); |
| 80 | 82 |
| 81 ClearFeatureListInstance(); | 83 ClearFeatureListInstance(); |
| 82 scoped_ptr<FeatureList> feature_list(new FeatureList); | 84 scoped_ptr<FeatureList> feature_list(new FeatureList); |
| 83 feature_list->InitializeFromCommandLine(test_case.enable_features, | 85 feature_list->InitializeFromCommandLine(test_case.enable_features, |
| 84 test_case.disable_features); | 86 test_case.disable_features); |
| 85 RegisterFeatureListInstance(feature_list.Pass()); | 87 RegisterFeatureListInstance(std::move(feature_list)); |
| 86 | 88 |
| 87 EXPECT_EQ(test_case.expected_feature_on_state, | 89 EXPECT_EQ(test_case.expected_feature_on_state, |
| 88 FeatureList::IsEnabled(kFeatureOnByDefault)) | 90 FeatureList::IsEnabled(kFeatureOnByDefault)) |
| 89 << i; | 91 << i; |
| 90 EXPECT_EQ(test_case.expected_feature_off_state, | 92 EXPECT_EQ(test_case.expected_feature_off_state, |
| 91 FeatureList::IsEnabled(kFeatureOffByDefault)) | 93 FeatureList::IsEnabled(kFeatureOffByDefault)) |
| 92 << i; | 94 << i; |
| 93 } | 95 } |
| 94 } | 96 } |
| 95 | 97 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 138 |
| 137 FieldTrialList field_trial_list(nullptr); | 139 FieldTrialList field_trial_list(nullptr); |
| 138 scoped_ptr<FeatureList> feature_list(new FeatureList); | 140 scoped_ptr<FeatureList> feature_list(new FeatureList); |
| 139 | 141 |
| 140 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("TrialExample1", "A"); | 142 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("TrialExample1", "A"); |
| 141 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("TrialExample2", "B"); | 143 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("TrialExample2", "B"); |
| 142 feature_list->RegisterFieldTrialOverride(kFeatureOnByDefaultName, | 144 feature_list->RegisterFieldTrialOverride(kFeatureOnByDefaultName, |
| 143 test_case.trial1_state, trial1); | 145 test_case.trial1_state, trial1); |
| 144 feature_list->RegisterFieldTrialOverride(kFeatureOffByDefaultName, | 146 feature_list->RegisterFieldTrialOverride(kFeatureOffByDefaultName, |
| 145 test_case.trial2_state, trial2); | 147 test_case.trial2_state, trial2); |
| 146 RegisterFeatureListInstance(feature_list.Pass()); | 148 RegisterFeatureListInstance(std::move(feature_list)); |
| 147 | 149 |
| 148 // Initially, neither trial should be active. | 150 // Initially, neither trial should be active. |
| 149 EXPECT_FALSE(FieldTrialList::IsTrialActive(trial1->trial_name())); | 151 EXPECT_FALSE(FieldTrialList::IsTrialActive(trial1->trial_name())); |
| 150 EXPECT_FALSE(FieldTrialList::IsTrialActive(trial2->trial_name())); | 152 EXPECT_FALSE(FieldTrialList::IsTrialActive(trial2->trial_name())); |
| 151 | 153 |
| 152 const bool expected_enabled_1 = | 154 const bool expected_enabled_1 = |
| 153 (test_case.trial1_state == FeatureList::OVERRIDE_ENABLE_FEATURE); | 155 (test_case.trial1_state == FeatureList::OVERRIDE_ENABLE_FEATURE); |
| 154 EXPECT_EQ(expected_enabled_1, FeatureList::IsEnabled(kFeatureOnByDefault)); | 156 EXPECT_EQ(expected_enabled_1, FeatureList::IsEnabled(kFeatureOnByDefault)); |
| 155 // The above should have activated |trial1|. | 157 // The above should have activated |trial1|. |
| 156 EXPECT_TRUE(FieldTrialList::IsTrialActive(trial1->trial_name())); | 158 EXPECT_TRUE(FieldTrialList::IsTrialActive(trial1->trial_name())); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 171 FieldTrialList field_trial_list(nullptr); | 173 FieldTrialList field_trial_list(nullptr); |
| 172 scoped_ptr<FeatureList> feature_list(new FeatureList); | 174 scoped_ptr<FeatureList> feature_list(new FeatureList); |
| 173 | 175 |
| 174 // The feature is explicitly enabled on the command-line. | 176 // The feature is explicitly enabled on the command-line. |
| 175 feature_list->InitializeFromCommandLine(kFeatureOffByDefaultName, ""); | 177 feature_list->InitializeFromCommandLine(kFeatureOffByDefaultName, ""); |
| 176 | 178 |
| 177 // But the FieldTrial would set the feature to disabled. | 179 // But the FieldTrial would set the feature to disabled. |
| 178 FieldTrial* trial = FieldTrialList::CreateFieldTrial("TrialExample2", "A"); | 180 FieldTrial* trial = FieldTrialList::CreateFieldTrial("TrialExample2", "A"); |
| 179 feature_list->RegisterFieldTrialOverride( | 181 feature_list->RegisterFieldTrialOverride( |
| 180 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, trial); | 182 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, trial); |
| 181 RegisterFeatureListInstance(feature_list.Pass()); | 183 RegisterFeatureListInstance(std::move(feature_list)); |
| 182 | 184 |
| 183 EXPECT_FALSE(FieldTrialList::IsTrialActive(trial->trial_name())); | 185 EXPECT_FALSE(FieldTrialList::IsTrialActive(trial->trial_name())); |
| 184 // Command-line should take precedence. | 186 // Command-line should take precedence. |
| 185 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); | 187 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 186 // Since the feature is on due to the command-line, and not as a result of the | 188 // Since the feature is on due to the command-line, and not as a result of the |
| 187 // field trial, the field trial should not be activated (since the Associate* | 189 // field trial, the field trial should not be activated (since the Associate* |
| 188 // API wasn't used.) | 190 // API wasn't used.) |
| 189 EXPECT_FALSE(FieldTrialList::IsTrialActive(trial->trial_name())); | 191 EXPECT_FALSE(FieldTrialList::IsTrialActive(trial->trial_name())); |
| 190 } | 192 } |
| 191 | 193 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 228 |
| 227 // Now, register a field trial to override |kFeatureOnByDefaultName| state | 229 // Now, register a field trial to override |kFeatureOnByDefaultName| state |
| 228 // and check that the function still returns false for that feature. | 230 // and check that the function still returns false for that feature. |
| 229 feature_list->RegisterFieldTrialOverride( | 231 feature_list->RegisterFieldTrialOverride( |
| 230 kFeatureOnByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, | 232 kFeatureOnByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 231 FieldTrialList::CreateFieldTrial("Trial2", "A")); | 233 FieldTrialList::CreateFieldTrial("Trial2", "A")); |
| 232 EXPECT_FALSE(feature_list->IsFeatureOverriddenFromCommandLine( | 234 EXPECT_FALSE(feature_list->IsFeatureOverriddenFromCommandLine( |
| 233 kFeatureOnByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE)); | 235 kFeatureOnByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE)); |
| 234 EXPECT_FALSE(feature_list->IsFeatureOverriddenFromCommandLine( | 236 EXPECT_FALSE(feature_list->IsFeatureOverriddenFromCommandLine( |
| 235 kFeatureOnByDefaultName, FeatureList::OVERRIDE_ENABLE_FEATURE)); | 237 kFeatureOnByDefaultName, FeatureList::OVERRIDE_ENABLE_FEATURE)); |
| 236 RegisterFeatureListInstance(feature_list.Pass()); | 238 RegisterFeatureListInstance(std::move(feature_list)); |
| 237 | 239 |
| 238 // Check the expected feature states for good measure. | 240 // Check the expected feature states for good measure. |
| 239 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); | 241 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 240 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOnByDefault)); | 242 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOnByDefault)); |
| 241 } | 243 } |
| 242 | 244 |
| 243 TEST_F(FeatureListTest, AssociateReportingFieldTrial) { | 245 TEST_F(FeatureListTest, AssociateReportingFieldTrial) { |
| 244 struct { | 246 struct { |
| 245 const char* enable_features; | 247 const char* enable_features; |
| 246 const char* disable_features; | 248 const char* disable_features; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE)) { | 288 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE)) { |
| 287 disable_trial = base::FieldTrialList::CreateFieldTrial( | 289 disable_trial = base::FieldTrialList::CreateFieldTrial( |
| 288 kTrialName, kForcedOffGroupName); | 290 kTrialName, kForcedOffGroupName); |
| 289 feature_list->AssociateReportingFieldTrial( | 291 feature_list->AssociateReportingFieldTrial( |
| 290 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, | 292 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 291 disable_trial); | 293 disable_trial); |
| 292 } | 294 } |
| 293 EXPECT_EQ(test_case.expected_enable_trial_created, enable_trial != nullptr); | 295 EXPECT_EQ(test_case.expected_enable_trial_created, enable_trial != nullptr); |
| 294 EXPECT_EQ(test_case.expected_disable_trial_created, | 296 EXPECT_EQ(test_case.expected_disable_trial_created, |
| 295 disable_trial != nullptr); | 297 disable_trial != nullptr); |
| 296 RegisterFeatureListInstance(feature_list.Pass()); | 298 RegisterFeatureListInstance(std::move(feature_list)); |
| 297 | 299 |
| 298 EXPECT_FALSE(FieldTrialList::IsTrialActive(kTrialName)); | 300 EXPECT_FALSE(FieldTrialList::IsTrialActive(kTrialName)); |
| 299 if (disable_trial) { | 301 if (disable_trial) { |
| 300 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault)); | 302 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 301 EXPECT_TRUE(FieldTrialList::IsTrialActive(kTrialName)); | 303 EXPECT_TRUE(FieldTrialList::IsTrialActive(kTrialName)); |
| 302 EXPECT_EQ(kForcedOffGroupName, disable_trial->group_name()); | 304 EXPECT_EQ(kForcedOffGroupName, disable_trial->group_name()); |
| 303 } else if (enable_trial) { | 305 } else if (enable_trial) { |
| 304 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); | 306 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 305 EXPECT_TRUE(FieldTrialList::IsTrialActive(kTrialName)); | 307 EXPECT_TRUE(FieldTrialList::IsTrialActive(kTrialName)); |
| 306 EXPECT_EQ(kForcedOnGroupName, enable_trial->group_name()); | 308 EXPECT_EQ(kForcedOnGroupName, enable_trial->group_name()); |
| 307 } | 309 } |
| 308 } | 310 } |
| 309 } | 311 } |
| 310 | 312 |
| 311 } // namespace base | 313 } // namespace base |
| OLD | NEW |