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

Side by Side Diff: chrome/browser/subresource_filter/subresource_filter_unittest.cc

Issue 2850373002: Make ChromeSubresourceFilterClient a WebContentsUserData (Closed)
Patch Set: 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 <memory> 5 #include <memory>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/feature_list.h" 8 #include "base/feature_list.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/metrics/field_trial.h" 12 #include "base/metrics/field_trial.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/test/scoped_feature_list.h" 14 #include "base/test/scoped_feature_list.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "chrome/browser/after_startup_task_utils.h" 16 #include "chrome/browser/after_startup_task_utils.h"
17 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 17 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
18 #include "chrome/browser/infobars/infobar_service.h" 18 #include "chrome/browser/infobars/infobar_service.h"
19 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" 19 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
20 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" 20 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
21 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" 21 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h"
22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
23 #include "chrome/test/base/testing_browser_process.h" 23 #include "chrome/test/base/testing_browser_process.h"
24 #include "components/prefs/testing_pref_service.h" 24 #include "components/prefs/testing_pref_service.h"
25 #include "components/safe_browsing_db/v4_protocol_manager_util.h" 25 #include "components/safe_browsing_db/v4_protocol_manager_util.h"
26 #include "components/subresource_filter/content/browser/content_ruleset_service. h" 26 #include "components/subresource_filter/content/browser/content_ruleset_service. h"
27 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" 27 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h"
engedy 2017/05/02 12:24:20 nit: Is this still needed?
Charlie Harrison 2017/05/02 16:01:57 Nope, Done.
28 #include "components/subresource_filter/content/browser/fake_safe_browsing_datab ase_manager.h" 28 #include "components/subresource_filter/content/browser/fake_safe_browsing_datab ase_manager.h"
29 #include "components/subresource_filter/core/browser/ruleset_service.h" 29 #include "components/subresource_filter/core/browser/ruleset_service.h"
30 #include "components/subresource_filter/core/browser/subresource_filter_features .h" 30 #include "components/subresource_filter/core/browser/subresource_filter_features .h"
31 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" 31 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h"
32 #include "components/subresource_filter/core/common/test_ruleset_creator.h" 32 #include "components/subresource_filter/core/common/test_ruleset_creator.h"
33 #include "content/public/browser/navigation_throttle.h" 33 #include "content/public/browser/navigation_throttle.h"
34 #include "content/public/test/navigation_simulator.h" 34 #include "content/public/test/navigation_simulator.h"
35 #include "content/public/test/test_renderer_host.h" 35 #include "content/public/test/test_renderer_host.h"
36 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
37 #include "url/gurl.h" 37 #include "url/gurl.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 subresource_filter::testing::TestRulesetPair test_ruleset_pair; 93 subresource_filter::testing::TestRulesetPair test_ruleset_pair;
94 ruleset_creator.CreateRulesetToDisallowURLsWithPathSuffix( 94 ruleset_creator.CreateRulesetToDisallowURLsWithPathSuffix(
95 "disallowed.html", &test_ruleset_pair); 95 "disallowed.html", &test_ruleset_pair);
96 subresource_filter::testing::TestRulesetPublisher test_ruleset_publisher; 96 subresource_filter::testing::TestRulesetPublisher test_ruleset_publisher;
97 ASSERT_NO_FATAL_FAILURE( 97 ASSERT_NO_FATAL_FAILURE(
98 test_ruleset_publisher.SetRuleset(test_ruleset_pair.unindexed)); 98 test_ruleset_publisher.SetRuleset(test_ruleset_pair.unindexed));
99 99
100 // Set up the tab helpers. 100 // Set up the tab helpers.
101 InfoBarService::CreateForWebContents(web_contents()); 101 InfoBarService::CreateForWebContents(web_contents());
102 TabSpecificContentSettings::CreateForWebContents(web_contents()); 102 TabSpecificContentSettings::CreateForWebContents(web_contents());
103 ChromeSubresourceFilterClient::CreateForWebContents(web_contents());
103 104
104 std::unique_ptr<ChromeSubresourceFilterClient> subresource_filter_client(
105 new ChromeSubresourceFilterClient(web_contents()));
106 client_ = subresource_filter_client.get();
107 subresource_filter::ContentSubresourceFilterDriverFactory::
108 CreateForWebContents(web_contents(),
109 std::move(subresource_filter_client));
110 base::RunLoop().RunUntilIdle(); 105 base::RunLoop().RunUntilIdle();
111 } 106 }
112 107
113 void TearDown() override { 108 void TearDown() override {
114 fake_safe_browsing_database_ = nullptr; 109 fake_safe_browsing_database_ = nullptr;
115 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); 110 TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown();
116 111
117 // Must explicitly set these to null and pump the run loop to ensure that 112 // Must explicitly set these to null and pump the run loop to ensure that
118 // all cleanup related to these classes actually happens. 113 // all cleanup related to these classes actually happens.
119 TestingBrowserProcess::GetGlobal()->SetRulesetService(nullptr); 114 TestingBrowserProcess::GetGlobal()->SetRulesetService(nullptr);
(...skipping 24 matching lines...) Expand all
144 content::RenderFrameHostTester::For(parent)->AppendChild("subframe"); 139 content::RenderFrameHostTester::For(parent)->AppendChild("subframe");
145 return SimulateNavigateAndCommit( 140 return SimulateNavigateAndCommit(
146 GURL("https://example.test/disallowed.html"), subframe); 141 GURL("https://example.test/disallowed.html"), subframe);
147 } 142 }
148 143
149 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { 144 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) {
150 fake_safe_browsing_database_->AddBlacklistedUrl( 145 fake_safe_browsing_database_->AddBlacklistedUrl(
151 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); 146 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER);
152 } 147 }
153 148
154 ChromeSubresourceFilterClient* client() { return client_; } 149 ChromeSubresourceFilterClient* client() {
150 return ChromeSubresourceFilterClient::FromWebContents(web_contents());
151 }
155 152
156 private: 153 private:
157 base::ScopedTempDir ruleset_service_dir_; 154 base::ScopedTempDir ruleset_service_dir_;
158 base::FieldTrialList field_trial_list_; 155 base::FieldTrialList field_trial_list_;
159 base::test::ScopedFeatureList scoped_feature_list_; 156 base::test::ScopedFeatureList scoped_feature_list_;
160 std::unique_ptr< 157 std::unique_ptr<
161 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle> 158 subresource_filter::testing::ScopedSubresourceFilterFeatureToggle>
162 scoped_feature_toggle_; 159 scoped_feature_toggle_;
163 TestingPrefServiceSimple pref_service_; 160 TestingPrefServiceSimple pref_service_;
164 161
165 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; 162 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_;
166 163
167 ChromeSubresourceFilterClient* client_ = nullptr;
168
169 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterTest); 164 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterTest);
170 }; 165 };
171 166
172 TEST_F(SubresourceFilterTest, SimpleAllowedLoad) { 167 TEST_F(SubresourceFilterTest, SimpleAllowedLoad) {
173 GURL url("https://example.test"); 168 GURL url("https://example.test");
174 SimulateNavigateAndCommit(url, main_rfh()); 169 SimulateNavigateAndCommit(url, main_rfh());
175 EXPECT_TRUE(CreateAndNavigateDisallowedSubframe(main_rfh())); 170 EXPECT_TRUE(CreateAndNavigateDisallowedSubframe(main_rfh()));
176 EXPECT_FALSE(client()->did_show_ui_for_navigation()); 171 EXPECT_FALSE(client()->did_show_ui_for_navigation());
177 } 172 }
178 173
179 TEST_F(SubresourceFilterTest, SimpleDisallowedLoad) { 174 TEST_F(SubresourceFilterTest, SimpleDisallowedLoad) {
180 GURL url("https://example.test"); 175 GURL url("https://example.test");
181 ConfigureAsSubresourceFilterOnlyURL(url); 176 ConfigureAsSubresourceFilterOnlyURL(url);
182 SimulateNavigateAndCommit(url, main_rfh()); 177 SimulateNavigateAndCommit(url, main_rfh());
183 EXPECT_FALSE(CreateAndNavigateDisallowedSubframe(main_rfh())); 178 EXPECT_FALSE(CreateAndNavigateDisallowedSubframe(main_rfh()));
184 EXPECT_TRUE(client()->did_show_ui_for_navigation()); 179 EXPECT_TRUE(client()->did_show_ui_for_navigation());
185 } 180 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698