Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(721)

Side by Side Diff: components/subresource_filter/core/browser/subresource_filter_features_unittest.cc

Issue 2838193002: Split the ScopedSubresourceFilterFeatureToggle into two helper classes. (Closed)
Patch Set: Rebase. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/subresource_filter/core/browser/subresource_filter_features_test_support.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/subresource_filter/core/browser/subresource_filter_features .h" 5 #include "components/subresource_filter/core/browser/subresource_filter_features .h"
6 6
7 #include <map>
8 #include <memory>
7 #include <string> 9 #include <string>
8 10
11 #include "base/feature_list.h"
12 #include "base/macros.h"
9 #include "base/metrics/field_trial.h" 13 #include "base/metrics/field_trial.h"
14 #include "base/metrics/field_trial_params.h"
10 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" 15 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h"
16 #include "components/variations/variations_associated_data.h"
11 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
12 18
13 namespace subresource_filter { 19 namespace subresource_filter {
20 namespace testing {
21
22 namespace {
23
24 constexpr const char kTestFieldTrialName[] = "FieldTrialNameShouldNotMatter";
25 constexpr const char kTestExperimentGroupName[] = "GroupNameShouldNotMatter";
26
27 class ScopedExperimentalStateToggle {
28 public:
29 ScopedExperimentalStateToggle(
30 base::FeatureList::OverrideState feature_state,
31 std::map<std::string, std::string> variation_params)
32 : field_trial_list_(nullptr /* entropy_provider */),
33 scoped_configurator_(nullptr) {
34 EXPECT_TRUE(base::AssociateFieldTrialParams(
35 kTestFieldTrialName, kTestExperimentGroupName, variation_params));
36 base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial(
37 kTestFieldTrialName, kTestExperimentGroupName);
38
39 std::unique_ptr<base::FeatureList> feature_list =
40 base::MakeUnique<base::FeatureList>();
41 feature_list->RegisterFieldTrialOverride(
42 kSafeBrowsingSubresourceFilter.name, feature_state, field_trial);
43 scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
44 }
45
46 ~ScopedExperimentalStateToggle() {
47 variations::testing::ClearAllVariationParams();
48 }
49
50 private:
51 base::FieldTrialList field_trial_list_;
52
53 ScopedSubresourceFilterConfigurator scoped_configurator_;
54 base::test::ScopedFeatureList scoped_feature_list_;
55
56 DISALLOW_COPY_AND_ASSIGN(ScopedExperimentalStateToggle);
57 };
58
59 } // namespace
60 } // namespace testing
14 61
15 TEST(SubresourceFilterFeaturesTest, ActivationLevel) { 62 TEST(SubresourceFilterFeaturesTest, ActivationLevel) {
16 const struct { 63 const struct {
17 bool feature_enabled; 64 bool feature_enabled;
18 const char* activation_level_param; 65 const char* activation_level_param;
19 ActivationLevel expected_activation_level; 66 ActivationLevel expected_activation_level;
20 } kTestCases[] = {{false, "", ActivationLevel::DISABLED}, 67 } kTestCases[] = {{false, "", ActivationLevel::DISABLED},
21 {false, "disabled", ActivationLevel::DISABLED}, 68 {false, "disabled", ActivationLevel::DISABLED},
22 {false, "dryrun", ActivationLevel::DISABLED}, 69 {false, "dryrun", ActivationLevel::DISABLED},
23 {false, "enabled", ActivationLevel::DISABLED}, 70 {false, "enabled", ActivationLevel::DISABLED},
24 {false, "%$ garbage !%", ActivationLevel::DISABLED}, 71 {false, "%$ garbage !%", ActivationLevel::DISABLED},
25 {true, "", ActivationLevel::DISABLED}, 72 {true, "", ActivationLevel::DISABLED},
26 {true, "disable", ActivationLevel::DISABLED}, 73 {true, "disable", ActivationLevel::DISABLED},
27 {true, "Disable", ActivationLevel::DISABLED}, 74 {true, "Disable", ActivationLevel::DISABLED},
28 {true, "disabled", ActivationLevel::DISABLED}, 75 {true, "disabled", ActivationLevel::DISABLED},
29 {true, "%$ garbage !%", ActivationLevel::DISABLED}, 76 {true, "%$ garbage !%", ActivationLevel::DISABLED},
30 {true, kActivationLevelDryRun, ActivationLevel::DRYRUN}, 77 {true, kActivationLevelDryRun, ActivationLevel::DRYRUN},
31 {true, kActivationLevelEnabled, ActivationLevel::ENABLED}, 78 {true, kActivationLevelEnabled, ActivationLevel::ENABLED},
32 {true, "Enabled", ActivationLevel::ENABLED}}; 79 {true, "Enabled", ActivationLevel::ENABLED}};
33 80
34 for (const auto& test_case : kTestCases) { 81 for (const auto& test_case : kTestCases) {
35 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 82 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
36 SCOPED_TRACE(::testing::Message("ActivationLevelParam = \"") 83 SCOPED_TRACE(::testing::Message("ActivationLevelParam = \"")
37 << test_case.activation_level_param << "\""); 84 << test_case.activation_level_param << "\"");
38 85
39 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 86 testing::ScopedExperimentalStateToggle scoped_experimental_state(
40 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
41 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 87 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
42 : base::FeatureList::OVERRIDE_USE_DEFAULT, 88 : base::FeatureList::OVERRIDE_USE_DEFAULT,
43 test_case.activation_level_param, kActivationScopeNoSites); 89 {{kActivationLevelParameterName, test_case.activation_level_param},
90 {kActivationScopeParameterName, kActivationScopeNoSites}});
44 91
45 const auto active_configurations = GetActiveConfigurations(); 92 const auto active_configurations = GetActiveConfigurations();
46 const Configuration& actual_configuration = 93 const Configuration& actual_configuration =
47 active_configurations->the_one_and_only(); 94 active_configurations->the_one_and_only();
48 EXPECT_EQ(test_case.expected_activation_level, 95 EXPECT_EQ(test_case.expected_activation_level,
49 actual_configuration.activation_level); 96 actual_configuration.activation_level);
50 EXPECT_EQ(ActivationScope::NO_SITES, actual_configuration.activation_scope); 97 EXPECT_EQ(ActivationScope::NO_SITES, actual_configuration.activation_scope);
51 } 98 }
52 } 99 }
53 100
(...skipping 14 matching lines...) Expand all
68 {true, "no_sites", ActivationScope::NO_SITES}, 115 {true, "no_sites", ActivationScope::NO_SITES},
69 {true, "%$ garbage !%", ActivationScope::NO_SITES}, 116 {true, "%$ garbage !%", ActivationScope::NO_SITES},
70 {true, kActivationScopeAllSites, ActivationScope::ALL_SITES}, 117 {true, kActivationScopeAllSites, ActivationScope::ALL_SITES},
71 {true, kActivationScopeActivationList, ActivationScope::ACTIVATION_LIST}}; 118 {true, kActivationScopeActivationList, ActivationScope::ACTIVATION_LIST}};
72 119
73 for (const auto& test_case : kTestCases) { 120 for (const auto& test_case : kTestCases) {
74 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 121 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
75 SCOPED_TRACE(::testing::Message("ActivationScopeParam = \"") 122 SCOPED_TRACE(::testing::Message("ActivationScopeParam = \"")
76 << test_case.activation_scope_param << "\""); 123 << test_case.activation_scope_param << "\"");
77 124
78 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 125 testing::ScopedExperimentalStateToggle scoped_experimental_state(
79 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
80 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 126 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
81 : base::FeatureList::OVERRIDE_USE_DEFAULT, 127 : base::FeatureList::OVERRIDE_USE_DEFAULT,
82 kActivationLevelDisabled, test_case.activation_scope_param); 128 {{kActivationLevelParameterName, kActivationLevelDisabled},
129 {kActivationScopeParameterName, test_case.activation_scope_param}});
83 130
84 const auto active_configurations = GetActiveConfigurations(); 131 const auto active_configurations = GetActiveConfigurations();
85 const Configuration& actual_configuration = 132 const Configuration& actual_configuration =
86 active_configurations->the_one_and_only(); 133 active_configurations->the_one_and_only();
87 EXPECT_EQ(ActivationLevel::DISABLED, actual_configuration.activation_level); 134 EXPECT_EQ(ActivationLevel::DISABLED, actual_configuration.activation_level);
88 EXPECT_EQ(test_case.expected_activation_scope, 135 EXPECT_EQ(test_case.expected_activation_scope,
89 actual_configuration.activation_scope); 136 actual_configuration.activation_scope);
90 } 137 }
91 } 138 }
92 139
(...skipping 28 matching lines...) Expand all
121 {true, kActivationLevelEnabled, ActivationLevel::ENABLED, 168 {true, kActivationLevelEnabled, ActivationLevel::ENABLED,
122 kActivationScopeAllSites, ActivationScope::ALL_SITES}, 169 kActivationScopeAllSites, ActivationScope::ALL_SITES},
123 {true, kActivationLevelEnabled, ActivationLevel::ENABLED, 170 {true, kActivationLevelEnabled, ActivationLevel::ENABLED,
124 kActivationScopeActivationList, ActivationScope::ACTIVATION_LIST}, 171 kActivationScopeActivationList, ActivationScope::ACTIVATION_LIST},
125 {true, kActivationLevelEnabled, ActivationLevel::ENABLED, 172 {true, kActivationLevelEnabled, ActivationLevel::ENABLED,
126 kActivationScopeAllSites, ActivationScope::ALL_SITES}, 173 kActivationScopeAllSites, ActivationScope::ALL_SITES},
127 {false, kActivationLevelEnabled, ActivationLevel::DISABLED, 174 {false, kActivationLevelEnabled, ActivationLevel::DISABLED,
128 kActivationScopeAllSites, ActivationScope::NO_SITES}}; 175 kActivationScopeAllSites, ActivationScope::NO_SITES}};
129 176
130 for (const auto& test_case : kTestCases) { 177 for (const auto& test_case : kTestCases) {
131 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 178 testing::ScopedExperimentalStateToggle scoped_experimental_state(
132 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
133 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 179 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
134 : base::FeatureList::OVERRIDE_USE_DEFAULT, 180 : base::FeatureList::OVERRIDE_USE_DEFAULT,
135 test_case.activation_level_param, test_case.activation_scope_param); 181 {{kActivationLevelParameterName, test_case.activation_level_param},
182 {kActivationScopeParameterName, test_case.activation_scope_param}});
136 183
137 const auto active_configurations = GetActiveConfigurations(); 184 const auto active_configurations = GetActiveConfigurations();
138 const Configuration& actual_configuration = 185 const Configuration& actual_configuration =
139 active_configurations->the_one_and_only(); 186 active_configurations->the_one_and_only();
140 EXPECT_EQ(test_case.expected_activation_level, 187 EXPECT_EQ(test_case.expected_activation_level,
141 actual_configuration.activation_level); 188 actual_configuration.activation_level);
142 EXPECT_EQ(test_case.expected_activation_scope, 189 EXPECT_EQ(test_case.expected_activation_scope,
143 actual_configuration.activation_scope); 190 actual_configuration.activation_scope);
144 } 191 }
145 } 192 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 ActivationList::PHISHING_INTERSTITIAL}, 225 ActivationList::PHISHING_INTERSTITIAL},
179 {true, socEngCommaPhisingCommaGarbage.c_str(), 226 {true, socEngCommaPhisingCommaGarbage.c_str(),
180 ActivationList::PHISHING_INTERSTITIAL}, 227 ActivationList::PHISHING_INTERSTITIAL},
181 {true, "List1, List2", ActivationList::NONE}}; 228 {true, "List1, List2", ActivationList::NONE}};
182 229
183 for (const auto& test_case : kTestCases) { 230 for (const auto& test_case : kTestCases) {
184 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 231 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
185 SCOPED_TRACE(::testing::Message("ActivationListParam = \"") 232 SCOPED_TRACE(::testing::Message("ActivationListParam = \"")
186 << test_case.activation_list_param << "\""); 233 << test_case.activation_list_param << "\"");
187 234
188 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 235 testing::ScopedExperimentalStateToggle scoped_experimental_state(
189 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
190 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 236 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
191 : base::FeatureList::OVERRIDE_USE_DEFAULT, 237 : base::FeatureList::OVERRIDE_USE_DEFAULT,
192 kActivationLevelDisabled, kActivationScopeNoSites, 238 {{kActivationLevelParameterName, kActivationLevelDisabled},
193 test_case.activation_list_param); 239 {kActivationScopeParameterName, kActivationScopeNoSites},
240 {kActivationListsParameterName, test_case.activation_list_param}});
194 241
195 const auto active_configurations = GetActiveConfigurations(); 242 const auto active_configurations = GetActiveConfigurations();
196 const Configuration& actual_configuration = 243 const Configuration& actual_configuration =
197 active_configurations->the_one_and_only(); 244 active_configurations->the_one_and_only();
198 EXPECT_EQ(test_case.expected_activation_list, 245 EXPECT_EQ(test_case.expected_activation_list,
199 actual_configuration.activation_list); 246 actual_configuration.activation_list);
200 } 247 }
201 } 248 }
202 249
203 TEST(SubresourceFilterFeaturesTest, PerfMeasurementRate) { 250 TEST(SubresourceFilterFeaturesTest, PerfMeasurementRate) {
(...skipping 13 matching lines...) Expand all
217 {true, "1", 1}, 264 {true, "1", 1},
218 {true, "1.0", 1}, 265 {true, "1.0", 1},
219 {true, "0.333", 0.333}, 266 {true, "0.333", 0.333},
220 {true, "1e0", 1}}; 267 {true, "1e0", 1}};
221 268
222 for (const auto& test_case : kTestCases) { 269 for (const auto& test_case : kTestCases) {
223 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 270 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
224 SCOPED_TRACE(::testing::Message("PerfMeasurementParam = \"") 271 SCOPED_TRACE(::testing::Message("PerfMeasurementParam = \"")
225 << test_case.perf_measurement_param << "\""); 272 << test_case.perf_measurement_param << "\"");
226 273
227 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 274 testing::ScopedExperimentalStateToggle scoped_experimental_state(
228 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
229 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 275 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
230 : base::FeatureList::OVERRIDE_USE_DEFAULT, 276 : base::FeatureList::OVERRIDE_USE_DEFAULT,
231 {{kPerformanceMeasurementRateParameterName, 277 {{kPerformanceMeasurementRateParameterName,
232 test_case.perf_measurement_param}}); 278 test_case.perf_measurement_param}});
233 279
234 const auto active_configurations = GetActiveConfigurations(); 280 const auto active_configurations = GetActiveConfigurations();
235 const Configuration& actual_configuration = 281 const Configuration& actual_configuration =
236 active_configurations->the_one_and_only(); 282 active_configurations->the_one_and_only();
237 EXPECT_EQ(test_case.expected_perf_measurement_rate, 283 EXPECT_EQ(test_case.expected_perf_measurement_rate,
238 actual_configuration.performance_measurement_rate); 284 actual_configuration.performance_measurement_rate);
(...skipping 14 matching lines...) Expand all
253 {true, "invalid value", false}, 299 {true, "invalid value", false},
254 {true, "True", true}, 300 {true, "True", true},
255 {true, "TRUE", true}, 301 {true, "TRUE", true},
256 {true, "true", true}}; 302 {true, "true", true}};
257 303
258 for (const auto& test_case : kTestCases) { 304 for (const auto& test_case : kTestCases) {
259 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 305 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
260 SCOPED_TRACE(::testing::Message("SuppressNotificationsParam = \"") 306 SCOPED_TRACE(::testing::Message("SuppressNotificationsParam = \"")
261 << test_case.suppress_notifications_param << "\""); 307 << test_case.suppress_notifications_param << "\"");
262 308
263 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 309 testing::ScopedExperimentalStateToggle scoped_experimental_state(
264 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
265 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 310 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
266 : base::FeatureList::OVERRIDE_USE_DEFAULT, 311 : base::FeatureList::OVERRIDE_USE_DEFAULT,
267 {{kSuppressNotificationsParameterName, 312 {{kSuppressNotificationsParameterName,
268 test_case.suppress_notifications_param}}); 313 test_case.suppress_notifications_param}});
269 314
270 const auto active_configurations = GetActiveConfigurations(); 315 const auto active_configurations = GetActiveConfigurations();
271 const Configuration& actual_configuration = 316 const Configuration& actual_configuration =
272 active_configurations->the_one_and_only(); 317 active_configurations->the_one_and_only();
273 EXPECT_EQ(test_case.expected_suppress_notifications_value, 318 EXPECT_EQ(test_case.expected_suppress_notifications_value,
274 actual_configuration.should_suppress_notifications); 319 actual_configuration.should_suppress_notifications);
(...skipping 14 matching lines...) Expand all
289 {true, "invalid value", false}, 334 {true, "invalid value", false},
290 {true, "True", true}, 335 {true, "True", true},
291 {true, "TRUE", true}, 336 {true, "TRUE", true},
292 {true, "true", true}}; 337 {true, "true", true}};
293 338
294 for (const auto& test_case : kTestCases) { 339 for (const auto& test_case : kTestCases) {
295 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 340 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
296 SCOPED_TRACE(::testing::Message("WhitelistSiteOnReloadParam = \"") 341 SCOPED_TRACE(::testing::Message("WhitelistSiteOnReloadParam = \"")
297 << test_case.whitelist_site_on_reload_param << "\""); 342 << test_case.whitelist_site_on_reload_param << "\"");
298 343
299 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 344 testing::ScopedExperimentalStateToggle scoped_experimental_state(
300 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
301 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 345 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
302 : base::FeatureList::OVERRIDE_USE_DEFAULT, 346 : base::FeatureList::OVERRIDE_USE_DEFAULT,
303 {{kWhitelistSiteOnReloadParameterName, 347 {{kWhitelistSiteOnReloadParameterName,
304 test_case.whitelist_site_on_reload_param}}); 348 test_case.whitelist_site_on_reload_param}});
305 349
306 const auto active_configurations = GetActiveConfigurations(); 350 const auto active_configurations = GetActiveConfigurations();
307 const Configuration& actual_configuration = 351 const Configuration& actual_configuration =
308 active_configurations->the_one_and_only(); 352 active_configurations->the_one_and_only();
309 EXPECT_EQ(test_case.expected_whitelist_site_on_reload_value, 353 EXPECT_EQ(test_case.expected_whitelist_site_on_reload_value,
310 actual_configuration.should_whitelist_site_on_reload); 354 actual_configuration.should_whitelist_site_on_reload);
311 } 355 }
312 } 356 }
313 357
358 TEST(SubresourceFilterFeaturesTest, RulesetFlavor) {
359 const struct {
360 bool feature_enabled;
361 const char* ruleset_flavor_param;
362 const char* expected_ruleset_flavor_value;
363 } kTestCases[] = {
364 {false, "", ""}, {false, "a", ""}, {false, "test value", ""},
365 {true, "", ""}, {true, "a", "a"}, {true, "test value", "test value"}};
366
367 for (const auto& test_case : kTestCases) {
368 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
369 SCOPED_TRACE(::testing::Message("Flavor = \"")
370 << test_case.ruleset_flavor_param << "\"");
371
372 testing::ScopedExperimentalStateToggle scoped_experimental_state(
373 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
374 : base::FeatureList::OVERRIDE_USE_DEFAULT,
375 {{kRulesetFlavorParameterName, test_case.ruleset_flavor_param}});
376
377 const auto active_configurations = GetActiveConfigurations();
378 const Configuration& actual_configuration =
379 active_configurations->the_one_and_only();
380 EXPECT_EQ(std::string(test_case.expected_ruleset_flavor_value),
381 actual_configuration.ruleset_flavor);
382 }
383 }
314 } // namespace subresource_filter 384 } // namespace subresource_filter
OLDNEW
« no previous file with comments | « components/subresource_filter/core/browser/subresource_filter_features_test_support.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698