| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/chrome_thread.h" |
| 5 #include "chrome/browser/download/download_request_manager.h" | 6 #include "chrome/browser/download/download_request_manager.h" |
| 6 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | 7 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 7 #include "chrome/browser/tab_contents/navigation_controller.h" | 8 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 8 #include "chrome/test/testing_profile.h" | 9 #include "chrome/test/testing_profile.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 class DownloadRequestManagerTest | 12 class DownloadRequestManagerTest |
| 12 : public RenderViewHostTestHarness, | 13 : public RenderViewHostTestHarness, |
| 13 public DownloadRequestManager::Callback { | 14 public DownloadRequestManager::Callback { |
| 14 public: | 15 public: |
| 16 DownloadRequestManagerTest() : io_thread_(ChromeThread::IO, &message_loop_) {} |
| 17 |
| 15 virtual void SetUp() { | 18 virtual void SetUp() { |
| 16 RenderViewHostTestHarness::SetUp(); | 19 RenderViewHostTestHarness::SetUp(); |
| 17 | 20 |
| 18 allow_download_ = true; | 21 allow_download_ = true; |
| 19 ask_allow_count_ = cancel_count_ = continue_count_ = 0; | 22 ask_allow_count_ = cancel_count_ = continue_count_ = 0; |
| 20 | 23 |
| 21 download_request_manager_ = new DownloadRequestManager(NULL, NULL); | 24 download_request_manager_ = new DownloadRequestManager(); |
| 22 test_delegate_.reset(new DownloadRequestManagerTestDelegate(this)); | 25 test_delegate_.reset(new DownloadRequestManagerTestDelegate(this)); |
| 23 DownloadRequestManager::SetTestingDelegate(test_delegate_.get()); | 26 DownloadRequestManager::SetTestingDelegate(test_delegate_.get()); |
| 24 } | 27 } |
| 25 | 28 |
| 26 virtual void TearDown() { | 29 virtual void TearDown() { |
| 27 DownloadRequestManager::SetTestingDelegate(NULL); | 30 DownloadRequestManager::SetTestingDelegate(NULL); |
| 28 | 31 |
| 29 RenderViewHostTestHarness::TearDown(); | 32 RenderViewHostTestHarness::TearDown(); |
| 30 } | 33 } |
| 31 | 34 |
| 32 virtual void ContinueDownload() { | 35 virtual void ContinueDownload() { |
| 33 continue_count_++; | 36 continue_count_++; |
| 34 } | 37 } |
| 35 virtual void CancelDownload() { | 38 virtual void CancelDownload() { |
| 36 cancel_count_++; | 39 cancel_count_++; |
| 37 } | 40 } |
| 38 | 41 |
| 39 void CanDownload() { | 42 void CanDownload() { |
| 40 download_request_manager_->CanDownloadImpl( | 43 download_request_manager_->CanDownloadImpl( |
| 41 controller().tab_contents(), this); | 44 controller().tab_contents(), this); |
| 45 message_loop_.RunAllPending(); |
| 42 } | 46 } |
| 43 | 47 |
| 44 bool ShouldAllowDownload() { | 48 bool ShouldAllowDownload() { |
| 45 ask_allow_count_++; | 49 ask_allow_count_++; |
| 46 return allow_download_; | 50 return allow_download_; |
| 47 } | 51 } |
| 48 | 52 |
| 49 protected: | 53 protected: |
| 50 class DownloadRequestManagerTestDelegate | 54 class DownloadRequestManagerTestDelegate |
| 51 : public DownloadRequestManager::TestingDelegate { | 55 : public DownloadRequestManager::TestingDelegate { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 int continue_count_; | 72 int continue_count_; |
| 69 | 73 |
| 70 // Number of times CancelDownload was invoked. | 74 // Number of times CancelDownload was invoked. |
| 71 int cancel_count_; | 75 int cancel_count_; |
| 72 | 76 |
| 73 // Whether the download should be allowed. | 77 // Whether the download should be allowed. |
| 74 bool allow_download_; | 78 bool allow_download_; |
| 75 | 79 |
| 76 // Number of times ShouldAllowDownload was invoked. | 80 // Number of times ShouldAllowDownload was invoked. |
| 77 int ask_allow_count_; | 81 int ask_allow_count_; |
| 82 |
| 83 ChromeThread io_thread_; |
| 78 }; | 84 }; |
| 79 | 85 |
| 80 TEST_F(DownloadRequestManagerTest, Allow) { | 86 TEST_F(DownloadRequestManagerTest, Allow) { |
| 81 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. | 87 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. |
| 82 ASSERT_EQ(DownloadRequestManager::ALLOW_ONE_DOWNLOAD, | 88 ASSERT_EQ(DownloadRequestManager::ALLOW_ONE_DOWNLOAD, |
| 83 download_request_manager_->GetDownloadStatus( | 89 download_request_manager_->GetDownloadStatus( |
| 84 controller().tab_contents())); | 90 controller().tab_contents())); |
| 85 | 91 |
| 86 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. | 92 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. |
| 87 CanDownload(); | 93 CanDownload(); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 ask_allow_count_ = continue_count_ = cancel_count_ = 0; | 199 ask_allow_count_ = continue_count_ = cancel_count_ = 0; |
| 194 CanDownload(); | 200 CanDownload(); |
| 195 ASSERT_EQ(0, ask_allow_count_); | 201 ASSERT_EQ(0, ask_allow_count_); |
| 196 ASSERT_EQ(0, continue_count_); | 202 ASSERT_EQ(0, continue_count_); |
| 197 ASSERT_EQ(1, cancel_count_); | 203 ASSERT_EQ(1, cancel_count_); |
| 198 // And the state shouldn't have changed. | 204 // And the state shouldn't have changed. |
| 199 ASSERT_EQ(DownloadRequestManager::DOWNLOADS_NOT_ALLOWED, | 205 ASSERT_EQ(DownloadRequestManager::DOWNLOADS_NOT_ALLOWED, |
| 200 download_request_manager_->GetDownloadStatus( | 206 download_request_manager_->GetDownloadStatus( |
| 201 controller().tab_contents())); | 207 controller().tab_contents())); |
| 202 } | 208 } |
| OLD | NEW |