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/format_macros.h" |
| 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/stringprintf.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
8 | 11 |
9 namespace base { | 12 namespace base { |
10 | 13 |
11 namespace { | 14 namespace { |
12 | 15 |
13 const char kFeatureOnByDefaultName[] = "OnByDefault"; | 16 const char kFeatureOnByDefaultName[] = "OnByDefault"; |
14 struct Feature kFeatureOnByDefault { | 17 struct Feature kFeatureOnByDefault { |
15 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT | 18 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT |
16 }; | 19 }; |
17 | 20 |
18 const char kFeatureOffByDefaultName[] = "OffByDefault"; | 21 const char kFeatureOffByDefaultName[] = "OffByDefault"; |
19 struct Feature kFeatureOffByDefault { | 22 struct Feature kFeatureOffByDefault { |
20 kFeatureOffByDefaultName, FEATURE_DISABLED_BY_DEFAULT | 23 kFeatureOffByDefaultName, FEATURE_DISABLED_BY_DEFAULT |
21 }; | 24 }; |
22 | 25 |
| 26 // Tests whether a field trial is active (i.e. group() has been called on it), |
| 27 // using public FieldTrial API (which doesn't expose this state on the object). |
| 28 bool IsFieldTrialActive(FieldTrial* trial) { |
| 29 base::FieldTrial::ActiveGroups active_groups; |
| 30 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
| 31 for (size_t i = 0; i < active_groups.size(); ++i) { |
| 32 if (active_groups[i].trial_name == trial->trial_name()) |
| 33 return true; |
| 34 } |
| 35 return false; |
| 36 } |
| 37 |
23 } // namespace | 38 } // namespace |
24 | 39 |
25 class FeatureListTest : public testing::Test { | 40 class FeatureListTest : public testing::Test { |
26 public: | 41 public: |
27 FeatureListTest() : feature_list_(nullptr) { | 42 FeatureListTest() : feature_list_(nullptr) { |
28 RegisterFeatureListInstance(make_scoped_ptr(new FeatureList)); | 43 RegisterFeatureListInstance(make_scoped_ptr(new FeatureList)); |
29 } | 44 } |
30 ~FeatureListTest() override { ClearFeatureListInstance(); } | 45 ~FeatureListTest() override { ClearFeatureListInstance(); } |
31 | 46 |
32 void RegisterFeatureListInstance(scoped_ptr<FeatureList> feature_list) { | 47 void RegisterFeatureListInstance(scoped_ptr<FeatureList> feature_list) { |
(...skipping 30 matching lines...) Expand all Loading... |
63 {"OffByDefault", "", true, true}, | 78 {"OffByDefault", "", true, true}, |
64 {"OffByDefault", "OnByDefault", false, true}, | 79 {"OffByDefault", "OnByDefault", false, true}, |
65 {"OnByDefault,OffByDefault", "", true, true}, | 80 {"OnByDefault,OffByDefault", "", true, true}, |
66 {"", "OnByDefault,OffByDefault", false, false}, | 81 {"", "OnByDefault,OffByDefault", false, false}, |
67 // In the case an entry is both, disable takes precedence. | 82 // In the case an entry is both, disable takes precedence. |
68 {"OnByDefault", "OnByDefault,OffByDefault", false, false}, | 83 {"OnByDefault", "OnByDefault,OffByDefault", false, false}, |
69 }; | 84 }; |
70 | 85 |
71 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 86 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
72 const auto& test_case = test_cases[i]; | 87 const auto& test_case = test_cases[i]; |
| 88 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: [%s] [%s]", i, |
| 89 test_case.enable_features, |
| 90 test_case.disable_features)); |
73 | 91 |
74 ClearFeatureListInstance(); | 92 ClearFeatureListInstance(); |
75 scoped_ptr<FeatureList> feature_list(new FeatureList); | 93 scoped_ptr<FeatureList> feature_list(new FeatureList); |
76 feature_list->InitializeFromCommandLine(test_case.enable_features, | 94 feature_list->InitializeFromCommandLine(test_case.enable_features, |
77 test_case.disable_features); | 95 test_case.disable_features); |
78 RegisterFeatureListInstance(feature_list.Pass()); | 96 RegisterFeatureListInstance(feature_list.Pass()); |
79 | 97 |
80 EXPECT_EQ(test_case.expected_feature_on_state, | 98 EXPECT_EQ(test_case.expected_feature_on_state, |
81 FeatureList::IsEnabled(kFeatureOnByDefault)) | 99 FeatureList::IsEnabled(kFeatureOnByDefault)) |
82 << i; | 100 << i; |
(...skipping 15 matching lines...) Expand all Loading... |
98 EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kFeatureOffByDefault)); | 116 EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kFeatureOffByDefault)); |
99 | 117 |
100 // Now, call it with a distinct struct for |kFeatureOnByDefaultName|, which | 118 // Now, call it with a distinct struct for |kFeatureOnByDefaultName|, which |
101 // should return false. | 119 // should return false. |
102 struct Feature kFeatureOnByDefault2 { | 120 struct Feature kFeatureOnByDefault2 { |
103 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT | 121 kFeatureOnByDefaultName, FEATURE_ENABLED_BY_DEFAULT |
104 }; | 122 }; |
105 EXPECT_FALSE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault2)); | 123 EXPECT_FALSE(feature_list()->CheckFeatureIdentity(kFeatureOnByDefault2)); |
106 } | 124 } |
107 | 125 |
| 126 TEST_F(FeatureListTest, FieldTrialOverrides) { |
| 127 struct { |
| 128 FeatureList::OverrideState trial1_state; |
| 129 FeatureList::OverrideState trial2_state; |
| 130 } test_cases[] = { |
| 131 {FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 132 FeatureList::OVERRIDE_DISABLE_FEATURE}, |
| 133 {FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 134 FeatureList::OVERRIDE_ENABLE_FEATURE}, |
| 135 {FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 136 FeatureList::OVERRIDE_DISABLE_FEATURE}, |
| 137 {FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 138 FeatureList::OVERRIDE_ENABLE_FEATURE}, |
| 139 }; |
| 140 |
| 141 FieldTrial::ActiveGroup active_group; |
| 142 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| 143 const auto& test_case = test_cases[i]; |
| 144 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]", i)); |
| 145 |
| 146 ClearFeatureListInstance(); |
| 147 |
| 148 FieldTrialList field_trial_list(nullptr); |
| 149 scoped_ptr<FeatureList> feature_list(new FeatureList); |
| 150 |
| 151 FieldTrial* trial1 = FieldTrialList::CreateFieldTrial("TrialExample1", "A"); |
| 152 FieldTrial* trial2 = FieldTrialList::CreateFieldTrial("TrialExample2", "B"); |
| 153 feature_list->RegisterFieldTrialOverride(kFeatureOnByDefaultName, |
| 154 test_case.trial1_state, trial1); |
| 155 feature_list->RegisterFieldTrialOverride(kFeatureOffByDefaultName, |
| 156 test_case.trial2_state, trial2); |
| 157 RegisterFeatureListInstance(feature_list.Pass()); |
| 158 |
| 159 // Initially, neither trial should be active. |
| 160 EXPECT_FALSE(IsFieldTrialActive(trial1)); |
| 161 EXPECT_FALSE(IsFieldTrialActive(trial2)); |
| 162 |
| 163 const bool expected_enabled_1 = |
| 164 (test_case.trial1_state == FeatureList::OVERRIDE_ENABLE_FEATURE); |
| 165 EXPECT_EQ(expected_enabled_1, FeatureList::IsEnabled(kFeatureOnByDefault)); |
| 166 // The above should have activated |trial1|. |
| 167 EXPECT_TRUE(IsFieldTrialActive(trial1)); |
| 168 EXPECT_FALSE(IsFieldTrialActive(trial2)); |
| 169 |
| 170 const bool expected_enabled_2 = |
| 171 (test_case.trial2_state == FeatureList::OVERRIDE_ENABLE_FEATURE); |
| 172 EXPECT_EQ(expected_enabled_2, FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 173 // The above should have activated |trial2|. |
| 174 EXPECT_TRUE(IsFieldTrialActive(trial1)); |
| 175 EXPECT_TRUE(IsFieldTrialActive(trial2)); |
| 176 } |
| 177 } |
| 178 |
| 179 TEST_F(FeatureListTest, CommandLineTakesPrecedenceOverFieldTrial) { |
| 180 ClearFeatureListInstance(); |
| 181 |
| 182 FieldTrialList field_trial_list(nullptr); |
| 183 scoped_ptr<FeatureList> feature_list(new FeatureList); |
| 184 |
| 185 // The feature is explicitly enabled on the command-line. |
| 186 feature_list->InitializeFromCommandLine(kFeatureOffByDefaultName, ""); |
| 187 |
| 188 // But the FieldTrial would set the feature to disabled. |
| 189 FieldTrial* trial = FieldTrialList::CreateFieldTrial("TrialExample2", "A"); |
| 190 feature_list->RegisterFieldTrialOverride( |
| 191 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, trial); |
| 192 RegisterFeatureListInstance(feature_list.Pass()); |
| 193 |
| 194 EXPECT_FALSE(IsFieldTrialActive(trial)); |
| 195 // Command-line should take precedence. |
| 196 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 197 // Since the feature is on due to the command-line, and not as a result of the |
| 198 // field trial, the field trial should not be activated (since the Associate* |
| 199 // API wasn't used.) |
| 200 EXPECT_FALSE(IsFieldTrialActive(trial)); |
| 201 } |
| 202 |
| 203 TEST_F(FeatureListTest, AssociateReportingFieldTrial) { |
| 204 struct { |
| 205 const char* enable_features; |
| 206 const char* disable_features; |
| 207 bool expected_enable_trial_created; |
| 208 bool expected_disable_trial_created; |
| 209 } test_cases[] = { |
| 210 // If no enable/disable flags are specified, no trials should be created. |
| 211 {"", "", false, false}, |
| 212 // Enabling the feature should result in the enable trial created. |
| 213 {kFeatureOffByDefaultName, "", true, false}, |
| 214 // Disabling the feature should result in the disable trial created. |
| 215 {"", kFeatureOffByDefaultName, false, true}, |
| 216 }; |
| 217 |
| 218 const char kTrialName[] = "ForcingTrial"; |
| 219 const char kForcedOnGroupName[] = "ForcedOn"; |
| 220 const char kForcedOffGroupName[] = "ForcedOff"; |
| 221 |
| 222 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| 223 const auto& test_case = test_cases[i]; |
| 224 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: [%s] [%s]", i, |
| 225 test_case.enable_features, |
| 226 test_case.disable_features)); |
| 227 |
| 228 ClearFeatureListInstance(); |
| 229 |
| 230 FieldTrialList field_trial_list(nullptr); |
| 231 scoped_ptr<FeatureList> feature_list(new FeatureList); |
| 232 feature_list->InitializeFromCommandLine(test_case.enable_features, |
| 233 test_case.disable_features); |
| 234 |
| 235 FieldTrial* enable_trial = feature_list->AssociateReportingFieldTrial( |
| 236 kFeatureOffByDefaultName, FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 237 kTrialName, kForcedOnGroupName); |
| 238 EXPECT_EQ(test_case.expected_enable_trial_created, enable_trial != nullptr); |
| 239 FieldTrial* disable_trial = feature_list->AssociateReportingFieldTrial( |
| 240 kFeatureOffByDefaultName, FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 241 kTrialName, kForcedOffGroupName); |
| 242 EXPECT_EQ(test_case.expected_disable_trial_created, |
| 243 disable_trial != nullptr); |
| 244 RegisterFeatureListInstance(feature_list.Pass()); |
| 245 |
| 246 if (disable_trial) { |
| 247 EXPECT_FALSE(IsFieldTrialActive(disable_trial)); |
| 248 EXPECT_FALSE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 249 EXPECT_TRUE(IsFieldTrialActive(disable_trial)); |
| 250 EXPECT_EQ(kForcedOffGroupName, disable_trial->group_name()); |
| 251 } else if (enable_trial) { |
| 252 EXPECT_FALSE(IsFieldTrialActive(enable_trial)); |
| 253 EXPECT_TRUE(FeatureList::IsEnabled(kFeatureOffByDefault)); |
| 254 EXPECT_TRUE(IsFieldTrialActive(enable_trial)); |
| 255 EXPECT_EQ(kForcedOnGroupName, enable_trial->group_name()); |
| 256 } |
| 257 } |
| 258 } |
| 259 |
108 } // namespace base | 260 } // namespace base |
OLD | NEW |