Chromium Code Reviews| 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 "base/metrics/field_trial.h" | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 9 |
| 9 namespace base { | 10 namespace base { |
| 10 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 const char kFeatureOnByDefaultName[] = "OnByDefault"; | 14 const char kFeatureOnByDefaultName[] = "OnByDefault"; |
| 14 struct Feature kFeatureOnByDefault { | 15 struct Feature kFeatureOnByDefault { |
| 15 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT | 16 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT |
| 16 }; | 17 }; |
| 17 | 18 |
| 18 const char kFeatureOffByDefaultName[] = "OffByDefault"; | 19 const char kFeatureOffByDefaultName[] = "OffByDefault"; |
| 19 struct Feature kFeatureOffByDefault { | 20 struct Feature kFeatureOffByDefault { |
| 20 kFeatureOffByDefaultName, FEATURE_DISABLED_BY_DEFAULT | 21 kFeatureOffByDefaultName, FEATURE_DISABLED_BY_DEFAULT |
| 21 }; | 22 }; |
| 22 | 23 |
| 24 // Tests whether a field trial is active (i.e. group() has been called on it), | |
| 25 // using public FieldTrial API (which doesn't expose this state on the object). | |
| 26 bool IsFieldTrialActive(FieldTrial* trial) { | |
| 27 base::FieldTrial::ActiveGroups active_groups; | |
| 28 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | |
| 29 for (size_t i = 0; i < active_groups.size(); ++i) { | |
| 30 if (active_groups[i].trial_name == trial->trial_name()) | |
| 31 return true; | |
| 32 } | |
| 33 return false; | |
| 34 } | |
| 35 | |
| 23 } // namespace | 36 } // namespace |
| 24 | 37 |
| 25 class FeatureListTest : public testing::Test { | 38 class FeatureListTest : public testing::Test { |
| 26 public: | 39 public: |
| 27 FeatureListTest() : feature_list_(nullptr) { | 40 FeatureListTest() : feature_list_(nullptr) { |
| 28 RegisterFeatureListInstance(make_scoped_ptr(new FeatureList)); | 41 RegisterFeatureListInstance(make_scoped_ptr(new FeatureList)); |
| 29 } | 42 } |
| 30 ~FeatureListTest() override { ClearFeatureListInstance(); } | 43 ~FeatureListTest() override { ClearFeatureListInstance(); } |
| 31 | 44 |
| 32 void RegisterFeatureListInstance(scoped_ptr<FeatureList> feature_list) { | 45 void RegisterFeatureListInstance(scoped_ptr<FeatureList> feature_list) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kFeatureOffByDefault)); | 111 EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kFeatureOffByDefault)); |
| 99 | 112 |
| 100 // Now, call it with a distinct struct for |kFeatureOnByDefaultName|, which | 113 // Now, call it with a distinct struct for |kFeatureOnByDefaultName|, which |
| 101 // should return false. | 114 // should return false. |
| 102 struct Feature kFeatureOnByDefault2 { | 115 struct Feature kFeatureOnByDefault2 { |
| 103 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT | 116 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT |
| 104 }; | 117 }; |
| 105 EXPECT_FALSE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault2)); | 118 EXPECT_FALSE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault2)); |
| 106 } | 119 } |
| 107 | 120 |
| 121 TEST_F(FeatureListTest, FieldTrialOverrides) { | |
| 122 struct { | |
| 123 FeatureList::OverrideState trial1_state; | |
| 124 FeatureList::OverrideState trial2_state; | |
| 125 } test_cases[] = { | |
| 126 {FeatureList::OVERRIDE_DISABLE_FEATURE, | |
| 127 FeatureList::OVERRIDE_DISABLE_FEATURE}, | |
| 128 {FeatureList::OVERRIDE_DISABLE_FEATURE, | |
| 129 FeatureList::OVERRIDE_ENABLE_FEATURE}, | |
| 130 {FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 131 FeatureList::OVERRIDE_DISABLE_FEATURE}, | |
| 132 {FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 133 FeatureList::OVERRIDE_ENABLE_FEATURE}, | |
| 134 }; | |
| 135 | |
| 136 FieldTrial::ActiveGroup active_group; | |
| 137 for (size_t i = 0; i < arraysize(test_cases); i++) { | |
| 138 const auto& test_case = test_cases[i]; | |
| 139 | |
| 140 ClearFeatureListInstance(); | |
| 141 | |
| 142 FieldTrialList field_trial_list(nullptr); | |
| 143 scoped_ptr<FeatureList> feature_list(new FeatureList); | |
| 144 | |
| 145 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("TrialExample1", "A"); | |
| 146 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("TrialExample2", "B"); | |
| 147 feature_list->RegisterFieldTrialOverride(kFeatureOnByDefaultName, | |
| 148 test_case.trial1_state, trial1); | |
| 149 feature_list->RegisterFieldTrialOverride(kFeatureOffByDefaultName, | |
| 150 test_case.trial2_state, trial2); | |
| 151 RegisterFeatureListInstance(feature_list.Pass()); | |
| 152 | |
| 153 // Initially, neither trial should be active. | |
| 154 EXPECT_FALSE(IsFieldTrialActive(trial1)); | |
| 155 EXPECT_FALSE(IsFieldTrialActive(trial2)); | |
| 156 | |
| 157 const bool expected_enabled_1 = | |
| 158 (test_case.trial1_state == FeatureList::OVERRIDE_ENABLE_FEATURE); | |
| 159 EXPECT_EQ(expected_enabled_1, FeatureList::IsEnabled(kFeatureOnByDefault)); | |
| 160 // The above should have activated |trial1|. | |
| 161 EXPECT_TRUE(IsFieldTrialActive(trial1)); | |
| 162 EXPECT_FALSE(IsFieldTrialActive(trial2)); | |
| 163 | |
| 164 const bool expected_enabled_2 = | |
| 165 (test_case.trial2_state == FeatureList::OVERRIDE_ENABLE_FEATURE); | |
| 166 EXPECT_EQ(expected_enabled_2, FeatureList::IsEnabled(kFeatureOffByDefault)); | |
| 167 // The above should have activated |trial2|. | |
| 168 EXPECT_TRUE(IsFieldTrialActive(trial1)); | |
| 169 EXPECT_TRUE(IsFieldTrialActive(trial2)); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 TEST_F(FeatureListTest, CommandLineTakesPrecedenceOverFieldTrial) { | |
| 174 ClearFeatureListInstance(); | |
| 175 | |
| 176 FieldTrialList field_trial_list(nullptr); | |
| 177 scoped_ptr<FeatureList> feature_list(new FeatureList); | |
| 178 | |
| 179 // The feature is explicitly enabled on the command-line. | |
| 180 feature_list->InitializeFromCommandLine(kFeatureOffByDefaultName, ""); | |
| 181 | |
| 182 // But the FieldTrial would set the feature to disabled. | |
| 183 FieldTrial* trial = FieldTrialList::CreateFieldTrial("TrialExample2", "A"); | |
| 184 feature_list->RegisterFieldTrialOverride( | |
| 185 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, trial); | |
| 186 RegisterFeatureListInstance(feature_list.Pass()); | |
| 187 | |
| 188 EXPECT_FALSE(IsFieldTrialActive(trial)); | |
| 189 // Command-line should take precedence. | |
| 190 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); | |
| 191 // Since the feature is on due to the command-line, and not as a result of the | |
| 192 // field trial, the field trial should not be activated (since the Associate* | |
| 193 // API wasn't used.) | |
| 194 EXPECT_FALSE(IsFieldTrialActive(trial)); | |
| 195 } | |
| 196 | |
| 197 TEST_F(FeatureListTest, AssociateReportingFieldTrial) { | |
| 198 struct { | |
| 199 const char* enable_features; | |
| 200 const char* disable_features; | |
| 201 bool expected_trial1; | |
| 202 bool expected_trial2; | |
| 203 } test_cases[] = { | |
|
rkaplow
2015/09/14 21:20:12
comment on what these different elements are for
Alexei Svitkine (slow)
2015/09/14 22:03:43
Done. Also tweaked the test body to give some more
| |
| 204 {"", "", false, false}, | |
| 205 {kFeatureOffByDefaultName, "", false, true}, | |
| 206 {"", kFeatureOffByDefaultName, true, false}, | |
| 207 }; | |
| 208 | |
| 209 for (size_t i = 0; i < arraysize(test_cases); i++) { | |
| 210 const auto& test_case = test_cases[i]; | |
| 211 | |
| 212 ClearFeatureListInstance(); | |
| 213 | |
| 214 FieldTrialList field_trial_list(nullptr); | |
| 215 scoped_ptr<FeatureList> feature_list(new FeatureList); | |
| 216 feature_list->InitializeFromCommandLine(test_case.enable_features, | |
| 217 test_case.disable_features); | |
| 218 | |
| 219 const std::string trial_name = "ForcingTrial"; | |
| 220 FieldTrial* trial1 = feature_list->AssociateReportingFieldTrial( | |
| 221 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, | |
| 222 trial_name, "ForcedOff"); | |
| 223 EXPECT_EQ(test_case.expected_trial1, trial1 != nullptr); | |
| 224 FieldTrial* trial2 = feature_list->AssociateReportingFieldTrial( | |
| 225 kFeatureOffByDefaultName, FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 226 trial_name, "ForcedOn"); | |
| 227 EXPECT_EQ(test_case.expected_trial2, trial2 != nullptr); | |
| 228 RegisterFeatureListInstance(feature_list.Pass()); | |
| 229 | |
| 230 if (trial1) { | |
| 231 EXPECT_FALSE(IsFieldTrialActive(trial1)); | |
| 232 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault)); | |
| 233 EXPECT_TRUE(IsFieldTrialActive(trial1)); | |
| 234 EXPECT_EQ("ForcedOff", trial1->group_name()); | |
| 235 } else if (trial2) { | |
| 236 EXPECT_FALSE(IsFieldTrialActive(trial2)); | |
| 237 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); | |
| 238 EXPECT_TRUE(IsFieldTrialActive(trial2)); | |
| 239 EXPECT_EQ("ForcedOn", trial2->group_name()); | |
| 240 } | |
| 241 } | |
| 242 } | |
| 243 | |
| 108 } // namespace base | 244 } // namespace base |
| OLD | NEW |