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