OLD | NEW |
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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/run_loop.h" |
6 #include "chrome/browser/download/download_request_infobar_delegate.h" | 7 #include "chrome/browser/download/download_request_infobar_delegate.h" |
7 #include "chrome/browser/download/download_request_limiter.h" | 8 #include "chrome/browser/download/download_request_limiter.h" |
8 #include "chrome/browser/infobars/infobar_service.h" | 9 #include "chrome/browser/infobars/infobar_service.h" |
9 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | 10 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" |
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
11 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
12 #include "content/public/browser/navigation_controller.h" | 13 #include "content/public/browser/navigation_controller.h" |
13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
14 #include "content/public/test/test_browser_thread.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using content::BrowserThread; | |
18 using content::WebContents; | 17 using content::WebContents; |
19 | 18 |
20 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { | 19 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { |
21 public: | 20 public: |
22 enum TestingAction { | 21 enum TestingAction { |
23 ACCEPT, | 22 ACCEPT, |
24 CANCEL, | 23 CANCEL, |
25 WAIT | 24 WAIT |
26 }; | 25 }; |
27 | 26 |
28 DownloadRequestLimiterTest() | |
29 : ui_thread_(BrowserThread::UI, &message_loop_), | |
30 file_user_blocking_thread_( | |
31 BrowserThread::FILE_USER_BLOCKING, &message_loop_), | |
32 io_thread_(BrowserThread::IO, &message_loop_) { | |
33 } | |
34 | |
35 virtual void SetUp() { | 27 virtual void SetUp() { |
36 ChromeRenderViewHostTestHarness::SetUp(); | 28 ChromeRenderViewHostTestHarness::SetUp(); |
37 BlockedContentTabHelper::CreateForWebContents(web_contents()); | 29 BlockedContentTabHelper::CreateForWebContents(web_contents()); |
38 InfoBarService::CreateForWebContents(web_contents()); | 30 InfoBarService::CreateForWebContents(web_contents()); |
39 testing_action_ = ACCEPT; | 31 testing_action_ = ACCEPT; |
40 ask_allow_count_ = cancel_count_ = continue_count_ = 0; | 32 ask_allow_count_ = cancel_count_ = continue_count_ = 0; |
41 download_request_limiter_ = new DownloadRequestLimiter(); | 33 download_request_limiter_ = new DownloadRequestLimiter(); |
42 fake_create_callback_ = base::Bind( | 34 fake_create_callback_ = base::Bind( |
43 &DownloadRequestLimiterTest::FakeCreate, base::Unretained(this)); | 35 &DownloadRequestLimiterTest::FakeCreate, base::Unretained(this)); |
44 DownloadRequestInfoBarDelegate::SetCallbackForTesting( | 36 DownloadRequestInfoBarDelegate::SetCallbackForTesting( |
(...skipping 29 matching lines...) Expand all Loading... |
74 CanDownloadFor(web_contents()); | 66 CanDownloadFor(web_contents()); |
75 } | 67 } |
76 | 68 |
77 void CanDownloadFor(WebContents* web_contents) { | 69 void CanDownloadFor(WebContents* web_contents) { |
78 download_request_limiter_->CanDownloadImpl( | 70 download_request_limiter_->CanDownloadImpl( |
79 web_contents, | 71 web_contents, |
80 -1, // request id | 72 -1, // request id |
81 "GET", // request method | 73 "GET", // request method |
82 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, | 74 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, |
83 base::Unretained(this))); | 75 base::Unretained(this))); |
84 message_loop_.RunUntilIdle(); | 76 base::RunLoop().RunUntilIdle(); |
85 } | 77 } |
86 | 78 |
87 void OnUserGesture() { | 79 void OnUserGesture() { |
88 OnUserGestureFor(web_contents()); | 80 OnUserGestureFor(web_contents()); |
89 } | 81 } |
90 | 82 |
91 void OnUserGestureFor(WebContents* web_contents) { | 83 void OnUserGestureFor(WebContents* web_contents) { |
92 DownloadRequestLimiter::TabDownloadState* state = | 84 DownloadRequestLimiter::TabDownloadState* state = |
93 download_request_limiter_->GetDownloadState(web_contents, NULL, false); | 85 download_request_limiter_->GetDownloadState(web_contents, NULL, false); |
94 if (state) | 86 if (state) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 121 |
130 // Number of times ContinueDownload was invoked. | 122 // Number of times ContinueDownload was invoked. |
131 int continue_count_; | 123 int continue_count_; |
132 | 124 |
133 // Number of times CancelDownload was invoked. | 125 // Number of times CancelDownload was invoked. |
134 int cancel_count_; | 126 int cancel_count_; |
135 | 127 |
136 // Number of times ShouldAllowDownload was invoked. | 128 // Number of times ShouldAllowDownload was invoked. |
137 int ask_allow_count_; | 129 int ask_allow_count_; |
138 | 130 |
139 content::TestBrowserThread ui_thread_; | |
140 content::TestBrowserThread file_user_blocking_thread_; | |
141 content::TestBrowserThread io_thread_; | |
142 | |
143 private: | 131 private: |
144 DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_; | 132 DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_; |
145 }; | 133 }; |
146 | 134 |
147 TEST_F(DownloadRequestLimiterTest, | 135 TEST_F(DownloadRequestLimiterTest, |
148 DownloadRequestLimiter_Allow) { | 136 DownloadRequestLimiter_Allow) { |
149 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. | 137 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. |
150 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 138 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
151 download_request_limiter_->GetDownloadStatus(web_contents())); | 139 download_request_limiter_->GetDownloadStatus(web_contents())); |
152 | 140 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 ExpectAndResetCounts(1, 0, 0, __LINE__); | 275 ExpectAndResetCounts(1, 0, 0, __LINE__); |
288 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 276 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
289 download_request_limiter_->GetDownloadStatus(web_contents())); | 277 download_request_limiter_->GetDownloadStatus(web_contents())); |
290 | 278 |
291 CanDownload(); | 279 CanDownload(); |
292 ExpectAndResetCounts(0, 0, 1, __LINE__); | 280 ExpectAndResetCounts(0, 0, 1, __LINE__); |
293 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 281 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
294 download_request_limiter_->GetDownloadStatus(web_contents())); | 282 download_request_limiter_->GetDownloadStatus(web_contents())); |
295 | 283 |
296 AboutToNavigateRenderView(); | 284 AboutToNavigateRenderView(); |
297 message_loop_.RunUntilIdle(); | 285 base::RunLoop().RunUntilIdle(); |
298 ExpectAndResetCounts(0, 1, 0, __LINE__); | 286 ExpectAndResetCounts(0, 1, 0, __LINE__); |
299 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 287 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
300 download_request_limiter_->GetDownloadStatus(web_contents())); | 288 download_request_limiter_->GetDownloadStatus(web_contents())); |
301 | 289 |
302 CanDownload(); | 290 CanDownload(); |
303 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 291 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
304 download_request_limiter_->GetDownloadStatus(web_contents())); | 292 download_request_limiter_->GetDownloadStatus(web_contents())); |
305 ExpectAndResetCounts(1, 0, 0, __LINE__); | 293 ExpectAndResetCounts(1, 0, 0, __LINE__); |
306 | 294 |
307 testing_action_ = CANCEL; | 295 testing_action_ = CANCEL; |
308 CanDownload(); | 296 CanDownload(); |
309 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 297 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
310 download_request_limiter_->GetDownloadStatus(web_contents())); | 298 download_request_limiter_->GetDownloadStatus(web_contents())); |
311 ExpectAndResetCounts(0, 1, 1, __LINE__); | 299 ExpectAndResetCounts(0, 1, 1, __LINE__); |
312 | 300 |
313 AboutToNavigateRenderView(); | 301 AboutToNavigateRenderView(); |
314 message_loop_.RunUntilIdle(); | 302 base::RunLoop().RunUntilIdle(); |
315 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 303 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
316 download_request_limiter_->GetDownloadStatus(web_contents())); | 304 download_request_limiter_->GetDownloadStatus(web_contents())); |
317 CanDownload(); | 305 CanDownload(); |
318 ExpectAndResetCounts(0, 1, 0, __LINE__); | 306 ExpectAndResetCounts(0, 1, 0, __LINE__); |
319 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 307 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
320 download_request_limiter_->GetDownloadStatus(web_contents())); | 308 download_request_limiter_->GetDownloadStatus(web_contents())); |
321 } | 309 } |
322 | 310 |
323 TEST_F(DownloadRequestLimiterTest, | 311 TEST_F(DownloadRequestLimiterTest, |
324 DownloadRequestLimiter_RawWebContents) { | 312 DownloadRequestLimiter_RawWebContents) { |
(...skipping 22 matching lines...) Expand all Loading... |
347 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 335 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
348 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 336 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
349 OnUserGestureFor(web_contents.get()); | 337 OnUserGestureFor(web_contents.get()); |
350 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 338 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
351 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 339 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
352 CanDownloadFor(web_contents.get()); | 340 CanDownloadFor(web_contents.get()); |
353 ExpectAndResetCounts(1, 0, 0, __LINE__); | 341 ExpectAndResetCounts(1, 0, 0, __LINE__); |
354 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 342 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
355 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 343 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
356 } | 344 } |
OLD | NEW |