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 "content/browser/download/download_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <iterator> | 9 #include <iterator> |
10 #include <queue> | 10 #include <queue> |
(...skipping 10 matching lines...) Expand all Loading... | |
21 #include "content/browser/download/download_file_factory.h" | 21 #include "content/browser/download/download_file_factory.h" |
22 #include "content/browser/download/download_item_impl_delegate.h" | 22 #include "content/browser/download/download_item_impl_delegate.h" |
23 #include "content/browser/download/download_request_handle.h" | 23 #include "content/browser/download/download_request_handle.h" |
24 #include "content/browser/download/mock_download_file.h" | 24 #include "content/browser/download/mock_download_file.h" |
25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/download_destination_observer.h" | 26 #include "content/public/browser/download_destination_observer.h" |
27 #include "content/public/browser/download_interrupt_reasons.h" | 27 #include "content/public/browser/download_interrupt_reasons.h" |
28 #include "content/public/browser/download_url_parameters.h" | 28 #include "content/public/browser/download_url_parameters.h" |
29 #include "content/public/common/content_features.h" | 29 #include "content/public/common/content_features.h" |
30 #include "content/public/test/mock_download_item.h" | 30 #include "content/public/test/mock_download_item.h" |
31 #include "content/public/test/mock_download_item.h" | |
32 #include "content/public/test/test_browser_context.h" | |
31 #include "content/public/test/test_browser_context.h" | 33 #include "content/public/test/test_browser_context.h" |
32 #include "content/public/test/test_browser_thread_bundle.h" | 34 #include "content/public/test/test_browser_thread_bundle.h" |
35 #include "content/public/test/web_contents_tester.h" | |
33 #include "testing/gmock/include/gmock/gmock.h" | 36 #include "testing/gmock/include/gmock/gmock.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
35 | 38 |
36 using ::testing::DoAll; | 39 using ::testing::DoAll; |
37 using ::testing::NiceMock; | 40 using ::testing::NiceMock; |
38 using ::testing::Property; | 41 using ::testing::Property; |
39 using ::testing::Return; | 42 using ::testing::Return; |
40 using ::testing::SaveArg; | 43 using ::testing::SaveArg; |
41 using ::testing::StrictMock; | 44 using ::testing::StrictMock; |
42 using ::testing::WithArg; | 45 using ::testing::WithArg; |
(...skipping 26 matching lines...) Expand all Loading... | |
69 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); | 72 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); |
70 | 73 |
71 void ResumeInterruptedDownload(scoped_ptr<DownloadUrlParameters> params, | 74 void ResumeInterruptedDownload(scoped_ptr<DownloadUrlParameters> params, |
72 uint32_t id) override { | 75 uint32_t id) override { |
73 MockResumeInterruptedDownload(params.get(), id); | 76 MockResumeInterruptedDownload(params.get(), id); |
74 } | 77 } |
75 MOCK_METHOD2(MockResumeInterruptedDownload, | 78 MOCK_METHOD2(MockResumeInterruptedDownload, |
76 void(DownloadUrlParameters* params, uint32_t id)); | 79 void(DownloadUrlParameters* params, uint32_t id)); |
77 | 80 |
78 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); | 81 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); |
79 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); | |
80 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); | 82 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); |
81 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); | 83 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); |
82 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); | 84 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); |
83 | 85 |
84 void VerifyAndClearExpectations() { | 86 void VerifyAndClearExpectations() { |
85 ::testing::Mock::VerifyAndClearExpectations(this); | 87 ::testing::Mock::VerifyAndClearExpectations(this); |
86 SetDefaultExpectations(); | 88 SetDefaultExpectations(); |
87 } | 89 } |
88 | 90 |
89 private: | 91 private: |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 int resume_count() const { return resume_count_; } | 133 int resume_count() const { return resume_count_; } |
132 | 134 |
133 bool CheckAndResetDownloadUpdated() { | 135 bool CheckAndResetDownloadUpdated() { |
134 bool was_updated = updated_; | 136 bool was_updated = updated_; |
135 updated_ = false; | 137 updated_ = false; |
136 return was_updated; | 138 return was_updated; |
137 } | 139 } |
138 | 140 |
139 private: | 141 private: |
140 void OnDownloadRemoved(DownloadItem* download) override { | 142 void OnDownloadRemoved(DownloadItem* download) override { |
141 DVLOG(20) << " " << __FUNCTION__ | 143 SCOPED_TRACE(::testing::Message() << " " << __FUNCTION__ << " download = " |
142 << " download = " << download->DebugString(false); | 144 << download->DebugString(false)); |
143 removed_ = true; | 145 removed_ = true; |
144 } | 146 } |
145 | 147 |
146 void OnDownloadUpdated(DownloadItem* download) override { | 148 void OnDownloadUpdated(DownloadItem* download) override { |
147 DVLOG(20) << " " << __FUNCTION__ | 149 DVLOG(20) << " " << __FUNCTION__ |
148 << " download = " << download->DebugString(false); | 150 << " download = " << download->DebugString(false); |
149 updated_ = true; | 151 updated_ = true; |
150 DownloadItem::DownloadState new_state = download->GetState(); | 152 DownloadItem::DownloadState new_state = download->GetState(); |
151 if (last_state_ == DownloadItem::IN_PROGRESS && | 153 if (last_state_ == DownloadItem::IN_PROGRESS && |
152 new_state == DownloadItem::INTERRUPTED) { | 154 new_state == DownloadItem::INTERRUPTED) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 } | 208 } |
207 | 209 |
208 ACTION_P(ScheduleClosure, closure) { | 210 ACTION_P(ScheduleClosure, closure) { |
209 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); | 211 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
210 } | 212 } |
211 | 213 |
212 } // namespace | 214 } // namespace |
213 | 215 |
214 class DownloadItemTest : public testing::Test { | 216 class DownloadItemTest : public testing::Test { |
215 public: | 217 public: |
216 DownloadItemTest() { | 218 DownloadItemTest() |
219 : delegate_(), next_download_id_(DownloadItem::kInvalidId + 1) { | |
217 base::FeatureList::ClearInstanceForTesting(); | 220 base::FeatureList::ClearInstanceForTesting(); |
218 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); | 221 scoped_ptr<base::FeatureList> feature_list(new base::FeatureList); |
219 feature_list->InitializeFromCommandLine(features::kDownloadResumption.name, | 222 feature_list->InitializeFromCommandLine(features::kDownloadResumption.name, |
220 std::string()); | 223 std::string()); |
221 base::FeatureList::SetInstance(std::move(feature_list)); | 224 base::FeatureList::SetInstance(std::move(feature_list)); |
225 | |
226 create_info_.reset(new DownloadCreateInfo()); | |
227 create_info_->save_info = | |
228 scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | |
229 create_info_->save_info->prompt_for_save_location = false; | |
230 create_info_->url_chain.push_back(GURL("http://example.com/download")); | |
231 create_info_->etag = "SomethingToSatisfyResumption"; | |
222 } | 232 } |
223 | 233 |
224 ~DownloadItemTest() { | 234 ~DownloadItemTest() { |
225 RunAllPendingInMessageLoops(); | 235 RunAllPendingInMessageLoops(); |
226 STLDeleteElements(&allocated_downloads_); | 236 STLDeleteElements(&allocated_downloads_); |
227 } | 237 } |
228 | 238 |
229 DownloadItemImpl* CreateDownloadItemWithCreateInfo( | 239 DownloadItemImpl* CreateDownloadItemWithCreateInfo( |
230 scoped_ptr<DownloadCreateInfo> info) { | 240 scoped_ptr<DownloadCreateInfo> info) { |
231 DownloadItemImpl* download = new DownloadItemImpl( | 241 DownloadItemImpl* download = new DownloadItemImpl( |
232 &delegate_, next_download_id_++, *(info.get()), net::BoundNetLog()); | 242 &delegate_, next_download_id_++, *(info.get()), net::BoundNetLog()); |
233 allocated_downloads_.insert(download); | 243 allocated_downloads_.insert(download); |
234 return download; | 244 return download; |
235 } | 245 } |
236 | 246 |
237 // This class keeps ownership of the created download item; it will | 247 // This class keeps ownership of the created download item; it will |
238 // be torn down at the end of the test unless DestroyDownloadItem is | 248 // be torn down at the end of the test unless DestroyDownloadItem is |
239 // called. | 249 // called. |
240 DownloadItemImpl* CreateDownloadItem() { | 250 DownloadItemImpl* CreateDownloadItem() { |
241 scoped_ptr<DownloadCreateInfo> info; | 251 create_info_->download_id = ++next_download_id_; |
242 | 252 DownloadItemImpl* download = |
243 info.reset(new DownloadCreateInfo()); | 253 new DownloadItemImpl(&delegate_, create_info_->download_id, |
244 info->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 254 *create_info_, net::BoundNetLog()); |
245 info->save_info->prompt_for_save_location = false; | 255 allocated_downloads_.insert(download); |
246 info->url_chain.push_back(GURL("http://example.com/download")); | 256 return download; |
247 info->etag = "SomethingToSatisfyResumption"; | |
248 | |
249 return CreateDownloadItemWithCreateInfo(std::move(info)); | |
250 } | 257 } |
251 | 258 |
252 // Add DownloadFile to DownloadItem | 259 // Add DownloadFile to DownloadItem. Set |callback| to nullptr if a download |
260 // target determination is not expected. | |
253 MockDownloadFile* CallDownloadItemStart( | 261 MockDownloadFile* CallDownloadItemStart( |
254 DownloadItemImpl* item, | 262 DownloadItemImpl* item, |
255 DownloadItemImplDelegate::DownloadTargetCallback* callback) { | 263 DownloadItemImplDelegate::DownloadTargetCallback* callback) { |
256 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); | 264 MockDownloadFile* mock_download_file = nullptr; |
257 scoped_ptr<DownloadFile> download_file(mock_download_file); | 265 scoped_ptr<DownloadFile> download_file; |
258 EXPECT_CALL(*mock_download_file, Initialize(_)); | |
259 if (callback) { | 266 if (callback) { |
260 // Save the callback. | |
261 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 267 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
262 .WillOnce(SaveArg<1>(callback)); | 268 .WillOnce(SaveArg<1>(callback)); |
263 } else { | 269 } else { |
264 // Drop it on the floor. | 270 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)).Times(0); |
265 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | |
266 } | 271 } |
267 | 272 |
268 scoped_ptr<DownloadRequestHandleInterface> request_handle( | 273 // Only create a DownloadFile if the request was successful. |
274 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) { | |
275 mock_download_file = new StrictMock<MockDownloadFile>; | |
276 download_file.reset(mock_download_file); | |
277 EXPECT_CALL(*mock_download_file, Initialize(_)) | |
278 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE)); | |
279 EXPECT_CALL(*mock_download_file, FullPath()) | |
280 .WillRepeatedly(Return(base::FilePath())); | |
281 } | |
282 | |
283 scoped_ptr<MockRequestHandle> request_handle( | |
269 new NiceMock<MockRequestHandle>); | 284 new NiceMock<MockRequestHandle>); |
270 item->Start(std::move(download_file), std::move(request_handle)); | 285 item->Start(std::move(download_file), std::move(request_handle), |
286 *create_info_); | |
271 RunAllPendingInMessageLoops(); | 287 RunAllPendingInMessageLoops(); |
272 | 288 |
273 // So that we don't have a function writing to a stack variable | 289 // So that we don't have a function writing to a stack variable |
274 // lying around if the above failed. | 290 // lying around if the above failed. |
275 mock_delegate()->VerifyAndClearExpectations(); | 291 mock_delegate()->VerifyAndClearExpectations(); |
276 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) | 292 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) |
277 .WillRepeatedly(Return()); | 293 .WillRepeatedly(Return()); |
278 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) | 294 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) |
279 .WillRepeatedly(Return(false)); | 295 .WillRepeatedly(Return(false)); |
280 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) | 296 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 | 360 |
345 MockDelegate* mock_delegate() { | 361 MockDelegate* mock_delegate() { |
346 return &delegate_; | 362 return &delegate_; |
347 } | 363 } |
348 | 364 |
349 void OnDownloadFileAcquired(base::FilePath* return_path, | 365 void OnDownloadFileAcquired(base::FilePath* return_path, |
350 const base::FilePath& path) { | 366 const base::FilePath& path) { |
351 *return_path = path; | 367 *return_path = path; |
352 } | 368 } |
353 | 369 |
370 DownloadCreateInfo* create_info() { return create_info_.get(); } | |
371 | |
372 BrowserContext* browser_context() { return &browser_context_; } | |
373 | |
354 private: | 374 private: |
355 int next_download_id_ = DownloadItem::kInvalidId + 1; | |
356 StrictMock<MockDelegate> delegate_; | 375 StrictMock<MockDelegate> delegate_; |
357 std::set<DownloadItem*> allocated_downloads_; | 376 std::set<DownloadItem*> allocated_downloads_; |
377 scoped_ptr<DownloadCreateInfo> create_info_; | |
378 uint32_t next_download_id_ = DownloadItem::kInvalidId + 1; | |
379 TestBrowserContext browser_context_; | |
358 TestBrowserThreadBundle thread_bundle_; | 380 TestBrowserThreadBundle thread_bundle_; |
359 }; | 381 }; |
360 | 382 |
361 // Tests to ensure calls that change a DownloadItem generate an update to | 383 // Tests to ensure calls that change a DownloadItem generate an update to |
362 // observers. | 384 // observers. |
363 // State changing functions not tested: | 385 // State changing functions not tested: |
364 // void OpenDownload(); | 386 // void OpenDownload(); |
365 // void ShowDownloadInShell(); | 387 // void ShowDownloadInShell(); |
366 // void CompleteDelayedDownload(); | 388 // void CompleteDelayedDownload(); |
367 // set_* mutators | 389 // set_* mutators |
368 | 390 |
369 TEST_F(DownloadItemTest, NotificationAfterUpdate) { | 391 TEST_F(DownloadItemTest, NotificationAfterUpdate) { |
370 DownloadItemImpl* item = CreateDownloadItem(); | 392 DownloadItemImpl* item = CreateDownloadItem(); |
371 MockDownloadFile* file = | 393 MockDownloadFile* file = |
372 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 394 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
373 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 395 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
374 TestDownloadItemObserver observer(item); | 396 TestDownloadItemObserver observer(item); |
375 | 397 |
376 item->DestinationUpdate(kDownloadChunkSize, kDownloadSpeed, std::string()); | 398 item->DestinationUpdate(kDownloadChunkSize, kDownloadSpeed, std::string()); |
377 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 399 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
378 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); | 400 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); |
379 CleanupItem(item, file, DownloadItem::IN_PROGRESS); | 401 CleanupItem(item, file, DownloadItem::IN_PROGRESS); |
380 } | 402 } |
381 | 403 |
382 TEST_F(DownloadItemTest, NotificationAfterCancel) { | 404 TEST_F(DownloadItemTest, NotificationAfterCancel) { |
383 DownloadItemImpl* user_cancel = CreateDownloadItem(); | 405 DownloadItemImpl* user_cancel = CreateDownloadItem(); |
384 MockDownloadFile* download_file = CallDownloadItemStart(user_cancel, nullptr); | 406 DownloadItemImplDelegate::DownloadTargetCallback target_callback; |
407 MockDownloadFile* download_file = | |
408 CallDownloadItemStart(user_cancel, &target_callback); | |
385 EXPECT_CALL(*download_file, Cancel()); | 409 EXPECT_CALL(*download_file, Cancel()); |
410 | |
386 TestDownloadItemObserver observer1(user_cancel); | 411 TestDownloadItemObserver observer1(user_cancel); |
387 | |
388 user_cancel->Cancel(true); | 412 user_cancel->Cancel(true); |
389 ASSERT_TRUE(observer1.CheckAndResetDownloadUpdated()); | 413 ASSERT_TRUE(observer1.CheckAndResetDownloadUpdated()); |
390 | 414 |
391 DownloadItemImpl* system_cancel = CreateDownloadItem(); | 415 DownloadItemImpl* system_cancel = CreateDownloadItem(); |
392 download_file = CallDownloadItemStart(system_cancel, nullptr); | 416 download_file = CallDownloadItemStart(system_cancel, &target_callback); |
393 EXPECT_CALL(*download_file, Cancel()); | 417 EXPECT_CALL(*download_file, Cancel()); |
418 | |
394 TestDownloadItemObserver observer2(system_cancel); | 419 TestDownloadItemObserver observer2(system_cancel); |
395 | |
396 system_cancel->Cancel(false); | 420 system_cancel->Cancel(false); |
397 ASSERT_TRUE(observer2.CheckAndResetDownloadUpdated()); | 421 ASSERT_TRUE(observer2.CheckAndResetDownloadUpdated()); |
398 } | 422 } |
399 | 423 |
400 TEST_F(DownloadItemTest, NotificationAfterComplete) { | 424 TEST_F(DownloadItemTest, NotificationAfterComplete) { |
401 DownloadItemImpl* item = CreateDownloadItem(); | 425 DownloadItemImpl* item = CreateDownloadItem(); |
402 TestDownloadItemObserver observer(item); | 426 TestDownloadItemObserver observer(item); |
403 MockDownloadFile* download_file = | 427 MockDownloadFile* download_file = |
404 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 428 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
405 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 429 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
(...skipping 25 matching lines...) Expand all Loading... | |
431 } | 455 } |
432 | 456 |
433 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 457 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
434 DownloadItemImpl* item = CreateDownloadItem(); | 458 DownloadItemImpl* item = CreateDownloadItem(); |
435 TestDownloadItemObserver observer(item); | 459 TestDownloadItemObserver observer(item); |
436 | 460 |
437 DestroyDownloadItem(item); | 461 DestroyDownloadItem(item); |
438 ASSERT_TRUE(observer.download_destroyed()); | 462 ASSERT_TRUE(observer.download_destroyed()); |
439 } | 463 } |
440 | 464 |
465 // Test that a download is resumed automatcially after a continuable interrupt. | |
441 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { | 466 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { |
442 TestBrowserContext test_browser_context; | |
443 DownloadItemImpl* item = CreateDownloadItem(); | 467 DownloadItemImpl* item = CreateDownloadItem(); |
444 TestDownloadItemObserver observer(item); | 468 TestDownloadItemObserver observer(item); |
445 MockDownloadFile* download_file = | 469 MockDownloadFile* download_file = |
446 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 470 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
447 | 471 |
448 // Interrupt the download, using a continuable interrupt. | 472 // Interrupt the download, using a continuable interrupt. |
449 EXPECT_CALL(*download_file, FullPath()) | 473 EXPECT_CALL(*download_file, FullPath()) |
450 .WillOnce(Return(base::FilePath())); | 474 .WillOnce(Return(base::FilePath())); |
451 EXPECT_CALL(*download_file, Detach()); | 475 EXPECT_CALL(*download_file, Detach()); |
452 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | 476 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) |
453 .WillRepeatedly(Return(&test_browser_context)); | 477 .WillRepeatedly(Return(browser_context())); |
454 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1); | 478 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1); |
455 item->DestinationObserverAsWeakPtr()->DestinationError( | 479 item->DestinationObserverAsWeakPtr()->DestinationError( |
456 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); | 480 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
457 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 481 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
458 // Since the download is resumed automatically, the interrupt count doesn't | 482 // Since the download is resumed automatically, the interrupt count doesn't |
459 // increase. | 483 // increase. |
460 ASSERT_EQ(0, observer.interrupt_count()); | 484 ASSERT_EQ(0, observer.interrupt_count()); |
461 | 485 |
462 // Test expectations verify that ResumeInterruptedDownload() is called (by way | 486 // Test expectations verify that ResumeInterruptedDownload() is called (by way |
463 // of MockResumeInterruptedDownload) after the download is interrupted. But | 487 // of MockResumeInterruptedDownload) after the download is interrupted. But |
464 // the mock doesn't follow through with the resumption. | 488 // the mock doesn't follow through with the resumption. |
465 // ResumeInterruptedDownload() being called is sufficient for verifying that | 489 // ResumeInterruptedDownload() being called is sufficient for verifying that |
466 // the automatic resumption was triggered. | 490 // the automatic resumption was triggered. |
467 RunAllPendingInMessageLoops(); | 491 RunAllPendingInMessageLoops(); |
468 | 492 |
469 // The download item is currently in RESUMING_INTERNAL state, which maps to | 493 // The download item is currently in RESUMING_INTERNAL state, which maps to |
470 // IN_PROGRESS. | 494 // IN_PROGRESS. |
471 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); | 495 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); |
472 } | 496 } |
473 | 497 |
474 // Same as above, but with a non-continuable interrupt. | 498 // Test that automatic resumption doesn't happen after a non-continuable |
499 // interrupt. | |
475 TEST_F(DownloadItemTest, RestartAfterInterrupted) { | 500 TEST_F(DownloadItemTest, RestartAfterInterrupted) { |
476 DownloadItemImpl* item = CreateDownloadItem(); | 501 DownloadItemImpl* item = CreateDownloadItem(); |
477 TestDownloadItemObserver observer(item); | 502 TestDownloadItemObserver observer(item); |
478 MockDownloadFile* download_file = | 503 MockDownloadFile* download_file = |
479 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 504 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
480 | 505 |
481 // Interrupt the download, using a restartable interrupt. | 506 // Interrupt the download, using a restartable interrupt. |
482 EXPECT_CALL(*download_file, Cancel()); | 507 EXPECT_CALL(*download_file, Cancel()); |
483 item->DestinationObserverAsWeakPtr()->DestinationError( | 508 item->DestinationObserverAsWeakPtr()->DestinationError( |
484 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | 509 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
(...skipping 28 matching lines...) Expand all Loading... | |
513 | 538 |
514 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 539 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
515 // Should not try to auto-resume. | 540 // Should not try to auto-resume. |
516 ASSERT_EQ(1, observer.interrupt_count()); | 541 ASSERT_EQ(1, observer.interrupt_count()); |
517 ASSERT_EQ(0, observer.resume_count()); | 542 ASSERT_EQ(0, observer.resume_count()); |
518 | 543 |
519 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); | 544 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); |
520 } | 545 } |
521 | 546 |
522 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { | 547 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { |
523 TestBrowserContext test_browser_context; | |
524 DownloadItemImpl* item = CreateDownloadItem(); | 548 DownloadItemImpl* item = CreateDownloadItem(); |
525 base::WeakPtr<DownloadDestinationObserver> as_observer( | 549 base::WeakPtr<DownloadDestinationObserver> as_observer( |
526 item->DestinationObserverAsWeakPtr()); | 550 item->DestinationObserverAsWeakPtr()); |
527 TestDownloadItemObserver observer(item); | 551 TestDownloadItemObserver observer(item); |
528 MockDownloadFile* mock_download_file(nullptr); | 552 MockDownloadFile* mock_download_file(nullptr); |
529 scoped_ptr<DownloadFile> download_file; | 553 scoped_ptr<DownloadFile> download_file; |
530 MockRequestHandle* mock_request_handle(nullptr); | 554 MockRequestHandle* mock_request_handle(nullptr); |
531 scoped_ptr<DownloadRequestHandleInterface> request_handle; | 555 scoped_ptr<DownloadRequestHandleInterface> request_handle; |
532 DownloadItemImplDelegate::DownloadTargetCallback callback; | 556 DownloadItemImplDelegate::DownloadTargetCallback callback; |
533 | 557 |
534 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 558 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
535 .WillRepeatedly(SaveArg<1>(&callback)); | 559 .WillRepeatedly(SaveArg<1>(&callback)); |
536 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | 560 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) |
537 .WillRepeatedly(Return(&test_browser_context)); | 561 .WillRepeatedly(Return(browser_context())); |
538 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)) | 562 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)) |
539 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); | 563 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); |
540 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { | 564 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { |
541 DVLOG(20) << "Loop iteration " << i; | 565 SCOPED_TRACE(::testing::Message() << "Iteration " << i); |
542 | 566 |
543 mock_download_file = new NiceMock<MockDownloadFile>; | 567 mock_download_file = new NiceMock<MockDownloadFile>; |
544 download_file.reset(mock_download_file); | 568 download_file.reset(mock_download_file); |
545 mock_request_handle = new NiceMock<MockRequestHandle>; | 569 mock_request_handle = new NiceMock<MockRequestHandle>; |
546 request_handle.reset(mock_request_handle); | 570 request_handle.reset(mock_request_handle); |
547 | 571 |
548 ON_CALL(*mock_download_file, FullPath()) | 572 ON_CALL(*mock_download_file, FullPath()) |
549 .WillByDefault(Return(base::FilePath())); | 573 .WillByDefault(Return(base::FilePath())); |
550 | 574 |
551 // Copied key parts of DoIntermediateRename & CallDownloadItemStart | 575 // Copied key parts of DoIntermediateRename & CallDownloadItemStart |
552 // to allow for holding onto the request handle. | 576 // to allow for holding onto the request handle. |
553 item->Start(std::move(download_file), std::move(request_handle)); | 577 item->Start(std::move(download_file), std::move(request_handle), |
578 *create_info()); | |
554 RunAllPendingInMessageLoops(); | 579 RunAllPendingInMessageLoops(); |
555 | 580 |
556 base::FilePath target_path(kDummyTargetPath); | 581 base::FilePath target_path(kDummyTargetPath); |
557 base::FilePath intermediate_path(kDummyIntermediatePath); | 582 base::FilePath intermediate_path(kDummyIntermediatePath); |
558 if (i == 0) { | 583 if (i == 0) { |
559 // RenameAndUniquify is only called the first time. In all the subsequent | 584 // RenameAndUniquify is only called the first time. In all the subsequent |
560 // iterations, the intermediate file already has the correct name, hence | 585 // iterations, the intermediate file already has the correct name, hence |
561 // no rename is necessary. | 586 // no rename is necessary. |
562 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _)) | 587 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _)) |
563 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 588 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
564 intermediate_path)); | 589 intermediate_path)); |
565 } | 590 } |
566 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 591 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
567 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 592 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
568 RunAllPendingInMessageLoops(); | 593 RunAllPendingInMessageLoops(); |
569 | 594 |
570 // Use a continuable interrupt. | 595 // Use a continuable interrupt. |
571 item->DestinationObserverAsWeakPtr()->DestinationError( | 596 item->DestinationObserverAsWeakPtr()->DestinationError( |
572 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); | 597 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
573 | 598 |
599 RunAllPendingInMessageLoops(); | |
574 ::testing::Mock::VerifyAndClearExpectations(mock_download_file); | 600 ::testing::Mock::VerifyAndClearExpectations(mock_download_file); |
575 } | 601 } |
576 | 602 |
577 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 603 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
578 EXPECT_EQ(1, observer.interrupt_count()); | 604 EXPECT_EQ(1, observer.interrupt_count()); |
579 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); | 605 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); |
580 } | 606 } |
581 | 607 |
608 // If the download attempts to resume and the resumption request fails, the | |
609 // subsequent Start() call shouldn't update the origin state (URL redirect | |
610 // chains, Content-Disposition, download URL, etc..) | |
611 TEST_F(DownloadItemTest, FailedResumptionDoesntUpdateOriginState) { | |
612 const char kContentDisposition[] = "attachment; filename=foo"; | |
613 const char kFirstETag[] = "ABC"; | |
614 const char kFirstLastModified[] = "Yesterday"; | |
615 const char kFirstURL[] = "http://www.example.com/download"; | |
616 create_info()->content_disposition = kContentDisposition; | |
617 create_info()->etag = kFirstETag; | |
618 create_info()->last_modified = kFirstLastModified; | |
619 create_info()->url_chain.push_back(GURL(kFirstURL)); | |
620 | |
621 DownloadItemImpl* item = CreateDownloadItem(); | |
622 MockDownloadFile* download_file = | |
623 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | |
624 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | |
625 EXPECT_EQ(kFirstETag, item->GetETag()); | |
626 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | |
627 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | |
628 | |
629 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); | |
630 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | |
631 .WillRepeatedly(Return(browser_context())); | |
632 EXPECT_CALL(*download_file, Detach()); | |
633 item->DestinationObserverAsWeakPtr()->DestinationError( | |
634 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); | |
635 RunAllPendingInMessageLoops(); | |
636 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | |
637 | |
638 // Now change the create info. The changes should not cause the DownloadItem | |
639 // to be updated. | |
640 const char kSecondContentDisposition[] = "attachment; filename=bar"; | |
641 const char kSecondETag[] = "123"; | |
642 const char kSecondLastModified[] = "Today"; | |
643 const char kSecondURL[] = "http://example.com/another-download"; | |
644 create_info()->content_disposition = kSecondContentDisposition; | |
645 create_info()->etag = kSecondETag; | |
646 create_info()->last_modified = kSecondLastModified; | |
647 create_info()->url_chain.clear(); | |
648 create_info()->url_chain.push_back(GURL(kSecondURL)); | |
649 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; | |
650 | |
651 // The following ends up calling DownloadItem::Start(), but shouldn't result | |
652 // in an origin update. | |
653 download_file = CallDownloadItemStart(item, nullptr); | |
654 | |
655 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | |
656 EXPECT_EQ(kFirstETag, item->GetETag()); | |
657 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | |
658 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | |
659 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | |
660 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); | |
661 } | |
662 | |
582 // Test that resumption uses the final URL in a URL chain when resuming. | 663 // Test that resumption uses the final URL in a URL chain when resuming. |
583 TEST_F(DownloadItemTest, ResumeUsingFinalURL) { | 664 TEST_F(DownloadItemTest, ResumeUsingFinalURL) { |
584 TestBrowserContext test_browser_context; | 665 create_info()->save_info->prompt_for_save_location = false; |
585 scoped_ptr<DownloadCreateInfo> create_info(new DownloadCreateInfo); | 666 create_info()->url_chain.clear(); |
586 create_info->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 667 create_info()->url_chain.push_back(GURL("http://example.com/a")); |
587 create_info->save_info->prompt_for_save_location = false; | 668 create_info()->url_chain.push_back(GURL("http://example.com/b")); |
588 create_info->etag = "SomethingToSatisfyResumption"; | 669 create_info()->url_chain.push_back(GURL("http://example.com/c")); |
589 create_info->url_chain.push_back(GURL("http://example.com/a")); | |
590 create_info->url_chain.push_back(GURL("http://example.com/b")); | |
591 create_info->url_chain.push_back(GURL("http://example.com/c")); | |
592 | 670 |
593 DownloadItemImpl* item = | 671 DownloadItemImpl* item = CreateDownloadItem(); |
594 CreateDownloadItemWithCreateInfo(std::move(create_info)); | |
595 TestDownloadItemObserver observer(item); | 672 TestDownloadItemObserver observer(item); |
596 MockDownloadFile* download_file = | 673 MockDownloadFile* download_file = |
597 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 674 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
598 | 675 |
599 // Interrupt the download, using a continuable interrupt. | 676 // Interrupt the download, using a continuable interrupt. |
600 EXPECT_CALL(*download_file, FullPath()).WillOnce(Return(base::FilePath())); | 677 EXPECT_CALL(*download_file, FullPath()).WillOnce(Return(base::FilePath())); |
601 EXPECT_CALL(*download_file, Detach()); | 678 EXPECT_CALL(*download_file, Detach()); |
602 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) | 679 EXPECT_CALL(*mock_delegate(), GetBrowserContext()) |
603 .WillRepeatedly(Return(&test_browser_context)); | 680 .WillRepeatedly(Return(browser_context())); |
604 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload( | 681 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload( |
605 Property(&DownloadUrlParameters::url, | 682 Property(&DownloadUrlParameters::url, |
606 GURL("http://example.com/c")), | 683 GURL("http://example.com/c")), |
607 _)) | 684 _)) |
608 .Times(1); | 685 .Times(1); |
609 item->DestinationObserverAsWeakPtr()->DestinationError( | 686 item->DestinationObserverAsWeakPtr()->DestinationError( |
610 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); | 687 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
611 | 688 |
612 // Test expectations verify that ResumeInterruptedDownload() is called (by way | 689 // Test expectations verify that ResumeInterruptedDownload() is called (by way |
613 // of MockResumeInterruptedDownload) after the download is interrupted. But | 690 // of MockResumeInterruptedDownload) after the download is interrupted. But |
614 // the mock doesn't follow through with the resumption. | 691 // the mock doesn't follow through with the resumption. |
615 // ResumeInterruptedDownload() being called is sufficient for verifying that | 692 // ResumeInterruptedDownload() being called is sufficient for verifying that |
616 // the resumption was triggered. | 693 // the resumption was triggered. |
617 RunAllPendingInMessageLoops(); | 694 RunAllPendingInMessageLoops(); |
618 | 695 |
619 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. | 696 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. |
620 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); | 697 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); |
621 } | 698 } |
622 | 699 |
623 TEST_F(DownloadItemTest, NotificationAfterRemove) { | |
Randy Smith (Not in Mondays)
2016/02/12 22:09:23
Why removed?
asanka
2016/02/12 23:34:47
Whoops. Restored.
| |
624 DownloadItemImpl* item = CreateDownloadItem(); | |
625 MockDownloadFile* download_file = CallDownloadItemStart(item, nullptr); | |
626 EXPECT_CALL(*download_file, Cancel()); | |
627 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); | |
628 TestDownloadItemObserver observer(item); | |
629 | |
630 item->Remove(); | |
631 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | |
632 ASSERT_TRUE(observer.download_removed()); | |
633 } | |
634 | |
635 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 700 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
636 // Setting to NOT_DANGEROUS does not trigger a notification. | 701 // Setting to NOT_DANGEROUS does not trigger a notification. |
637 DownloadItemImpl* safe_item = CreateDownloadItem(); | 702 DownloadItemImpl* safe_item = CreateDownloadItem(); |
638 MockDownloadFile* download_file = | 703 MockDownloadFile* download_file = |
639 DoIntermediateRename(safe_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 704 DoIntermediateRename(safe_item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
640 TestDownloadItemObserver safe_observer(safe_item); | 705 TestDownloadItemObserver safe_observer(safe_item); |
641 | 706 |
642 safe_item->OnAllDataSaved(std::string()); | 707 safe_item->OnAllDataSaved(std::string()); |
643 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); | 708 EXPECT_TRUE(safe_observer.CheckAndResetDownloadUpdated()); |
644 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 709 safe_item->OnContentCheckCompleted(DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
716 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 781 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
717 DownloadItemImpl* item = CreateDownloadItem(); | 782 DownloadItemImpl* item = CreateDownloadItem(); |
718 TestDownloadItemObserver observer(item); | 783 TestDownloadItemObserver observer(item); |
719 MockDownloadFile* mock_download_file(new MockDownloadFile); | 784 MockDownloadFile* mock_download_file(new MockDownloadFile); |
720 scoped_ptr<DownloadFile> download_file(mock_download_file); | 785 scoped_ptr<DownloadFile> download_file(mock_download_file); |
721 scoped_ptr<DownloadRequestHandleInterface> request_handle( | 786 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
722 new NiceMock<MockRequestHandle>); | 787 new NiceMock<MockRequestHandle>); |
723 | 788 |
724 EXPECT_CALL(*mock_download_file, Initialize(_)); | 789 EXPECT_CALL(*mock_download_file, Initialize(_)); |
725 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); | 790 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); |
726 item->Start(std::move(download_file), std::move(request_handle)); | 791 item->Start(std::move(download_file), std::move(request_handle), |
792 *create_info()); | |
727 | 793 |
728 item->Pause(); | 794 item->Pause(); |
729 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 795 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
730 | 796 |
731 ASSERT_TRUE(item->IsPaused()); | 797 ASSERT_TRUE(item->IsPaused()); |
732 | 798 |
733 item->Resume(); | 799 item->Resume(); |
734 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 800 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
735 | 801 |
736 RunAllPendingInMessageLoops(); | 802 RunAllPendingInMessageLoops(); |
(...skipping 26 matching lines...) Expand all Loading... | |
763 | 829 |
764 // Test to make sure that Start method calls DF initialize properly. | 830 // Test to make sure that Start method calls DF initialize properly. |
765 TEST_F(DownloadItemTest, Start) { | 831 TEST_F(DownloadItemTest, Start) { |
766 MockDownloadFile* mock_download_file(new MockDownloadFile); | 832 MockDownloadFile* mock_download_file(new MockDownloadFile); |
767 scoped_ptr<DownloadFile> download_file(mock_download_file); | 833 scoped_ptr<DownloadFile> download_file(mock_download_file); |
768 DownloadItemImpl* item = CreateDownloadItem(); | 834 DownloadItemImpl* item = CreateDownloadItem(); |
769 EXPECT_CALL(*mock_download_file, Initialize(_)); | 835 EXPECT_CALL(*mock_download_file, Initialize(_)); |
770 scoped_ptr<DownloadRequestHandleInterface> request_handle( | 836 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
771 new NiceMock<MockRequestHandle>); | 837 new NiceMock<MockRequestHandle>); |
772 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | 838 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
773 item->Start(std::move(download_file), std::move(request_handle)); | 839 item->Start(std::move(download_file), std::move(request_handle), |
840 *create_info()); | |
774 RunAllPendingInMessageLoops(); | 841 RunAllPendingInMessageLoops(); |
775 | 842 |
776 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); | 843 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
777 } | 844 } |
778 | 845 |
779 // Download file and the request should be cancelled as a result of download | 846 // Download file and the request should be cancelled as a result of download |
780 // file initialization failing. | 847 // file initialization failing. |
781 TEST_F(DownloadItemTest, InitDownloadFileFails) { | 848 TEST_F(DownloadItemTest, InitDownloadFileFails) { |
849 DownloadItemImpl* item = CreateDownloadItem(); | |
782 scoped_ptr<MockDownloadFile> file(new MockDownloadFile()); | 850 scoped_ptr<MockDownloadFile> file(new MockDownloadFile()); |
783 scoped_ptr<MockRequestHandle> request_handle(new MockRequestHandle()); | 851 scoped_ptr<MockRequestHandle> request_handle(new MockRequestHandle()); |
784 EXPECT_CALL(*file, Cancel()); | 852 EXPECT_CALL(*file, Cancel()); |
785 EXPECT_CALL(*request_handle, CancelRequest()); | 853 EXPECT_CALL(*request_handle, CancelRequest()); |
786 EXPECT_CALL(*file, Initialize(_)) | 854 EXPECT_CALL(*file, Initialize(_)) |
787 .WillOnce(ScheduleCallbackWithParam( | 855 .WillOnce(ScheduleCallbackWithParam( |
788 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED)); | 856 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED)); |
789 | 857 |
790 DownloadItemImpl* item = CreateDownloadItem(); | 858 base::RunLoop start_download_loop; |
791 item->Start(std::move(file), std::move(request_handle)); | 859 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback; |
860 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | |
861 .WillOnce(DoAll(SaveArg<1>(&download_target_callback), | |
862 ScheduleClosure(start_download_loop.QuitClosure()))); | |
863 | |
864 item->Start(std::move(file), std::move(request_handle), *create_info()); | |
865 start_download_loop.Run(); | |
866 | |
867 download_target_callback.Run(base::FilePath(kDummyTargetPath), | |
868 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
869 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
870 base::FilePath(kDummyIntermediatePath)); | |
792 RunAllPendingInMessageLoops(); | 871 RunAllPendingInMessageLoops(); |
793 | 872 |
794 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 873 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
795 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 874 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
796 item->GetLastReason()); | 875 item->GetLastReason()); |
797 } | 876 } |
798 | 877 |
878 // Handling of downloads initiated via a failed request. In this case, Start() | |
879 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason. | |
880 TEST_F(DownloadItemTest, StartFailedDownload) { | |
881 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; | |
882 DownloadItemImpl* item = CreateDownloadItem(); | |
883 | |
884 // DownloadFile and DownloadRequestHandleInterface objects aren't created for | |
885 // failed downloads. | |
886 scoped_ptr<DownloadFile> null_download_file; | |
887 scoped_ptr<DownloadRequestHandleInterface> null_request_handle; | |
888 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback; | |
889 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | |
890 .WillOnce(SaveArg<1>(&download_target_callback)); | |
891 item->Start(std::move(null_download_file), std::move(null_request_handle), | |
892 *create_info()); | |
893 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | |
894 RunAllPendingInMessageLoops(); | |
895 | |
896 // The DownloadItemImpl should attempt to determine a target path even if the | |
897 // download was interrupted. | |
898 ASSERT_FALSE(download_target_callback.is_null()); | |
899 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | |
900 base::FilePath target_path(FILE_PATH_LITERAL("foo")); | |
901 download_target_callback.Run(target_path, | |
902 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
903 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path); | |
904 RunAllPendingInMessageLoops(); | |
905 | |
906 EXPECT_EQ(target_path, item->GetTargetFilePath()); | |
907 CleanupItem(item, NULL, DownloadItem::INTERRUPTED); | |
908 } | |
909 | |
799 // Test that the delegate is invoked after the download file is renamed. | 910 // Test that the delegate is invoked after the download file is renamed. |
800 TEST_F(DownloadItemTest, CallbackAfterRename) { | 911 TEST_F(DownloadItemTest, CallbackAfterRename) { |
801 DownloadItemImpl* item = CreateDownloadItem(); | 912 DownloadItemImpl* item = CreateDownloadItem(); |
802 DownloadItemImplDelegate::DownloadTargetCallback callback; | 913 DownloadItemImplDelegate::DownloadTargetCallback callback; |
803 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); | 914 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); |
804 base::FilePath final_path( | 915 base::FilePath final_path( |
805 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); | 916 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); |
806 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 917 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
807 base::FilePath new_intermediate_path( | 918 base::FilePath new_intermediate_path( |
808 final_path.InsertBeforeExtensionASCII("y")); | 919 final_path.InsertBeforeExtensionASCII("y")); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
971 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1082 ::testing::Mock::VerifyAndClearExpectations(download_file); |
972 mock_delegate()->VerifyAndClearExpectations(); | 1083 mock_delegate()->VerifyAndClearExpectations(); |
973 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1084 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
974 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); | 1085 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); |
975 EXPECT_TRUE(item->GetFullPath().empty()); | 1086 EXPECT_TRUE(item->GetFullPath().empty()); |
976 EXPECT_EQ(final_path, item->GetTargetFilePath()); | 1087 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
977 } | 1088 } |
978 | 1089 |
979 TEST_F(DownloadItemTest, Canceled) { | 1090 TEST_F(DownloadItemTest, Canceled) { |
980 DownloadItemImpl* item = CreateDownloadItem(); | 1091 DownloadItemImpl* item = CreateDownloadItem(); |
981 MockDownloadFile* download_file = CallDownloadItemStart(item, nullptr); | 1092 DownloadItemImplDelegate::DownloadTargetCallback target_callback; |
1093 MockDownloadFile* download_file = | |
1094 CallDownloadItemStart(item, &target_callback); | |
982 | 1095 |
983 // Confirm cancel sets state properly. | 1096 // Confirm cancel sets state properly. |
984 EXPECT_CALL(*download_file, Cancel()); | 1097 EXPECT_CALL(*download_file, Cancel()); |
985 item->Cancel(true); | 1098 item->Cancel(true); |
986 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); | 1099 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
987 } | 1100 } |
988 | 1101 |
989 TEST_F(DownloadItemTest, FileRemoved) { | 1102 TEST_F(DownloadItemTest, FileRemoved) { |
990 DownloadItemImpl* item = CreateDownloadItem(); | 1103 DownloadItemImpl* item = CreateDownloadItem(); |
991 | 1104 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1044 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 1157 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
1045 mock_delegate()->VerifyAndClearExpectations(); | 1158 mock_delegate()->VerifyAndClearExpectations(); |
1046 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); | 1159 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); |
1047 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1160 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
1048 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 1161 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
1049 item->GetLastReason()); | 1162 item->GetLastReason()); |
1050 } | 1163 } |
1051 | 1164 |
1052 TEST_F(DownloadItemTest, DestinationCompleted) { | 1165 TEST_F(DownloadItemTest, DestinationCompleted) { |
1053 DownloadItemImpl* item = CreateDownloadItem(); | 1166 DownloadItemImpl* item = CreateDownloadItem(); |
1054 MockDownloadFile* download_file = CallDownloadItemStart(item, nullptr); | 1167 MockDownloadFile* download_file = |
1168 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | |
1055 base::WeakPtr<DownloadDestinationObserver> as_observer( | 1169 base::WeakPtr<DownloadDestinationObserver> as_observer( |
1056 item->DestinationObserverAsWeakPtr()); | 1170 item->DestinationObserverAsWeakPtr()); |
1057 TestDownloadItemObserver observer(item); | 1171 TestDownloadItemObserver observer(item); |
1058 | 1172 |
1059 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1173 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1060 EXPECT_EQ("", item->GetHash()); | 1174 EXPECT_EQ("", item->GetHash()); |
1061 EXPECT_EQ("", item->GetHashState()); | 1175 EXPECT_EQ("", item->GetHashState()); |
1062 EXPECT_FALSE(item->AllDataSaved()); | 1176 EXPECT_FALSE(item->AllDataSaved()); |
1063 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); | 1177 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); |
1064 | 1178 |
1065 as_observer->DestinationUpdate(10, 20, "deadbeef"); | 1179 as_observer->DestinationUpdate(10, 20, "deadbeef"); |
1066 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); | 1180 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); |
1067 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); // Confirm reset. | 1181 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); // Confirm reset. |
1068 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1182 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1069 EXPECT_EQ("", item->GetHash()); | 1183 EXPECT_EQ("", item->GetHash()); |
1070 EXPECT_EQ("deadbeef", item->GetHashState()); | 1184 EXPECT_EQ("deadbeef", item->GetHashState()); |
1071 EXPECT_FALSE(item->AllDataSaved()); | 1185 EXPECT_FALSE(item->AllDataSaved()); |
1072 | 1186 |
1187 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)); | |
1073 as_observer->DestinationCompleted("livebeef"); | 1188 as_observer->DestinationCompleted("livebeef"); |
1074 mock_delegate()->VerifyAndClearExpectations(); | 1189 mock_delegate()->VerifyAndClearExpectations(); |
1075 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1190 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
1076 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); | 1191 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); |
1077 EXPECT_EQ("livebeef", item->GetHash()); | 1192 EXPECT_EQ("livebeef", item->GetHash()); |
1078 EXPECT_EQ("", item->GetHashState()); | 1193 EXPECT_EQ("", item->GetHashState()); |
1079 EXPECT_TRUE(item->AllDataSaved()); | 1194 EXPECT_TRUE(item->AllDataSaved()); |
1080 | 1195 |
1081 // Even though the DownloadItem receives a DestinationCompleted() event, | 1196 // Even though the DownloadItem receives a DestinationCompleted() event, |
1082 // target determination hasn't completed, hence the download item is stuck in | 1197 // target determination hasn't completed, hence the download item is stuck in |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1640 // Expect that the download file and the request will be cancelled as a | 1755 // Expect that the download file and the request will be cancelled as a |
1641 // result. | 1756 // result. |
1642 EXPECT_CALL(*file_, Cancel()); | 1757 EXPECT_CALL(*file_, Cancel()); |
1643 EXPECT_CALL(*request_handle_, CancelRequest()); | 1758 EXPECT_CALL(*request_handle_, CancelRequest()); |
1644 | 1759 |
1645 base::RunLoop download_start_loop; | 1760 base::RunLoop download_start_loop; |
1646 DownloadFile::InitializeCallback initialize_callback; | 1761 DownloadFile::InitializeCallback initialize_callback; |
1647 EXPECT_CALL(*file_, Initialize(_)) | 1762 EXPECT_CALL(*file_, Initialize(_)) |
1648 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 1763 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
1649 ScheduleClosure(download_start_loop.QuitClosure()))); | 1764 ScheduleClosure(download_start_loop.QuitClosure()))); |
1650 item_->Start(std::move(file_), std::move(request_handle_)); | 1765 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
1651 download_start_loop.Run(); | 1766 download_start_loop.Run(); |
1652 | 1767 |
1653 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 1768 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
1654 item_->DestinationObserverAsWeakPtr(); | 1769 item_->DestinationObserverAsWeakPtr(); |
1655 | 1770 |
1656 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 1771 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
1657 RunAllPendingInMessageLoops(); | 1772 RunAllPendingInMessageLoops(); |
1658 | 1773 |
1659 base::RunLoop initialize_completion_loop; | 1774 base::RunLoop initialize_completion_loop; |
1660 DownloadItemImplDelegate::DownloadTargetCallback target_callback; | 1775 DownloadItemImplDelegate::DownloadTargetCallback target_callback; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1693 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) | 1808 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) |
1694 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), | 1809 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), |
1695 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); | 1810 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); |
1696 | 1811 |
1697 base::RunLoop download_start_loop; | 1812 base::RunLoop download_start_loop; |
1698 DownloadFile::InitializeCallback initialize_callback; | 1813 DownloadFile::InitializeCallback initialize_callback; |
1699 EXPECT_CALL(*file_, Initialize(_)) | 1814 EXPECT_CALL(*file_, Initialize(_)) |
1700 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 1815 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
1701 ScheduleClosure(download_start_loop.QuitClosure()))); | 1816 ScheduleClosure(download_start_loop.QuitClosure()))); |
1702 | 1817 |
1703 item_->Start(std::move(file_), std::move(request_handle_)); | 1818 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
1704 download_start_loop.Run(); | 1819 download_start_loop.Run(); |
1705 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 1820 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
1706 item_->DestinationObserverAsWeakPtr(); | 1821 item_->DestinationObserverAsWeakPtr(); |
1707 | 1822 |
1708 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 1823 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
1709 RunAllPendingInMessageLoops(); | 1824 RunAllPendingInMessageLoops(); |
1710 | 1825 |
1711 base::RunLoop initialize_completion_loop; | 1826 base::RunLoop initialize_completion_loop; |
1712 DownloadItemImplDelegate::DownloadTargetCallback target_callback; | 1827 DownloadItemImplDelegate::DownloadTargetCallback target_callback; |
1713 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) | 1828 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1762 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) | 1877 EXPECT_CALL(*file_, RenameAndUniquify(_, _)) |
1763 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), | 1878 .WillOnce(DoAll(SaveArg<1>(&intermediate_rename_callback), |
1764 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); | 1879 ScheduleClosure(intermediate_rename_loop.QuitClosure()))); |
1765 | 1880 |
1766 base::RunLoop download_start_loop; | 1881 base::RunLoop download_start_loop; |
1767 DownloadFile::InitializeCallback initialize_callback; | 1882 DownloadFile::InitializeCallback initialize_callback; |
1768 EXPECT_CALL(*file_, Initialize(_)) | 1883 EXPECT_CALL(*file_, Initialize(_)) |
1769 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 1884 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
1770 ScheduleClosure(download_start_loop.QuitClosure()))); | 1885 ScheduleClosure(download_start_loop.QuitClosure()))); |
1771 | 1886 |
1772 item_->Start(std::move(file_), std::move(request_handle_)); | 1887 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
1773 download_start_loop.Run(); | 1888 download_start_loop.Run(); |
1774 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 1889 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
1775 item_->DestinationObserverAsWeakPtr(); | 1890 item_->DestinationObserverAsWeakPtr(); |
1776 | 1891 |
1777 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 1892 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
1778 RunAllPendingInMessageLoops(); | 1893 RunAllPendingInMessageLoops(); |
1779 | 1894 |
1780 base::RunLoop initialize_completion_loop; | 1895 base::RunLoop initialize_completion_loop; |
1781 DownloadItemImplDelegate::DownloadTargetCallback target_callback; | 1896 DownloadItemImplDelegate::DownloadTargetCallback target_callback; |
1782 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) | 1897 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1823 | 1938 |
1824 item_->Cancel(true); | 1939 item_->Cancel(true); |
1825 RunAllPendingInMessageLoops(); | 1940 RunAllPendingInMessageLoops(); |
1826 } | 1941 } |
1827 | 1942 |
1828 TEST(MockDownloadItem, Compiles) { | 1943 TEST(MockDownloadItem, Compiles) { |
1829 MockDownloadItem mock_item; | 1944 MockDownloadItem mock_item; |
1830 } | 1945 } |
1831 | 1946 |
1832 } // namespace content | 1947 } // namespace content |
OLD | NEW |