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