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

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

Issue 2844063002: Add support for multiple simultaneous subresource_filter::Configurations. (Closed)
Patch Set: Polish + 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
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> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <vector>
10 11
11 #include "base/feature_list.h" 12 #include "base/feature_list.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/metrics/field_trial.h" 14 #include "base/metrics/field_trial.h"
14 #include "base/metrics/field_trial_params.h" 15 #include "base/metrics/field_trial_params.h"
16 #include "base/strings/string_util.h"
15 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" 17 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h"
16 #include "components/variations/variations_associated_data.h" 18 #include "components/variations/variations_associated_data.h"
19 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
18 21
19 namespace subresource_filter { 22 namespace subresource_filter {
20 namespace testing {
21 23
22 namespace { 24 namespace {
23 25
24 constexpr const char kTestFieldTrialName[] = "FieldTrialNameShouldNotMatter"; 26 constexpr const char kTestFieldTrialName[] = "FieldTrialNameShouldNotMatter";
25 constexpr const char kTestExperimentGroupName[] = "GroupNameShouldNotMatter"; 27 constexpr const char kTestExperimentGroupName[] = "GroupNameShouldNotMatter";
26 28
27 class ScopedExperimentalStateToggle { 29 class ScopedExperimentalStateToggle {
28 public: 30 public:
29 ScopedExperimentalStateToggle( 31 ScopedExperimentalStateToggle(
30 base::FeatureList::OverrideState feature_state, 32 base::FeatureList::OverrideState feature_state,
(...skipping 12 matching lines...) Expand all
43 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); 45 scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
44 } 46 }
45 47
46 ~ScopedExperimentalStateToggle() { 48 ~ScopedExperimentalStateToggle() {
47 variations::testing::ClearAllVariationParams(); 49 variations::testing::ClearAllVariationParams();
48 } 50 }
49 51
50 private: 52 private:
51 base::FieldTrialList field_trial_list_; 53 base::FieldTrialList field_trial_list_;
52 54
53 ScopedSubresourceFilterConfigurator scoped_configurator_; 55 testing::ScopedSubresourceFilterConfigurator scoped_configurator_;
54 base::test::ScopedFeatureList scoped_feature_list_; 56 base::test::ScopedFeatureList scoped_feature_list_;
55 57
56 DISALLOW_COPY_AND_ASSIGN(ScopedExperimentalStateToggle); 58 DISALLOW_COPY_AND_ASSIGN(ScopedExperimentalStateToggle);
57 }; 59 };
58 60
61 template <class T, size_t N>
62 std::vector<T> AsVector(const T (&array)[N]) {
63 return std::vector<T>(std::begin(array), std::end(array));
64 }
65
66 void ExpectAndRetrieveExactlyOneEnabledConfig(Configuration* actual_config) {
67 const auto config_list = GetEnabledConfigurations();
68 ASSERT_EQ(1u, config_list->configs_by_decreasing_priority().size());
69 *actual_config = config_list->configs_by_decreasing_priority().front();
70 }
71
72 void ExpectPresetCanBeEnabledByName(Configuration preset, const char* name) {
73 ScopedExperimentalStateToggle scoped_experimental_state(
74 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
75 {{kEnablePresetsParameterName, name}});
76
77 const auto config_list = GetEnabledConfigurations();
78 EXPECT_THAT(config_list->configs_by_decreasing_priority(),
79 ::testing::ElementsAre(preset, Configuration()));
80 }
81
82 void ExpectPresetIsEquivalentToVariationParams(
83 Configuration preset,
84 std::map<std::string, std::string> variation_params) {
85 ScopedExperimentalStateToggle scoped_experimental_state(
86 base::FeatureList::OVERRIDE_ENABLE_FEATURE, variation_params);
87
88 Configuration experimental_configuration;
89 ExpectAndRetrieveExactlyOneEnabledConfig(&experimental_configuration);
90 EXPECT_EQ(preset, experimental_configuration);
91 }
92
59 } // namespace 93 } // namespace
60 } // namespace testing
61 94
62 TEST(SubresourceFilterFeaturesTest, ActivationLevel) { 95 TEST(SubresourceFilterFeaturesTest, ActivationLevel) {
63 const struct { 96 const struct {
64 bool feature_enabled; 97 bool feature_enabled;
65 const char* activation_level_param; 98 const char* activation_level_param;
66 ActivationLevel expected_activation_level; 99 ActivationLevel expected_activation_level;
67 } kTestCases[] = {{false, "", ActivationLevel::DISABLED}, 100 } kTestCases[] = {{false, "", ActivationLevel::DISABLED},
68 {false, "disabled", ActivationLevel::DISABLED}, 101 {false, "disabled", ActivationLevel::DISABLED},
69 {false, "dryrun", ActivationLevel::DISABLED}, 102 {false, "dryrun", ActivationLevel::DISABLED},
70 {false, "enabled", ActivationLevel::DISABLED}, 103 {false, "enabled", ActivationLevel::DISABLED},
71 {false, "%$ garbage !%", ActivationLevel::DISABLED}, 104 {false, "%$ garbage !%", ActivationLevel::DISABLED},
72 {true, "", ActivationLevel::DISABLED}, 105 {true, "", ActivationLevel::DISABLED},
73 {true, "disable", ActivationLevel::DISABLED}, 106 {true, "disable", ActivationLevel::DISABLED},
74 {true, "Disable", ActivationLevel::DISABLED}, 107 {true, "Disable", ActivationLevel::DISABLED},
75 {true, "disabled", ActivationLevel::DISABLED}, 108 {true, "disabled", ActivationLevel::DISABLED},
76 {true, "%$ garbage !%", ActivationLevel::DISABLED}, 109 {true, "%$ garbage !%", ActivationLevel::DISABLED},
77 {true, kActivationLevelDryRun, ActivationLevel::DRYRUN}, 110 {true, kActivationLevelDryRun, ActivationLevel::DRYRUN},
78 {true, kActivationLevelEnabled, ActivationLevel::ENABLED}, 111 {true, kActivationLevelEnabled, ActivationLevel::ENABLED},
79 {true, "Enabled", ActivationLevel::ENABLED}}; 112 {true, "Enabled", ActivationLevel::ENABLED}};
80 113
81 for (const auto& test_case : kTestCases) { 114 for (const auto& test_case : kTestCases) {
82 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 115 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
83 SCOPED_TRACE(::testing::Message("ActivationLevelParam = \"") 116 SCOPED_TRACE(::testing::Message("ActivationLevelParam = \"")
84 << test_case.activation_level_param << "\""); 117 << test_case.activation_level_param << "\"");
85 118
86 testing::ScopedExperimentalStateToggle scoped_experimental_state( 119 ScopedExperimentalStateToggle scoped_experimental_state(
87 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 120 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
88 : base::FeatureList::OVERRIDE_USE_DEFAULT, 121 : base::FeatureList::OVERRIDE_USE_DEFAULT,
89 {{kActivationLevelParameterName, test_case.activation_level_param}, 122 {{kActivationLevelParameterName, test_case.activation_level_param},
90 {kActivationScopeParameterName, kActivationScopeNoSites}}); 123 {kActivationScopeParameterName, kActivationScopeNoSites}});
91 124
92 const auto active_configurations = GetActiveConfigurations(); 125 Configuration actual_configuration;
93 const Configuration& actual_configuration = 126 ExpectAndRetrieveExactlyOneEnabledConfig(&actual_configuration);
94 active_configurations->the_one_and_only();
95 EXPECT_EQ(test_case.expected_activation_level, 127 EXPECT_EQ(test_case.expected_activation_level,
96 actual_configuration.activation_level); 128 actual_configuration.activation_options.activation_level);
97 EXPECT_EQ(ActivationScope::NO_SITES, actual_configuration.activation_scope); 129 EXPECT_EQ(ActivationScope::NO_SITES,
130 actual_configuration.activation_conditions.activation_scope);
98 } 131 }
99 } 132 }
100 133
101 TEST(SubresourceFilterFeaturesTest, ActivationScope) { 134 TEST(SubresourceFilterFeaturesTest, ActivationScope) {
102 const struct { 135 const struct {
103 bool feature_enabled; 136 bool feature_enabled;
104 const char* activation_scope_param; 137 const char* activation_scope_param;
105 ActivationScope expected_activation_scope; 138 ActivationScope expected_activation_scope;
106 } kTestCases[] = { 139 } kTestCases[] = {
107 {false, "", ActivationScope::NO_SITES}, 140 {false, "", ActivationScope::NO_SITES},
108 {false, "no_sites", ActivationScope::NO_SITES}, 141 {false, "no_sites", ActivationScope::NO_SITES},
109 {false, "allsites", ActivationScope::NO_SITES}, 142 {false, "allsites", ActivationScope::NO_SITES},
110 {false, "enabled", ActivationScope::NO_SITES}, 143 {false, "enabled", ActivationScope::NO_SITES},
111 {false, "%$ garbage !%", ActivationScope::NO_SITES}, 144 {false, "%$ garbage !%", ActivationScope::NO_SITES},
112 {true, "", ActivationScope::NO_SITES}, 145 {true, "", ActivationScope::NO_SITES},
113 {true, "nosites", ActivationScope::NO_SITES}, 146 {true, "nosites", ActivationScope::NO_SITES},
114 {true, "No_sites", ActivationScope::NO_SITES}, 147 {true, "No_sites", ActivationScope::NO_SITES},
115 {true, "no_sites", ActivationScope::NO_SITES}, 148 {true, "no_sites", ActivationScope::NO_SITES},
116 {true, "%$ garbage !%", ActivationScope::NO_SITES}, 149 {true, "%$ garbage !%", ActivationScope::NO_SITES},
117 {true, kActivationScopeAllSites, ActivationScope::ALL_SITES}, 150 {true, kActivationScopeAllSites, ActivationScope::ALL_SITES},
118 {true, kActivationScopeActivationList, ActivationScope::ACTIVATION_LIST}}; 151 {true, kActivationScopeActivationList, ActivationScope::ACTIVATION_LIST}};
119 152
120 for (const auto& test_case : kTestCases) { 153 for (const auto& test_case : kTestCases) {
121 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 154 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
122 SCOPED_TRACE(::testing::Message("ActivationScopeParam = \"") 155 SCOPED_TRACE(::testing::Message("ActivationScopeParam = \"")
123 << test_case.activation_scope_param << "\""); 156 << test_case.activation_scope_param << "\"");
124 157
125 testing::ScopedExperimentalStateToggle scoped_experimental_state( 158 ScopedExperimentalStateToggle scoped_experimental_state(
126 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 159 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
127 : base::FeatureList::OVERRIDE_USE_DEFAULT, 160 : base::FeatureList::OVERRIDE_USE_DEFAULT,
128 {{kActivationLevelParameterName, kActivationLevelDisabled}, 161 {{kActivationLevelParameterName, kActivationLevelDisabled},
129 {kActivationScopeParameterName, test_case.activation_scope_param}}); 162 {kActivationScopeParameterName, test_case.activation_scope_param}});
130 163
131 const auto active_configurations = GetActiveConfigurations(); 164 Configuration actual_configuration;
132 const Configuration& actual_configuration = 165 ExpectAndRetrieveExactlyOneEnabledConfig(&actual_configuration);
133 active_configurations->the_one_and_only(); 166 EXPECT_EQ(ActivationLevel::DISABLED,
134 EXPECT_EQ(ActivationLevel::DISABLED, actual_configuration.activation_level); 167 actual_configuration.activation_options.activation_level);
135 EXPECT_EQ(test_case.expected_activation_scope, 168 EXPECT_EQ(test_case.expected_activation_scope,
136 actual_configuration.activation_scope); 169 actual_configuration.activation_conditions.activation_scope);
137 } 170 }
138 } 171 }
139 172
140 TEST(SubresourceFilterFeaturesTest, ActivationLevelAndScope) { 173 TEST(SubresourceFilterFeaturesTest, ActivationLevelAndScope) {
141 const struct { 174 const struct {
142 bool feature_enabled; 175 bool feature_enabled;
143 const char* activation_level_param; 176 const char* activation_level_param;
144 ActivationLevel expected_activation_level; 177 ActivationLevel expected_activation_level;
145 const char* activation_scope_param; 178 const char* activation_scope_param;
146 ActivationScope expected_activation_scope; 179 ActivationScope expected_activation_scope;
(...skipping 21 matching lines...) Expand all
168 {true, kActivationLevelEnabled, ActivationLevel::ENABLED, 201 {true, kActivationLevelEnabled, ActivationLevel::ENABLED,
169 kActivationScopeAllSites, ActivationScope::ALL_SITES}, 202 kActivationScopeAllSites, ActivationScope::ALL_SITES},
170 {true, kActivationLevelEnabled, ActivationLevel::ENABLED, 203 {true, kActivationLevelEnabled, ActivationLevel::ENABLED,
171 kActivationScopeActivationList, ActivationScope::ACTIVATION_LIST}, 204 kActivationScopeActivationList, ActivationScope::ACTIVATION_LIST},
172 {true, kActivationLevelEnabled, ActivationLevel::ENABLED, 205 {true, kActivationLevelEnabled, ActivationLevel::ENABLED,
173 kActivationScopeAllSites, ActivationScope::ALL_SITES}, 206 kActivationScopeAllSites, ActivationScope::ALL_SITES},
174 {false, kActivationLevelEnabled, ActivationLevel::DISABLED, 207 {false, kActivationLevelEnabled, ActivationLevel::DISABLED,
175 kActivationScopeAllSites, ActivationScope::NO_SITES}}; 208 kActivationScopeAllSites, ActivationScope::NO_SITES}};
176 209
177 for (const auto& test_case : kTestCases) { 210 for (const auto& test_case : kTestCases) {
178 testing::ScopedExperimentalStateToggle scoped_experimental_state( 211 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
212 SCOPED_TRACE(::testing::Message("ActivationLevelParam = \"")
213 << test_case.activation_level_param << "\"");
214 SCOPED_TRACE(::testing::Message("ActivationScopeParam = \"")
215 << test_case.activation_scope_param << "\"");
216
217 ScopedExperimentalStateToggle scoped_experimental_state(
179 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 218 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
180 : base::FeatureList::OVERRIDE_USE_DEFAULT, 219 : base::FeatureList::OVERRIDE_USE_DEFAULT,
181 {{kActivationLevelParameterName, test_case.activation_level_param}, 220 {{kActivationLevelParameterName, test_case.activation_level_param},
182 {kActivationScopeParameterName, test_case.activation_scope_param}}); 221 {kActivationScopeParameterName, test_case.activation_scope_param}});
183 222
184 const auto active_configurations = GetActiveConfigurations(); 223 Configuration actual_configuration;
185 const Configuration& actual_configuration = 224 ExpectAndRetrieveExactlyOneEnabledConfig(&actual_configuration);
186 active_configurations->the_one_and_only();
187 EXPECT_EQ(test_case.expected_activation_level, 225 EXPECT_EQ(test_case.expected_activation_level,
188 actual_configuration.activation_level); 226 actual_configuration.activation_options.activation_level);
189 EXPECT_EQ(test_case.expected_activation_scope, 227 EXPECT_EQ(test_case.expected_activation_scope,
190 actual_configuration.activation_scope); 228 actual_configuration.activation_conditions.activation_scope);
191 } 229 }
192 } 230 }
193 231
194 TEST(SubresourceFilterFeaturesTest, ActivationList) { 232 TEST(SubresourceFilterFeaturesTest, ActivationList) {
195 const std::string activation_soc_eng( 233 const std::string activation_soc_eng(
196 kActivationListSocialEngineeringAdsInterstitial); 234 kActivationListSocialEngineeringAdsInterstitial);
197 const std::string activation_phishing(kActivationListPhishingInterstitial); 235 const std::string activation_phishing(kActivationListPhishingInterstitial);
198 const std::string socEngPhising = activation_soc_eng + activation_phishing; 236 const std::string socEngPhising = activation_soc_eng + activation_phishing;
199 const std::string socEngCommaPhising = 237 const std::string socEngCommaPhising =
200 activation_soc_eng + "," + activation_phishing; 238 activation_soc_eng + "," + activation_phishing;
(...skipping 24 matching lines...) Expand all
225 ActivationList::PHISHING_INTERSTITIAL}, 263 ActivationList::PHISHING_INTERSTITIAL},
226 {true, socEngCommaPhisingCommaGarbage.c_str(), 264 {true, socEngCommaPhisingCommaGarbage.c_str(),
227 ActivationList::PHISHING_INTERSTITIAL}, 265 ActivationList::PHISHING_INTERSTITIAL},
228 {true, "List1, List2", ActivationList::NONE}}; 266 {true, "List1, List2", ActivationList::NONE}};
229 267
230 for (const auto& test_case : kTestCases) { 268 for (const auto& test_case : kTestCases) {
231 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 269 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
232 SCOPED_TRACE(::testing::Message("ActivationListParam = \"") 270 SCOPED_TRACE(::testing::Message("ActivationListParam = \"")
233 << test_case.activation_list_param << "\""); 271 << test_case.activation_list_param << "\"");
234 272
235 testing::ScopedExperimentalStateToggle scoped_experimental_state( 273 ScopedExperimentalStateToggle scoped_experimental_state(
236 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 274 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
237 : base::FeatureList::OVERRIDE_USE_DEFAULT, 275 : base::FeatureList::OVERRIDE_USE_DEFAULT,
238 {{kActivationLevelParameterName, kActivationLevelDisabled}, 276 {{kActivationLevelParameterName, kActivationLevelDisabled},
239 {kActivationScopeParameterName, kActivationScopeNoSites}, 277 {kActivationScopeParameterName, kActivationScopeNoSites},
240 {kActivationListsParameterName, test_case.activation_list_param}}); 278 {kActivationListsParameterName, test_case.activation_list_param}});
241 279
242 const auto active_configurations = GetActiveConfigurations(); 280 Configuration actual_configuration;
243 const Configuration& actual_configuration = 281 ExpectAndRetrieveExactlyOneEnabledConfig(&actual_configuration);
244 active_configurations->the_one_and_only();
245 EXPECT_EQ(test_case.expected_activation_list, 282 EXPECT_EQ(test_case.expected_activation_list,
246 actual_configuration.activation_list); 283 actual_configuration.activation_conditions.activation_list);
247 } 284 }
248 } 285 }
249 286
250 TEST(SubresourceFilterFeaturesTest, PerfMeasurementRate) { 287 TEST(SubresourceFilterFeaturesTest, PerfMeasurementRate) {
251 const struct { 288 const struct {
252 bool feature_enabled; 289 bool feature_enabled;
253 const char* perf_measurement_param; 290 const char* perf_measurement_param;
254 double expected_perf_measurement_rate; 291 double expected_perf_measurement_rate;
255 } kTestCases[] = {{false, "not_a_number", 0}, 292 } kTestCases[] = {{false, "not_a_number", 0},
256 {false, "0", 0}, 293 {false, "0", 0},
257 {false, "1", 0}, 294 {false, "1", 0},
258 {true, "not_a_number", 0}, 295 {true, "not_a_number", 0},
259 {true, "0.5not_a_number", 0}, 296 {true, "0.5not_a_number", 0},
260 {true, "0", 0}, 297 {true, "0", 0},
261 {true, "0.000", 0}, 298 {true, "0.000", 0},
262 {true, "0.05", 0.05}, 299 {true, "0.05", 0.05},
263 {true, "0.5", 0.5}, 300 {true, "0.5", 0.5},
264 {true, "1", 1}, 301 {true, "1", 1},
265 {true, "1.0", 1}, 302 {true, "1.0", 1},
266 {true, "0.333", 0.333}, 303 {true, "0.333", 0.333},
267 {true, "1e0", 1}}; 304 {true, "1e0", 1}};
268 305
269 for (const auto& test_case : kTestCases) { 306 for (const auto& test_case : kTestCases) {
270 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 307 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
271 SCOPED_TRACE(::testing::Message("PerfMeasurementParam = \"") 308 SCOPED_TRACE(::testing::Message("PerfMeasurementParam = \"")
272 << test_case.perf_measurement_param << "\""); 309 << test_case.perf_measurement_param << "\"");
273 310
274 testing::ScopedExperimentalStateToggle scoped_experimental_state( 311 ScopedExperimentalStateToggle scoped_experimental_state(
275 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 312 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
276 : base::FeatureList::OVERRIDE_USE_DEFAULT, 313 : base::FeatureList::OVERRIDE_USE_DEFAULT,
277 {{kPerformanceMeasurementRateParameterName, 314 {{kPerformanceMeasurementRateParameterName,
278 test_case.perf_measurement_param}}); 315 test_case.perf_measurement_param}});
279 316
280 const auto active_configurations = GetActiveConfigurations(); 317 Configuration actual_configuration;
281 const Configuration& actual_configuration = 318 ExpectAndRetrieveExactlyOneEnabledConfig(&actual_configuration);
282 active_configurations->the_one_and_only(); 319 EXPECT_EQ(
283 EXPECT_EQ(test_case.expected_perf_measurement_rate, 320 test_case.expected_perf_measurement_rate,
284 actual_configuration.performance_measurement_rate); 321 actual_configuration.activation_options.performance_measurement_rate);
285 } 322 }
286 } 323 }
287 324
288 TEST(SubresourceFilterFeaturesTest, SuppressNotifications) { 325 TEST(SubresourceFilterFeaturesTest, SuppressNotifications) {
289 const struct { 326 const struct {
290 bool feature_enabled; 327 bool feature_enabled;
291 const char* suppress_notifications_param; 328 const char* suppress_notifications_param;
292 bool expected_suppress_notifications_value; 329 bool expected_suppress_notifications_value;
293 } kTestCases[] = {{false, "", false}, 330 } kTestCases[] = {{false, "", false},
294 {false, "true", false}, 331 {false, "true", false},
295 {false, "false", false}, 332 {false, "false", false},
296 {false, "invalid value", false}, 333 {false, "invalid value", false},
297 {true, "", false}, 334 {true, "", false},
298 {true, "false", false}, 335 {true, "false", false},
299 {true, "invalid value", false}, 336 {true, "invalid value", false},
300 {true, "True", true}, 337 {true, "True", true},
301 {true, "TRUE", true}, 338 {true, "TRUE", true},
302 {true, "true", true}}; 339 {true, "true", true}};
303 340
304 for (const auto& test_case : kTestCases) { 341 for (const auto& test_case : kTestCases) {
305 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 342 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
306 SCOPED_TRACE(::testing::Message("SuppressNotificationsParam = \"") 343 SCOPED_TRACE(::testing::Message("SuppressNotificationsParam = \"")
307 << test_case.suppress_notifications_param << "\""); 344 << test_case.suppress_notifications_param << "\"");
308 345
309 testing::ScopedExperimentalStateToggle scoped_experimental_state( 346 ScopedExperimentalStateToggle scoped_experimental_state(
310 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 347 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
311 : base::FeatureList::OVERRIDE_USE_DEFAULT, 348 : base::FeatureList::OVERRIDE_USE_DEFAULT,
312 {{kSuppressNotificationsParameterName, 349 {{kSuppressNotificationsParameterName,
313 test_case.suppress_notifications_param}}); 350 test_case.suppress_notifications_param}});
314 351
315 const auto active_configurations = GetActiveConfigurations(); 352 Configuration actual_configuration;
316 const Configuration& actual_configuration = 353 ExpectAndRetrieveExactlyOneEnabledConfig(&actual_configuration);
317 active_configurations->the_one_and_only(); 354 EXPECT_EQ(
318 EXPECT_EQ(test_case.expected_suppress_notifications_value, 355 test_case.expected_suppress_notifications_value,
319 actual_configuration.should_suppress_notifications); 356 actual_configuration.activation_options.should_suppress_notifications);
320 } 357 }
321 } 358 }
322 359
323 TEST(SubresourceFilterFeaturesTest, WhitelistSiteOnReload) { 360 TEST(SubresourceFilterFeaturesTest, WhitelistSiteOnReload) {
324 const struct { 361 const struct {
325 bool feature_enabled; 362 bool feature_enabled;
326 const char* whitelist_site_on_reload_param; 363 const char* whitelist_site_on_reload_param;
327 bool expected_whitelist_site_on_reload_value; 364 bool expected_whitelist_site_on_reload_value;
328 } kTestCases[] = {{false, "", false}, 365 } kTestCases[] = {{false, "", false},
329 {false, "true", false}, 366 {false, "true", false},
330 {false, "false", false}, 367 {false, "false", false},
331 {false, "invalid value", false}, 368 {false, "invalid value", false},
332 {true, "", false}, 369 {true, "", false},
333 {true, "false", false}, 370 {true, "false", false},
334 {true, "invalid value", false}, 371 {true, "invalid value", false},
335 {true, "True", true}, 372 {true, "True", true},
336 {true, "TRUE", true}, 373 {true, "TRUE", true},
337 {true, "true", true}}; 374 {true, "true", true}};
338 375
339 for (const auto& test_case : kTestCases) { 376 for (const auto& test_case : kTestCases) {
340 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 377 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
341 SCOPED_TRACE(::testing::Message("WhitelistSiteOnReloadParam = \"") 378 SCOPED_TRACE(::testing::Message("WhitelistSiteOnReloadParam = \"")
342 << test_case.whitelist_site_on_reload_param << "\""); 379 << test_case.whitelist_site_on_reload_param << "\"");
343 380
344 testing::ScopedExperimentalStateToggle scoped_experimental_state( 381 ScopedExperimentalStateToggle scoped_experimental_state(
345 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 382 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
346 : base::FeatureList::OVERRIDE_USE_DEFAULT, 383 : base::FeatureList::OVERRIDE_USE_DEFAULT,
347 {{kWhitelistSiteOnReloadParameterName, 384 {{kWhitelistSiteOnReloadParameterName,
348 test_case.whitelist_site_on_reload_param}}); 385 test_case.whitelist_site_on_reload_param}});
349 386
350 const auto active_configurations = GetActiveConfigurations(); 387 Configuration actual_configuration;
351 const Configuration& actual_configuration = 388 ExpectAndRetrieveExactlyOneEnabledConfig(&actual_configuration);
352 active_configurations->the_one_and_only();
353 EXPECT_EQ(test_case.expected_whitelist_site_on_reload_value, 389 EXPECT_EQ(test_case.expected_whitelist_site_on_reload_value,
354 actual_configuration.should_whitelist_site_on_reload); 390 actual_configuration.activation_options
391 .should_whitelist_site_on_reload);
355 } 392 }
356 } 393 }
357 394
358 TEST(SubresourceFilterFeaturesTest, RulesetFlavor) { 395 TEST(SubresourceFilterFeaturesTest, RulesetFlavor) {
359 const struct { 396 const struct {
360 bool feature_enabled; 397 bool feature_enabled;
361 const char* ruleset_flavor_param; 398 const char* ruleset_flavor_param;
362 const char* expected_ruleset_flavor_value; 399 const char* expected_ruleset_flavor_value;
363 } kTestCases[] = { 400 } kTestCases[] = {
364 {false, "", ""}, {false, "a", ""}, {false, "test value", ""}, 401 {false, "", ""}, {false, "a", ""}, {false, "test value", ""},
365 {true, "", ""}, {true, "a", "a"}, {true, "test value", "test value"}}; 402 {true, "", ""}, {true, "a", "a"}, {true, "test value", "test value"}};
366 403
367 for (const auto& test_case : kTestCases) { 404 for (const auto& test_case : kTestCases) {
368 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled); 405 SCOPED_TRACE(::testing::Message("Enabled = ") << test_case.feature_enabled);
369 SCOPED_TRACE(::testing::Message("Flavor = \"") 406 SCOPED_TRACE(::testing::Message("Flavor = \"")
370 << test_case.ruleset_flavor_param << "\""); 407 << test_case.ruleset_flavor_param << "\"");
371 408
372 testing::ScopedExperimentalStateToggle scoped_experimental_state( 409 ScopedExperimentalStateToggle scoped_experimental_state(
373 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE 410 test_case.feature_enabled ? base::FeatureList::OVERRIDE_ENABLE_FEATURE
374 : base::FeatureList::OVERRIDE_USE_DEFAULT, 411 : base::FeatureList::OVERRIDE_USE_DEFAULT,
375 {{kRulesetFlavorParameterName, test_case.ruleset_flavor_param}}); 412 {{kRulesetFlavorParameterName, test_case.ruleset_flavor_param}});
376 413
377 const auto active_configurations = GetActiveConfigurations(); 414 Configuration actual_configuration;
378 const Configuration& actual_configuration = 415 ExpectAndRetrieveExactlyOneEnabledConfig(&actual_configuration);
379 active_configurations->the_one_and_only();
380 EXPECT_EQ(std::string(test_case.expected_ruleset_flavor_value), 416 EXPECT_EQ(std::string(test_case.expected_ruleset_flavor_value),
381 actual_configuration.ruleset_flavor); 417 actual_configuration.general_settings.ruleset_flavor);
382 } 418 }
383 } 419 }
420
421 TEST(SubresourceFilterFeaturesTest, ConfigurationPriorities) {
422 const Configuration kConfigurationsByDecreasingPriority[] = {
pkalinnikov 2017/05/04 12:04:23 nit: Can you make this const std::vector<Configura
engedy 2017/05/05 12:25:42 Done.
423 {ActivationLevel::ENABLED, ActivationScope::ACTIVATION_LIST,
424 ActivationList::SUBRESOURCE_FILTER},
425 {ActivationLevel::ENABLED, ActivationScope::ACTIVATION_LIST,
426 ActivationList::PHISHING_INTERSTITIAL},
427 {ActivationLevel::ENABLED, ActivationScope::ACTIVATION_LIST,
428 ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL},
429 {ActivationLevel::ENABLED, ActivationScope::ALL_SITES},
430 {ActivationLevel::ENABLED, ActivationScope::NO_SITES},
431 {ActivationLevel::DRYRUN, ActivationScope::ACTIVATION_LIST},
432 {ActivationLevel::DRYRUN, ActivationScope::ALL_SITES},
433 {ActivationLevel::DRYRUN, ActivationScope::NO_SITES},
434 {ActivationLevel::DISABLED, ActivationScope::ACTIVATION_LIST},
435 {ActivationLevel::DISABLED, ActivationScope::ALL_SITES},
436 {ActivationLevel::DISABLED, ActivationScope::NO_SITES},
437 Configuration() /* default constructor */
pkalinnikov 2017/05/04 12:04:23 nit: comma?
engedy 2017/05/05 12:25:42 I am not sure I understand. We don't have trailing
pkalinnikov 2017/05/05 13:29:58 I suggested to put comma after /* default_construc
engedy 2017/05/05 19:24:10 OK, for consistency, let's not have a trailing com
438 };
439
440 const std::vector<Configuration> expected_order_by_decreasing_priority =
441 AsVector(kConfigurationsByDecreasingPriority);
442 std::vector<Configuration> reverse_order(
443 expected_order_by_decreasing_priority.rbegin(),
444 expected_order_by_decreasing_priority.rend());
445 subresource_filter::testing::ScopedSubresourceFilterConfigurator
446 scoped_configuration(std::move(reverse_order));
pkalinnikov 2017/05/04 12:04:23 #include <utility> for std::move.
engedy 2017/05/05 12:25:42 Done.
447 EXPECT_THAT(
448 GetEnabledConfigurations()->configs_by_decreasing_priority(),
449 ::testing::ElementsAreArray(expected_order_by_decreasing_priority));
450 }
451
452 TEST(SubresourceFilterFeaturesTest, MostSpecificRulesetFlavor) {
453 const struct {
454 const char* expected_ruleset_flavor_selected;
455 std::vector<std::string> ruleset_flavors;
456 } kTestCases[] = {{"", std::vector<std::string>()},
457 {"", {""}},
458 {"a", {"a"}},
459 {"e", {"e"}},
460 {"foo", {"foo"}},
461 {"", {"", ""}},
462 {"a", {"a", ""}},
463 {"a", {"", "a"}},
464 {"a", {"a", "a"}},
465 {"c", {"b", "", "c"}},
466 {"b", {"", "b", "a"}},
467 {"foo", {"", "a", "foo"}},
468 {"foo", {"foo", "bar", "b"}}};
469
470 for (const auto& test_case : kTestCases) {
471 SCOPED_TRACE(::testing::Message()
472 << "ruleset_flavors: "
473 << ::testing::PrintToString(test_case.ruleset_flavors));
474
475 std::vector<Configuration> configs;
476 for (const auto& ruleset_flavor : test_case.ruleset_flavors) {
477 Configuration config;
478 config.general_settings.ruleset_flavor = ruleset_flavor;
479 configs.push_back(std::move(config));
480 }
481
482 subresource_filter::testing::ScopedSubresourceFilterConfigurator
483 scoped_configuration(std::move(configs));
484 EXPECT_EQ(test_case.expected_ruleset_flavor_selected,
485 GetEnabledConfigurations()->GetMostSpecificRulesetFlavor());
486 }
487 }
488
489 TEST(SubresourceFilterFeaturesTest, EnabledConfigurations_FeatureDisabled) {
490 ScopedExperimentalStateToggle scoped_experimental_state(
491 base::FeatureList::OVERRIDE_USE_DEFAULT,
492 std::map<std::string, std::string>());
493
494 const auto config_list = GetEnabledConfigurations();
495 EXPECT_THAT(config_list->configs_by_decreasing_priority(),
496 ::testing::ElementsAre(Configuration()));
497 EXPECT_EQ(std::string(), config_list->GetMostSpecificRulesetFlavor());
498 }
499
500 TEST(SubresourceFilterFeaturesTest,
501 EnabledConfigurations_FeatureEnabledWithNoParameters) {
502 ScopedExperimentalStateToggle scoped_experimental_state(
503 base::FeatureList::OVERRIDE_USE_DEFAULT, {});
504
505 const auto config_list = GetEnabledConfigurations();
506 EXPECT_THAT(config_list->configs_by_decreasing_priority(),
507 ::testing::ElementsAre(Configuration()));
508 EXPECT_EQ(std::string(), config_list->GetMostSpecificRulesetFlavor());
509 }
510
511 TEST(SubresourceFilterFeaturesTest, PresetForLiveRunOnPhishingSites) {
512 ExpectPresetCanBeEnabledByName(
513 Configuration::MakePresetForLiveRunOnPhishingSites(),
514 kPresetLiveRunOnPhishingSites);
515 ExpectPresetIsEquivalentToVariationParams(
516 Configuration::MakePresetForLiveRunOnPhishingSites(),
517 {{kActivationLevelParameterName, kActivationLevelEnabled},
518 {kActivationScopeParameterName, kActivationScopeActivationList},
519 {kActivationListsParameterName, kActivationListPhishingInterstitial}});
520 }
521
522 TEST(SubresourceFilterFeaturesTest,
523 PresetForPerformanceTestingDryRunOnAllSites) {
524 ExpectPresetCanBeEnabledByName(
525 Configuration::MakePresetForPerformanceTestingDryRunOnAllSites(),
526 kPresetPerformanceTestingDryRunOnAllSites);
527 ExpectPresetIsEquivalentToVariationParams(
528 Configuration::MakePresetForPerformanceTestingDryRunOnAllSites(),
529 {{kActivationLevelParameterName, kActivationLevelDryRun},
530 {kActivationScopeParameterName, kActivationScopeAllSites},
531 {kPerformanceMeasurementRateParameterName, "1.0"}});
532 }
533
534 TEST(SubresourceFilterFeaturesTest, EnabledConfigurations_MultiplePresets) {
535 const std::string kPhishing(kPresetLiveRunOnPhishingSites);
536 const std::string kPerfTest(kPresetPerformanceTestingDryRunOnAllSites);
537 const struct {
538 std::string preset_name_list;
539 } kTestCases[] = {
540 {kPhishing + "," + kPerfTest},
541 {kPerfTest + "," + kPhishing},
542 {base::ToUpperASCII(kPhishing) + "," + base::ToUpperASCII(kPerfTest)},
543 {",, ," + kPerfTest + ",," + kPhishing},
544 {"garbage,garbage2," + kPerfTest + "," + kPhishing}};
545
546 for (const auto& test_case : kTestCases) {
547 SCOPED_TRACE(::testing::Message()
548 << "preset_name_list: " << test_case.preset_name_list);
549
550 ScopedExperimentalStateToggle scoped_experimental_state(
551 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
552 {{kEnablePresetsParameterName, test_case.preset_name_list}});
553
554 const auto config_list = GetEnabledConfigurations();
555 EXPECT_THAT(
556 config_list->configs_by_decreasing_priority(),
557 ::testing::ElementsAre(
558 Configuration::MakePresetForLiveRunOnPhishingSites(),
559 Configuration::MakePresetForPerformanceTestingDryRunOnAllSites(),
560 Configuration()));
561 EXPECT_EQ(std::string(), config_list->GetMostSpecificRulesetFlavor());
562 }
563 }
564
565 TEST(SubresourceFilterFeaturesTest,
566 EnabledConfigurations_MultiplePresetsAndExperimentalConfig) {
567 const std::string kPhishing(kPresetLiveRunOnPhishingSites);
568 const std::string kPerfTest(kPresetPerformanceTestingDryRunOnAllSites);
569 const std::string kTestRulesetFlavor("foobar");
570
571 ScopedExperimentalStateToggle scoped_experimental_state(
572 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
573 {{kEnablePresetsParameterName, kPhishing + "," + kPerfTest},
574 {kActivationLevelParameterName, kActivationLevelDryRun},
575 {kActivationScopeParameterName, kActivationScopeActivationList},
576 {kActivationListsParameterName, kActivationListSubresourceFilter},
577 {kRulesetFlavorParameterName, kTestRulesetFlavor}});
578
579 Configuration experimental_config(ActivationLevel::DRYRUN,
580 ActivationScope::ACTIVATION_LIST,
581 ActivationList::SUBRESOURCE_FILTER);
582 experimental_config.general_settings.ruleset_flavor = kTestRulesetFlavor;
583
584 const auto config_list = GetEnabledConfigurations();
585 EXPECT_THAT(
586 config_list->configs_by_decreasing_priority(),
587 ::testing::ElementsAre(
588 Configuration::MakePresetForLiveRunOnPhishingSites(),
589 experimental_config,
590 Configuration::MakePresetForPerformanceTestingDryRunOnAllSites()));
591 EXPECT_EQ(kTestRulesetFlavor, config_list->GetMostSpecificRulesetFlavor());
592 }
593
384 } // namespace subresource_filter 594 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698