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
_test_support.h" | 5 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" |
6 | 6 |
7 #include <map> | 7 #include <utility> |
8 #include <memory> | |
9 | 8 |
10 #include "base/metrics/field_trial.h" | 9 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/field_trial_params.h" | 10 #include "base/memory/ref_counted.h" |
12 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" | 11 #include "base/strings/string_util.h" |
13 #include "components/variations/variations_associated_data.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | 12 |
16 namespace subresource_filter { | 13 namespace subresource_filter { |
17 namespace testing { | 14 namespace testing { |
18 | 15 |
19 namespace { | 16 // ScopedSubresourceFilterConfigurator ---------------------------------------- |
20 constexpr const char kTestFieldTrialName[] = "FieldTrialNameShouldNotMatter"; | 17 |
21 constexpr const char kTestExperimentGroupName[] = "GroupNameShouldNotMatter"; | 18 ScopedSubresourceFilterConfigurator::ScopedSubresourceFilterConfigurator( |
22 } // namespace | 19 scoped_refptr<ConfigurationList> configs) |
| 20 : original_config_(GetAndSetActivateConfigurations(configs)) {} |
| 21 |
| 22 ScopedSubresourceFilterConfigurator::ScopedSubresourceFilterConfigurator( |
| 23 Configuration config) |
| 24 : ScopedSubresourceFilterConfigurator( |
| 25 base::MakeShared<ConfigurationList>(std::move(config))) {} |
| 26 |
| 27 ScopedSubresourceFilterConfigurator::~ScopedSubresourceFilterConfigurator() { |
| 28 GetAndSetActivateConfigurations(std::move(original_config_)); |
| 29 } |
| 30 |
| 31 void ScopedSubresourceFilterConfigurator::ResetConfiguration( |
| 32 Configuration config) { |
| 33 GetAndSetActivateConfigurations( |
| 34 base::MakeShared<ConfigurationList>(std::move(config))); |
| 35 } |
| 36 |
| 37 // ScopedSubresourceFilterFeatureToggle --------------------------------------- |
23 | 38 |
24 ScopedSubresourceFilterFeatureToggle::ScopedSubresourceFilterFeatureToggle( | 39 ScopedSubresourceFilterFeatureToggle::ScopedSubresourceFilterFeatureToggle( |
25 base::FeatureList::OverrideState feature_state, | 40 base::FeatureList::OverrideState feature_state) { |
26 const std::string& maximum_activation_level, | 41 if (feature_state == base::FeatureList::OVERRIDE_ENABLE_FEATURE) |
27 const std::string& activation_scope, | 42 scoped_feature_list_.InitAndEnableFeature(kSafeBrowsingSubresourceFilter); |
28 const std::string& activation_lists, | 43 else if (feature_state == base::FeatureList::OVERRIDE_DISABLE_FEATURE) |
29 const std::string& performance_measurement_rate, | 44 scoped_feature_list_.InitAndDisableFeature(kSafeBrowsingSubresourceFilter); |
30 const std::string& suppress_notifications, | |
31 const std::string& whitelist_site_on_reload) | |
32 : ScopedSubresourceFilterFeatureToggle( | |
33 feature_state, | |
34 {{kActivationLevelParameterName, maximum_activation_level}, | |
35 {kActivationScopeParameterName, activation_scope}, | |
36 {kActivationListsParameterName, activation_lists}, | |
37 {kPerformanceMeasurementRateParameterName, | |
38 performance_measurement_rate}, | |
39 {kSuppressNotificationsParameterName, suppress_notifications}, | |
40 {kWhitelistSiteOnReloadParameterName, whitelist_site_on_reload}}) {} | |
41 | |
42 ScopedSubresourceFilterFeatureToggle::ScopedSubresourceFilterFeatureToggle( | |
43 base::FeatureList::OverrideState feature_state, | |
44 std::map<std::string, std::string> variation_params) { | |
45 EXPECT_TRUE(base::AssociateFieldTrialParams( | |
46 kTestFieldTrialName, kTestExperimentGroupName, variation_params)); | |
47 | |
48 base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial( | |
49 kTestFieldTrialName, kTestExperimentGroupName); | |
50 | |
51 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
52 feature_list->RegisterFieldTrialOverride(kSafeBrowsingSubresourceFilter.name, | |
53 feature_state, field_trial); | |
54 | |
55 // Since we are adding a scoped feature list after browser start, copy over | |
56 // the existing feature list to prevent inconsistency. | |
57 base::FeatureList* existing_feature_list = base::FeatureList::GetInstance(); | |
58 if (existing_feature_list) { | |
59 std::string enabled_features; | |
60 std::string disabled_features; | |
61 base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features, | |
62 &disabled_features); | |
63 feature_list->InitializeFromCommandLine(enabled_features, | |
64 disabled_features); | |
65 } | |
66 | |
67 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); | |
68 | |
69 // Force the active ConfigurationList to be reparsed on next access so that | |
70 // the variation parameters come into effect. | |
71 ClearCachedActiveConfigurations(); | |
72 } | 45 } |
73 | 46 |
74 ScopedSubresourceFilterFeatureToggle::~ScopedSubresourceFilterFeatureToggle() { | 47 ScopedSubresourceFilterFeatureToggle::~ScopedSubresourceFilterFeatureToggle() {} |
75 variations::testing::ClearAllVariationParams(); | |
76 | |
77 // Force the active ConfigurationList to be reparsed on next access, so that | |
78 // the overrides from this instance are no longer in effect. | |
79 ClearCachedActiveConfigurations(); | |
80 } | |
81 | 48 |
82 } // namespace testing | 49 } // namespace testing |
83 } // namespace subresource_filter | 50 } // namespace subresource_filter |
OLD | NEW |