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 "chrome/browser/component_updater/subresource_filter_component_installe
r.h" | 5 #include "chrome/browser/component_updater/subresource_filter_component_installe
r.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/metrics/field_trial.h" | 16 #include "base/metrics/field_trial.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
19 #include "base/test/test_simple_task_runner.h" | 19 #include "base/test/test_simple_task_runner.h" |
20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
21 #include "base/version.h" | 21 #include "base/version.h" |
22 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" |
23 #include "components/component_updater/mock_component_updater_service.h" | 23 #include "components/component_updater/mock_component_updater_service.h" |
24 #include "components/prefs/testing_pref_service.h" | 24 #include "components/prefs/testing_pref_service.h" |
25 #include "components/subresource_filter/content/browser/content_ruleset_service_
delegate.h" | 25 #include "components/subresource_filter/content/browser/content_ruleset_service.
h" |
26 #include "components/subresource_filter/core/browser/ruleset_service.h" | 26 #include "components/subresource_filter/core/browser/ruleset_service.h" |
27 #include "components/subresource_filter/core/browser/subresource_filter_constant
s.h" | 27 #include "components/subresource_filter/core/browser/subresource_filter_constant
s.h" |
28 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" | 28 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
29 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" | 29 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" |
30 #include "content/public/test/test_browser_thread_bundle.h" | 30 #include "content/public/test/test_browser_thread_bundle.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "testing/platform_test.h" | 32 #include "testing/platform_test.h" |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 static const char kTestRulesetVersion[] = "1.2.3.4"; | 36 static const char kTestRulesetVersion[] = "1.2.3.4"; |
37 | 37 |
38 class TestRulesetService : public subresource_filter::RulesetService { | 38 class TestRulesetService : public subresource_filter::RulesetService { |
39 public: | 39 public: |
40 TestRulesetService(PrefService* local_state, | 40 TestRulesetService(PrefService* local_state, |
41 scoped_refptr<base::SequencedTaskRunner> task_runner, | 41 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 42 subresource_filter::ContentRulesetService* content_service, |
42 const base::FilePath& base_dir) | 43 const base::FilePath& base_dir) |
43 : subresource_filter::RulesetService( | 44 : subresource_filter::RulesetService(local_state, |
44 local_state, | 45 task_runner, |
45 task_runner, | 46 content_service, |
46 base::MakeUnique< | 47 base_dir) {} |
47 subresource_filter::ContentRulesetServiceDelegate>(), | |
48 base_dir) {} | |
49 | 48 |
50 ~TestRulesetService() override {} | 49 ~TestRulesetService() override {} |
51 | 50 |
52 using UnindexedRulesetInfo = subresource_filter::UnindexedRulesetInfo; | 51 using UnindexedRulesetInfo = subresource_filter::UnindexedRulesetInfo; |
53 void IndexAndStoreAndPublishRulesetIfNeeded( | 52 void IndexAndStoreAndPublishRulesetIfNeeded( |
54 const UnindexedRulesetInfo& unindexed_ruleset_info) override { | 53 const UnindexedRulesetInfo& unindexed_ruleset_info) override { |
55 unindexed_ruleset_info_ = unindexed_ruleset_info; | 54 unindexed_ruleset_info_ = unindexed_ruleset_info; |
56 } | 55 } |
57 | 56 |
58 const base::FilePath& ruleset_path() const { | 57 const base::FilePath& ruleset_path() const { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 SubresourceFilterComponentInstallerTest() {} | 95 SubresourceFilterComponentInstallerTest() {} |
97 | 96 |
98 void SetUp() override { | 97 void SetUp() override { |
99 PlatformTest::SetUp(); | 98 PlatformTest::SetUp(); |
100 | 99 |
101 ASSERT_TRUE(component_install_dir_.CreateUniqueTempDir()); | 100 ASSERT_TRUE(component_install_dir_.CreateUniqueTempDir()); |
102 ASSERT_TRUE(ruleset_service_dir_.CreateUniqueTempDir()); | 101 ASSERT_TRUE(ruleset_service_dir_.CreateUniqueTempDir()); |
103 subresource_filter::IndexedRulesetVersion::RegisterPrefs( | 102 subresource_filter::IndexedRulesetVersion::RegisterPrefs( |
104 pref_service_.registry()); | 103 pref_service_.registry()); |
105 | 104 |
106 std::unique_ptr<subresource_filter::RulesetService> service( | 105 auto content_service = |
107 new TestRulesetService(&pref_service_, task_runner_, | 106 base::MakeUnique<subresource_filter::ContentRulesetService>(); |
108 ruleset_service_dir_.GetPath())); | 107 auto test_ruleset_service = base::MakeUnique<TestRulesetService>( |
| 108 &pref_service_, task_runner_, content_service.get(), |
| 109 ruleset_service_dir_.GetPath()); |
| 110 test_ruleset_service_ = test_ruleset_service.get(); |
| 111 content_service->set_ruleset_service(std::move(test_ruleset_service)); |
109 | 112 |
110 TestingBrowserProcess::GetGlobal()->SetRulesetService(std::move(service)); | 113 TestingBrowserProcess::GetGlobal()->SetRulesetService( |
| 114 std::move(content_service)); |
111 traits_.reset(new SubresourceFilterComponentInstallerTraits()); | 115 traits_.reset(new SubresourceFilterComponentInstallerTraits()); |
112 } | 116 } |
113 | 117 |
114 TestRulesetService* service() { | 118 TestRulesetService* service() { return test_ruleset_service_; } |
115 return static_cast<TestRulesetService*>( | |
116 TestingBrowserProcess::GetGlobal() | |
117 ->subresource_filter_ruleset_service()); | |
118 } | |
119 | 119 |
120 void WriteStringToFile(const std::string data, const base::FilePath& path) { | 120 void WriteStringToFile(const std::string data, const base::FilePath& path) { |
121 ASSERT_EQ(static_cast<int32_t>(data.length()), | 121 ASSERT_EQ(static_cast<int32_t>(data.length()), |
122 base::WriteFile(path, data.data(), data.length())); | 122 base::WriteFile(path, data.data(), data.length())); |
123 } | 123 } |
124 | 124 |
125 base::FilePath component_install_dir() { | 125 base::FilePath component_install_dir() { |
126 return component_install_dir_.GetPath(); | 126 return component_install_dir_.GetPath(); |
127 } | 127 } |
128 | 128 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 172 |
173 private: | 173 private: |
174 base::ScopedTempDir component_install_dir_; | 174 base::ScopedTempDir component_install_dir_; |
175 base::ScopedTempDir ruleset_service_dir_; | 175 base::ScopedTempDir ruleset_service_dir_; |
176 | 176 |
177 content::TestBrowserThreadBundle thread_bundle_; | 177 content::TestBrowserThreadBundle thread_bundle_; |
178 std::unique_ptr<SubresourceFilterComponentInstallerTraits> traits_; | 178 std::unique_ptr<SubresourceFilterComponentInstallerTraits> traits_; |
179 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 179 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
180 TestingPrefServiceSimple pref_service_; | 180 TestingPrefServiceSimple pref_service_; |
181 | 181 |
| 182 TestRulesetService* test_ruleset_service_ = nullptr; |
| 183 |
182 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterComponentInstallerTest); | 184 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterComponentInstallerTest); |
183 }; | 185 }; |
184 | 186 |
185 TEST_F(SubresourceFilterComponentInstallerTest, | 187 TEST_F(SubresourceFilterComponentInstallerTest, |
186 TestComponentRegistrationWhenFeatureDisabled) { | 188 TestComponentRegistrationWhenFeatureDisabled) { |
187 base::FieldTrialList field_trial_list(nullptr); | 189 base::FieldTrialList field_trial_list(nullptr); |
188 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle | 190 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle |
189 scoped_feature_toggle(base::FeatureList::OVERRIDE_DISABLE_FEATURE, | 191 scoped_feature_toggle(base::FeatureList::OVERRIDE_DISABLE_FEATURE, |
190 subresource_filter::kActivationLevelEnabled, | 192 subresource_filter::kActivationLevelEnabled, |
191 subresource_filter::kActivationScopeNoSites); | 193 subresource_filter::kActivationScopeNoSites); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 294 |
293 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); | 295 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); |
294 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle | 296 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle |
295 scoped_feature_toggle( | 297 scoped_feature_toggle( |
296 base::FeatureList::OVERRIDE_USE_DEFAULT, | 298 base::FeatureList::OVERRIDE_USE_DEFAULT, |
297 {{subresource_filter::kRulesetFlavorParameterName, kTagValue}}); | 299 {{subresource_filter::kRulesetFlavorParameterName, kTagValue}}); |
298 EXPECT_EQ(update_client::InstallerAttributes(), GetInstallerAttributes()); | 300 EXPECT_EQ(update_client::InstallerAttributes(), GetInstallerAttributes()); |
299 } | 301 } |
300 | 302 |
301 } // namespace component_updater | 303 } // namespace component_updater |
OLD | NEW |