| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/stl_util.h" |
| 6 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
| 7 #include "chrome/test/base/testing_profile.h" | 8 #include "chrome/test/base/testing_profile.h" |
| 8 #include "content/browser/download/download_create_info.h" | 9 #include "content/browser/download/download_create_info.h" |
| 9 #include "content/browser/download/download_id.h" | 10 #include "content/browser/download/download_id.h" |
| 10 #include "content/browser/download/download_id_factory.h" | 11 #include "content/browser/download/download_id_factory.h" |
| 11 #include "content/browser/download/download_item.h" | 12 #include "content/browser/download/download_item_impl.h" |
| 12 #include "content/browser/download/download_request_handle.h" | 13 #include "content/browser/download/download_request_handle.h" |
| 13 #include "content/browser/download/download_status_updater.h" | 14 #include "content/browser/download/download_status_updater.h" |
| 14 #include "content/browser/download/interrupt_reasons.h" | 15 #include "content/browser/download/interrupt_reasons.h" |
| 15 #include "content/browser/download/mock_download_item.h" | 16 #include "content/browser/download/mock_download_item.h" |
| 16 #include "content/browser/download/mock_download_manager.h" | |
| 17 #include "content/browser/download/mock_download_manager_delegate.h" | |
| 18 #include "content/test/test_browser_thread.h" | 17 #include "content/test/test_browser_thread.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 | 22 |
| 23 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 23 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
| 24 | 24 |
| 25 namespace { |
| 26 class MockDelegate : public DownloadItemImpl::Delegate { |
| 27 public: |
| 28 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); |
| 29 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem* download)); |
| 30 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItem* download)); |
| 31 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItem* download)); |
| 32 MOCK_CONST_METHOD0(BrowserContext, content::BrowserContext*()); |
| 33 MOCK_METHOD1(DownloadCancelled, void(DownloadItem* download)); |
| 34 MOCK_METHOD1(DownloadCompleted, void(DownloadItem* download)); |
| 35 MOCK_METHOD1(DownloadOpened, void(DownloadItem* download)); |
| 36 MOCK_METHOD1(DownloadRemoved, void(DownloadItem* download)); |
| 37 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItem* download)); |
| 38 }; |
| 39 |
| 40 class MockRequestHandle : public DownloadRequestHandleInterface { |
| 41 public: |
| 42 MOCK_CONST_METHOD0(GetTabContents, TabContents*()); |
| 43 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
| 44 MOCK_CONST_METHOD0(PauseRequest, void()); |
| 45 MOCK_CONST_METHOD0(ResumeRequest, void()); |
| 46 MOCK_CONST_METHOD0(CancelRequest, void()); |
| 47 MOCK_CONST_METHOD0(DebugString, std::string()); |
| 48 }; |
| 49 |
| 50 } |
| 51 |
| 25 class DownloadItemTest : public testing::Test { | 52 class DownloadItemTest : public testing::Test { |
| 26 public: | 53 public: |
| 27 class MockObserver : public DownloadItem::Observer { | 54 class MockObserver : public DownloadItem::Observer { |
| 28 public: | 55 public: |
| 29 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) { | 56 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) { |
| 30 item_->AddObserver(this); | 57 item_->AddObserver(this); |
| 31 } | 58 } |
| 32 ~MockObserver() { item_->RemoveObserver(this); } | 59 ~MockObserver() { item_->RemoveObserver(this); } |
| 33 | 60 |
| 34 virtual void OnDownloadUpdated(DownloadItem* download) { updated_ = true; } | 61 virtual void OnDownloadUpdated(DownloadItem* download) { updated_ = true; } |
| 35 | 62 |
| 36 virtual void OnDownloadOpened(DownloadItem* download) { } | 63 virtual void OnDownloadOpened(DownloadItem* download) { } |
| 37 | 64 |
| 38 bool CheckUpdated() { | 65 bool CheckUpdated() { |
| 39 bool was_updated = updated_; | 66 bool was_updated = updated_; |
| 40 updated_ = false; | 67 updated_ = false; |
| 41 return was_updated; | 68 return was_updated; |
| 42 } | 69 } |
| 43 | 70 |
| 44 private: | 71 private: |
| 45 DownloadItem* item_; | 72 DownloadItem* item_; |
| 46 bool updated_; | 73 bool updated_; |
| 47 }; | 74 }; |
| 48 | 75 |
| 49 DownloadItemTest() | 76 DownloadItemTest() |
| 50 : id_factory_(new DownloadIdFactory(kValidDownloadItemIdDomain)), | 77 : id_factory_(new DownloadIdFactory(kValidDownloadItemIdDomain)), |
| 51 profile_(new TestingProfile()), | |
| 52 ui_thread_(BrowserThread::UI, &loop_) { | 78 ui_thread_(BrowserThread::UI, &loop_) { |
| 53 } | 79 } |
| 54 | 80 |
| 55 ~DownloadItemTest() { | 81 ~DownloadItemTest() { |
| 56 } | 82 } |
| 57 | 83 |
| 58 virtual void SetUp() { | 84 virtual void SetUp() { |
| 59 download_manager_delegate_.reset(new MockDownloadManagerDelegate()); | |
| 60 download_manager_ = new MockDownloadManager( | |
| 61 download_manager_delegate_.get(), | |
| 62 id_factory_, | |
| 63 &download_status_updater_); | |
| 64 download_manager_->Init(profile_.get()); | |
| 65 } | 85 } |
| 66 | 86 |
| 67 virtual void TearDown() { | 87 virtual void TearDown() { |
| 68 download_manager_->Shutdown(); | |
| 69 // When a DownloadManager's reference count drops to 0, it is not | |
| 70 // deleted immediately. Instead, a task is posted to the UI thread's | |
| 71 // message loop to delete it. | |
| 72 // So, drop the reference count to 0 and run the message loop once | |
| 73 // to ensure that all resources are cleaned up before the test exits. | |
| 74 download_manager_ = NULL; | |
| 75 profile_.reset(NULL); | |
| 76 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); | 88 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); |
| 89 STLDeleteElements(&allocated_downloads_); |
| 90 allocated_downloads_.clear(); |
| 77 } | 91 } |
| 78 | 92 |
| 93 // This class keeps ownership of the created download item; it will |
| 94 // be torn down at the end of the test. |
| 79 DownloadItem* CreateDownloadItem(DownloadItem::DownloadState state) { | 95 DownloadItem* CreateDownloadItem(DownloadItem::DownloadState state) { |
| 80 // Normally, the download system takes ownership of info, and is | 96 // Normally, the download system takes ownership of info, and is |
| 81 // responsible for deleting it. In these unit tests, however, we | 97 // responsible for deleting it. In these unit tests, however, we |
| 82 // don't call the function that deletes it, so we do so ourselves. | 98 // don't call the function that deletes it, so we do so ourselves. |
| 83 scoped_ptr<DownloadCreateInfo> info_; | 99 scoped_ptr<DownloadCreateInfo> info_; |
| 84 | 100 |
| 85 info_.reset(new DownloadCreateInfo()); | 101 info_.reset(new DownloadCreateInfo()); |
| 86 info_->download_id = id_factory_->GetNextId(); | 102 info_->download_id = id_factory_->GetNextId(); |
| 87 info_->prompt_user_for_save_location = false; | 103 info_->prompt_user_for_save_location = false; |
| 88 info_->url_chain.push_back(GURL()); | 104 info_->url_chain.push_back(GURL()); |
| 89 info_->state = state; | 105 info_->state = state; |
| 90 | 106 |
| 91 download_manager_->CreateDownloadItem(info_.get(), DownloadRequestHandle()); | 107 MockRequestHandle* request_handle = |
| 92 return download_manager_->GetActiveDownloadItem(info_->download_id.local()); | 108 new testing::NiceMock<MockRequestHandle>; |
| 109 DownloadItem* download = |
| 110 new DownloadItemImpl(&delegate_, *(info_.get()), |
| 111 request_handle, false); |
| 112 allocated_downloads_.push_back(download); |
| 113 return download; |
| 93 } | 114 } |
| 94 | 115 |
| 95 protected: | 116 protected: |
| 96 DownloadStatusUpdater download_status_updater_; | 117 DownloadStatusUpdater download_status_updater_; |
| 97 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; | |
| 98 scoped_refptr<DownloadManager> download_manager_; | |
| 99 | 118 |
| 100 private: | 119 private: |
| 101 scoped_refptr<DownloadIdFactory> id_factory_; | 120 scoped_refptr<DownloadIdFactory> id_factory_; |
| 102 scoped_ptr<TestingProfile> profile_; | |
| 103 MessageLoopForUI loop_; | 121 MessageLoopForUI loop_; |
| 104 // UI thread. | 122 // UI thread. |
| 105 content::TestBrowserThread ui_thread_; | 123 content::TestBrowserThread ui_thread_; |
| 124 testing::NiceMock<MockDelegate> delegate_; |
| 125 std::vector<DownloadItem*> allocated_downloads_; |
| 106 }; | 126 }; |
| 107 | 127 |
| 108 namespace { | 128 namespace { |
| 109 | 129 |
| 110 const int kDownloadChunkSize = 1000; | 130 const int kDownloadChunkSize = 1000; |
| 111 const int kDownloadSpeed = 1000; | 131 const int kDownloadSpeed = 1000; |
| 112 const int kDummyDBHandle = 10; | 132 const int kDummyDBHandle = 10; |
| 113 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | 133 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); |
| 114 | 134 |
| 115 } // namespace | 135 } // namespace |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 item->TogglePause(); | 270 item->TogglePause(); |
| 251 ASSERT_TRUE(observer.CheckUpdated()); | 271 ASSERT_TRUE(observer.CheckUpdated()); |
| 252 | 272 |
| 253 item->TogglePause(); | 273 item->TogglePause(); |
| 254 ASSERT_TRUE(observer.CheckUpdated()); | 274 ASSERT_TRUE(observer.CheckUpdated()); |
| 255 } | 275 } |
| 256 | 276 |
| 257 TEST(MockDownloadItem, Compiles) { | 277 TEST(MockDownloadItem, Compiles) { |
| 258 MockDownloadItem mock_item; | 278 MockDownloadItem mock_item; |
| 259 } | 279 } |
| OLD | NEW |