| 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 <deque> | 9 #include <deque> |
| 10 #include <iterator> | 10 #include <iterator> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "content/browser/download/download_item_impl_delegate.h" | 28 #include "content/browser/download/download_item_impl_delegate.h" |
| 29 #include "content/browser/download/download_request_handle.h" | 29 #include "content/browser/download/download_request_handle.h" |
| 30 #include "content/browser/download/mock_download_file.h" | 30 #include "content/browser/download/mock_download_file.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/download_interrupt_reasons.h" | 32 #include "content/public/browser/download_interrupt_reasons.h" |
| 33 #include "content/public/browser/download_url_parameters.h" | 33 #include "content/public/browser/download_url_parameters.h" |
| 34 #include "content/public/common/content_features.h" | 34 #include "content/public/common/content_features.h" |
| 35 #include "content/public/test/mock_download_item.h" | 35 #include "content/public/test/mock_download_item.h" |
| 36 #include "content/public/test/test_browser_context.h" | 36 #include "content/public/test/test_browser_context.h" |
| 37 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
| 38 #include "content/public/test/test_utils.h" |
| 38 #include "content/public/test/web_contents_tester.h" | 39 #include "content/public/test/web_contents_tester.h" |
| 39 #include "crypto/secure_hash.h" | 40 #include "crypto/secure_hash.h" |
| 40 #include "net/http/http_response_headers.h" | 41 #include "net/http/http_response_headers.h" |
| 41 #include "testing/gmock/include/gmock/gmock.h" | 42 #include "testing/gmock/include/gmock/gmock.h" |
| 42 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
| 43 | 44 |
| 44 using ::testing::DoAll; | 45 using ::testing::DoAll; |
| 45 using ::testing::Invoke; | 46 using ::testing::Invoke; |
| 46 using ::testing::InvokeWithoutArgs; | 47 using ::testing::InvokeWithoutArgs; |
| 47 using ::testing::NiceMock; | 48 using ::testing::NiceMock; |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 DownloadItemTest() | 260 DownloadItemTest() |
| 260 : delegate_(), next_download_id_(DownloadItem::kInvalidId + 1) { | 261 : delegate_(), next_download_id_(DownloadItem::kInvalidId + 1) { |
| 261 create_info_.reset(new DownloadCreateInfo()); | 262 create_info_.reset(new DownloadCreateInfo()); |
| 262 create_info_->save_info = | 263 create_info_->save_info = |
| 263 std::unique_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 264 std::unique_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); |
| 264 create_info_->save_info->prompt_for_save_location = false; | 265 create_info_->save_info->prompt_for_save_location = false; |
| 265 create_info_->url_chain.push_back(GURL("http://example.com/download")); | 266 create_info_->url_chain.push_back(GURL("http://example.com/download")); |
| 266 create_info_->etag = "SomethingToSatisfyResumption"; | 267 create_info_->etag = "SomethingToSatisfyResumption"; |
| 267 } | 268 } |
| 268 | 269 |
| 269 ~DownloadItemTest() { | 270 ~DownloadItemTest() {} |
| 270 RunAllPendingInMessageLoops(); | |
| 271 } | |
| 272 | 271 |
| 273 DownloadItemImpl* CreateDownloadItemWithCreateInfo( | 272 DownloadItemImpl* CreateDownloadItemWithCreateInfo( |
| 274 std::unique_ptr<DownloadCreateInfo> info) { | 273 std::unique_ptr<DownloadCreateInfo> info) { |
| 275 DownloadItemImpl* download = | 274 DownloadItemImpl* download = |
| 276 new DownloadItemImpl(&delegate_, next_download_id_++, *(info.get()), | 275 new DownloadItemImpl(&delegate_, next_download_id_++, *(info.get()), |
| 277 net::NetLogWithSource()); | 276 net::NetLogWithSource()); |
| 278 allocated_downloads_[download] = base::WrapUnique(download); | 277 allocated_downloads_[download] = base::WrapUnique(download); |
| 279 return download; | 278 return download; |
| 280 } | 279 } |
| 281 | 280 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)) | 312 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)) |
| 314 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE)); | 313 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE)); |
| 315 EXPECT_CALL(*mock_download_file, FullPath()) | 314 EXPECT_CALL(*mock_download_file, FullPath()) |
| 316 .WillRepeatedly(ReturnRefOfCopy(base::FilePath())); | 315 .WillRepeatedly(ReturnRefOfCopy(base::FilePath())); |
| 317 } | 316 } |
| 318 | 317 |
| 319 std::unique_ptr<MockRequestHandle> request_handle = | 318 std::unique_ptr<MockRequestHandle> request_handle = |
| 320 base::MakeUnique<NiceMock<MockRequestHandle>>(); | 319 base::MakeUnique<NiceMock<MockRequestHandle>>(); |
| 321 item->Start(std::move(download_file), std::move(request_handle), | 320 item->Start(std::move(download_file), std::move(request_handle), |
| 322 *create_info_); | 321 *create_info_); |
| 323 RunAllPendingInMessageLoops(); | 322 RunAllBlockingPoolTasksUntilIdle(); |
| 324 | 323 |
| 325 // So that we don't have a function writing to a stack variable | 324 // So that we don't have a function writing to a stack variable |
| 326 // lying around if the above failed. | 325 // lying around if the above failed. |
| 327 mock_delegate()->VerifyAndClearExpectations(); | 326 mock_delegate()->VerifyAndClearExpectations(); |
| 328 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) | 327 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) |
| 329 .WillRepeatedly(Return()); | 328 .WillRepeatedly(Return()); |
| 330 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) | 329 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) |
| 331 .WillRepeatedly(Return(false)); | 330 .WillRepeatedly(Return(false)); |
| 332 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) | 331 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) |
| 333 .WillRepeatedly(Return(true)); | 332 .WillRepeatedly(Return(true)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 345 DownloadTargetCallback callback; | 344 DownloadTargetCallback callback; |
| 346 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); | 345 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); |
| 347 base::FilePath target_path(kDummyTargetPath); | 346 base::FilePath target_path(kDummyTargetPath); |
| 348 base::FilePath intermediate_path(kDummyIntermediatePath); | 347 base::FilePath intermediate_path(kDummyIntermediatePath); |
| 349 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 348 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 350 .WillOnce(ScheduleRenameAndUniquifyCallback( | 349 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 351 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); | 350 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); |
| 352 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 351 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 353 danger_type, intermediate_path, | 352 danger_type, intermediate_path, |
| 354 DOWNLOAD_INTERRUPT_REASON_NONE); | 353 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 355 RunAllPendingInMessageLoops(); | 354 RunAllBlockingPoolTasksUntilIdle(); |
| 356 return download_file; | 355 return download_file; |
| 357 } | 356 } |
| 358 | 357 |
| 359 void DoDestinationComplete(DownloadItemImpl* item, | 358 void DoDestinationComplete(DownloadItemImpl* item, |
| 360 MockDownloadFile* download_file) { | 359 MockDownloadFile* download_file) { |
| 361 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) | 360 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) |
| 362 .WillOnce(Return(true)); | 361 .WillOnce(Return(true)); |
| 363 base::FilePath final_path(kDummyTargetPath); | 362 base::FilePath final_path(kDummyTargetPath); |
| 364 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) | 363 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) |
| 365 .WillOnce(ScheduleRenameAndAnnotateCallback( | 364 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 366 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); | 365 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); |
| 367 EXPECT_CALL(*download_file, FullPath()) | 366 EXPECT_CALL(*download_file, FullPath()) |
| 368 .WillRepeatedly(ReturnRefOfCopy(base::FilePath(kDummyTargetPath))); | 367 .WillRepeatedly(ReturnRefOfCopy(base::FilePath(kDummyTargetPath))); |
| 369 EXPECT_CALL(*download_file, Detach()); | 368 EXPECT_CALL(*download_file, Detach()); |
| 370 | 369 |
| 371 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 370 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 372 0, std::unique_ptr<crypto::SecureHash>()); | 371 0, std::unique_ptr<crypto::SecureHash>()); |
| 373 RunAllPendingInMessageLoops(); | 372 RunAllBlockingPoolTasksUntilIdle(); |
| 374 } | 373 } |
| 375 | 374 |
| 376 // Cleanup a download item (specifically get rid of the DownloadFile on it). | 375 // Cleanup a download item (specifically get rid of the DownloadFile on it). |
| 377 // The item must be in the expected state. | 376 // The item must be in the expected state. |
| 378 void CleanupItem(DownloadItemImpl* item, | 377 void CleanupItem(DownloadItemImpl* item, |
| 379 MockDownloadFile* download_file, | 378 MockDownloadFile* download_file, |
| 380 DownloadItem::DownloadState expected_state) { | 379 DownloadItem::DownloadState expected_state) { |
| 381 EXPECT_EQ(expected_state, item->GetState()); | 380 EXPECT_EQ(expected_state, item->GetState()); |
| 382 | 381 |
| 383 if (expected_state == DownloadItem::IN_PROGRESS) { | 382 if (expected_state == DownloadItem::IN_PROGRESS) { |
| 384 if (download_file) | 383 if (download_file) |
| 385 EXPECT_CALL(*download_file, Cancel()); | 384 EXPECT_CALL(*download_file, Cancel()); |
| 386 item->Cancel(true); | 385 item->Cancel(true); |
| 387 RunAllPendingInMessageLoops(); | 386 RunAllBlockingPoolTasksUntilIdle(); |
| 388 } | 387 } |
| 389 } | 388 } |
| 390 | 389 |
| 391 // Destroy a previously created download item. | 390 // Destroy a previously created download item. |
| 392 void DestroyDownloadItem(DownloadItem* item) { | 391 void DestroyDownloadItem(DownloadItem* item) { |
| 393 allocated_downloads_.erase(item); | 392 allocated_downloads_.erase(item); |
| 394 } | 393 } |
| 395 | 394 |
| 396 void RunAllPendingInMessageLoops() { base::RunLoop().RunUntilIdle(); } | |
| 397 | |
| 398 MockDelegate* mock_delegate() { | 395 MockDelegate* mock_delegate() { |
| 399 return &delegate_; | 396 return &delegate_; |
| 400 } | 397 } |
| 401 | 398 |
| 402 void OnDownloadFileAcquired(base::FilePath* return_path, | 399 void OnDownloadFileAcquired(base::FilePath* return_path, |
| 403 const base::FilePath& path) { | 400 const base::FilePath& path) { |
| 404 *return_path = path; | 401 *return_path = path; |
| 405 } | 402 } |
| 406 | 403 |
| 407 DownloadCreateInfo* create_info() { return create_info_.get(); } | 404 DownloadCreateInfo* create_info() { return create_info_.get(); } |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 577 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 581 .WillOnce(ScheduleRenameAndUniquifyCallback( | 578 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 582 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); | 579 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); |
| 583 | 580 |
| 584 // Currently, a notification would be generated if the danger type is anything | 581 // Currently, a notification would be generated if the danger type is anything |
| 585 // other than NOT_DANGEROUS. | 582 // other than NOT_DANGEROUS. |
| 586 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 583 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 587 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 584 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 588 DOWNLOAD_INTERRUPT_REASON_NONE); | 585 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 589 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); | 586 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); |
| 590 RunAllPendingInMessageLoops(); | 587 RunAllBlockingPoolTasksUntilIdle(); |
| 591 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); | 588 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 592 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 589 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 593 | 590 |
| 594 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); | 591 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 595 } | 592 } |
| 596 | 593 |
| 597 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 594 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
| 598 DownloadItemImpl* item = CreateDownloadItem(); | 595 DownloadItemImpl* item = CreateDownloadItem(); |
| 599 TestDownloadItemObserver observer(item); | 596 TestDownloadItemObserver observer(item); |
| 600 MockDownloadFile* mock_download_file(new MockDownloadFile); | 597 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 601 std::unique_ptr<DownloadFile> download_file(mock_download_file); | 598 std::unique_ptr<DownloadFile> download_file(mock_download_file); |
| 602 std::unique_ptr<DownloadRequestHandleInterface> request_handle( | 599 std::unique_ptr<DownloadRequestHandleInterface> request_handle( |
| 603 new NiceMock<MockRequestHandle>); | 600 new NiceMock<MockRequestHandle>); |
| 604 | 601 |
| 605 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)); | 602 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)); |
| 606 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); | 603 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); |
| 607 item->Start(std::move(download_file), std::move(request_handle), | 604 item->Start(std::move(download_file), std::move(request_handle), |
| 608 *create_info()); | 605 *create_info()); |
| 609 | 606 |
| 610 item->Pause(); | 607 item->Pause(); |
| 611 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 608 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 612 | 609 |
| 613 ASSERT_TRUE(item->IsPaused()); | 610 ASSERT_TRUE(item->IsPaused()); |
| 614 | 611 |
| 615 item->Resume(); | 612 item->Resume(); |
| 616 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 613 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 617 | 614 |
| 618 RunAllPendingInMessageLoops(); | 615 RunAllBlockingPoolTasksUntilIdle(); |
| 619 | 616 |
| 620 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); | 617 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 621 } | 618 } |
| 622 | 619 |
| 623 // Test that a download is resumed automatically after a continuable interrupt. | 620 // Test that a download is resumed automatically after a continuable interrupt. |
| 624 TEST_F(DownloadItemTest, AutomaticResumption_Continue) { | 621 TEST_F(DownloadItemTest, AutomaticResumption_Continue) { |
| 625 DownloadItemImpl* item = CreateDownloadItem(); | 622 DownloadItemImpl* item = CreateDownloadItem(); |
| 626 TestDownloadItemObserver observer(item); | 623 TestDownloadItemObserver observer(item); |
| 627 MockDownloadFile* download_file = | 624 MockDownloadFile* download_file = |
| 628 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 625 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 651 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 648 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 652 // Since the download is resumed automatically, the interrupt count doesn't | 649 // Since the download is resumed automatically, the interrupt count doesn't |
| 653 // increase. | 650 // increase. |
| 654 ASSERT_EQ(0, observer.interrupt_count()); | 651 ASSERT_EQ(0, observer.interrupt_count()); |
| 655 | 652 |
| 656 // Test expectations verify that ResumeInterruptedDownload() is called (by way | 653 // Test expectations verify that ResumeInterruptedDownload() is called (by way |
| 657 // of MockResumeInterruptedDownload) after the download is interrupted. But | 654 // of MockResumeInterruptedDownload) after the download is interrupted. But |
| 658 // the mock doesn't follow through with the resumption. | 655 // the mock doesn't follow through with the resumption. |
| 659 // ResumeInterruptedDownload() being called is sufficient for verifying that | 656 // ResumeInterruptedDownload() being called is sufficient for verifying that |
| 660 // the automatic resumption was triggered. | 657 // the automatic resumption was triggered. |
| 661 RunAllPendingInMessageLoops(); | 658 RunAllBlockingPoolTasksUntilIdle(); |
| 662 | 659 |
| 663 // The download item is currently in RESUMING_INTERNAL state, which maps to | 660 // The download item is currently in RESUMING_INTERNAL state, which maps to |
| 664 // IN_PROGRESS. | 661 // IN_PROGRESS. |
| 665 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); | 662 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); |
| 666 } | 663 } |
| 667 | 664 |
| 668 // Automatic resumption should restart and discard the intermediate file if the | 665 // Automatic resumption should restart and discard the intermediate file if the |
| 669 // interrupt reason requires it. | 666 // interrupt reason requires it. |
| 670 TEST_F(DownloadItemTest, AutomaticResumption_Restart) { | 667 TEST_F(DownloadItemTest, AutomaticResumption_Restart) { |
| 671 DownloadItemImpl* item = CreateDownloadItem(); | 668 DownloadItemImpl* item = CreateDownloadItem(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 685 _)); | 682 _)); |
| 686 | 683 |
| 687 item->DestinationObserverAsWeakPtr()->DestinationError( | 684 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 688 DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE, 1, | 685 DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE, 1, |
| 689 std::unique_ptr<crypto::SecureHash>()); | 686 std::unique_ptr<crypto::SecureHash>()); |
| 690 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 687 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 691 | 688 |
| 692 // Since the download is resumed automatically, the interrupt count doesn't | 689 // Since the download is resumed automatically, the interrupt count doesn't |
| 693 // increase. | 690 // increase. |
| 694 ASSERT_EQ(0, observer.interrupt_count()); | 691 ASSERT_EQ(0, observer.interrupt_count()); |
| 695 RunAllPendingInMessageLoops(); | 692 RunAllBlockingPoolTasksUntilIdle(); |
| 696 | 693 |
| 697 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); | 694 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); |
| 698 } | 695 } |
| 699 | 696 |
| 700 // Test that automatic resumption doesn't happen after an interrupt that | 697 // Test that automatic resumption doesn't happen after an interrupt that |
| 701 // requires user action to resolve. | 698 // requires user action to resolve. |
| 702 TEST_F(DownloadItemTest, AutomaticResumption_NeedsUserAction) { | 699 TEST_F(DownloadItemTest, AutomaticResumption_NeedsUserAction) { |
| 703 DownloadItemImpl* item = CreateDownloadItem(); | 700 DownloadItemImpl* item = CreateDownloadItem(); |
| 704 TestDownloadItemObserver observer(item); | 701 TestDownloadItemObserver observer(item); |
| 705 MockDownloadFile* download_file = | 702 MockDownloadFile* download_file = |
| 706 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 703 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 707 | 704 |
| 708 // Interrupt the download, using a restartable interrupt. | 705 // Interrupt the download, using a restartable interrupt. |
| 709 EXPECT_CALL(*download_file, Cancel()); | 706 EXPECT_CALL(*download_file, Cancel()); |
| 710 item->DestinationObserverAsWeakPtr()->DestinationError( | 707 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 711 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1, | 708 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1, |
| 712 std::unique_ptr<crypto::SecureHash>()); | 709 std::unique_ptr<crypto::SecureHash>()); |
| 713 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 710 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 714 // Should not try to auto-resume. | 711 // Should not try to auto-resume. |
| 715 ASSERT_EQ(1, observer.interrupt_count()); | 712 ASSERT_EQ(1, observer.interrupt_count()); |
| 716 ASSERT_EQ(0, observer.resume_count()); | 713 ASSERT_EQ(0, observer.resume_count()); |
| 717 RunAllPendingInMessageLoops(); | 714 RunAllBlockingPoolTasksUntilIdle(); |
| 718 | 715 |
| 719 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); | 716 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); |
| 720 } | 717 } |
| 721 | 718 |
| 722 // Test that a download is resumed automatically after a content length mismatch | 719 // Test that a download is resumed automatically after a content length mismatch |
| 723 // error. | 720 // error. |
| 724 TEST_F(DownloadItemTest, AutomaticResumption_ContentLengthMismatch) { | 721 TEST_F(DownloadItemTest, AutomaticResumption_ContentLengthMismatch) { |
| 725 DownloadItemImpl* item = CreateDownloadItem(); | 722 DownloadItemImpl* item = CreateDownloadItem(); |
| 726 TestDownloadItemObserver observer(item); | 723 TestDownloadItemObserver observer(item); |
| 727 MockDownloadFile* download_file = | 724 MockDownloadFile* download_file = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 746 | 743 |
| 747 item->DestinationObserverAsWeakPtr()->DestinationError( | 744 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 748 DOWNLOAD_INTERRUPT_REASON_SERVER_CONTENT_LENGTH_MISMATCH, 1, | 745 DOWNLOAD_INTERRUPT_REASON_SERVER_CONTENT_LENGTH_MISMATCH, 1, |
| 749 std::unique_ptr<crypto::SecureHash>()); | 746 std::unique_ptr<crypto::SecureHash>()); |
| 750 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 747 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 751 // Since the download is resumed automatically, the observer shouldn't notice | 748 // Since the download is resumed automatically, the observer shouldn't notice |
| 752 // the interruption. | 749 // the interruption. |
| 753 ASSERT_EQ(0, observer.interrupt_count()); | 750 ASSERT_EQ(0, observer.interrupt_count()); |
| 754 ASSERT_EQ(0, observer.resume_count()); | 751 ASSERT_EQ(0, observer.resume_count()); |
| 755 | 752 |
| 756 RunAllPendingInMessageLoops(); | 753 RunAllBlockingPoolTasksUntilIdle(); |
| 757 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); | 754 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); |
| 758 } | 755 } |
| 759 | 756 |
| 760 // Check we do correct cleanup for RESUME_MODE_INVALID interrupts. | 757 // Check we do correct cleanup for RESUME_MODE_INVALID interrupts. |
| 761 TEST_F(DownloadItemTest, UnresumableInterrupt) { | 758 TEST_F(DownloadItemTest, UnresumableInterrupt) { |
| 762 DownloadItemImpl* item = CreateDownloadItem(); | 759 DownloadItemImpl* item = CreateDownloadItem(); |
| 763 TestDownloadItemObserver observer(item); | 760 TestDownloadItemObserver observer(item); |
| 764 MockDownloadFile* download_file = | 761 MockDownloadFile* download_file = |
| 765 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 762 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 766 | 763 |
| 767 // Fail final rename with unresumable reason. | 764 // Fail final rename with unresumable reason. |
| 768 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 765 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
| 769 .WillOnce(Return(true)); | 766 .WillOnce(Return(true)); |
| 770 EXPECT_CALL(*download_file, | 767 EXPECT_CALL(*download_file, |
| 771 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 768 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 772 .WillOnce(ScheduleRenameAndAnnotateCallback( | 769 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 773 DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED, base::FilePath())); | 770 DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED, base::FilePath())); |
| 774 EXPECT_CALL(*download_file, Cancel()); | 771 EXPECT_CALL(*download_file, Cancel()); |
| 775 | 772 |
| 776 // Complete download to trigger final rename. | 773 // Complete download to trigger final rename. |
| 777 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 774 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 778 0, std::unique_ptr<crypto::SecureHash>()); | 775 0, std::unique_ptr<crypto::SecureHash>()); |
| 779 RunAllPendingInMessageLoops(); | 776 RunAllBlockingPoolTasksUntilIdle(); |
| 780 | 777 |
| 781 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 778 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 782 // Should not try to auto-resume. | 779 // Should not try to auto-resume. |
| 783 ASSERT_EQ(1, observer.interrupt_count()); | 780 ASSERT_EQ(1, observer.interrupt_count()); |
| 784 ASSERT_EQ(0, observer.resume_count()); | 781 ASSERT_EQ(0, observer.resume_count()); |
| 785 | 782 |
| 786 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); | 783 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); |
| 787 } | 784 } |
| 788 | 785 |
| 789 TEST_F(DownloadItemTest, AutomaticResumption_AttemptLimit) { | 786 TEST_F(DownloadItemTest, AutomaticResumption_AttemptLimit) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 815 mock_download_file_ref = mock_download_file.get(); | 812 mock_download_file_ref = mock_download_file.get(); |
| 816 mock_request_handle = base::MakeUnique<NiceMock<MockRequestHandle>>(); | 813 mock_request_handle = base::MakeUnique<NiceMock<MockRequestHandle>>(); |
| 817 | 814 |
| 818 ON_CALL(*mock_download_file_ref, FullPath()) | 815 ON_CALL(*mock_download_file_ref, FullPath()) |
| 819 .WillByDefault(ReturnRefOfCopy(base::FilePath())); | 816 .WillByDefault(ReturnRefOfCopy(base::FilePath())); |
| 820 | 817 |
| 821 // Copied key parts of DoIntermediateRename & CallDownloadItemStart | 818 // Copied key parts of DoIntermediateRename & CallDownloadItemStart |
| 822 // to allow for holding onto the request handle. | 819 // to allow for holding onto the request handle. |
| 823 item->Start(std::move(mock_download_file), std::move(mock_request_handle), | 820 item->Start(std::move(mock_download_file), std::move(mock_request_handle), |
| 824 *create_info()); | 821 *create_info()); |
| 825 RunAllPendingInMessageLoops(); | 822 RunAllBlockingPoolTasksUntilIdle(); |
| 826 | 823 |
| 827 base::FilePath target_path(kDummyTargetPath); | 824 base::FilePath target_path(kDummyTargetPath); |
| 828 base::FilePath intermediate_path(kDummyIntermediatePath); | 825 base::FilePath intermediate_path(kDummyIntermediatePath); |
| 829 | 826 |
| 830 // Target of RenameAndUniquify is always the intermediate path. | 827 // Target of RenameAndUniquify is always the intermediate path. |
| 831 ON_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)) | 828 ON_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)) |
| 832 .WillByDefault(ScheduleRenameAndUniquifyCallback( | 829 .WillByDefault(ScheduleRenameAndUniquifyCallback( |
| 833 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); | 830 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); |
| 834 | 831 |
| 835 // RenameAndUniquify is only called the first time. In all the subsequent | 832 // RenameAndUniquify is only called the first time. In all the subsequent |
| 836 // iterations, the intermediate file already has the correct name, hence no | 833 // iterations, the intermediate file already has the correct name, hence no |
| 837 // rename is necessary. | 834 // rename is necessary. |
| 838 EXPECT_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)).Times(i == 0); | 835 EXPECT_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)).Times(i == 0); |
| 839 | 836 |
| 840 ASSERT_FALSE(callback.is_null()); | 837 ASSERT_FALSE(callback.is_null()); |
| 841 base::ResetAndReturn(&callback).Run( | 838 base::ResetAndReturn(&callback).Run( |
| 842 target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 839 target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 843 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 840 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 844 DOWNLOAD_INTERRUPT_REASON_NONE); | 841 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 845 RunAllPendingInMessageLoops(); | 842 RunAllBlockingPoolTasksUntilIdle(); |
| 846 | 843 |
| 847 // Use a continuable interrupt. | 844 // Use a continuable interrupt. |
| 848 EXPECT_CALL(*mock_download_file_ref, Cancel()).Times(0); | 845 EXPECT_CALL(*mock_download_file_ref, Cancel()).Times(0); |
| 849 item->DestinationObserverAsWeakPtr()->DestinationError( | 846 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 850 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, | 847 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, |
| 851 std::unique_ptr<crypto::SecureHash>()); | 848 std::unique_ptr<crypto::SecureHash>()); |
| 852 | 849 |
| 853 RunAllPendingInMessageLoops(); | 850 RunAllBlockingPoolTasksUntilIdle(); |
| 854 ::testing::Mock::VerifyAndClearExpectations(mock_download_file_ref); | 851 ::testing::Mock::VerifyAndClearExpectations(mock_download_file_ref); |
| 855 } | 852 } |
| 856 | 853 |
| 857 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 854 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 858 EXPECT_EQ(1, observer.interrupt_count()); | 855 EXPECT_EQ(1, observer.interrupt_count()); |
| 859 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); | 856 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); |
| 860 } | 857 } |
| 861 | 858 |
| 862 // If the download attempts to resume and the resumption request fails, the | 859 // If the download attempts to resume and the resumption request fails, the |
| 863 // subsequent Start() call shouldn't update the origin state (URL redirect | 860 // subsequent Start() call shouldn't update the origin state (URL redirect |
| (...skipping 27 matching lines...) Expand all Loading... |
| 891 MockResumeInterruptedDownload( | 888 MockResumeInterruptedDownload( |
| 892 AllOf(Property(&DownloadUrlParameters::file_path, | 889 AllOf(Property(&DownloadUrlParameters::file_path, |
| 893 Property(&base::FilePath::value, | 890 Property(&base::FilePath::value, |
| 894 kDummyIntermediatePath)), | 891 kDummyIntermediatePath)), |
| 895 Property(&DownloadUrlParameters::offset, 1)), | 892 Property(&DownloadUrlParameters::offset, 1)), |
| 896 _)); | 893 _)); |
| 897 EXPECT_CALL(*download_file, Detach()); | 894 EXPECT_CALL(*download_file, Detach()); |
| 898 item->DestinationObserverAsWeakPtr()->DestinationError( | 895 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 899 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, | 896 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, |
| 900 std::unique_ptr<crypto::SecureHash>()); | 897 std::unique_ptr<crypto::SecureHash>()); |
| 901 RunAllPendingInMessageLoops(); | 898 RunAllBlockingPoolTasksUntilIdle(); |
| 902 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 899 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 903 | 900 |
| 904 // Now change the create info. The changes should not cause the DownloadItem | 901 // Now change the create info. The changes should not cause the DownloadItem |
| 905 // to be updated. | 902 // to be updated. |
| 906 constexpr int kSecondResponseCode = 418; | 903 constexpr int kSecondResponseCode = 418; |
| 907 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 904 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
| 908 const char kSecondETag[] = "123"; | 905 const char kSecondETag[] = "123"; |
| 909 const char kSecondLastModified[] = "Today"; | 906 const char kSecondLastModified[] = "Today"; |
| 910 const char kSecondURL[] = "http://example.com/another-download"; | 907 const char kSecondURL[] = "http://example.com/another-download"; |
| 911 const char kSecondMimeType[] = "text/html"; | 908 const char kSecondMimeType[] = "text/html"; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 923 // Calling Start() with a response indicating failure shouldn't cause a target | 920 // Calling Start() with a response indicating failure shouldn't cause a target |
| 924 // update, nor should it result in discarding the intermediate file. | 921 // update, nor should it result in discarding the intermediate file. |
| 925 DownloadTargetCallback target_callback; | 922 DownloadTargetCallback target_callback; |
| 926 download_file = CallDownloadItemStart(item, &target_callback); | 923 download_file = CallDownloadItemStart(item, &target_callback); |
| 927 ASSERT_FALSE(target_callback.is_null()); | 924 ASSERT_FALSE(target_callback.is_null()); |
| 928 target_callback.Run(base::FilePath(kDummyTargetPath), | 925 target_callback.Run(base::FilePath(kDummyTargetPath), |
| 929 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 926 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 930 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 927 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 931 base::FilePath(kDummyIntermediatePath), | 928 base::FilePath(kDummyIntermediatePath), |
| 932 DOWNLOAD_INTERRUPT_REASON_NONE); | 929 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 933 RunAllPendingInMessageLoops(); | 930 RunAllBlockingPoolTasksUntilIdle(); |
| 934 | 931 |
| 935 ASSERT_TRUE(item->GetResponseHeaders()); | 932 ASSERT_TRUE(item->GetResponseHeaders()); |
| 936 EXPECT_EQ(kFirstResponseCode, item->GetResponseHeaders()->response_code()); | 933 EXPECT_EQ(kFirstResponseCode, item->GetResponseHeaders()->response_code()); |
| 937 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | 934 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); |
| 938 EXPECT_EQ(kFirstETag, item->GetETag()); | 935 EXPECT_EQ(kFirstETag, item->GetETag()); |
| 939 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | 936 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); |
| 940 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | 937 EXPECT_EQ(kFirstURL, item->GetURL().spec()); |
| 941 EXPECT_EQ(kMimeType, item->GetMimeType()); | 938 EXPECT_EQ(kMimeType, item->GetMimeType()); |
| 942 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 939 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 943 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); | 940 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 962 create_info()->mime_type = kMimeType; | 959 create_info()->mime_type = kMimeType; |
| 963 | 960 |
| 964 DownloadItemImpl* item = CreateDownloadItem(); | 961 DownloadItemImpl* item = CreateDownloadItem(); |
| 965 MockDownloadFile* download_file = | 962 MockDownloadFile* download_file = |
| 966 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 963 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 967 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); | 964 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); |
| 968 EXPECT_CALL(*download_file, Detach()); | 965 EXPECT_CALL(*download_file, Detach()); |
| 969 item->DestinationObserverAsWeakPtr()->DestinationError( | 966 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 970 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 967 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
| 971 std::unique_ptr<crypto::SecureHash>()); | 968 std::unique_ptr<crypto::SecureHash>()); |
| 972 RunAllPendingInMessageLoops(); | 969 RunAllBlockingPoolTasksUntilIdle(); |
| 973 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 970 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 974 | 971 |
| 975 // Now change the create info. The changes should not cause the DownloadItem | 972 // Now change the create info. The changes should not cause the DownloadItem |
| 976 // to be updated. | 973 // to be updated. |
| 977 constexpr int kSecondResponseCode = 201; | 974 constexpr int kSecondResponseCode = 201; |
| 978 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 975 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
| 979 const char kSecondETag[] = "123"; | 976 const char kSecondETag[] = "123"; |
| 980 const char kSecondLastModified[] = "Today"; | 977 const char kSecondLastModified[] = "Today"; |
| 981 const char kSecondURL[] = "http://example.com/another-download"; | 978 const char kSecondURL[] = "http://example.com/another-download"; |
| 982 const char kSecondMimeType[] = "text/html"; | 979 const char kSecondMimeType[] = "text/html"; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1021 item->DestinationObserverAsWeakPtr()->DestinationUpdate(10, 100, | 1018 item->DestinationObserverAsWeakPtr()->DestinationUpdate(10, 100, |
| 1022 kReceivedSlice); | 1019 kReceivedSlice); |
| 1023 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); | 1020 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); |
| 1024 EXPECT_EQ(10, item->GetReceivedBytes()); | 1021 EXPECT_EQ(10, item->GetReceivedBytes()); |
| 1025 | 1022 |
| 1026 item->DestinationObserverAsWeakPtr()->DestinationError( | 1023 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1027 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 1024 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
| 1028 std::unique_ptr<crypto::SecureHash>()); | 1025 std::unique_ptr<crypto::SecureHash>()); |
| 1029 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); | 1026 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); |
| 1030 | 1027 |
| 1031 RunAllPendingInMessageLoops(); | 1028 RunAllBlockingPoolTasksUntilIdle(); |
| 1032 | 1029 |
| 1033 // Change the strong validator and resume the download, the received slices | 1030 // Change the strong validator and resume the download, the received slices |
| 1034 // should be cleared. | 1031 // should be cleared. |
| 1035 create_info()->etag = kSecondETag; | 1032 create_info()->etag = kSecondETag; |
| 1036 DownloadTargetCallback target_callback; | 1033 DownloadTargetCallback target_callback; |
| 1037 download_file = CallDownloadItemStart(item, &target_callback); | 1034 download_file = CallDownloadItemStart(item, &target_callback); |
| 1038 EXPECT_TRUE(item->GetReceivedSlices().empty()); | 1035 EXPECT_TRUE(item->GetReceivedSlices().empty()); |
| 1039 EXPECT_EQ(0, item->GetReceivedBytes()); | 1036 EXPECT_EQ(0, item->GetReceivedBytes()); |
| 1040 | 1037 |
| 1041 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); | 1038 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1065 .Times(1); | 1062 .Times(1); |
| 1066 item->DestinationObserverAsWeakPtr()->DestinationError( | 1063 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1067 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, | 1064 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, |
| 1068 std::unique_ptr<crypto::SecureHash>()); | 1065 std::unique_ptr<crypto::SecureHash>()); |
| 1069 | 1066 |
| 1070 // Test expectations verify that ResumeInterruptedDownload() is called (by way | 1067 // Test expectations verify that ResumeInterruptedDownload() is called (by way |
| 1071 // of MockResumeInterruptedDownload) after the download is interrupted. But | 1068 // of MockResumeInterruptedDownload) after the download is interrupted. But |
| 1072 // the mock doesn't follow through with the resumption. | 1069 // the mock doesn't follow through with the resumption. |
| 1073 // ResumeInterruptedDownload() being called is sufficient for verifying that | 1070 // ResumeInterruptedDownload() being called is sufficient for verifying that |
| 1074 // the resumption was triggered. | 1071 // the resumption was triggered. |
| 1075 RunAllPendingInMessageLoops(); | 1072 RunAllBlockingPoolTasksUntilIdle(); |
| 1076 | 1073 |
| 1077 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. | 1074 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. |
| 1078 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); | 1075 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); |
| 1079 } | 1076 } |
| 1080 | 1077 |
| 1081 TEST_F(DownloadItemTest, DisplayName) { | 1078 TEST_F(DownloadItemTest, DisplayName) { |
| 1082 DownloadItemImpl* item = CreateDownloadItem(); | 1079 DownloadItemImpl* item = CreateDownloadItem(); |
| 1083 DownloadTargetCallback callback; | 1080 DownloadTargetCallback callback; |
| 1084 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); | 1081 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); |
| 1085 base::FilePath target_path( | 1082 base::FilePath target_path( |
| 1086 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); | 1083 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); |
| 1087 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 1084 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 1088 EXPECT_EQ(FILE_PATH_LITERAL(""), | 1085 EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 1089 item->GetFileNameToReportUser().value()); | 1086 item->GetFileNameToReportUser().value()); |
| 1090 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) | 1087 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) |
| 1091 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1088 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1092 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); | 1089 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); |
| 1093 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1090 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1094 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1091 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1095 DOWNLOAD_INTERRUPT_REASON_NONE); | 1092 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1096 RunAllPendingInMessageLoops(); | 1093 RunAllBlockingPoolTasksUntilIdle(); |
| 1097 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 1094 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
| 1098 item->GetFileNameToReportUser().value()); | 1095 item->GetFileNameToReportUser().value()); |
| 1099 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name"))); | 1096 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name"))); |
| 1100 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 1097 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
| 1101 item->GetFileNameToReportUser().value()); | 1098 item->GetFileNameToReportUser().value()); |
| 1102 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); | 1099 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 1103 } | 1100 } |
| 1104 | 1101 |
| 1105 // Test to make sure that Start method calls DF initialize properly. | 1102 // Test to make sure that Start method calls DF initialize properly. |
| 1106 TEST_F(DownloadItemTest, Start) { | 1103 TEST_F(DownloadItemTest, Start) { |
| 1107 MockDownloadFile* mock_download_file(new MockDownloadFile); | 1104 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 1108 std::unique_ptr<DownloadFile> download_file(mock_download_file); | 1105 std::unique_ptr<DownloadFile> download_file(mock_download_file); |
| 1109 DownloadItemImpl* item = CreateDownloadItem(); | 1106 DownloadItemImpl* item = CreateDownloadItem(); |
| 1110 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)); | 1107 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)); |
| 1111 std::unique_ptr<DownloadRequestHandleInterface> request_handle( | 1108 std::unique_ptr<DownloadRequestHandleInterface> request_handle( |
| 1112 new NiceMock<MockRequestHandle>); | 1109 new NiceMock<MockRequestHandle>); |
| 1113 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | 1110 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
| 1114 item->Start(std::move(download_file), std::move(request_handle), | 1111 item->Start(std::move(download_file), std::move(request_handle), |
| 1115 *create_info()); | 1112 *create_info()); |
| 1116 RunAllPendingInMessageLoops(); | 1113 RunAllBlockingPoolTasksUntilIdle(); |
| 1117 | 1114 |
| 1118 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); | 1115 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 1119 } | 1116 } |
| 1120 | 1117 |
| 1121 // Download file and the request should be cancelled as a result of download | 1118 // Download file and the request should be cancelled as a result of download |
| 1122 // file initialization failing. | 1119 // file initialization failing. |
| 1123 TEST_F(DownloadItemTest, InitDownloadFileFails) { | 1120 TEST_F(DownloadItemTest, InitDownloadFileFails) { |
| 1124 DownloadItemImpl* item = CreateDownloadItem(); | 1121 DownloadItemImpl* item = CreateDownloadItem(); |
| 1125 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>(); | 1122 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>(); |
| 1126 std::unique_ptr<MockRequestHandle> request_handle = | 1123 std::unique_ptr<MockRequestHandle> request_handle = |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1139 ScheduleClosure(start_download_loop.QuitClosure()))); | 1136 ScheduleClosure(start_download_loop.QuitClosure()))); |
| 1140 | 1137 |
| 1141 item->Start(std::move(file), std::move(request_handle), *create_info()); | 1138 item->Start(std::move(file), std::move(request_handle), *create_info()); |
| 1142 start_download_loop.Run(); | 1139 start_download_loop.Run(); |
| 1143 | 1140 |
| 1144 download_target_callback.Run(base::FilePath(kDummyTargetPath), | 1141 download_target_callback.Run(base::FilePath(kDummyTargetPath), |
| 1145 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1142 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1146 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1143 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1147 base::FilePath(kDummyIntermediatePath), | 1144 base::FilePath(kDummyIntermediatePath), |
| 1148 DOWNLOAD_INTERRUPT_REASON_NONE); | 1145 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1149 RunAllPendingInMessageLoops(); | 1146 RunAllBlockingPoolTasksUntilIdle(); |
| 1150 | 1147 |
| 1151 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1148 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1152 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 1149 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 1153 item->GetLastReason()); | 1150 item->GetLastReason()); |
| 1154 EXPECT_FALSE(item->GetTargetFilePath().empty()); | 1151 EXPECT_FALSE(item->GetTargetFilePath().empty()); |
| 1155 EXPECT_TRUE(item->GetFullPath().empty()); | 1152 EXPECT_TRUE(item->GetFullPath().empty()); |
| 1156 } | 1153 } |
| 1157 | 1154 |
| 1158 // Handling of downloads initiated via a failed request. In this case, Start() | 1155 // Handling of downloads initiated via a failed request. In this case, Start() |
| 1159 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason. | 1156 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason. |
| 1160 TEST_F(DownloadItemTest, StartFailedDownload) { | 1157 TEST_F(DownloadItemTest, StartFailedDownload) { |
| 1161 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; | 1158 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; |
| 1162 DownloadItemImpl* item = CreateDownloadItem(); | 1159 DownloadItemImpl* item = CreateDownloadItem(); |
| 1163 | 1160 |
| 1164 // DownloadFile and DownloadRequestHandleInterface objects aren't created for | 1161 // DownloadFile and DownloadRequestHandleInterface objects aren't created for |
| 1165 // failed downloads. | 1162 // failed downloads. |
| 1166 std::unique_ptr<DownloadFile> null_download_file; | 1163 std::unique_ptr<DownloadFile> null_download_file; |
| 1167 std::unique_ptr<DownloadRequestHandleInterface> null_request_handle; | 1164 std::unique_ptr<DownloadRequestHandleInterface> null_request_handle; |
| 1168 DownloadTargetCallback download_target_callback; | 1165 DownloadTargetCallback download_target_callback; |
| 1169 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 1166 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
| 1170 .WillOnce(SaveArg<1>(&download_target_callback)); | 1167 .WillOnce(SaveArg<1>(&download_target_callback)); |
| 1171 item->Start(std::move(null_download_file), std::move(null_request_handle), | 1168 item->Start(std::move(null_download_file), std::move(null_request_handle), |
| 1172 *create_info()); | 1169 *create_info()); |
| 1173 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1170 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1174 RunAllPendingInMessageLoops(); | 1171 RunAllBlockingPoolTasksUntilIdle(); |
| 1175 | 1172 |
| 1176 // The DownloadItemImpl should attempt to determine a target path even if the | 1173 // The DownloadItemImpl should attempt to determine a target path even if the |
| 1177 // download was interrupted. | 1174 // download was interrupted. |
| 1178 ASSERT_FALSE(download_target_callback.is_null()); | 1175 ASSERT_FALSE(download_target_callback.is_null()); |
| 1179 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1176 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1180 base::FilePath target_path(FILE_PATH_LITERAL("foo")); | 1177 base::FilePath target_path(FILE_PATH_LITERAL("foo")); |
| 1181 download_target_callback.Run(target_path, | 1178 download_target_callback.Run(target_path, |
| 1182 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1179 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1183 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path, | 1180 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path, |
| 1184 DOWNLOAD_INTERRUPT_REASON_NONE); | 1181 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1185 RunAllPendingInMessageLoops(); | 1182 RunAllBlockingPoolTasksUntilIdle(); |
| 1186 | 1183 |
| 1187 EXPECT_EQ(target_path, item->GetTargetFilePath()); | 1184 EXPECT_EQ(target_path, item->GetTargetFilePath()); |
| 1188 CleanupItem(item, NULL, DownloadItem::INTERRUPTED); | 1185 CleanupItem(item, NULL, DownloadItem::INTERRUPTED); |
| 1189 } | 1186 } |
| 1190 | 1187 |
| 1191 // Test that the delegate is invoked after the download file is renamed. | 1188 // Test that the delegate is invoked after the download file is renamed. |
| 1192 TEST_F(DownloadItemTest, CallbackAfterRename) { | 1189 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 1193 DownloadItemImpl* item = CreateDownloadItem(); | 1190 DownloadItemImpl* item = CreateDownloadItem(); |
| 1194 DownloadTargetCallback callback; | 1191 DownloadTargetCallback callback; |
| 1195 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); | 1192 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); |
| 1196 base::FilePath final_path( | 1193 base::FilePath final_path( |
| 1197 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); | 1194 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); |
| 1198 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 1195 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 1199 base::FilePath new_intermediate_path( | 1196 base::FilePath new_intermediate_path( |
| 1200 final_path.InsertBeforeExtensionASCII("y")); | 1197 final_path.InsertBeforeExtensionASCII("y")); |
| 1201 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1198 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1202 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1199 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1203 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); | 1200 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); |
| 1204 | 1201 |
| 1205 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1202 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1206 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1203 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1207 DOWNLOAD_INTERRUPT_REASON_NONE); | 1204 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1208 RunAllPendingInMessageLoops(); | 1205 RunAllBlockingPoolTasksUntilIdle(); |
| 1209 // All the callbacks should have happened by now. | 1206 // All the callbacks should have happened by now. |
| 1210 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1207 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1211 mock_delegate()->VerifyAndClearExpectations(); | 1208 mock_delegate()->VerifyAndClearExpectations(); |
| 1212 | 1209 |
| 1213 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1210 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
| 1214 .WillOnce(Return(true)); | 1211 .WillOnce(Return(true)); |
| 1215 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _)) | 1212 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _)) |
| 1216 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1213 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1217 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); | 1214 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); |
| 1218 EXPECT_CALL(*download_file, FullPath()) | 1215 EXPECT_CALL(*download_file, FullPath()) |
| 1219 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1216 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1220 EXPECT_CALL(*download_file, Detach()); | 1217 EXPECT_CALL(*download_file, Detach()); |
| 1221 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 1218 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 1222 0, std::unique_ptr<crypto::SecureHash>()); | 1219 0, std::unique_ptr<crypto::SecureHash>()); |
| 1223 RunAllPendingInMessageLoops(); | 1220 RunAllBlockingPoolTasksUntilIdle(); |
| 1224 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1221 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1225 mock_delegate()->VerifyAndClearExpectations(); | 1222 mock_delegate()->VerifyAndClearExpectations(); |
| 1226 } | 1223 } |
| 1227 | 1224 |
| 1228 // Test that the delegate is invoked after the download file is renamed and the | 1225 // Test that the delegate is invoked after the download file is renamed and the |
| 1229 // download item is in an interrupted state. | 1226 // download item is in an interrupted state. |
| 1230 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { | 1227 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { |
| 1231 DownloadItemImpl* item = CreateDownloadItem(); | 1228 DownloadItemImpl* item = CreateDownloadItem(); |
| 1232 DownloadTargetCallback callback; | 1229 DownloadTargetCallback callback; |
| 1233 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); | 1230 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); |
| 1234 base::FilePath final_path( | 1231 base::FilePath final_path( |
| 1235 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); | 1232 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); |
| 1236 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 1233 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 1237 base::FilePath new_intermediate_path( | 1234 base::FilePath new_intermediate_path( |
| 1238 final_path.InsertBeforeExtensionASCII("y")); | 1235 final_path.InsertBeforeExtensionASCII("y")); |
| 1239 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1236 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1240 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1237 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1241 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); | 1238 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); |
| 1242 EXPECT_CALL(*download_file, Cancel()) | 1239 EXPECT_CALL(*download_file, Cancel()) |
| 1243 .Times(1); | 1240 .Times(1); |
| 1244 | 1241 |
| 1245 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1242 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1246 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1243 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1247 DOWNLOAD_INTERRUPT_REASON_NONE); | 1244 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1248 RunAllPendingInMessageLoops(); | 1245 RunAllBlockingPoolTasksUntilIdle(); |
| 1249 // All the callbacks should have happened by now. | 1246 // All the callbacks should have happened by now. |
| 1250 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1247 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1251 mock_delegate()->VerifyAndClearExpectations(); | 1248 mock_delegate()->VerifyAndClearExpectations(); |
| 1252 } | 1249 } |
| 1253 | 1250 |
| 1254 TEST_F(DownloadItemTest, Interrupted) { | 1251 TEST_F(DownloadItemTest, Interrupted) { |
| 1255 DownloadItemImpl* item = CreateDownloadItem(); | 1252 DownloadItemImpl* item = CreateDownloadItem(); |
| 1256 MockDownloadFile* download_file = | 1253 MockDownloadFile* download_file = |
| 1257 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1254 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 1258 | 1255 |
| 1259 const DownloadInterruptReason reason( | 1256 const DownloadInterruptReason reason( |
| 1260 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 1257 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 1261 | 1258 |
| 1262 // Confirm interrupt sets state properly. | 1259 // Confirm interrupt sets state properly. |
| 1263 EXPECT_CALL(*download_file, Cancel()); | 1260 EXPECT_CALL(*download_file, Cancel()); |
| 1264 item->DestinationObserverAsWeakPtr()->DestinationError( | 1261 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1265 reason, 0, std::unique_ptr<crypto::SecureHash>()); | 1262 reason, 0, std::unique_ptr<crypto::SecureHash>()); |
| 1266 RunAllPendingInMessageLoops(); | 1263 RunAllBlockingPoolTasksUntilIdle(); |
| 1267 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1264 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1268 EXPECT_EQ(reason, item->GetLastReason()); | 1265 EXPECT_EQ(reason, item->GetLastReason()); |
| 1269 | 1266 |
| 1270 // Cancel should kill it. | 1267 // Cancel should kill it. |
| 1271 item->Cancel(true); | 1268 item->Cancel(true); |
| 1272 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); | 1269 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
| 1273 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); | 1270 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); |
| 1274 } | 1271 } |
| 1275 | 1272 |
| 1276 // Destination errors that occur before the intermediate rename shouldn't cause | 1273 // Destination errors that occur before the intermediate rename shouldn't cause |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1291 final_path.InsertBeforeExtensionASCII("y")); | 1288 final_path.InsertBeforeExtensionASCII("y")); |
| 1292 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1289 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1293 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1290 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1294 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); | 1291 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); |
| 1295 EXPECT_CALL(*download_file, Cancel()) | 1292 EXPECT_CALL(*download_file, Cancel()) |
| 1296 .Times(1); | 1293 .Times(1); |
| 1297 | 1294 |
| 1298 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1295 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1299 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1296 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1300 DOWNLOAD_INTERRUPT_REASON_NONE); | 1297 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1301 RunAllPendingInMessageLoops(); | 1298 RunAllBlockingPoolTasksUntilIdle(); |
| 1302 // All the callbacks should have happened by now. | 1299 // All the callbacks should have happened by now. |
| 1303 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1300 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1304 mock_delegate()->VerifyAndClearExpectations(); | 1301 mock_delegate()->VerifyAndClearExpectations(); |
| 1305 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1302 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1306 EXPECT_TRUE(item->GetFullPath().empty()); | 1303 EXPECT_TRUE(item->GetFullPath().empty()); |
| 1307 EXPECT_EQ(final_path, item->GetTargetFilePath()); | 1304 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
| 1308 } | 1305 } |
| 1309 | 1306 |
| 1310 // As above. But if the download can be resumed by continuing, then the | 1307 // As above. But if the download can be resumed by continuing, then the |
| 1311 // intermediate path should be retained when the download is interrupted after | 1308 // intermediate path should be retained when the download is interrupted after |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1330 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1327 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1331 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1328 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1332 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); | 1329 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); |
| 1333 EXPECT_CALL(*download_file, FullPath()) | 1330 EXPECT_CALL(*download_file, FullPath()) |
| 1334 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path))); | 1331 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path))); |
| 1335 EXPECT_CALL(*download_file, Detach()); | 1332 EXPECT_CALL(*download_file, Detach()); |
| 1336 | 1333 |
| 1337 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1334 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1338 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1335 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1339 DOWNLOAD_INTERRUPT_REASON_NONE); | 1336 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1340 RunAllPendingInMessageLoops(); | 1337 RunAllBlockingPoolTasksUntilIdle(); |
| 1341 // All the callbacks should have happened by now. | 1338 // All the callbacks should have happened by now. |
| 1342 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1339 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1343 mock_delegate()->VerifyAndClearExpectations(); | 1340 mock_delegate()->VerifyAndClearExpectations(); |
| 1344 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1341 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1345 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 1342 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 1346 EXPECT_EQ(final_path, item->GetTargetFilePath()); | 1343 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
| 1347 } | 1344 } |
| 1348 | 1345 |
| 1349 // As above. If the intermediate rename fails, then the interrupt reason should | 1346 // As above. If the intermediate rename fails, then the interrupt reason should |
| 1350 // be set to the file error and the intermediate path should be empty. | 1347 // be set to the file error and the intermediate path should be empty. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1364 final_path.InsertBeforeExtensionASCII("y")); | 1361 final_path.InsertBeforeExtensionASCII("y")); |
| 1365 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1362 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1366 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1363 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1367 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); | 1364 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); |
| 1368 EXPECT_CALL(*download_file, Cancel()) | 1365 EXPECT_CALL(*download_file, Cancel()) |
| 1369 .Times(1); | 1366 .Times(1); |
| 1370 | 1367 |
| 1371 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1368 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1372 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1369 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1373 DOWNLOAD_INTERRUPT_REASON_NONE); | 1370 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1374 RunAllPendingInMessageLoops(); | 1371 RunAllBlockingPoolTasksUntilIdle(); |
| 1375 // All the callbacks should have happened by now. | 1372 // All the callbacks should have happened by now. |
| 1376 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1373 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1377 mock_delegate()->VerifyAndClearExpectations(); | 1374 mock_delegate()->VerifyAndClearExpectations(); |
| 1378 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1375 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1379 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason()); | 1376 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason()); |
| 1380 EXPECT_TRUE(item->GetFullPath().empty()); | 1377 EXPECT_TRUE(item->GetFullPath().empty()); |
| 1381 EXPECT_EQ(final_path, item->GetTargetFilePath()); | 1378 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
| 1382 } | 1379 } |
| 1383 | 1380 |
| 1384 TEST_F(DownloadItemTest, Canceled) { | 1381 TEST_F(DownloadItemTest, Canceled) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1587 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) | 1584 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) |
| 1588 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1585 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1589 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1586 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1590 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1587 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
| 1591 .WillOnce(Return(true)); | 1588 .WillOnce(Return(true)); |
| 1592 EXPECT_CALL(*download_file, FullPath()) | 1589 EXPECT_CALL(*download_file, FullPath()) |
| 1593 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1590 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1594 EXPECT_CALL(*download_file, Detach()); | 1591 EXPECT_CALL(*download_file, Detach()); |
| 1595 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 1592 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 1596 0, std::unique_ptr<crypto::SecureHash>()); | 1593 0, std::unique_ptr<crypto::SecureHash>()); |
| 1597 RunAllPendingInMessageLoops(); | 1594 RunAllBlockingPoolTasksUntilIdle(); |
| 1598 | 1595 |
| 1599 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1596 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1600 EXPECT_TRUE(item->CanShowInFolder()); | 1597 EXPECT_TRUE(item->CanShowInFolder()); |
| 1601 EXPECT_TRUE(item->CanOpenDownload()); | 1598 EXPECT_TRUE(item->CanOpenDownload()); |
| 1602 } | 1599 } |
| 1603 | 1600 |
| 1604 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { | 1601 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { |
| 1605 // A download created with a non-empty FilePath is considered a temporary | 1602 // A download created with a non-empty FilePath is considered a temporary |
| 1606 // download. | 1603 // download. |
| 1607 create_info()->save_info->file_path = base::FilePath(kDummyTargetPath); | 1604 create_info()->save_info->file_path = base::FilePath(kDummyTargetPath); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1620 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1617 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
| 1621 .WillOnce(Return(true)); | 1618 .WillOnce(Return(true)); |
| 1622 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) | 1619 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) |
| 1623 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1620 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1624 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1621 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1625 EXPECT_CALL(*download_file, FullPath()) | 1622 EXPECT_CALL(*download_file, FullPath()) |
| 1626 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1623 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1627 EXPECT_CALL(*download_file, Detach()); | 1624 EXPECT_CALL(*download_file, Detach()); |
| 1628 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 1625 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 1629 0, std::unique_ptr<crypto::SecureHash>()); | 1626 0, std::unique_ptr<crypto::SecureHash>()); |
| 1630 RunAllPendingInMessageLoops(); | 1627 RunAllBlockingPoolTasksUntilIdle(); |
| 1631 | 1628 |
| 1632 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1629 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1633 EXPECT_FALSE(item->CanShowInFolder()); | 1630 EXPECT_FALSE(item->CanShowInFolder()); |
| 1634 EXPECT_FALSE(item->CanOpenDownload()); | 1631 EXPECT_FALSE(item->CanOpenDownload()); |
| 1635 } | 1632 } |
| 1636 | 1633 |
| 1637 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { | 1634 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { |
| 1638 DownloadItemImpl* item = CreateDownloadItem(); | 1635 DownloadItemImpl* item = CreateDownloadItem(); |
| 1639 MockDownloadFile* download_file = | 1636 MockDownloadFile* download_file = |
| 1640 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1637 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 1641 | 1638 |
| 1642 EXPECT_CALL(*download_file, Cancel()); | 1639 EXPECT_CALL(*download_file, Cancel()); |
| 1643 item->DestinationObserverAsWeakPtr()->DestinationError( | 1640 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1644 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, | 1641 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, |
| 1645 std::unique_ptr<crypto::SecureHash>()); | 1642 std::unique_ptr<crypto::SecureHash>()); |
| 1646 RunAllPendingInMessageLoops(); | 1643 RunAllBlockingPoolTasksUntilIdle(); |
| 1647 | 1644 |
| 1648 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1645 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1649 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 1646 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
| 1650 EXPECT_FALSE(item->CanShowInFolder()); | 1647 EXPECT_FALSE(item->CanShowInFolder()); |
| 1651 EXPECT_TRUE(item->CanOpenDownload()); | 1648 EXPECT_TRUE(item->CanOpenDownload()); |
| 1652 } | 1649 } |
| 1653 | 1650 |
| 1654 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { | 1651 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { |
| 1655 DownloadItemImpl* item = CreateDownloadItem(); | 1652 DownloadItemImpl* item = CreateDownloadItem(); |
| 1656 MockDownloadFile* download_file = | 1653 MockDownloadFile* download_file = |
| 1657 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1654 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 1658 | 1655 |
| 1659 EXPECT_CALL(*download_file, Cancel()); | 1656 EXPECT_CALL(*download_file, Cancel()); |
| 1660 item->Cancel(true); | 1657 item->Cancel(true); |
| 1661 RunAllPendingInMessageLoops(); | 1658 RunAllBlockingPoolTasksUntilIdle(); |
| 1662 | 1659 |
| 1663 ASSERT_EQ(DownloadItem::CANCELLED, item->GetState()); | 1660 ASSERT_EQ(DownloadItem::CANCELLED, item->GetState()); |
| 1664 EXPECT_FALSE(item->CanShowInFolder()); | 1661 EXPECT_FALSE(item->CanShowInFolder()); |
| 1665 EXPECT_FALSE(item->CanOpenDownload()); | 1662 EXPECT_FALSE(item->CanOpenDownload()); |
| 1666 } | 1663 } |
| 1667 | 1664 |
| 1668 // Test various aspects of the delegate completion blocker. | 1665 // Test various aspects of the delegate completion blocker. |
| 1669 | 1666 |
| 1670 // Just allowing completion. | 1667 // Just allowing completion. |
| 1671 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { | 1668 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1686 // Make sure the download can complete. | 1683 // Make sure the download can complete. |
| 1687 EXPECT_CALL(*download_file, | 1684 EXPECT_CALL(*download_file, |
| 1688 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1685 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1689 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1686 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1690 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1687 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1691 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1688 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 1692 .WillOnce(Return(true)); | 1689 .WillOnce(Return(true)); |
| 1693 EXPECT_CALL(*download_file, FullPath()) | 1690 EXPECT_CALL(*download_file, FullPath()) |
| 1694 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1691 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1695 EXPECT_CALL(*download_file, Detach()); | 1692 EXPECT_CALL(*download_file, Detach()); |
| 1696 RunAllPendingInMessageLoops(); | 1693 RunAllBlockingPoolTasksUntilIdle(); |
| 1697 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1694 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1698 } | 1695 } |
| 1699 | 1696 |
| 1700 // Just delaying completion. | 1697 // Just delaying completion. |
| 1701 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { | 1698 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { |
| 1702 // Test to confirm that if we have a callback that returns true, | 1699 // Test to confirm that if we have a callback that returns true, |
| 1703 // we complete immediately. | 1700 // we complete immediately. |
| 1704 DownloadItemImpl* item = CreateDownloadItem(); | 1701 DownloadItemImpl* item = CreateDownloadItem(); |
| 1705 MockDownloadFile* download_file = | 1702 MockDownloadFile* download_file = |
| 1706 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1703 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1726 // Make sure the download can complete. | 1723 // Make sure the download can complete. |
| 1727 EXPECT_CALL(*download_file, | 1724 EXPECT_CALL(*download_file, |
| 1728 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1725 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1729 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1726 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1730 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1727 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1731 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1728 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 1732 .WillOnce(Return(true)); | 1729 .WillOnce(Return(true)); |
| 1733 EXPECT_CALL(*download_file, FullPath()) | 1730 EXPECT_CALL(*download_file, FullPath()) |
| 1734 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1731 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1735 EXPECT_CALL(*download_file, Detach()); | 1732 EXPECT_CALL(*download_file, Detach()); |
| 1736 RunAllPendingInMessageLoops(); | 1733 RunAllBlockingPoolTasksUntilIdle(); |
| 1737 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1734 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1738 } | 1735 } |
| 1739 | 1736 |
| 1740 // Delay and set danger. | 1737 // Delay and set danger. |
| 1741 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { | 1738 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { |
| 1742 // Test to confirm that if we have a callback that returns true, | 1739 // Test to confirm that if we have a callback that returns true, |
| 1743 // we complete immediately. | 1740 // we complete immediately. |
| 1744 DownloadItemImpl* item = CreateDownloadItem(); | 1741 DownloadItemImpl* item = CreateDownloadItem(); |
| 1745 MockDownloadFile* download_file = | 1742 MockDownloadFile* download_file = |
| 1746 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1743 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1769 // Make sure the download doesn't complete until we've validated it. | 1766 // Make sure the download doesn't complete until we've validated it. |
| 1770 EXPECT_CALL(*download_file, | 1767 EXPECT_CALL(*download_file, |
| 1771 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1768 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1772 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1769 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1773 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1770 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1774 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1771 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 1775 .WillOnce(Return(true)); | 1772 .WillOnce(Return(true)); |
| 1776 EXPECT_CALL(*download_file, FullPath()) | 1773 EXPECT_CALL(*download_file, FullPath()) |
| 1777 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1774 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1778 EXPECT_CALL(*download_file, Detach()); | 1775 EXPECT_CALL(*download_file, Detach()); |
| 1779 RunAllPendingInMessageLoops(); | 1776 RunAllBlockingPoolTasksUntilIdle(); |
| 1780 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1777 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1781 EXPECT_TRUE(item->IsDangerous()); | 1778 EXPECT_TRUE(item->IsDangerous()); |
| 1782 | 1779 |
| 1783 item->ValidateDangerousDownload(); | 1780 item->ValidateDangerousDownload(); |
| 1784 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); | 1781 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); |
| 1785 RunAllPendingInMessageLoops(); | 1782 RunAllBlockingPoolTasksUntilIdle(); |
| 1786 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1783 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1787 } | 1784 } |
| 1788 | 1785 |
| 1789 // Just delaying completion twice. | 1786 // Just delaying completion twice. |
| 1790 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { | 1787 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { |
| 1791 // Test to confirm that if we have a callback that returns true, | 1788 // Test to confirm that if we have a callback that returns true, |
| 1792 // we complete immediately. | 1789 // we complete immediately. |
| 1793 DownloadItemImpl* item = CreateDownloadItem(); | 1790 DownloadItemImpl* item = CreateDownloadItem(); |
| 1794 MockDownloadFile* download_file = | 1791 MockDownloadFile* download_file = |
| 1795 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1792 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1822 // Make sure the download can complete. | 1819 // Make sure the download can complete. |
| 1823 EXPECT_CALL(*download_file, | 1820 EXPECT_CALL(*download_file, |
| 1824 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1821 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1825 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1822 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1826 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1823 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1827 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1824 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 1828 .WillOnce(Return(true)); | 1825 .WillOnce(Return(true)); |
| 1829 EXPECT_CALL(*download_file, FullPath()) | 1826 EXPECT_CALL(*download_file, FullPath()) |
| 1830 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1827 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1831 EXPECT_CALL(*download_file, Detach()); | 1828 EXPECT_CALL(*download_file, Detach()); |
| 1832 RunAllPendingInMessageLoops(); | 1829 RunAllBlockingPoolTasksUntilIdle(); |
| 1833 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1830 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1834 } | 1831 } |
| 1835 | 1832 |
| 1836 TEST_F(DownloadItemTest, StealDangerousDownloadAndDiscard) { | 1833 TEST_F(DownloadItemTest, StealDangerousDownloadAndDiscard) { |
| 1837 DownloadItemImpl* item = CreateDownloadItem(); | 1834 DownloadItemImpl* item = CreateDownloadItem(); |
| 1838 MockDownloadFile* download_file = | 1835 MockDownloadFile* download_file = |
| 1839 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1836 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1840 ASSERT_TRUE(item->IsDangerous()); | 1837 ASSERT_TRUE(item->IsDangerous()); |
| 1841 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); | 1838 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); |
| 1842 base::FilePath returned_path; | 1839 base::FilePath returned_path; |
| 1843 | 1840 |
| 1844 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); | 1841 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); |
| 1845 EXPECT_CALL(*download_file, Detach()); | 1842 EXPECT_CALL(*download_file, Detach()); |
| 1846 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); | 1843 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
| 1847 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); | 1844 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1848 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 1845 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 1849 item->StealDangerousDownload( | 1846 item->StealDangerousDownload( |
| 1850 true, // delete_file_after_feedback | 1847 true, // delete_file_after_feedback |
| 1851 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, | 1848 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1852 weak_ptr_factory.GetWeakPtr(), | 1849 weak_ptr_factory.GetWeakPtr(), |
| 1853 base::Unretained(&returned_path))); | 1850 base::Unretained(&returned_path))); |
| 1854 RunAllPendingInMessageLoops(); | 1851 RunAllBlockingPoolTasksUntilIdle(); |
| 1855 EXPECT_EQ(full_path, returned_path); | 1852 EXPECT_EQ(full_path, returned_path); |
| 1856 } | 1853 } |
| 1857 | 1854 |
| 1858 TEST_F(DownloadItemTest, StealDangerousDownloadAndKeep) { | 1855 TEST_F(DownloadItemTest, StealDangerousDownloadAndKeep) { |
| 1859 DownloadItemImpl* item = CreateDownloadItem(); | 1856 DownloadItemImpl* item = CreateDownloadItem(); |
| 1860 MockDownloadFile* download_file = | 1857 MockDownloadFile* download_file = |
| 1861 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1858 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1862 ASSERT_TRUE(item->IsDangerous()); | 1859 ASSERT_TRUE(item->IsDangerous()); |
| 1863 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); | 1860 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); |
| 1864 base::FilePath returned_path; | 1861 base::FilePath returned_path; |
| 1865 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); | 1862 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); |
| 1866 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); | 1863 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1867 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 1864 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 1868 item->StealDangerousDownload( | 1865 item->StealDangerousDownload( |
| 1869 false, // delete_file_after_feedback | 1866 false, // delete_file_after_feedback |
| 1870 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, | 1867 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1871 weak_ptr_factory.GetWeakPtr(), | 1868 weak_ptr_factory.GetWeakPtr(), |
| 1872 base::Unretained(&returned_path))); | 1869 base::Unretained(&returned_path))); |
| 1873 RunAllPendingInMessageLoops(); | 1870 RunAllBlockingPoolTasksUntilIdle(); |
| 1874 EXPECT_NE(full_path, returned_path); | 1871 EXPECT_NE(full_path, returned_path); |
| 1875 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); | 1872 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 1876 } | 1873 } |
| 1877 | 1874 |
| 1878 TEST_F(DownloadItemTest, StealInterruptedContinuableDangerousDownload) { | 1875 TEST_F(DownloadItemTest, StealInterruptedContinuableDangerousDownload) { |
| 1879 base::FilePath returned_path; | 1876 base::FilePath returned_path; |
| 1880 DownloadItemImpl* item = CreateDownloadItem(); | 1877 DownloadItemImpl* item = CreateDownloadItem(); |
| 1881 MockDownloadFile* download_file = | 1878 MockDownloadFile* download_file = |
| 1882 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1879 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1883 base::FilePath full_path = item->GetFullPath(); | 1880 base::FilePath full_path = item->GetFullPath(); |
| 1884 EXPECT_FALSE(full_path.empty()); | 1881 EXPECT_FALSE(full_path.empty()); |
| 1885 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); | 1882 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); |
| 1886 EXPECT_CALL(*download_file, Detach()); | 1883 EXPECT_CALL(*download_file, Detach()); |
| 1887 item->DestinationObserverAsWeakPtr()->DestinationError( | 1884 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1888 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1, | 1885 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1, |
| 1889 std::unique_ptr<crypto::SecureHash>()); | 1886 std::unique_ptr<crypto::SecureHash>()); |
| 1890 ASSERT_TRUE(item->IsDangerous()); | 1887 ASSERT_TRUE(item->IsDangerous()); |
| 1891 | 1888 |
| 1892 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); | 1889 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
| 1893 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); | 1890 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1894 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 1891 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 1895 item->StealDangerousDownload( | 1892 item->StealDangerousDownload( |
| 1896 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, | 1893 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1897 weak_ptr_factory.GetWeakPtr(), | 1894 weak_ptr_factory.GetWeakPtr(), |
| 1898 base::Unretained(&returned_path))); | 1895 base::Unretained(&returned_path))); |
| 1899 RunAllPendingInMessageLoops(); | 1896 RunAllBlockingPoolTasksUntilIdle(); |
| 1900 EXPECT_EQ(full_path, returned_path); | 1897 EXPECT_EQ(full_path, returned_path); |
| 1901 } | 1898 } |
| 1902 | 1899 |
| 1903 TEST_F(DownloadItemTest, StealInterruptedNonContinuableDangerousDownload) { | 1900 TEST_F(DownloadItemTest, StealInterruptedNonContinuableDangerousDownload) { |
| 1904 base::FilePath returned_path; | 1901 base::FilePath returned_path; |
| 1905 DownloadItemImpl* item = CreateDownloadItem(); | 1902 DownloadItemImpl* item = CreateDownloadItem(); |
| 1906 MockDownloadFile* download_file = | 1903 MockDownloadFile* download_file = |
| 1907 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1904 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1908 EXPECT_CALL(*download_file, Cancel()); | 1905 EXPECT_CALL(*download_file, Cancel()); |
| 1909 item->DestinationObserverAsWeakPtr()->DestinationError( | 1906 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1910 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1, | 1907 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1, |
| 1911 std::unique_ptr<crypto::SecureHash>()); | 1908 std::unique_ptr<crypto::SecureHash>()); |
| 1912 ASSERT_TRUE(item->IsDangerous()); | 1909 ASSERT_TRUE(item->IsDangerous()); |
| 1913 | 1910 |
| 1914 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); | 1911 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
| 1915 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); | 1912 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1916 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 1913 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 1917 item->StealDangerousDownload( | 1914 item->StealDangerousDownload( |
| 1918 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, | 1915 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1919 weak_ptr_factory.GetWeakPtr(), | 1916 weak_ptr_factory.GetWeakPtr(), |
| 1920 base::Unretained(&returned_path))); | 1917 base::Unretained(&returned_path))); |
| 1921 RunAllPendingInMessageLoops(); | 1918 RunAllBlockingPoolTasksUntilIdle(); |
| 1922 EXPECT_TRUE(returned_path.empty()); | 1919 EXPECT_TRUE(returned_path.empty()); |
| 1923 } | 1920 } |
| 1924 | 1921 |
| 1925 namespace { | 1922 namespace { |
| 1926 | 1923 |
| 1927 // The DownloadItemDestinationUpdateRaceTest fixture (defined below) is used to | 1924 // The DownloadItemDestinationUpdateRaceTest fixture (defined below) is used to |
| 1928 // test for race conditions between download destination events received via the | 1925 // test for race conditions between download destination events received via the |
| 1929 // DownloadDestinationObserver interface, and the target determination logic. | 1926 // DownloadDestinationObserver interface, and the target determination logic. |
| 1930 // | 1927 // |
| 1931 // The general control flow for DownloadItemImpl looks like this: | 1928 // The general control flow for DownloadItemImpl looks like this: |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2168 EXPECT_CALL(*file_, Initialize(_, _, _, _)) | 2165 EXPECT_CALL(*file_, Initialize(_, _, _, _)) |
| 2169 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 2166 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
| 2170 ScheduleClosure(download_start_loop.QuitClosure()))); | 2167 ScheduleClosure(download_start_loop.QuitClosure()))); |
| 2171 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); | 2168 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
| 2172 download_start_loop.Run(); | 2169 download_start_loop.Run(); |
| 2173 | 2170 |
| 2174 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 2171 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
| 2175 item_->DestinationObserverAsWeakPtr(); | 2172 item_->DestinationObserverAsWeakPtr(); |
| 2176 | 2173 |
| 2177 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 2174 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
| 2178 RunAllPendingInMessageLoops(); | 2175 RunAllBlockingPoolTasksUntilIdle(); |
| 2179 | 2176 |
| 2180 base::RunLoop initialize_completion_loop; | 2177 base::RunLoop initialize_completion_loop; |
| 2181 DownloadTargetCallback target_callback; | 2178 DownloadTargetCallback target_callback; |
| 2182 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) | 2179 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) |
| 2183 .WillOnce( | 2180 .WillOnce( |
| 2184 DoAll(SaveArg<1>(&target_callback), | 2181 DoAll(SaveArg<1>(&target_callback), |
| 2185 ScheduleClosure(initialize_completion_loop.QuitClosure()))); | 2182 ScheduleClosure(initialize_completion_loop.QuitClosure()))); |
| 2186 ScheduleObservations(PostInitializeFileObservations(), destination_observer); | 2183 ScheduleObservations(PostInitializeFileObservations(), destination_observer); |
| 2187 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); | 2184 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2188 initialize_completion_loop.Run(); | 2185 initialize_completion_loop.Run(); |
| 2189 | 2186 |
| 2190 RunAllPendingInMessageLoops(); | 2187 RunAllBlockingPoolTasksUntilIdle(); |
| 2191 | 2188 |
| 2192 ASSERT_FALSE(target_callback.is_null()); | 2189 ASSERT_FALSE(target_callback.is_null()); |
| 2193 ScheduleObservations(PostTargetDeterminationObservations(), | 2190 ScheduleObservations(PostTargetDeterminationObservations(), |
| 2194 destination_observer); | 2191 destination_observer); |
| 2195 target_callback.Run(base::FilePath(), | 2192 target_callback.Run(base::FilePath(), |
| 2196 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 2193 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 2197 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath(), | 2194 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath(), |
| 2198 DOWNLOAD_INTERRUPT_REASON_NONE); | 2195 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2199 EXPECT_EQ(DownloadItem::CANCELLED, item_->GetState()); | 2196 EXPECT_EQ(DownloadItem::CANCELLED, item_->GetState()); |
| 2200 RunAllPendingInMessageLoops(); | 2197 RunAllBlockingPoolTasksUntilIdle(); |
| 2201 } | 2198 } |
| 2202 | 2199 |
| 2203 // Run through the DII workflow, but the intermediate rename fails. | 2200 // Run through the DII workflow, but the intermediate rename fails. |
| 2204 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameFails) { | 2201 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameFails) { |
| 2205 // Expect that the download file and the request will be cancelled as a | 2202 // Expect that the download file and the request will be cancelled as a |
| 2206 // result. | 2203 // result. |
| 2207 EXPECT_CALL(*file_, Cancel()); | 2204 EXPECT_CALL(*file_, Cancel()); |
| 2208 EXPECT_CALL(*request_handle_, CancelRequest(_)); | 2205 EXPECT_CALL(*request_handle_, CancelRequest(_)); |
| 2209 | 2206 |
| 2210 // Intermediate rename loop is not used immediately, but let's set up the | 2207 // Intermediate rename loop is not used immediately, but let's set up the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 2221 EXPECT_CALL(*file_, Initialize(_, _, _, _)) | 2218 EXPECT_CALL(*file_, Initialize(_, _, _, _)) |
| 2222 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 2219 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
| 2223 ScheduleClosure(download_start_loop.QuitClosure()))); | 2220 ScheduleClosure(download_start_loop.QuitClosure()))); |
| 2224 | 2221 |
| 2225 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); | 2222 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
| 2226 download_start_loop.Run(); | 2223 download_start_loop.Run(); |
| 2227 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 2224 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
| 2228 item_->DestinationObserverAsWeakPtr(); | 2225 item_->DestinationObserverAsWeakPtr(); |
| 2229 | 2226 |
| 2230 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 2227 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
| 2231 RunAllPendingInMessageLoops(); | 2228 RunAllBlockingPoolTasksUntilIdle(); |
| 2232 | 2229 |
| 2233 base::RunLoop initialize_completion_loop; | 2230 base::RunLoop initialize_completion_loop; |
| 2234 DownloadTargetCallback target_callback; | 2231 DownloadTargetCallback target_callback; |
| 2235 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) | 2232 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) |
| 2236 .WillOnce( | 2233 .WillOnce( |
| 2237 DoAll(SaveArg<1>(&target_callback), | 2234 DoAll(SaveArg<1>(&target_callback), |
| 2238 ScheduleClosure(initialize_completion_loop.QuitClosure()))); | 2235 ScheduleClosure(initialize_completion_loop.QuitClosure()))); |
| 2239 ScheduleObservations(PostInitializeFileObservations(), destination_observer); | 2236 ScheduleObservations(PostInitializeFileObservations(), destination_observer); |
| 2240 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); | 2237 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2241 initialize_completion_loop.Run(); | 2238 initialize_completion_loop.Run(); |
| 2242 | 2239 |
| 2243 RunAllPendingInMessageLoops(); | 2240 RunAllBlockingPoolTasksUntilIdle(); |
| 2244 ASSERT_FALSE(target_callback.is_null()); | 2241 ASSERT_FALSE(target_callback.is_null()); |
| 2245 | 2242 |
| 2246 ScheduleObservations(PostTargetDeterminationObservations(), | 2243 ScheduleObservations(PostTargetDeterminationObservations(), |
| 2247 destination_observer); | 2244 destination_observer); |
| 2248 target_callback.Run(base::FilePath(kDummyTargetPath), | 2245 target_callback.Run(base::FilePath(kDummyTargetPath), |
| 2249 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 2246 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 2250 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2247 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 2251 base::FilePath(kDummyIntermediatePath), | 2248 base::FilePath(kDummyIntermediatePath), |
| 2252 DOWNLOAD_INTERRUPT_REASON_NONE); | 2249 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2253 | 2250 |
| 2254 intermediate_rename_loop.Run(); | 2251 intermediate_rename_loop.Run(); |
| 2255 ASSERT_FALSE(intermediate_rename_callback.is_null()); | 2252 ASSERT_FALSE(intermediate_rename_callback.is_null()); |
| 2256 | 2253 |
| 2257 ScheduleObservations(PostIntermediateRenameObservations(), | 2254 ScheduleObservations(PostIntermediateRenameObservations(), |
| 2258 destination_observer); | 2255 destination_observer); |
| 2259 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, | 2256 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, |
| 2260 base::FilePath()); | 2257 base::FilePath()); |
| 2261 RunAllPendingInMessageLoops(); | 2258 RunAllBlockingPoolTasksUntilIdle(); |
| 2262 | 2259 |
| 2263 EXPECT_EQ(DownloadItem::INTERRUPTED, item_->GetState()); | 2260 EXPECT_EQ(DownloadItem::INTERRUPTED, item_->GetState()); |
| 2264 } | 2261 } |
| 2265 | 2262 |
| 2266 // Run through the DII workflow. Download file initialization, target | 2263 // Run through the DII workflow. Download file initialization, target |
| 2267 // determination and intermediate rename all succeed. | 2264 // determination and intermediate rename all succeed. |
| 2268 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameSucceeds) { | 2265 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameSucceeds) { |
| 2269 // We expect either that the download will fail (in which case the request and | 2266 // We expect either that the download will fail (in which case the request and |
| 2270 // the download file will be cancelled), or it will succeed (in which case the | 2267 // the download file will be cancelled), or it will succeed (in which case the |
| 2271 // DownloadFile will Detach()). It depends on the list of observations that | 2268 // DownloadFile will Detach()). It depends on the list of observations that |
| (...skipping 19 matching lines...) Expand all Loading... |
| 2291 EXPECT_CALL(*file_, Initialize(_, _, _, _)) | 2288 EXPECT_CALL(*file_, Initialize(_, _, _, _)) |
| 2292 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 2289 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
| 2293 ScheduleClosure(download_start_loop.QuitClosure()))); | 2290 ScheduleClosure(download_start_loop.QuitClosure()))); |
| 2294 | 2291 |
| 2295 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); | 2292 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
| 2296 download_start_loop.Run(); | 2293 download_start_loop.Run(); |
| 2297 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 2294 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
| 2298 item_->DestinationObserverAsWeakPtr(); | 2295 item_->DestinationObserverAsWeakPtr(); |
| 2299 | 2296 |
| 2300 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 2297 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
| 2301 RunAllPendingInMessageLoops(); | 2298 RunAllBlockingPoolTasksUntilIdle(); |
| 2302 | 2299 |
| 2303 base::RunLoop initialize_completion_loop; | 2300 base::RunLoop initialize_completion_loop; |
| 2304 DownloadTargetCallback target_callback; | 2301 DownloadTargetCallback target_callback; |
| 2305 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) | 2302 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) |
| 2306 .WillOnce( | 2303 .WillOnce( |
| 2307 DoAll(SaveArg<1>(&target_callback), | 2304 DoAll(SaveArg<1>(&target_callback), |
| 2308 ScheduleClosure(initialize_completion_loop.QuitClosure()))); | 2305 ScheduleClosure(initialize_completion_loop.QuitClosure()))); |
| 2309 ScheduleObservations(PostInitializeFileObservations(), destination_observer); | 2306 ScheduleObservations(PostInitializeFileObservations(), destination_observer); |
| 2310 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); | 2307 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2311 initialize_completion_loop.Run(); | 2308 initialize_completion_loop.Run(); |
| 2312 | 2309 |
| 2313 RunAllPendingInMessageLoops(); | 2310 RunAllBlockingPoolTasksUntilIdle(); |
| 2314 ASSERT_FALSE(target_callback.is_null()); | 2311 ASSERT_FALSE(target_callback.is_null()); |
| 2315 | 2312 |
| 2316 ScheduleObservations(PostTargetDeterminationObservations(), | 2313 ScheduleObservations(PostTargetDeterminationObservations(), |
| 2317 destination_observer); | 2314 destination_observer); |
| 2318 target_callback.Run(base::FilePath(kDummyTargetPath), | 2315 target_callback.Run(base::FilePath(kDummyTargetPath), |
| 2319 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 2316 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 2320 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2317 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 2321 base::FilePath(kDummyIntermediatePath), | 2318 base::FilePath(kDummyIntermediatePath), |
| 2322 DOWNLOAD_INTERRUPT_REASON_NONE); | 2319 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2323 | 2320 |
| 2324 intermediate_rename_loop.Run(); | 2321 intermediate_rename_loop.Run(); |
| 2325 ASSERT_FALSE(intermediate_rename_callback.is_null()); | 2322 ASSERT_FALSE(intermediate_rename_callback.is_null()); |
| 2326 | 2323 |
| 2327 // This may or may not be called, depending on whether there are any errors in | 2324 // This may or may not be called, depending on whether there are any errors in |
| 2328 // our action list. | 2325 // our action list. |
| 2329 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) | 2326 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) |
| 2330 .Times(::testing::AnyNumber()); | 2327 .Times(::testing::AnyNumber()); |
| 2331 | 2328 |
| 2332 ScheduleObservations(PostIntermediateRenameObservations(), | 2329 ScheduleObservations(PostIntermediateRenameObservations(), |
| 2333 destination_observer); | 2330 destination_observer); |
| 2334 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE, | 2331 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 2335 base::FilePath(kDummyIntermediatePath)); | 2332 base::FilePath(kDummyIntermediatePath)); |
| 2336 RunAllPendingInMessageLoops(); | 2333 RunAllBlockingPoolTasksUntilIdle(); |
| 2337 | 2334 |
| 2338 // The state of the download depends on the observer events that were played | 2335 // The state of the download depends on the observer events that were played |
| 2339 // back to the DownloadItemImpl. Hence we can't establish a single expectation | 2336 // back to the DownloadItemImpl. Hence we can't establish a single expectation |
| 2340 // here. On Debug builds, the DCHECKs will verify that the state transitions | 2337 // here. On Debug builds, the DCHECKs will verify that the state transitions |
| 2341 // were correct. On Release builds, tests are expected to run to completion | 2338 // were correct. On Release builds, tests are expected to run to completion |
| 2342 // without crashing on success. | 2339 // without crashing on success. |
| 2343 EXPECT_TRUE(item_->GetState() == DownloadItem::IN_PROGRESS || | 2340 EXPECT_TRUE(item_->GetState() == DownloadItem::IN_PROGRESS || |
| 2344 item_->GetState() == DownloadItem::INTERRUPTED); | 2341 item_->GetState() == DownloadItem::INTERRUPTED); |
| 2345 if (item_->GetState() == DownloadItem::INTERRUPTED) | 2342 if (item_->GetState() == DownloadItem::INTERRUPTED) |
| 2346 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item_->GetLastReason()); | 2343 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item_->GetLastReason()); |
| 2347 | 2344 |
| 2348 item_->Cancel(true); | 2345 item_->Cancel(true); |
| 2349 RunAllPendingInMessageLoops(); | 2346 RunAllBlockingPoolTasksUntilIdle(); |
| 2350 } | 2347 } |
| 2351 | 2348 |
| 2352 TEST(MockDownloadItem, Compiles) { | 2349 TEST(MockDownloadItem, Compiles) { |
| 2353 MockDownloadItem mock_item; | 2350 MockDownloadItem mock_item; |
| 2354 } | 2351 } |
| 2355 | 2352 |
| 2356 } // namespace content | 2353 } // namespace content |
| OLD | NEW |