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