| 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 "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/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 10 #include "chrome/browser/download/download_request_infobar_delegate.h" | 10 #include "chrome/browser/download/download_request_infobar_delegate.h" |
| 11 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/infobars/infobar_service.h" |
| 12 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h" | 12 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.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" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "content/public/browser/navigation_controller.h" | 15 #include "content/public/browser/navigation_controller.h" |
| 16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 using content::WebContents; | 20 using content::WebContents; |
| 20 | 21 |
| 21 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { | 22 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { |
| 22 public: | 23 public: |
| 23 enum TestingAction { | 24 enum TestingAction { |
| 24 ACCEPT, | 25 ACCEPT, |
| 25 CANCEL, | 26 CANCEL, |
| 26 WAIT | 27 WAIT |
| 27 }; | 28 }; |
| 28 | 29 |
| 29 virtual void SetUp() { | 30 virtual void SetUp() { |
| 30 ChromeRenderViewHostTestHarness::SetUp(); | 31 ChromeRenderViewHostTestHarness::SetUp(); |
| 31 BlockedContentTabHelper::CreateForWebContents(web_contents()); | 32 BlockedContentTabHelper::CreateForWebContents(web_contents()); |
| 32 InfoBarService::CreateForWebContents(web_contents()); | 33 InfoBarService::CreateForWebContents(web_contents()); |
| 33 testing_action_ = ACCEPT; | 34 testing_action_ = ACCEPT; |
| 34 ask_allow_count_ = cancel_count_ = continue_count_ = 0; | 35 ask_allow_count_ = cancel_count_ = continue_count_ = 0; |
| 35 download_request_limiter_ = new DownloadRequestLimiter(); | 36 download_request_limiter_ = new DownloadRequestLimiter(); |
| 36 fake_create_callback_ = base::Bind( | 37 fake_create_callback_ = base::Bind( |
| 37 &DownloadRequestLimiterTest::FakeCreate, base::Unretained(this)); | 38 &DownloadRequestLimiterTest::FakeCreate, base::Unretained(this)); |
| 38 DownloadRequestInfoBarDelegate::SetCallbackForTesting( | 39 DownloadRequestInfoBarDelegate::SetCallbackForTesting( |
| 39 &fake_create_callback_); | 40 &fake_create_callback_); |
| 40 content_settings_ = new HostContentSettingsMap(profile_.GetPrefs(), false); | 41 content_settings_ = new HostContentSettingsMap( |
| 42 Profile::FromBrowserContext(browser_context())->GetPrefs(), false); |
| 41 DownloadRequestLimiter::SetContentSettingsForTesting( | 43 DownloadRequestLimiter::SetContentSettingsForTesting( |
| 42 content_settings_.get()); | 44 content_settings_.get()); |
| 43 } | 45 } |
| 44 | 46 |
| 45 void FakeCreate( | 47 void FakeCreate( |
| 46 InfoBarService* infobar_service, | 48 InfoBarService* infobar_service, |
| 47 base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host) { | 49 base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host) { |
| 48 ask_allow_count_++; | 50 ask_allow_count_++; |
| 49 switch (testing_action_) { | 51 switch (testing_action_) { |
| 50 case ACCEPT: | 52 case ACCEPT: |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // Number of times CancelDownload was invoked. | 134 // Number of times CancelDownload was invoked. |
| 133 int cancel_count_; | 135 int cancel_count_; |
| 134 | 136 |
| 135 // Number of times ShouldAllowDownload was invoked. | 137 // Number of times ShouldAllowDownload was invoked. |
| 136 int ask_allow_count_; | 138 int ask_allow_count_; |
| 137 | 139 |
| 138 scoped_refptr<HostContentSettingsMap> content_settings_; | 140 scoped_refptr<HostContentSettingsMap> content_settings_; |
| 139 | 141 |
| 140 private: | 142 private: |
| 141 DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_; | 143 DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_; |
| 142 TestingProfile profile_; | 144 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { |
| 145 return new TestingProfile(); |
| 146 } |
| 143 }; | 147 }; |
| 144 | 148 |
| 145 TEST_F(DownloadRequestLimiterTest, | 149 TEST_F(DownloadRequestLimiterTest, |
| 146 DownloadRequestLimiter_Allow) { | 150 DownloadRequestLimiter_Allow) { |
| 147 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. | 151 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. |
| 148 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 152 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 149 download_request_limiter_->GetDownloadStatus(web_contents())); | 153 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 150 | 154 |
| 151 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. | 155 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. |
| 152 CanDownload(); | 156 CanDownload(); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 348 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 345 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 349 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 346 OnUserGestureFor(web_contents.get()); | 350 OnUserGestureFor(web_contents.get()); |
| 347 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 351 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 348 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 352 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 349 CanDownloadFor(web_contents.get()); | 353 CanDownloadFor(web_contents.get()); |
| 350 ExpectAndResetCounts(1, 0, 0, __LINE__); | 354 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 351 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 355 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 352 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 356 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 353 } | 357 } |
| OLD | NEW |