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 "base/command_line.h" |
5 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
6 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
7 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
8 #include "content/browser/download/byte_stream.h" | 9 #include "content/browser/download/byte_stream.h" |
9 #include "content/browser/download/download_create_info.h" | 10 #include "content/browser/download/download_create_info.h" |
10 #include "content/browser/download/download_file_factory.h" | 11 #include "content/browser/download/download_file_factory.h" |
11 #include "content/browser/download/download_item_impl.h" | 12 #include "content/browser/download/download_item_impl.h" |
12 #include "content/browser/download/download_item_impl_delegate.h" | 13 #include "content/browser/download/download_item_impl_delegate.h" |
13 #include "content/browser/download/download_request_handle.h" | 14 #include "content/browser/download/download_request_handle.h" |
14 #include "content/browser/download/mock_download_file.h" | 15 #include "content/browser/download/mock_download_file.h" |
15 #include "content/public/browser/download_id.h" | 16 #include "content/public/browser/download_id.h" |
16 #include "content/public/browser/download_destination_observer.h" | 17 #include "content/public/browser/download_destination_observer.h" |
17 #include "content/public/browser/download_interrupt_reasons.h" | 18 #include "content/public/browser/download_interrupt_reasons.h" |
| 19 #include "content/public/browser/download_url_parameters.h" |
| 20 #include "content/public/common/content_switches.h" |
18 #include "content/public/test/mock_download_item.h" | 21 #include "content/public/test/mock_download_item.h" |
19 #include "content/public/test/test_browser_thread.h" | 22 #include "content/public/test/test_browser_thread.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
22 | 25 |
23 using ::testing::_; | 26 using ::testing::_; |
| 27 using ::testing::NiceMock; |
24 using ::testing::Property; | 28 using ::testing::Property; |
25 using ::testing::Return; | 29 using ::testing::Return; |
26 using ::testing::SaveArg; | 30 using ::testing::SaveArg; |
27 using ::testing::StrictMock; | 31 using ::testing::StrictMock; |
28 | 32 |
29 namespace { | 33 namespace { |
30 | 34 |
31 const int kDownloadChunkSize = 1000; | 35 const int kDownloadChunkSize = 1000; |
32 const int kDownloadSpeed = 1000; | 36 const int kDownloadSpeed = 1000; |
33 const int kDummyDBHandle = 10; | 37 const int kDummyDBHandle = 10; |
34 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | 38 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); |
35 | 39 |
36 } // namespace | 40 } // namespace |
37 | 41 |
38 namespace content { | 42 namespace content { |
39 | 43 |
40 namespace { | 44 namespace { |
41 | 45 |
42 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 46 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
43 | 47 |
44 class MockDelegate : public DownloadItemImplDelegate { | 48 class MockDelegate : public DownloadItemImplDelegate { |
45 public: | 49 public: |
| 50 MockDelegate() : DownloadItemImplDelegate() { |
| 51 SetDefaultExpectations(); |
| 52 } |
| 53 |
46 MOCK_METHOD2(DetermineDownloadTarget, void( | 54 MOCK_METHOD2(DetermineDownloadTarget, void( |
47 DownloadItemImpl*, const DownloadTargetCallback&)); | 55 DownloadItemImpl*, const DownloadTargetCallback&)); |
48 MOCK_METHOD2(ShouldCompleteDownload, | 56 MOCK_METHOD2(ShouldCompleteDownload, |
49 bool(DownloadItemImpl*, const base::Closure&)); | 57 bool(DownloadItemImpl*, const base::Closure&)); |
50 MOCK_METHOD2(ShouldOpenDownload, | 58 MOCK_METHOD2(ShouldOpenDownload, |
51 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); | 59 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); |
52 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); | 60 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); |
53 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); | 61 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); |
| 62 |
| 63 virtual void ResumeInterruptedDownload( |
| 64 scoped_ptr<DownloadUrlParameters> params, DownloadId id) OVERRIDE { |
| 65 MockResumeInterruptedDownload(params.get(), id); |
| 66 } |
| 67 MOCK_METHOD2(MockResumeInterruptedDownload, |
| 68 void(DownloadUrlParameters* params, DownloadId id)); |
| 69 |
54 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); | 70 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); |
55 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); | 71 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); |
56 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); | 72 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); |
57 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); | 73 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); |
58 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); | 74 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); |
59 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); | 75 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); |
| 76 |
| 77 void VerifyAndClearExpectations() { |
| 78 ::testing::Mock::VerifyAndClearExpectations(this); |
| 79 SetDefaultExpectations(); |
| 80 } |
| 81 |
| 82 private: |
| 83 void SetDefaultExpectations() { |
| 84 EXPECT_CALL(*this, AssertStateConsistent(_)) |
| 85 .WillRepeatedly(Return()); |
| 86 EXPECT_CALL(*this, ShouldOpenFileBasedOnExtension(_)) |
| 87 .WillRepeatedly(Return(false)); |
| 88 EXPECT_CALL(*this, ShouldOpenDownload(_, _)) |
| 89 .WillRepeatedly(Return(true)); |
| 90 } |
60 }; | 91 }; |
61 | 92 |
62 class MockRequestHandle : public DownloadRequestHandleInterface { | 93 class MockRequestHandle : public DownloadRequestHandleInterface { |
63 public: | 94 public: |
64 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); | 95 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); |
65 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 96 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
66 MOCK_CONST_METHOD0(PauseRequest, void()); | 97 MOCK_CONST_METHOD0(PauseRequest, void()); |
67 MOCK_CONST_METHOD0(ResumeRequest, void()); | 98 MOCK_CONST_METHOD0(ResumeRequest, void()); |
68 MOCK_CONST_METHOD0(CancelRequest, void()); | 99 MOCK_CONST_METHOD0(CancelRequest, void()); |
69 MOCK_CONST_METHOD0(DebugString, std::string()); | 100 MOCK_CONST_METHOD0(DebugString, std::string()); |
(...skipping 12 matching lines...) Expand all Loading... |
82 } | 113 } |
83 | 114 |
84 } // namespace | 115 } // namespace |
85 | 116 |
86 class DownloadItemTest : public testing::Test { | 117 class DownloadItemTest : public testing::Test { |
87 public: | 118 public: |
88 class MockObserver : public DownloadItem::Observer { | 119 class MockObserver : public DownloadItem::Observer { |
89 public: | 120 public: |
90 explicit MockObserver(DownloadItem* item) | 121 explicit MockObserver(DownloadItem* item) |
91 : item_(item), | 122 : item_(item), |
| 123 last_state_(item->GetState()), |
92 removed_(false), | 124 removed_(false), |
93 destroyed_(false), | 125 destroyed_(false), |
94 updated_(false) { | 126 updated_(false), |
| 127 interrupt_count_(0), |
| 128 resume_count_(0) { |
95 item_->AddObserver(this); | 129 item_->AddObserver(this); |
96 } | 130 } |
97 | 131 |
98 virtual ~MockObserver() { | 132 virtual ~MockObserver() { |
99 if (item_) item_->RemoveObserver(this); | 133 if (item_) item_->RemoveObserver(this); |
100 } | 134 } |
101 | 135 |
102 virtual void OnDownloadRemoved(DownloadItem* download) { | 136 virtual void OnDownloadRemoved(DownloadItem* download) { |
| 137 DVLOG(20) << " " << __FUNCTION__ |
| 138 << " download = " << download->DebugString(false); |
103 removed_ = true; | 139 removed_ = true; |
104 } | 140 } |
105 | 141 |
106 virtual void OnDownloadUpdated(DownloadItem* download) { | 142 virtual void OnDownloadUpdated(DownloadItem* download) { |
| 143 DVLOG(20) << " " << __FUNCTION__ |
| 144 << " download = " << download->DebugString(false); |
107 updated_ = true; | 145 updated_ = true; |
| 146 DownloadItem::DownloadState new_state = download->GetState(); |
| 147 if (last_state_ == DownloadItem::IN_PROGRESS && |
| 148 new_state == DownloadItem::INTERRUPTED) { |
| 149 interrupt_count_++; |
| 150 } |
| 151 if (last_state_ == DownloadItem::INTERRUPTED && |
| 152 new_state == DownloadItem::IN_PROGRESS) { |
| 153 resume_count_++; |
| 154 } |
| 155 last_state_ = new_state; |
108 } | 156 } |
109 | 157 |
110 virtual void OnDownloadOpened(DownloadItem* download) { | 158 virtual void OnDownloadOpened(DownloadItem* download) { |
| 159 DVLOG(20) << " " << __FUNCTION__ |
| 160 << " download = " << download->DebugString(false); |
111 } | 161 } |
112 | 162 |
113 virtual void OnDownloadDestroyed(DownloadItem* download) { | 163 virtual void OnDownloadDestroyed(DownloadItem* download) { |
| 164 DVLOG(20) << " " << __FUNCTION__ |
| 165 << " download = " << download->DebugString(false); |
114 destroyed_ = true; | 166 destroyed_ = true; |
115 item_->RemoveObserver(this); | 167 item_->RemoveObserver(this); |
116 item_ = NULL; | 168 item_ = NULL; |
117 } | 169 } |
118 | 170 |
119 bool CheckRemoved() { | 171 bool CheckRemoved() { |
120 return removed_; | 172 return removed_; |
121 } | 173 } |
122 | 174 |
123 bool CheckDestroyed() { | 175 bool CheckDestroyed() { |
124 return destroyed_; | 176 return destroyed_; |
125 } | 177 } |
126 | 178 |
127 bool CheckUpdated() { | 179 bool CheckUpdated() { |
128 bool was_updated = updated_; | 180 bool was_updated = updated_; |
129 updated_ = false; | 181 updated_ = false; |
130 return was_updated; | 182 return was_updated; |
131 } | 183 } |
132 | 184 |
| 185 int GetInterruptCount() { |
| 186 return interrupt_count_; |
| 187 } |
| 188 |
| 189 int GetResumeCount() { |
| 190 return resume_count_; |
| 191 } |
| 192 |
133 private: | 193 private: |
134 DownloadItem* item_; | 194 DownloadItem* item_; |
| 195 DownloadItem::DownloadState last_state_; |
135 bool removed_; | 196 bool removed_; |
136 bool destroyed_; | 197 bool destroyed_; |
137 bool updated_; | 198 bool updated_; |
| 199 int interrupt_count_; |
| 200 int resume_count_; |
138 }; | 201 }; |
139 | 202 |
140 DownloadItemTest() | 203 DownloadItemTest() |
141 : ui_thread_(BrowserThread::UI, &loop_), | 204 : ui_thread_(BrowserThread::UI, &loop_), |
142 file_thread_(BrowserThread::FILE, &loop_), | 205 file_thread_(BrowserThread::FILE, &loop_), |
143 delegate_() { | 206 delegate_() { |
144 } | 207 } |
145 | 208 |
146 ~DownloadItemTest() { | 209 ~DownloadItemTest() { |
147 } | 210 } |
(...skipping 16 matching lines...) Expand all Loading... |
164 // don't call the function that deletes it, so we do so ourselves. | 227 // don't call the function that deletes it, so we do so ourselves. |
165 scoped_ptr<DownloadCreateInfo> info_; | 228 scoped_ptr<DownloadCreateInfo> info_; |
166 | 229 |
167 info_.reset(new DownloadCreateInfo()); | 230 info_.reset(new DownloadCreateInfo()); |
168 static int next_id; | 231 static int next_id; |
169 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); | 232 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); |
170 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 233 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); |
171 info_->save_info->prompt_for_save_location = false; | 234 info_->save_info->prompt_for_save_location = false; |
172 info_->url_chain.push_back(GURL()); | 235 info_->url_chain.push_back(GURL()); |
173 | 236 |
174 scoped_ptr<DownloadRequestHandleInterface> request_handle( | |
175 new testing::NiceMock<MockRequestHandle>); | |
176 DownloadItemImpl* download = | 237 DownloadItemImpl* download = |
177 new DownloadItemImpl(&delegate_, *(info_.get()), | 238 new DownloadItemImpl(&delegate_, *(info_.get()), net::BoundNetLog()); |
178 request_handle.Pass(), net::BoundNetLog()); | |
179 allocated_downloads_.insert(download); | 239 allocated_downloads_.insert(download); |
180 return download; | 240 return download; |
181 } | 241 } |
182 | 242 |
183 // Add DownloadFile to DownloadItem | 243 // Add DownloadFile to DownloadItem |
184 MockDownloadFile* AddDownloadFileToDownloadItem( | 244 MockDownloadFile* AddDownloadFileToDownloadItem( |
185 DownloadItemImpl* item, | 245 DownloadItemImpl* item, |
186 DownloadItemImplDelegate::DownloadTargetCallback *callback) { | 246 DownloadItemImplDelegate::DownloadTargetCallback *callback) { |
187 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); | 247 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); |
188 scoped_ptr<DownloadFile> download_file(mock_download_file); | 248 scoped_ptr<DownloadFile> download_file(mock_download_file); |
189 EXPECT_CALL(*mock_download_file, Initialize(_)); | 249 EXPECT_CALL(*mock_download_file, Initialize(_)); |
190 if (callback) { | 250 if (callback) { |
191 // Save the callback. | 251 // Save the callback. |
192 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 252 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
193 .WillOnce(SaveArg<1>(callback)); | 253 .WillOnce(SaveArg<1>(callback)); |
194 } else { | 254 } else { |
195 // Drop it on the floor. | 255 // Drop it on the floor. |
196 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | 256 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
197 } | 257 } |
198 | 258 |
199 item->Start(download_file.Pass()); | 259 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 260 new NiceMock<MockRequestHandle>); |
| 261 item->Start(download_file.Pass(), request_handle.Pass()); |
200 loop_.RunUntilIdle(); | 262 loop_.RunUntilIdle(); |
201 | 263 |
202 // So that we don't have a function writing to a stack variable | 264 // So that we don't have a function writing to a stack variable |
203 // lying around if the above failed. | 265 // lying around if the above failed. |
204 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 266 mock_delegate()->VerifyAndClearExpectations(); |
| 267 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) |
| 268 .WillRepeatedly(Return()); |
| 269 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) |
| 270 .WillRepeatedly(Return(false)); |
| 271 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) |
| 272 .WillRepeatedly(Return(true)); |
205 | 273 |
206 return mock_download_file; | 274 return mock_download_file; |
207 } | 275 } |
208 | 276 |
209 // Perform the intermediate rename for |item|. The target path for the | 277 // Perform the intermediate rename for |item|. The target path for the |
210 // download will be set to kDummyPath. Returns the MockDownloadFile* that was | 278 // download will be set to kDummyPath. Returns the MockDownloadFile* that was |
211 // added to the DownloadItem. | 279 // added to the DownloadItem. |
212 MockDownloadFile* DoIntermediateRename(DownloadItemImpl* item) { | 280 MockDownloadFile* DoIntermediateRename(DownloadItemImpl* item) { |
213 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 281 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
214 EXPECT_TRUE(item->GetTargetFilePath().empty()); | 282 EXPECT_TRUE(item->GetTargetFilePath().empty()); |
215 DownloadItemImplDelegate::DownloadTargetCallback callback; | 283 DownloadItemImplDelegate::DownloadTargetCallback callback; |
216 MockDownloadFile* download_file = | 284 MockDownloadFile* download_file = |
217 AddDownloadFileToDownloadItem(item, &callback); | 285 AddDownloadFileToDownloadItem(item, &callback); |
218 FilePath target_path(kDummyPath); | 286 FilePath target_path(kDummyPath); |
219 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 287 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
220 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 288 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
221 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 289 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
222 intermediate_path)); | 290 intermediate_path)); |
| 291 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(_)); |
223 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 292 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
224 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 293 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
225 RunAllPendingInMessageLoops(); | 294 RunAllPendingInMessageLoops(); |
226 return download_file; | 295 return download_file; |
227 } | 296 } |
228 | 297 |
229 // Cleanup a download item (specifically get rid of the DownloadFile on it). | 298 // Cleanup a download item (specifically get rid of the DownloadFile on it). |
230 // The item must be in the IN_PROGRESS state. | 299 // The item must be in the expected state. |
231 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) { | 300 void CleanupItem(DownloadItemImpl* item, |
232 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 301 MockDownloadFile* download_file, |
| 302 DownloadItem::DownloadState expected_state) { |
| 303 EXPECT_EQ(expected_state, item->GetState()); |
233 | 304 |
234 EXPECT_CALL(*download_file, Cancel()); | 305 if (expected_state == DownloadItem::IN_PROGRESS) { |
235 item->Cancel(true); | 306 EXPECT_CALL(*download_file, Cancel()); |
236 loop_.RunUntilIdle(); | 307 item->Cancel(true); |
| 308 loop_.RunUntilIdle(); |
| 309 } |
237 } | 310 } |
238 | 311 |
239 // Destroy a previously created download item. | 312 // Destroy a previously created download item. |
240 void DestroyDownloadItem(DownloadItem* item) { | 313 void DestroyDownloadItem(DownloadItem* item) { |
241 allocated_downloads_.erase(item); | 314 allocated_downloads_.erase(item); |
242 delete item; | 315 delete item; |
243 } | 316 } |
244 | 317 |
245 void RunAllPendingInMessageLoops() { | 318 void RunAllPendingInMessageLoops() { |
246 loop_.RunUntilIdle(); | 319 loop_.RunUntilIdle(); |
247 } | 320 } |
248 | 321 |
249 MockDelegate* mock_delegate() { | 322 MockDelegate* mock_delegate() { |
250 return &delegate_; | 323 return &delegate_; |
251 } | 324 } |
252 | 325 |
253 private: | 326 private: |
254 MessageLoopForUI loop_; | 327 MessageLoopForUI loop_; |
255 TestBrowserThread ui_thread_; // UI thread | 328 TestBrowserThread ui_thread_; // UI thread |
256 TestBrowserThread file_thread_; // FILE thread | 329 TestBrowserThread file_thread_; // FILE thread |
257 testing::NiceMock<MockDelegate> delegate_; | 330 StrictMock<MockDelegate> delegate_; |
258 std::set<DownloadItem*> allocated_downloads_; | 331 std::set<DownloadItem*> allocated_downloads_; |
259 }; | 332 }; |
260 | 333 |
261 // Tests to ensure calls that change a DownloadItem generate an update to | 334 // Tests to ensure calls that change a DownloadItem generate an update to |
262 // observers. | 335 // observers. |
263 // State changing functions not tested: | 336 // State changing functions not tested: |
264 // void OpenDownload(); | 337 // void OpenDownload(); |
265 // void ShowDownloadInShell(); | 338 // void ShowDownloadInShell(); |
266 // void CompleteDelayedDownload(); | 339 // void CompleteDelayedDownload(); |
267 // set_* mutators | 340 // set_* mutators |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 item->OnDownloadedFileRemoved(); | 385 item->OnDownloadedFileRemoved(); |
313 ASSERT_TRUE(observer.CheckUpdated()); | 386 ASSERT_TRUE(observer.CheckUpdated()); |
314 } | 387 } |
315 | 388 |
316 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { | 389 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { |
317 DownloadItemImpl* item = CreateDownloadItem(); | 390 DownloadItemImpl* item = CreateDownloadItem(); |
318 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 391 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
319 EXPECT_CALL(*download_file, Cancel()); | 392 EXPECT_CALL(*download_file, Cancel()); |
320 MockObserver observer(item); | 393 MockObserver observer(item); |
321 | 394 |
| 395 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) |
| 396 .Times(0); |
| 397 |
322 item->DestinationObserverAsWeakPtr()->DestinationError( | 398 item->DestinationObserverAsWeakPtr()->DestinationError( |
323 DOWNLOAD_INTERRUPT_REASON_NONE); | 399 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
324 ASSERT_TRUE(observer.CheckUpdated()); | 400 ASSERT_TRUE(observer.CheckUpdated()); |
325 } | 401 } |
326 | 402 |
327 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 403 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
328 DownloadItemImpl* item = CreateDownloadItem(); | 404 DownloadItemImpl* item = CreateDownloadItem(); |
329 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 405 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
330 EXPECT_CALL(*download_file, Cancel()); | 406 EXPECT_CALL(*download_file, Cancel()); |
| 407 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
331 MockObserver observer(item); | 408 MockObserver observer(item); |
332 | 409 |
333 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 410 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
334 ASSERT_TRUE(observer.CheckUpdated()); | 411 ASSERT_TRUE(observer.CheckUpdated()); |
335 } | 412 } |
336 | 413 |
337 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 414 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
338 DownloadItemImpl* item = CreateDownloadItem(); | 415 DownloadItemImpl* item = CreateDownloadItem(); |
339 MockObserver observer(item); | 416 MockObserver observer(item); |
340 | 417 |
341 DestroyDownloadItem(item); | 418 DestroyDownloadItem(item); |
342 ASSERT_TRUE(observer.CheckDestroyed()); | 419 ASSERT_TRUE(observer.CheckDestroyed()); |
343 } | 420 } |
344 | 421 |
| 422 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { |
| 423 DownloadItemImpl* item = CreateDownloadItem(); |
| 424 MockObserver observer(item); |
| 425 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 426 MockDownloadFile* download_file = DoIntermediateRename(item); |
| 427 |
| 428 // Interrupt the download, using a continuable interrupt. |
| 429 EXPECT_CALL(*download_file, Detach()); |
| 430 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 431 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
| 432 ASSERT_TRUE(observer.CheckUpdated()); |
| 433 // Should attempt to auto-resume. Because we don't have a mock WebContents, |
| 434 // ResumeInterruptedDownload() will abort early, with another interrupt, |
| 435 // which will be ignored. |
| 436 ASSERT_EQ(1, observer.GetInterruptCount()); |
| 437 ASSERT_EQ(0, observer.GetResumeCount()); |
| 438 RunAllPendingInMessageLoops(); |
| 439 |
| 440 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); |
| 441 } |
| 442 |
| 443 // Same as above, but with a non-continuable interrupt. |
| 444 TEST_F(DownloadItemTest, RestartAfterInterrupted) { |
| 445 DownloadItemImpl* item = CreateDownloadItem(); |
| 446 MockObserver observer(item); |
| 447 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 448 MockDownloadFile* download_file = DoIntermediateRename(item); |
| 449 |
| 450 // Interrupt the download, using a restartable interrupt. |
| 451 EXPECT_CALL(*download_file, Cancel()); |
| 452 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 453 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 454 ASSERT_TRUE(observer.CheckUpdated()); |
| 455 // Should not try to auto-resume. |
| 456 ASSERT_EQ(1, observer.GetInterruptCount()); |
| 457 ASSERT_EQ(0, observer.GetResumeCount()); |
| 458 RunAllPendingInMessageLoops(); |
| 459 |
| 460 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); |
| 461 } |
| 462 |
| 463 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { |
| 464 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 465 switches::kEnableDownloadResumption); |
| 466 |
| 467 DownloadItemImpl* item = CreateDownloadItem(); |
| 468 base::WeakPtr<DownloadDestinationObserver> as_observer( |
| 469 item->DestinationObserverAsWeakPtr()); |
| 470 MockObserver observer(item); |
| 471 MockDownloadFile* mock_download_file(NULL); |
| 472 scoped_ptr<DownloadFile> download_file; |
| 473 MockRequestHandle* mock_request_handle(NULL); |
| 474 scoped_ptr<DownloadRequestHandleInterface> request_handle; |
| 475 |
| 476 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
| 477 .WillRepeatedly(Return()); |
| 478 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { |
| 479 DVLOG(20) << "Loop iteration " << i; |
| 480 |
| 481 mock_download_file = new NiceMock<MockDownloadFile>; |
| 482 download_file.reset(mock_download_file); |
| 483 mock_request_handle = new NiceMock<MockRequestHandle>; |
| 484 request_handle.reset(mock_request_handle); |
| 485 |
| 486 // It's too complicated to set up a WebContents instance that would cause |
| 487 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function |
| 488 // to be callled, so we simply verify that GetWebContents() is called. |
| 489 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) { |
| 490 EXPECT_CALL(*mock_request_handle, GetWebContents()) |
| 491 .WillOnce(Return(static_cast<WebContents*>(NULL))); |
| 492 } |
| 493 |
| 494 item->Start(download_file.Pass(), request_handle.Pass()); |
| 495 |
| 496 ASSERT_EQ(i, observer.GetResumeCount()); |
| 497 |
| 498 // Use a continuable interrupt. |
| 499 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 500 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
| 501 |
| 502 ASSERT_EQ(i + 1, observer.GetInterruptCount()); |
| 503 } |
| 504 |
| 505 CleanupItem(item, mock_download_file, DownloadItem::INTERRUPTED); |
| 506 } |
| 507 |
345 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 508 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
346 DownloadItemImpl* item = CreateDownloadItem(); | 509 DownloadItemImpl* item = CreateDownloadItem(); |
347 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 510 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
348 EXPECT_CALL(*download_file, Cancel()); | 511 EXPECT_CALL(*download_file, Cancel()); |
| 512 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
349 MockObserver observer(item); | 513 MockObserver observer(item); |
350 | 514 |
351 item->Remove(); | 515 item->Remove(); |
352 ASSERT_TRUE(observer.CheckUpdated()); | 516 ASSERT_TRUE(observer.CheckUpdated()); |
353 ASSERT_TRUE(observer.CheckRemoved()); | 517 ASSERT_TRUE(observer.CheckRemoved()); |
354 } | 518 } |
355 | 519 |
356 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 520 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
357 // Setting to NOT_DANGEROUS does not trigger a notification. | 521 // Setting to NOT_DANGEROUS does not trigger a notification. |
358 DownloadItemImpl* safe_item = CreateDownloadItem(); | 522 DownloadItemImpl* safe_item = CreateDownloadItem(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 DownloadItemImplDelegate::DownloadTargetCallback callback; | 563 DownloadItemImplDelegate::DownloadTargetCallback callback; |
400 MockDownloadFile* download_file = | 564 MockDownloadFile* download_file = |
401 AddDownloadFileToDownloadItem(item, &callback); | 565 AddDownloadFileToDownloadItem(item, &callback); |
402 MockObserver observer(item); | 566 MockObserver observer(item); |
403 FilePath target_path(kDummyPath); | 567 FilePath target_path(kDummyPath); |
404 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 568 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
405 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); | 569 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); |
406 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 570 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
407 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 571 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
408 new_intermediate_path)); | 572 new_intermediate_path)); |
| 573 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(_)); |
409 | 574 |
410 // Currently, a notification would be generated if the danger type is anything | 575 // Currently, a notification would be generated if the danger type is anything |
411 // other than NOT_DANGEROUS. | 576 // other than NOT_DANGEROUS. |
412 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 577 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
413 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 578 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
414 EXPECT_FALSE(observer.CheckUpdated()); | 579 EXPECT_FALSE(observer.CheckUpdated()); |
415 RunAllPendingInMessageLoops(); | 580 RunAllPendingInMessageLoops(); |
416 EXPECT_TRUE(observer.CheckUpdated()); | 581 EXPECT_TRUE(observer.CheckUpdated()); |
417 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 582 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
418 | 583 |
419 CleanupItem(item, download_file); | 584 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
420 } | 585 } |
421 | 586 |
422 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 587 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
423 DownloadItemImpl* item = CreateDownloadItem(); | 588 DownloadItemImpl* item = CreateDownloadItem(); |
424 MockObserver observer(item); | 589 MockObserver observer(item); |
| 590 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 591 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 592 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 593 new NiceMock<MockRequestHandle>); |
| 594 |
| 595 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 596 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); |
| 597 item->Start(download_file.Pass(), request_handle.Pass()); |
425 | 598 |
426 item->Pause(); | 599 item->Pause(); |
427 ASSERT_TRUE(observer.CheckUpdated()); | 600 ASSERT_TRUE(observer.CheckUpdated()); |
428 | 601 |
429 ASSERT_TRUE(item->IsPaused()); | 602 ASSERT_TRUE(item->IsPaused()); |
430 | 603 |
431 item->Resume(); | 604 item->Resume(); |
432 ASSERT_TRUE(observer.CheckUpdated()); | 605 ASSERT_TRUE(observer.CheckUpdated()); |
| 606 |
| 607 RunAllPendingInMessageLoops(); |
| 608 |
| 609 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
433 } | 610 } |
434 | 611 |
435 TEST_F(DownloadItemTest, DisplayName) { | 612 TEST_F(DownloadItemTest, DisplayName) { |
436 DownloadItemImpl* item = CreateDownloadItem(); | 613 DownloadItemImpl* item = CreateDownloadItem(); |
437 DownloadItemImplDelegate::DownloadTargetCallback callback; | 614 DownloadItemImplDelegate::DownloadTargetCallback callback; |
438 MockDownloadFile* download_file = | 615 MockDownloadFile* download_file = |
439 AddDownloadFileToDownloadItem(item, &callback); | 616 AddDownloadFileToDownloadItem(item, &callback); |
440 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 617 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
441 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 618 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
442 EXPECT_EQ(FILE_PATH_LITERAL(""), | 619 EXPECT_EQ(FILE_PATH_LITERAL(""), |
443 item->GetFileNameToReportUser().value()); | 620 item->GetFileNameToReportUser().value()); |
444 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) | 621 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) |
445 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 622 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
446 intermediate_path)); | 623 intermediate_path)); |
447 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 624 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
448 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 625 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 626 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(_)); |
449 RunAllPendingInMessageLoops(); | 627 RunAllPendingInMessageLoops(); |
450 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 628 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
451 item->GetFileNameToReportUser().value()); | 629 item->GetFileNameToReportUser().value()); |
452 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 630 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
453 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 631 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
454 item->GetFileNameToReportUser().value()); | 632 item->GetFileNameToReportUser().value()); |
455 CleanupItem(item, download_file); | 633 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
456 } | 634 } |
457 | 635 |
458 // Test to make sure that Start method calls DF initialize properly. | 636 // Test to make sure that Start method calls DF initialize properly. |
459 TEST_F(DownloadItemTest, Start) { | 637 TEST_F(DownloadItemTest, Start) { |
460 MockDownloadFile* mock_download_file(new MockDownloadFile); | 638 MockDownloadFile* mock_download_file(new MockDownloadFile); |
461 scoped_ptr<DownloadFile> download_file(mock_download_file); | 639 scoped_ptr<DownloadFile> download_file(mock_download_file); |
462 DownloadItemImpl* item = CreateDownloadItem(); | 640 DownloadItemImpl* item = CreateDownloadItem(); |
463 EXPECT_CALL(*mock_download_file, Initialize(_)); | 641 EXPECT_CALL(*mock_download_file, Initialize(_)); |
464 item->Start(download_file.Pass()); | 642 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 643 new NiceMock<MockRequestHandle>); |
| 644 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
| 645 item->Start(download_file.Pass(), request_handle.Pass()); |
465 | 646 |
466 CleanupItem(item, mock_download_file); | 647 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
467 } | 648 } |
468 | 649 |
469 // Test that the delegate is invoked after the download file is renamed. | 650 // Test that the delegate is invoked after the download file is renamed. |
470 TEST_F(DownloadItemTest, CallbackAfterRename) { | 651 TEST_F(DownloadItemTest, CallbackAfterRename) { |
471 DownloadItemImpl* item = CreateDownloadItem(); | 652 DownloadItemImpl* item = CreateDownloadItem(); |
472 DownloadItemImplDelegate::DownloadTargetCallback callback; | 653 DownloadItemImplDelegate::DownloadTargetCallback callback; |
473 MockDownloadFile* download_file = | 654 MockDownloadFile* download_file = |
474 AddDownloadFileToDownloadItem(item, &callback); | 655 AddDownloadFileToDownloadItem(item, &callback); |
475 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 656 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
476 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 657 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
477 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 658 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
478 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 659 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
479 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 660 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
480 new_intermediate_path)); | 661 new_intermediate_path)); |
481 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) | 662 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) |
482 .Times(1); | 663 .Times(1); |
483 | 664 |
484 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 665 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
485 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 666 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
486 RunAllPendingInMessageLoops(); | 667 RunAllPendingInMessageLoops(); |
487 // All the callbacks should have happened by now. | 668 // All the callbacks should have happened by now. |
488 ::testing::Mock::VerifyAndClearExpectations(download_file); | 669 ::testing::Mock::VerifyAndClearExpectations(download_file); |
489 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 670 mock_delegate()->VerifyAndClearExpectations(); |
490 | 671 |
491 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 672 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
492 .WillOnce(Return(true)); | 673 .WillOnce(Return(true)); |
493 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) | 674 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) |
494 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 675 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
495 final_path)); | 676 final_path)); |
496 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | |
497 .WillOnce(Return(true)); | |
498 EXPECT_CALL(*download_file, Detach()); | 677 EXPECT_CALL(*download_file, Detach()); |
499 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); | 678 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); |
500 RunAllPendingInMessageLoops(); | 679 RunAllPendingInMessageLoops(); |
501 ::testing::Mock::VerifyAndClearExpectations(download_file); | 680 ::testing::Mock::VerifyAndClearExpectations(download_file); |
502 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 681 mock_delegate()->VerifyAndClearExpectations(); |
503 } | 682 } |
504 | 683 |
505 // Test that the delegate is invoked after the download file is renamed and the | 684 // Test that the delegate is invoked after the download file is renamed and the |
506 // download item is in an interrupted state. | 685 // download item is in an interrupted state. |
507 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { | 686 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { |
508 DownloadItemImpl* item = CreateDownloadItem(); | 687 DownloadItemImpl* item = CreateDownloadItem(); |
509 DownloadItemImplDelegate::DownloadTargetCallback callback; | 688 DownloadItemImplDelegate::DownloadTargetCallback callback; |
510 MockDownloadFile* download_file = | 689 MockDownloadFile* download_file = |
511 AddDownloadFileToDownloadItem(item, &callback); | 690 AddDownloadFileToDownloadItem(item, &callback); |
512 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 691 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
513 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 692 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
514 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 693 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
515 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 694 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
516 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, | 695 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, |
517 new_intermediate_path)); | 696 new_intermediate_path)); |
518 EXPECT_CALL(*download_file, Cancel()) | 697 EXPECT_CALL(*download_file, Cancel()) |
519 .Times(1); | 698 .Times(1); |
520 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) | 699 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) |
521 .Times(1); | 700 .Times(1); |
522 | 701 |
523 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 702 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
524 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 703 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
525 RunAllPendingInMessageLoops(); | 704 RunAllPendingInMessageLoops(); |
526 // All the callbacks should have happened by now. | 705 // All the callbacks should have happened by now. |
527 ::testing::Mock::VerifyAndClearExpectations(download_file); | 706 ::testing::Mock::VerifyAndClearExpectations(download_file); |
528 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 707 mock_delegate()->VerifyAndClearExpectations(); |
529 } | 708 } |
530 | 709 |
531 TEST_F(DownloadItemTest, Interrupted) { | 710 TEST_F(DownloadItemTest, Interrupted) { |
532 DownloadItemImpl* item = CreateDownloadItem(); | 711 DownloadItemImpl* item = CreateDownloadItem(); |
533 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 712 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
534 | 713 |
535 const DownloadInterruptReason reason( | 714 const DownloadInterruptReason reason( |
536 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 715 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
537 | 716 |
538 // Confirm interrupt sets state properly. | 717 // Confirm interrupt sets state properly. |
539 EXPECT_CALL(*download_file, Cancel()); | 718 EXPECT_CALL(*download_file, Cancel()); |
540 item->DestinationObserverAsWeakPtr()->DestinationError(reason); | 719 item->DestinationObserverAsWeakPtr()->DestinationError(reason); |
541 RunAllPendingInMessageLoops(); | 720 RunAllPendingInMessageLoops(); |
542 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 721 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
543 EXPECT_EQ(reason, item->GetLastReason()); | 722 EXPECT_EQ(reason, item->GetLastReason()); |
544 | 723 |
545 // Cancel should result in no change. | 724 // Cancel should kill it. |
546 item->Cancel(true); | 725 item->Cancel(true); |
547 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 726 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
548 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); | 727 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); |
549 } | 728 } |
550 | 729 |
551 TEST_F(DownloadItemTest, Canceled) { | 730 TEST_F(DownloadItemTest, Canceled) { |
552 DownloadItemImpl* item = CreateDownloadItem(); | 731 DownloadItemImpl* item = CreateDownloadItem(); |
553 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 732 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
554 | 733 |
555 // Confirm cancel sets state properly. | 734 // Confirm cancel sets state properly. |
556 EXPECT_CALL(*download_file, Cancel()); | 735 EXPECT_CALL(*download_file, Cancel()); |
557 item->Cancel(true); | 736 item->Cancel(true); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 item->DestinationObserverAsWeakPtr()); | 781 item->DestinationObserverAsWeakPtr()); |
603 MockObserver observer(item); | 782 MockObserver observer(item); |
604 | 783 |
605 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 784 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
606 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); | 785 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); |
607 EXPECT_FALSE(observer.CheckUpdated()); | 786 EXPECT_FALSE(observer.CheckUpdated()); |
608 | 787 |
609 EXPECT_CALL(*download_file, Cancel()); | 788 EXPECT_CALL(*download_file, Cancel()); |
610 as_observer->DestinationError( | 789 as_observer->DestinationError( |
611 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 790 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
612 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 791 mock_delegate()->VerifyAndClearExpectations(); |
613 EXPECT_TRUE(observer.CheckUpdated()); | 792 EXPECT_TRUE(observer.CheckUpdated()); |
614 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 793 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
615 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 794 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
616 item->GetLastReason()); | 795 item->GetLastReason()); |
617 } | 796 } |
618 | 797 |
619 TEST_F(DownloadItemTest, DestinationCompleted) { | 798 TEST_F(DownloadItemTest, DestinationCompleted) { |
620 DownloadItemImpl* item = CreateDownloadItem(); | 799 DownloadItemImpl* item = CreateDownloadItem(); |
621 base::WeakPtr<DownloadDestinationObserver> as_observer( | 800 base::WeakPtr<DownloadDestinationObserver> as_observer( |
622 item->DestinationObserverAsWeakPtr()); | 801 item->DestinationObserverAsWeakPtr()); |
623 MockObserver observer(item); | 802 MockObserver observer(item); |
624 | 803 |
625 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 804 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
626 EXPECT_EQ("", item->GetHash()); | 805 EXPECT_EQ("", item->GetHash()); |
627 EXPECT_EQ("", item->GetHashState()); | 806 EXPECT_EQ("", item->GetHashState()); |
628 EXPECT_FALSE(item->AllDataSaved()); | 807 EXPECT_FALSE(item->AllDataSaved()); |
629 EXPECT_FALSE(observer.CheckUpdated()); | 808 EXPECT_FALSE(observer.CheckUpdated()); |
630 | 809 |
631 as_observer->DestinationUpdate(10, 20, "deadbeef"); | 810 as_observer->DestinationUpdate(10, 20, "deadbeef"); |
632 EXPECT_TRUE(observer.CheckUpdated()); | 811 EXPECT_TRUE(observer.CheckUpdated()); |
633 EXPECT_FALSE(observer.CheckUpdated()); // Confirm reset. | 812 EXPECT_FALSE(observer.CheckUpdated()); // Confirm reset. |
634 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 813 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
635 EXPECT_EQ("", item->GetHash()); | 814 EXPECT_EQ("", item->GetHash()); |
636 EXPECT_EQ("deadbeef", item->GetHashState()); | 815 EXPECT_EQ("deadbeef", item->GetHashState()); |
637 EXPECT_FALSE(item->AllDataSaved()); | 816 EXPECT_FALSE(item->AllDataSaved()); |
638 | 817 |
639 as_observer->DestinationCompleted("livebeef"); | 818 as_observer->DestinationCompleted("livebeef"); |
640 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 819 mock_delegate()->VerifyAndClearExpectations(); |
641 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 820 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
642 EXPECT_TRUE(observer.CheckUpdated()); | 821 EXPECT_TRUE(observer.CheckUpdated()); |
643 EXPECT_EQ("livebeef", item->GetHash()); | 822 EXPECT_EQ("livebeef", item->GetHash()); |
644 EXPECT_EQ("", item->GetHashState()); | 823 EXPECT_EQ("", item->GetHashState()); |
645 EXPECT_TRUE(item->AllDataSaved()); | 824 EXPECT_TRUE(item->AllDataSaved()); |
646 } | 825 } |
647 | 826 |
648 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { | 827 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { |
649 DownloadItemImpl* item = CreateDownloadItem(); | 828 DownloadItemImpl* item = CreateDownloadItem(); |
650 MockDownloadFile* download_file = DoIntermediateRename(item); | 829 MockDownloadFile* download_file = DoIntermediateRename(item); |
651 | 830 |
652 // InProgress | 831 // InProgress |
653 ASSERT_TRUE(item->IsInProgress()); | 832 ASSERT_TRUE(item->IsInProgress()); |
654 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 833 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
655 EXPECT_TRUE(item->CanShowInFolder()); | 834 EXPECT_TRUE(item->CanShowInFolder()); |
656 EXPECT_TRUE(item->CanOpenDownload()); | 835 EXPECT_TRUE(item->CanOpenDownload()); |
657 | 836 |
658 // Complete | 837 // Complete |
659 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) | 838 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) |
660 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 839 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
661 FilePath(kDummyPath))); | 840 FilePath(kDummyPath))); |
662 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 841 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
663 .WillOnce(Return(true)); | 842 .WillOnce(Return(true)); |
664 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | |
665 .WillOnce(Return(true)); | |
666 EXPECT_CALL(*download_file, Detach()); | 843 EXPECT_CALL(*download_file, Detach()); |
667 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); | 844 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); |
668 RunAllPendingInMessageLoops(); | 845 RunAllPendingInMessageLoops(); |
669 | 846 |
670 ASSERT_TRUE(item->IsComplete()); | 847 ASSERT_TRUE(item->IsComplete()); |
671 EXPECT_TRUE(item->CanShowInFolder()); | 848 EXPECT_TRUE(item->CanShowInFolder()); |
672 EXPECT_TRUE(item->CanOpenDownload()); | 849 EXPECT_TRUE(item->CanOpenDownload()); |
673 } | 850 } |
674 | 851 |
675 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { | 852 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { |
676 DownloadItemImpl* item = CreateDownloadItem(); | 853 DownloadItemImpl* item = CreateDownloadItem(); |
677 MockDownloadFile* download_file = DoIntermediateRename(item); | 854 MockDownloadFile* download_file = DoIntermediateRename(item); |
678 item->SetIsTemporary(true); | 855 item->SetIsTemporary(true); |
679 | 856 |
680 // InProgress Temporary | 857 // InProgress Temporary |
681 ASSERT_TRUE(item->IsInProgress()); | 858 ASSERT_TRUE(item->IsInProgress()); |
682 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 859 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
683 ASSERT_TRUE(item->IsTemporary()); | 860 ASSERT_TRUE(item->IsTemporary()); |
684 EXPECT_FALSE(item->CanShowInFolder()); | 861 EXPECT_FALSE(item->CanShowInFolder()); |
685 EXPECT_FALSE(item->CanOpenDownload()); | 862 EXPECT_FALSE(item->CanOpenDownload()); |
686 | 863 |
687 // Complete Temporary | 864 // Complete Temporary |
688 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 865 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
689 .WillOnce(Return(true)); | 866 .WillOnce(Return(true)); |
690 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) | 867 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) |
691 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 868 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
692 FilePath(kDummyPath))); | 869 FilePath(kDummyPath))); |
693 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | |
694 .WillOnce(Return(true)); | |
695 EXPECT_CALL(*download_file, Detach()); | 870 EXPECT_CALL(*download_file, Detach()); |
696 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); | 871 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); |
697 RunAllPendingInMessageLoops(); | 872 RunAllPendingInMessageLoops(); |
698 | 873 |
699 ASSERT_TRUE(item->IsComplete()); | 874 ASSERT_TRUE(item->IsComplete()); |
700 EXPECT_FALSE(item->CanShowInFolder()); | 875 EXPECT_FALSE(item->CanShowInFolder()); |
701 EXPECT_FALSE(item->CanOpenDownload()); | 876 EXPECT_FALSE(item->CanOpenDownload()); |
702 } | 877 } |
703 | 878 |
704 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { | 879 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 EXPECT_CALL(*download_file, Detach()); | 1050 EXPECT_CALL(*download_file, Detach()); |
876 RunAllPendingInMessageLoops(); | 1051 RunAllPendingInMessageLoops(); |
877 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1052 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
878 } | 1053 } |
879 | 1054 |
880 TEST(MockDownloadItem, Compiles) { | 1055 TEST(MockDownloadItem, Compiles) { |
881 MockDownloadItem mock_item; | 1056 MockDownloadItem mock_item; |
882 } | 1057 } |
883 | 1058 |
884 } // namespace content | 1059 } // namespace content |
OLD | NEW |