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

Side by Side Diff: chrome/browser/download/download_request_limiter_unittest.cc

Issue 2561673003: Handle per-tab AUTOMATIC_DOWNLOADS setting in DownloadRequestLimiter. (Closed)
Patch Set: Remove custom HostContentSettingsMap from DRL unit tests. Created 3 years, 9 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/download/download_request_limiter.h" 5 #include "chrome/browser/download/download_request_limiter.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "chrome/browser/chrome_notification_types.h" 11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h" 14 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "content/public/browser/navigation_controller.h" 15 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_details.h" 16 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/notification_service.h" 17 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h" 18 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/frame_navigate_params.h" 20 #include "content/public/common/frame_navigate_params.h"
22 #include "content/public/test/web_contents_tester.h" 21 #include "content/public/test/web_contents_tester.h"
23 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
24 23
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 private: 135 private:
137 std::unique_ptr<MockPermissionPromptFactory> mock_permission_prompt_factory_; 136 std::unique_ptr<MockPermissionPromptFactory> mock_permission_prompt_factory_;
138 }; 137 };
139 #endif 138 #endif
140 } // namespace 139 } // namespace
141 140
142 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { 141 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness {
143 public: 142 public:
144 void SetUp() override { 143 void SetUp() override {
145 ChromeRenderViewHostTestHarness::SetUp(); 144 ChromeRenderViewHostTestHarness::SetUp();
146 profile_.reset(new TestingProfile());
147 testing_delegate_.SetUp(web_contents()); 145 testing_delegate_.SetUp(web_contents());
148 146
149 UpdateExpectations(ACCEPT); 147 UpdateExpectations(ACCEPT);
150 cancel_count_ = continue_count_ = 0; 148 cancel_count_ = continue_count_ = 0;
151 download_request_limiter_ = new DownloadRequestLimiter(); 149 download_request_limiter_ = new DownloadRequestLimiter();
152
153 content_settings_ = new HostContentSettingsMap(
154 profile_->GetPrefs(), false /* incognito_profile */,
155 false /* guest_profile */);
156 DownloadRequestLimiter::SetContentSettingsForTesting(
157 content_settings_.get());
158 } 150 }
159 151
160 void TearDown() override { 152 void TearDown() override {
alshabalin 2017/03/01 12:18:23 DownloadRequestLimiter::TabDownloadState now depen
161 content_settings_->ShutdownOnUIThread();
162 content_settings_ = nullptr;
163 testing_delegate_.TearDown(); 153 testing_delegate_.TearDown();
164 154
165 // |profile_| must be teared down before
166 // |RenderViewHostTestHarness::thread_bundle_|.
167 profile_.reset();
168
169 ChromeRenderViewHostTestHarness::TearDown(); 155 ChromeRenderViewHostTestHarness::TearDown();
170 } 156 }
171 157
172 void CanDownload() { 158 void CanDownload() {
173 CanDownloadFor(web_contents()); 159 CanDownloadFor(web_contents());
174 } 160 }
175 161
176 void CanDownloadFor(WebContents* web_contents) { 162 void CanDownloadFor(WebContents* web_contents) {
177 download_request_limiter_->CanDownloadImpl( 163 download_request_limiter_->CanDownloadImpl(
178 web_contents, 164 web_contents,
(...skipping 26 matching lines...) Expand all
205 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; 191 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line;
206 continue_count_ = cancel_count_ = 0; 192 continue_count_ = cancel_count_ = 0;
207 testing_delegate_.ResetCounts(); 193 testing_delegate_.ResetCounts();
208 } 194 }
209 195
210 void UpdateContentSettings(WebContents* web_contents, 196 void UpdateContentSettings(WebContents* web_contents,
211 ContentSetting setting) { 197 ContentSetting setting) {
212 // Ensure a download state exists. 198 // Ensure a download state exists.
213 download_request_limiter_->GetDownloadState(web_contents, nullptr, true); 199 download_request_limiter_->GetDownloadState(web_contents, nullptr, true);
214 SetHostContentSetting(web_contents, setting); 200 SetHostContentSetting(web_contents, setting);
215
216 // Manually send the update notification. In the browser, this is sent from
217 // ContentSettingRPHBubbleModel.
218 content::NotificationService::current()->Notify(
219 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
220 content::Source<WebContents>(web_contents),
221 content::NotificationService::NoDetails());
222 } 201 }
223 202
224 protected: 203 protected:
225 void ContinueDownload(bool allow) { 204 void ContinueDownload(bool allow) {
226 if (allow) { 205 if (allow) {
227 continue_count_++; 206 continue_count_++;
228 } else { 207 } else {
229 cancel_count_++; 208 cancel_count_++;
230 } 209 }
231 } 210 }
232 211
233 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { 212 void SetHostContentSetting(WebContents* contents, ContentSetting setting) {
234 content_settings_->SetContentSettingDefaultScope( 213 DownloadRequestLimiter::GetContentSettings(contents)
dominickn 2017/03/02 03:32:52 Instead of exposing GetContentSettings, can you no
alshabalin 2017/03/02 09:54:01 Done. DownloadRequestLimiter::GetContentSettings i
235 contents->GetURL(), GURL(), CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, 214 ->SetContentSettingDefaultScope(
236 std::string(), setting); 215 contents->GetURL(), GURL(),
216 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, std::string(), setting);
237 } 217 }
238 218
239 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } 219 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); }
240 220
241 int AskAllowCount() { return testing_delegate_.AllowCount(); } 221 int AskAllowCount() { return testing_delegate_.AllowCount(); }
242 222
243 void UpdateExpectations(TestingAction action) { 223 void UpdateExpectations(TestingAction action) {
244 testing_delegate_.UpdateExpectations(action); 224 testing_delegate_.UpdateExpectations(action);
245 } 225 }
246 226
247 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; 227 scoped_refptr<DownloadRequestLimiter> download_request_limiter_;
248 228
249 // Number of times ContinueDownload was invoked. 229 // Number of times ContinueDownload was invoked.
250 int continue_count_; 230 int continue_count_;
251 231
252 // Number of times CancelDownload was invoked. 232 // Number of times CancelDownload was invoked.
253 int cancel_count_; 233 int cancel_count_;
254 234
255 scoped_refptr<HostContentSettingsMap> content_settings_;
256 TestingDelegate testing_delegate_; 235 TestingDelegate testing_delegate_;
257
258 private:
259 std::unique_ptr<TestingProfile> profile_;
260 }; 236 };
261 237
262 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_Allow) { 238 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_Allow) {
263 NavigateAndCommit(GURL("http://foo.com/bar")); 239 NavigateAndCommit(GURL("http://foo.com/bar"));
264 LoadCompleted(); 240 LoadCompleted();
265 241
266 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. 242 // All tabs should initially start at ALLOW_ONE_DOWNLOAD.
267 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, 243 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
268 download_request_limiter_->GetDownloadStatus(web_contents())); 244 download_request_limiter_->GetDownloadStatus(web_contents()));
269 245
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 LoadCompleted(); 619 LoadCompleted();
644 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); 620 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW);
645 621
646 CanDownload(); 622 CanDownload();
647 ExpectAndResetCounts(1, 0, 0, __LINE__); 623 ExpectAndResetCounts(1, 0, 0, __LINE__);
648 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 624 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
649 download_request_limiter_->GetDownloadStatus(web_contents())); 625 download_request_limiter_->GetDownloadStatus(web_contents()));
650 626
651 CanDownload(); 627 CanDownload();
652 ExpectAndResetCounts(1, 0, 0, __LINE__); 628 ExpectAndResetCounts(1, 0, 0, __LINE__);
653 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 629 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS,
654 download_request_limiter_->GetDownloadStatus(web_contents())); 630 download_request_limiter_->GetDownloadStatus(web_contents()));
655 631
656 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); 632 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK);
657 633
658 CanDownload(); 634 CanDownload();
659 ExpectAndResetCounts(0, 1, 0, __LINE__); 635 ExpectAndResetCounts(0, 1, 0, __LINE__);
660 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 636 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
661 download_request_limiter_->GetDownloadStatus(web_contents())); 637 download_request_limiter_->GetDownloadStatus(web_contents()));
662 638
663 CanDownload(); 639 CanDownload();
664 ExpectAndResetCounts(0, 1, 0, __LINE__); 640 ExpectAndResetCounts(0, 1, 0, __LINE__);
665 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 641 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED,
666 download_request_limiter_->GetDownloadStatus(web_contents())); 642 download_request_limiter_->GetDownloadStatus(web_contents()));
667 } 643 }
668 644
669 TEST_F(DownloadRequestLimiterTest, 645 TEST_F(DownloadRequestLimiterTest,
670 DownloadRequestLimiter_ContentSettingChanged) { 646 DownloadRequestLimiter_ContentSettingChanged) {
671 NavigateAndCommit(GURL("http://foo.com/bar")); 647 NavigateAndCommit(GURL("http://foo.com/bar"));
672 LoadCompleted(); 648 LoadCompleted();
673 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, 649 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD,
674 download_request_limiter_->GetDownloadStatus(web_contents())); 650 download_request_limiter_->GetDownloadStatus(web_contents()));
675 651
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 // changing the content setting). 690 // changing the content setting).
715 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); 691 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK);
716 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 692 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
717 download_request_limiter_->GetDownloadStatus(web_contents())); 693 download_request_limiter_->GetDownloadStatus(web_contents()));
718 UpdateExpectations(WAIT); 694 UpdateExpectations(WAIT);
719 CanDownload(); 695 CanDownload();
720 ExpectAndResetCounts(0, 0, 1, __LINE__); 696 ExpectAndResetCounts(0, 0, 1, __LINE__);
721 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, 697 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD,
722 download_request_limiter_->GetDownloadStatus(web_contents())); 698 download_request_limiter_->GetDownloadStatus(web_contents()));
723 } 699 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698