Chromium Code Reviews| 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() { RunAllBlockingPoolTasksUntilIdle(); } |
|
fdoray
2017/06/22 13:01:35
Redundant. TestBrowserThreadBundle does this in it
Sigurður Ásgeirsson
2017/06/22 14:50:11
Done.
| |
| 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 18 matching lines...) Expand all Loading... | |
| 808 Property(&DownloadUrlParameters::offset, 1)), | 805 Property(&DownloadUrlParameters::offset, 1)), |
| 809 _)) | 806 _)) |
| 810 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); | 807 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); |
| 811 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { | 808 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { |
| 812 SCOPED_TRACE(::testing::Message() << "Iteration " << i); | 809 SCOPED_TRACE(::testing::Message() << "Iteration " << i); |
| 813 | 810 |
| 814 mock_download_file = base::MakeUnique<NiceMock<MockDownloadFile>>(); | 811 mock_download_file = base::MakeUnique<NiceMock<MockDownloadFile>>(); |
| 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 |
| 815 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)) | |
| 816 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE)); | |
| 818 ON_CALL(*mock_download_file_ref, FullPath()) | 817 ON_CALL(*mock_download_file_ref, FullPath()) |
| 819 .WillByDefault(ReturnRefOfCopy(base::FilePath())); | 818 .WillByDefault(ReturnRefOfCopy(base::FilePath())); |
| 820 | 819 |
| 821 // Copied key parts of DoIntermediateRename & CallDownloadItemStart | 820 // Copied key parts of DoIntermediateRename & CallDownloadItemStart |
| 822 // to allow for holding onto the request handle. | 821 // to allow for holding onto the request handle. |
| 823 item->Start(std::move(mock_download_file), std::move(mock_request_handle), | 822 item->Start(std::move(mock_download_file), std::move(mock_request_handle), |
| 824 *create_info()); | 823 *create_info()); |
| 825 RunAllPendingInMessageLoops(); | 824 RunAllBlockingPoolTasksUntilIdle(); |
| 826 | 825 |
| 827 base::FilePath target_path(kDummyTargetPath); | 826 base::FilePath target_path(kDummyTargetPath); |
| 828 base::FilePath intermediate_path(kDummyIntermediatePath); | 827 base::FilePath intermediate_path(kDummyIntermediatePath); |
| 829 | 828 |
| 830 // Target of RenameAndUniquify is always the intermediate path. | 829 // Target of RenameAndUniquify is always the intermediate path. |
| 831 ON_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)) | 830 ON_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)) |
| 832 .WillByDefault(ScheduleRenameAndUniquifyCallback( | 831 .WillByDefault(ScheduleRenameAndUniquifyCallback( |
| 833 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); | 832 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); |
| 834 | 833 |
| 835 // RenameAndUniquify is only called the first time. In all the subsequent | 834 // RenameAndUniquify is only called the first time. In all the subsequent |
| 836 // iterations, the intermediate file already has the correct name, hence no | 835 // iterations, the intermediate file already has the correct name, hence no |
| 837 // rename is necessary. | 836 // rename is necessary. |
| 838 EXPECT_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)).Times(i == 0); | 837 EXPECT_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)).Times(i == 0); |
| 839 | 838 |
| 840 ASSERT_FALSE(callback.is_null()); | 839 ASSERT_FALSE(callback.is_null()); |
| 841 base::ResetAndReturn(&callback).Run( | 840 base::ResetAndReturn(&callback).Run( |
| 842 target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 841 target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 843 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 842 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 844 DOWNLOAD_INTERRUPT_REASON_NONE); | 843 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 845 RunAllPendingInMessageLoops(); | 844 RunAllBlockingPoolTasksUntilIdle(); |
| 846 | 845 |
| 847 // Use a continuable interrupt. | 846 // Use a continuable interrupt. |
| 848 EXPECT_CALL(*mock_download_file_ref, Cancel()).Times(0); | 847 EXPECT_CALL(*mock_download_file_ref, Cancel()).Times(0); |
| 849 item->DestinationObserverAsWeakPtr()->DestinationError( | 848 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 850 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, | 849 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, |
| 851 std::unique_ptr<crypto::SecureHash>()); | 850 std::unique_ptr<crypto::SecureHash>()); |
| 852 | 851 |
| 853 RunAllPendingInMessageLoops(); | 852 RunAllBlockingPoolTasksUntilIdle(); |
| 854 ::testing::Mock::VerifyAndClearExpectations(mock_download_file_ref); | 853 ::testing::Mock::VerifyAndClearExpectations(mock_download_file_ref); |
| 855 } | 854 } |
| 856 | 855 |
| 857 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 856 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 858 EXPECT_EQ(1, observer.interrupt_count()); | 857 EXPECT_EQ(1, observer.interrupt_count()); |
| 859 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); | 858 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); |
| 860 } | 859 } |
| 861 | 860 |
| 862 // If the download attempts to resume and the resumption request fails, the | 861 // If the download attempts to resume and the resumption request fails, the |
| 863 // subsequent Start() call shouldn't update the origin state (URL redirect | 862 // subsequent Start() call shouldn't update the origin state (URL redirect |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 891 MockResumeInterruptedDownload( | 890 MockResumeInterruptedDownload( |
| 892 AllOf(Property(&DownloadUrlParameters::file_path, | 891 AllOf(Property(&DownloadUrlParameters::file_path, |
| 893 Property(&base::FilePath::value, | 892 Property(&base::FilePath::value, |
| 894 kDummyIntermediatePath)), | 893 kDummyIntermediatePath)), |
| 895 Property(&DownloadUrlParameters::offset, 1)), | 894 Property(&DownloadUrlParameters::offset, 1)), |
| 896 _)); | 895 _)); |
| 897 EXPECT_CALL(*download_file, Detach()); | 896 EXPECT_CALL(*download_file, Detach()); |
| 898 item->DestinationObserverAsWeakPtr()->DestinationError( | 897 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 899 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, | 898 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, |
| 900 std::unique_ptr<crypto::SecureHash>()); | 899 std::unique_ptr<crypto::SecureHash>()); |
| 901 RunAllPendingInMessageLoops(); | 900 RunAllBlockingPoolTasksUntilIdle(); |
| 902 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 901 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 903 | 902 |
| 904 // Now change the create info. The changes should not cause the DownloadItem | 903 // Now change the create info. The changes should not cause the DownloadItem |
| 905 // to be updated. | 904 // to be updated. |
| 906 constexpr int kSecondResponseCode = 418; | 905 constexpr int kSecondResponseCode = 418; |
| 907 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 906 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
| 908 const char kSecondETag[] = "123"; | 907 const char kSecondETag[] = "123"; |
| 909 const char kSecondLastModified[] = "Today"; | 908 const char kSecondLastModified[] = "Today"; |
| 910 const char kSecondURL[] = "http://example.com/another-download"; | 909 const char kSecondURL[] = "http://example.com/another-download"; |
| 911 const char kSecondMimeType[] = "text/html"; | 910 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 | 922 // Calling Start() with a response indicating failure shouldn't cause a target |
| 924 // update, nor should it result in discarding the intermediate file. | 923 // update, nor should it result in discarding the intermediate file. |
| 925 DownloadTargetCallback target_callback; | 924 DownloadTargetCallback target_callback; |
| 926 download_file = CallDownloadItemStart(item, &target_callback); | 925 download_file = CallDownloadItemStart(item, &target_callback); |
| 927 ASSERT_FALSE(target_callback.is_null()); | 926 ASSERT_FALSE(target_callback.is_null()); |
| 928 target_callback.Run(base::FilePath(kDummyTargetPath), | 927 target_callback.Run(base::FilePath(kDummyTargetPath), |
| 929 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 928 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 930 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 929 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 931 base::FilePath(kDummyIntermediatePath), | 930 base::FilePath(kDummyIntermediatePath), |
| 932 DOWNLOAD_INTERRUPT_REASON_NONE); | 931 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 933 RunAllPendingInMessageLoops(); | 932 RunAllBlockingPoolTasksUntilIdle(); |
| 934 | 933 |
| 935 ASSERT_TRUE(item->GetResponseHeaders()); | 934 ASSERT_TRUE(item->GetResponseHeaders()); |
| 936 EXPECT_EQ(kFirstResponseCode, item->GetResponseHeaders()->response_code()); | 935 EXPECT_EQ(kFirstResponseCode, item->GetResponseHeaders()->response_code()); |
| 937 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); | 936 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); |
| 938 EXPECT_EQ(kFirstETag, item->GetETag()); | 937 EXPECT_EQ(kFirstETag, item->GetETag()); |
| 939 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); | 938 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); |
| 940 EXPECT_EQ(kFirstURL, item->GetURL().spec()); | 939 EXPECT_EQ(kFirstURL, item->GetURL().spec()); |
| 941 EXPECT_EQ(kMimeType, item->GetMimeType()); | 940 EXPECT_EQ(kMimeType, item->GetMimeType()); |
| 942 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 941 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 943 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); | 942 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 962 create_info()->mime_type = kMimeType; | 961 create_info()->mime_type = kMimeType; |
| 963 | 962 |
| 964 DownloadItemImpl* item = CreateDownloadItem(); | 963 DownloadItemImpl* item = CreateDownloadItem(); |
| 965 MockDownloadFile* download_file = | 964 MockDownloadFile* download_file = |
| 966 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 965 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 967 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); | 966 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); |
| 968 EXPECT_CALL(*download_file, Detach()); | 967 EXPECT_CALL(*download_file, Detach()); |
| 969 item->DestinationObserverAsWeakPtr()->DestinationError( | 968 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 970 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 969 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
| 971 std::unique_ptr<crypto::SecureHash>()); | 970 std::unique_ptr<crypto::SecureHash>()); |
| 972 RunAllPendingInMessageLoops(); | 971 RunAllBlockingPoolTasksUntilIdle(); |
| 973 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 972 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 974 | 973 |
| 975 // Now change the create info. The changes should not cause the DownloadItem | 974 // Now change the create info. The changes should not cause the DownloadItem |
| 976 // to be updated. | 975 // to be updated. |
| 977 constexpr int kSecondResponseCode = 201; | 976 constexpr int kSecondResponseCode = 201; |
| 978 const char kSecondContentDisposition[] = "attachment; filename=bar"; | 977 const char kSecondContentDisposition[] = "attachment; filename=bar"; |
| 979 const char kSecondETag[] = "123"; | 978 const char kSecondETag[] = "123"; |
| 980 const char kSecondLastModified[] = "Today"; | 979 const char kSecondLastModified[] = "Today"; |
| 981 const char kSecondURL[] = "http://example.com/another-download"; | 980 const char kSecondURL[] = "http://example.com/another-download"; |
| 982 const char kSecondMimeType[] = "text/html"; | 981 const char kSecondMimeType[] = "text/html"; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1021 item->DestinationObserverAsWeakPtr()->DestinationUpdate(10, 100, | 1020 item->DestinationObserverAsWeakPtr()->DestinationUpdate(10, 100, |
| 1022 kReceivedSlice); | 1021 kReceivedSlice); |
| 1023 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); | 1022 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); |
| 1024 EXPECT_EQ(10, item->GetReceivedBytes()); | 1023 EXPECT_EQ(10, item->GetReceivedBytes()); |
| 1025 | 1024 |
| 1026 item->DestinationObserverAsWeakPtr()->DestinationError( | 1025 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1027 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, | 1026 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, |
| 1028 std::unique_ptr<crypto::SecureHash>()); | 1027 std::unique_ptr<crypto::SecureHash>()); |
| 1029 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); | 1028 EXPECT_EQ(kReceivedSlice, item->GetReceivedSlices()); |
| 1030 | 1029 |
| 1031 RunAllPendingInMessageLoops(); | 1030 RunAllBlockingPoolTasksUntilIdle(); |
| 1032 | 1031 |
| 1033 // Change the strong validator and resume the download, the received slices | 1032 // Change the strong validator and resume the download, the received slices |
| 1034 // should be cleared. | 1033 // should be cleared. |
| 1035 create_info()->etag = kSecondETag; | 1034 create_info()->etag = kSecondETag; |
| 1036 DownloadTargetCallback target_callback; | 1035 DownloadTargetCallback target_callback; |
| 1037 download_file = CallDownloadItemStart(item, &target_callback); | 1036 download_file = CallDownloadItemStart(item, &target_callback); |
| 1038 EXPECT_TRUE(item->GetReceivedSlices().empty()); | 1037 EXPECT_TRUE(item->GetReceivedSlices().empty()); |
| 1039 EXPECT_EQ(0, item->GetReceivedBytes()); | 1038 EXPECT_EQ(0, item->GetReceivedBytes()); |
| 1040 | 1039 |
| 1041 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); | 1040 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 1065 .Times(1); | 1064 .Times(1); |
| 1066 item->DestinationObserverAsWeakPtr()->DestinationError( | 1065 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1067 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, | 1066 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1, |
| 1068 std::unique_ptr<crypto::SecureHash>()); | 1067 std::unique_ptr<crypto::SecureHash>()); |
| 1069 | 1068 |
| 1070 // Test expectations verify that ResumeInterruptedDownload() is called (by way | 1069 // Test expectations verify that ResumeInterruptedDownload() is called (by way |
| 1071 // of MockResumeInterruptedDownload) after the download is interrupted. But | 1070 // of MockResumeInterruptedDownload) after the download is interrupted. But |
| 1072 // the mock doesn't follow through with the resumption. | 1071 // the mock doesn't follow through with the resumption. |
| 1073 // ResumeInterruptedDownload() being called is sufficient for verifying that | 1072 // ResumeInterruptedDownload() being called is sufficient for verifying that |
| 1074 // the resumption was triggered. | 1073 // the resumption was triggered. |
| 1075 RunAllPendingInMessageLoops(); | 1074 RunAllBlockingPoolTasksUntilIdle(); |
| 1076 | 1075 |
| 1077 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. | 1076 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. |
| 1078 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); | 1077 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); |
| 1079 } | 1078 } |
| 1080 | 1079 |
| 1081 TEST_F(DownloadItemTest, DisplayName) { | 1080 TEST_F(DownloadItemTest, DisplayName) { |
| 1082 DownloadItemImpl* item = CreateDownloadItem(); | 1081 DownloadItemImpl* item = CreateDownloadItem(); |
| 1083 DownloadTargetCallback callback; | 1082 DownloadTargetCallback callback; |
| 1084 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); | 1083 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); |
| 1085 base::FilePath target_path( | 1084 base::FilePath target_path( |
| 1086 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); | 1085 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); |
| 1087 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 1086 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 1088 EXPECT_EQ(FILE_PATH_LITERAL(""), | 1087 EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 1089 item->GetFileNameToReportUser().value()); | 1088 item->GetFileNameToReportUser().value()); |
| 1090 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) | 1089 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) |
| 1091 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1090 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1092 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); | 1091 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); |
| 1093 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1092 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1094 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1093 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1095 DOWNLOAD_INTERRUPT_REASON_NONE); | 1094 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1096 RunAllPendingInMessageLoops(); | 1095 RunAllBlockingPoolTasksUntilIdle(); |
| 1097 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 1096 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
| 1098 item->GetFileNameToReportUser().value()); | 1097 item->GetFileNameToReportUser().value()); |
| 1099 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name"))); | 1098 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name"))); |
| 1100 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 1099 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
| 1101 item->GetFileNameToReportUser().value()); | 1100 item->GetFileNameToReportUser().value()); |
| 1102 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); | 1101 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 1103 } | 1102 } |
| 1104 | 1103 |
| 1105 // Test to make sure that Start method calls DF initialize properly. | 1104 // Test to make sure that Start method calls DF initialize properly. |
| 1106 TEST_F(DownloadItemTest, Start) { | 1105 TEST_F(DownloadItemTest, Start) { |
| 1107 MockDownloadFile* mock_download_file(new MockDownloadFile); | 1106 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 1108 std::unique_ptr<DownloadFile> download_file(mock_download_file); | 1107 std::unique_ptr<DownloadFile> download_file(mock_download_file); |
| 1109 DownloadItemImpl* item = CreateDownloadItem(); | 1108 DownloadItemImpl* item = CreateDownloadItem(); |
| 1110 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)); | 1109 EXPECT_CALL(*mock_download_file, Initialize(_, _, _, _)); |
| 1111 std::unique_ptr<DownloadRequestHandleInterface> request_handle( | 1110 std::unique_ptr<DownloadRequestHandleInterface> request_handle( |
| 1112 new NiceMock<MockRequestHandle>); | 1111 new NiceMock<MockRequestHandle>); |
| 1113 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | 1112 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
| 1114 item->Start(std::move(download_file), std::move(request_handle), | 1113 item->Start(std::move(download_file), std::move(request_handle), |
| 1115 *create_info()); | 1114 *create_info()); |
| 1116 RunAllPendingInMessageLoops(); | 1115 RunAllBlockingPoolTasksUntilIdle(); |
| 1117 | 1116 |
| 1118 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); | 1117 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 1119 } | 1118 } |
| 1120 | 1119 |
| 1121 // Download file and the request should be cancelled as a result of download | 1120 // Download file and the request should be cancelled as a result of download |
| 1122 // file initialization failing. | 1121 // file initialization failing. |
| 1123 TEST_F(DownloadItemTest, InitDownloadFileFails) { | 1122 TEST_F(DownloadItemTest, InitDownloadFileFails) { |
| 1124 DownloadItemImpl* item = CreateDownloadItem(); | 1123 DownloadItemImpl* item = CreateDownloadItem(); |
| 1125 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>(); | 1124 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>(); |
| 1126 std::unique_ptr<MockRequestHandle> request_handle = | 1125 std::unique_ptr<MockRequestHandle> request_handle = |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 1139 ScheduleClosure(start_download_loop.QuitClosure()))); | 1138 ScheduleClosure(start_download_loop.QuitClosure()))); |
| 1140 | 1139 |
| 1141 item->Start(std::move(file), std::move(request_handle), *create_info()); | 1140 item->Start(std::move(file), std::move(request_handle), *create_info()); |
| 1142 start_download_loop.Run(); | 1141 start_download_loop.Run(); |
| 1143 | 1142 |
| 1144 download_target_callback.Run(base::FilePath(kDummyTargetPath), | 1143 download_target_callback.Run(base::FilePath(kDummyTargetPath), |
| 1145 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1144 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1146 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1145 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1147 base::FilePath(kDummyIntermediatePath), | 1146 base::FilePath(kDummyIntermediatePath), |
| 1148 DOWNLOAD_INTERRUPT_REASON_NONE); | 1147 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1149 RunAllPendingInMessageLoops(); | 1148 RunAllBlockingPoolTasksUntilIdle(); |
| 1150 | 1149 |
| 1151 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1150 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1152 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 1151 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 1153 item->GetLastReason()); | 1152 item->GetLastReason()); |
| 1154 EXPECT_FALSE(item->GetTargetFilePath().empty()); | 1153 EXPECT_FALSE(item->GetTargetFilePath().empty()); |
| 1155 EXPECT_TRUE(item->GetFullPath().empty()); | 1154 EXPECT_TRUE(item->GetFullPath().empty()); |
| 1156 } | 1155 } |
| 1157 | 1156 |
| 1158 // Handling of downloads initiated via a failed request. In this case, Start() | 1157 // 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. | 1158 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason. |
| 1160 TEST_F(DownloadItemTest, StartFailedDownload) { | 1159 TEST_F(DownloadItemTest, StartFailedDownload) { |
| 1161 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; | 1160 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; |
| 1162 DownloadItemImpl* item = CreateDownloadItem(); | 1161 DownloadItemImpl* item = CreateDownloadItem(); |
| 1163 | 1162 |
| 1164 // DownloadFile and DownloadRequestHandleInterface objects aren't created for | 1163 // DownloadFile and DownloadRequestHandleInterface objects aren't created for |
| 1165 // failed downloads. | 1164 // failed downloads. |
| 1166 std::unique_ptr<DownloadFile> null_download_file; | 1165 std::unique_ptr<DownloadFile> null_download_file; |
| 1167 std::unique_ptr<DownloadRequestHandleInterface> null_request_handle; | 1166 std::unique_ptr<DownloadRequestHandleInterface> null_request_handle; |
| 1168 DownloadTargetCallback download_target_callback; | 1167 DownloadTargetCallback download_target_callback; |
| 1169 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 1168 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
| 1170 .WillOnce(SaveArg<1>(&download_target_callback)); | 1169 .WillOnce(SaveArg<1>(&download_target_callback)); |
| 1171 item->Start(std::move(null_download_file), std::move(null_request_handle), | 1170 item->Start(std::move(null_download_file), std::move(null_request_handle), |
| 1172 *create_info()); | 1171 *create_info()); |
| 1173 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1172 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1174 RunAllPendingInMessageLoops(); | 1173 RunAllBlockingPoolTasksUntilIdle(); |
| 1175 | 1174 |
| 1176 // The DownloadItemImpl should attempt to determine a target path even if the | 1175 // The DownloadItemImpl should attempt to determine a target path even if the |
| 1177 // download was interrupted. | 1176 // download was interrupted. |
| 1178 ASSERT_FALSE(download_target_callback.is_null()); | 1177 ASSERT_FALSE(download_target_callback.is_null()); |
| 1179 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1178 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1180 base::FilePath target_path(FILE_PATH_LITERAL("foo")); | 1179 base::FilePath target_path(FILE_PATH_LITERAL("foo")); |
| 1181 download_target_callback.Run(target_path, | 1180 download_target_callback.Run(target_path, |
| 1182 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1181 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1183 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path, | 1182 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path, |
| 1184 DOWNLOAD_INTERRUPT_REASON_NONE); | 1183 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1185 RunAllPendingInMessageLoops(); | 1184 RunAllBlockingPoolTasksUntilIdle(); |
| 1186 | 1185 |
| 1187 EXPECT_EQ(target_path, item->GetTargetFilePath()); | 1186 EXPECT_EQ(target_path, item->GetTargetFilePath()); |
| 1188 CleanupItem(item, NULL, DownloadItem::INTERRUPTED); | 1187 CleanupItem(item, NULL, DownloadItem::INTERRUPTED); |
| 1189 } | 1188 } |
| 1190 | 1189 |
| 1191 // Test that the delegate is invoked after the download file is renamed. | 1190 // Test that the delegate is invoked after the download file is renamed. |
| 1192 TEST_F(DownloadItemTest, CallbackAfterRename) { | 1191 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 1193 DownloadItemImpl* item = CreateDownloadItem(); | 1192 DownloadItemImpl* item = CreateDownloadItem(); |
| 1194 DownloadTargetCallback callback; | 1193 DownloadTargetCallback callback; |
| 1195 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); | 1194 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); |
| 1196 base::FilePath final_path( | 1195 base::FilePath final_path( |
| 1197 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); | 1196 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); |
| 1198 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 1197 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 1199 base::FilePath new_intermediate_path( | 1198 base::FilePath new_intermediate_path( |
| 1200 final_path.InsertBeforeExtensionASCII("y")); | 1199 final_path.InsertBeforeExtensionASCII("y")); |
| 1201 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1200 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1202 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1201 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1203 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); | 1202 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); |
| 1204 | 1203 |
| 1205 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1204 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1206 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1205 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1207 DOWNLOAD_INTERRUPT_REASON_NONE); | 1206 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1208 RunAllPendingInMessageLoops(); | 1207 RunAllBlockingPoolTasksUntilIdle(); |
| 1209 // All the callbacks should have happened by now. | 1208 // All the callbacks should have happened by now. |
| 1210 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1209 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1211 mock_delegate()->VerifyAndClearExpectations(); | 1210 mock_delegate()->VerifyAndClearExpectations(); |
| 1212 | 1211 |
| 1213 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1212 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
| 1214 .WillOnce(Return(true)); | 1213 .WillOnce(Return(true)); |
| 1215 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _)) | 1214 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _)) |
| 1216 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1215 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1217 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); | 1216 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); |
| 1218 EXPECT_CALL(*download_file, FullPath()) | 1217 EXPECT_CALL(*download_file, FullPath()) |
| 1219 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1218 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1220 EXPECT_CALL(*download_file, Detach()); | 1219 EXPECT_CALL(*download_file, Detach()); |
| 1221 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 1220 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 1222 0, std::unique_ptr<crypto::SecureHash>()); | 1221 0, std::unique_ptr<crypto::SecureHash>()); |
| 1223 RunAllPendingInMessageLoops(); | 1222 RunAllBlockingPoolTasksUntilIdle(); |
| 1224 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1223 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1225 mock_delegate()->VerifyAndClearExpectations(); | 1224 mock_delegate()->VerifyAndClearExpectations(); |
| 1226 } | 1225 } |
| 1227 | 1226 |
| 1228 // Test that the delegate is invoked after the download file is renamed and the | 1227 // Test that the delegate is invoked after the download file is renamed and the |
| 1229 // download item is in an interrupted state. | 1228 // download item is in an interrupted state. |
| 1230 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { | 1229 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { |
| 1231 DownloadItemImpl* item = CreateDownloadItem(); | 1230 DownloadItemImpl* item = CreateDownloadItem(); |
| 1232 DownloadTargetCallback callback; | 1231 DownloadTargetCallback callback; |
| 1233 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); | 1232 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); |
| 1234 base::FilePath final_path( | 1233 base::FilePath final_path( |
| 1235 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); | 1234 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); |
| 1236 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 1235 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 1237 base::FilePath new_intermediate_path( | 1236 base::FilePath new_intermediate_path( |
| 1238 final_path.InsertBeforeExtensionASCII("y")); | 1237 final_path.InsertBeforeExtensionASCII("y")); |
| 1239 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1238 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1240 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1239 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1241 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); | 1240 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); |
| 1242 EXPECT_CALL(*download_file, Cancel()) | 1241 EXPECT_CALL(*download_file, Cancel()) |
| 1243 .Times(1); | 1242 .Times(1); |
| 1244 | 1243 |
| 1245 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1244 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1246 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1245 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1247 DOWNLOAD_INTERRUPT_REASON_NONE); | 1246 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1248 RunAllPendingInMessageLoops(); | 1247 RunAllBlockingPoolTasksUntilIdle(); |
| 1249 // All the callbacks should have happened by now. | 1248 // All the callbacks should have happened by now. |
| 1250 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1249 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1251 mock_delegate()->VerifyAndClearExpectations(); | 1250 mock_delegate()->VerifyAndClearExpectations(); |
| 1252 } | 1251 } |
| 1253 | 1252 |
| 1254 TEST_F(DownloadItemTest, Interrupted) { | 1253 TEST_F(DownloadItemTest, Interrupted) { |
| 1255 DownloadItemImpl* item = CreateDownloadItem(); | 1254 DownloadItemImpl* item = CreateDownloadItem(); |
| 1256 MockDownloadFile* download_file = | 1255 MockDownloadFile* download_file = |
| 1257 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1256 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 1258 | 1257 |
| 1259 const DownloadInterruptReason reason( | 1258 const DownloadInterruptReason reason( |
| 1260 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 1259 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 1261 | 1260 |
| 1262 // Confirm interrupt sets state properly. | 1261 // Confirm interrupt sets state properly. |
| 1263 EXPECT_CALL(*download_file, Cancel()); | 1262 EXPECT_CALL(*download_file, Cancel()); |
| 1264 item->DestinationObserverAsWeakPtr()->DestinationError( | 1263 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1265 reason, 0, std::unique_ptr<crypto::SecureHash>()); | 1264 reason, 0, std::unique_ptr<crypto::SecureHash>()); |
| 1266 RunAllPendingInMessageLoops(); | 1265 RunAllBlockingPoolTasksUntilIdle(); |
| 1267 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1266 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1268 EXPECT_EQ(reason, item->GetLastReason()); | 1267 EXPECT_EQ(reason, item->GetLastReason()); |
| 1269 | 1268 |
| 1270 // Cancel should kill it. | 1269 // Cancel should kill it. |
| 1271 item->Cancel(true); | 1270 item->Cancel(true); |
| 1272 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); | 1271 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
| 1273 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); | 1272 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); |
| 1274 } | 1273 } |
| 1275 | 1274 |
| 1276 // Destination errors that occur before the intermediate rename shouldn't cause | 1275 // Destination errors that occur before the intermediate rename shouldn't cause |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 1291 final_path.InsertBeforeExtensionASCII("y")); | 1290 final_path.InsertBeforeExtensionASCII("y")); |
| 1292 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1291 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1293 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1292 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1294 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); | 1293 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); |
| 1295 EXPECT_CALL(*download_file, Cancel()) | 1294 EXPECT_CALL(*download_file, Cancel()) |
| 1296 .Times(1); | 1295 .Times(1); |
| 1297 | 1296 |
| 1298 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1297 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1299 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1298 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1300 DOWNLOAD_INTERRUPT_REASON_NONE); | 1299 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1301 RunAllPendingInMessageLoops(); | 1300 RunAllBlockingPoolTasksUntilIdle(); |
| 1302 // All the callbacks should have happened by now. | 1301 // All the callbacks should have happened by now. |
| 1303 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1302 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1304 mock_delegate()->VerifyAndClearExpectations(); | 1303 mock_delegate()->VerifyAndClearExpectations(); |
| 1305 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1304 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1306 EXPECT_TRUE(item->GetFullPath().empty()); | 1305 EXPECT_TRUE(item->GetFullPath().empty()); |
| 1307 EXPECT_EQ(final_path, item->GetTargetFilePath()); | 1306 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
| 1308 } | 1307 } |
| 1309 | 1308 |
| 1310 // As above. But if the download can be resumed by continuing, then the | 1309 // 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 | 1310 // 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, _)) | 1329 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1331 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1330 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1332 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); | 1331 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); |
| 1333 EXPECT_CALL(*download_file, FullPath()) | 1332 EXPECT_CALL(*download_file, FullPath()) |
| 1334 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path))); | 1333 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path))); |
| 1335 EXPECT_CALL(*download_file, Detach()); | 1334 EXPECT_CALL(*download_file, Detach()); |
| 1336 | 1335 |
| 1337 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1336 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1338 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1337 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1339 DOWNLOAD_INTERRUPT_REASON_NONE); | 1338 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1340 RunAllPendingInMessageLoops(); | 1339 RunAllBlockingPoolTasksUntilIdle(); |
| 1341 // All the callbacks should have happened by now. | 1340 // All the callbacks should have happened by now. |
| 1342 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1341 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1343 mock_delegate()->VerifyAndClearExpectations(); | 1342 mock_delegate()->VerifyAndClearExpectations(); |
| 1344 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1343 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1345 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 1344 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 1346 EXPECT_EQ(final_path, item->GetTargetFilePath()); | 1345 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
| 1347 } | 1346 } |
| 1348 | 1347 |
| 1349 // As above. If the intermediate rename fails, then the interrupt reason should | 1348 // 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. | 1349 // 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")); | 1363 final_path.InsertBeforeExtensionASCII("y")); |
| 1365 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 1364 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 1366 .WillOnce(ScheduleRenameAndUniquifyCallback( | 1365 .WillOnce(ScheduleRenameAndUniquifyCallback( |
| 1367 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); | 1366 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); |
| 1368 EXPECT_CALL(*download_file, Cancel()) | 1367 EXPECT_CALL(*download_file, Cancel()) |
| 1369 .Times(1); | 1368 .Times(1); |
| 1370 | 1369 |
| 1371 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 1370 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1372 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, | 1371 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path, |
| 1373 DOWNLOAD_INTERRUPT_REASON_NONE); | 1372 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 1374 RunAllPendingInMessageLoops(); | 1373 RunAllBlockingPoolTasksUntilIdle(); |
| 1375 // All the callbacks should have happened by now. | 1374 // All the callbacks should have happened by now. |
| 1376 ::testing::Mock::VerifyAndClearExpectations(download_file); | 1375 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 1377 mock_delegate()->VerifyAndClearExpectations(); | 1376 mock_delegate()->VerifyAndClearExpectations(); |
| 1378 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1377 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1379 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason()); | 1378 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason()); |
| 1380 EXPECT_TRUE(item->GetFullPath().empty()); | 1379 EXPECT_TRUE(item->GetFullPath().empty()); |
| 1381 EXPECT_EQ(final_path, item->GetTargetFilePath()); | 1380 EXPECT_EQ(final_path, item->GetTargetFilePath()); |
| 1382 } | 1381 } |
| 1383 | 1382 |
| 1384 TEST_F(DownloadItemTest, Canceled) { | 1383 TEST_F(DownloadItemTest, Canceled) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1587 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) | 1586 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) |
| 1588 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1587 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1589 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1588 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1590 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1589 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
| 1591 .WillOnce(Return(true)); | 1590 .WillOnce(Return(true)); |
| 1592 EXPECT_CALL(*download_file, FullPath()) | 1591 EXPECT_CALL(*download_file, FullPath()) |
| 1593 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1592 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1594 EXPECT_CALL(*download_file, Detach()); | 1593 EXPECT_CALL(*download_file, Detach()); |
| 1595 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 1594 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 1596 0, std::unique_ptr<crypto::SecureHash>()); | 1595 0, std::unique_ptr<crypto::SecureHash>()); |
| 1597 RunAllPendingInMessageLoops(); | 1596 RunAllBlockingPoolTasksUntilIdle(); |
| 1598 | 1597 |
| 1599 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1598 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1600 EXPECT_TRUE(item->CanShowInFolder()); | 1599 EXPECT_TRUE(item->CanShowInFolder()); |
| 1601 EXPECT_TRUE(item->CanOpenDownload()); | 1600 EXPECT_TRUE(item->CanOpenDownload()); |
| 1602 } | 1601 } |
| 1603 | 1602 |
| 1604 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { | 1603 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { |
| 1605 // A download created with a non-empty FilePath is considered a temporary | 1604 // A download created with a non-empty FilePath is considered a temporary |
| 1606 // download. | 1605 // download. |
| 1607 create_info()->save_info->file_path = base::FilePath(kDummyTargetPath); | 1606 create_info()->save_info->file_path = base::FilePath(kDummyTargetPath); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 1620 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) | 1619 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) |
| 1621 .WillOnce(Return(true)); | 1620 .WillOnce(Return(true)); |
| 1622 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) | 1621 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) |
| 1623 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1622 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1624 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1623 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1625 EXPECT_CALL(*download_file, FullPath()) | 1624 EXPECT_CALL(*download_file, FullPath()) |
| 1626 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1625 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1627 EXPECT_CALL(*download_file, Detach()); | 1626 EXPECT_CALL(*download_file, Detach()); |
| 1628 item->DestinationObserverAsWeakPtr()->DestinationCompleted( | 1627 item->DestinationObserverAsWeakPtr()->DestinationCompleted( |
| 1629 0, std::unique_ptr<crypto::SecureHash>()); | 1628 0, std::unique_ptr<crypto::SecureHash>()); |
| 1630 RunAllPendingInMessageLoops(); | 1629 RunAllBlockingPoolTasksUntilIdle(); |
| 1631 | 1630 |
| 1632 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1631 ASSERT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1633 EXPECT_FALSE(item->CanShowInFolder()); | 1632 EXPECT_FALSE(item->CanShowInFolder()); |
| 1634 EXPECT_FALSE(item->CanOpenDownload()); | 1633 EXPECT_FALSE(item->CanOpenDownload()); |
| 1635 } | 1634 } |
| 1636 | 1635 |
| 1637 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { | 1636 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { |
| 1638 DownloadItemImpl* item = CreateDownloadItem(); | 1637 DownloadItemImpl* item = CreateDownloadItem(); |
| 1639 MockDownloadFile* download_file = | 1638 MockDownloadFile* download_file = |
| 1640 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1639 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 1641 | 1640 |
| 1642 EXPECT_CALL(*download_file, Cancel()); | 1641 EXPECT_CALL(*download_file, Cancel()); |
| 1643 item->DestinationObserverAsWeakPtr()->DestinationError( | 1642 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1644 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, | 1643 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, |
| 1645 std::unique_ptr<crypto::SecureHash>()); | 1644 std::unique_ptr<crypto::SecureHash>()); |
| 1646 RunAllPendingInMessageLoops(); | 1645 RunAllBlockingPoolTasksUntilIdle(); |
| 1647 | 1646 |
| 1648 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 1647 ASSERT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 1649 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 1648 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
| 1650 EXPECT_FALSE(item->CanShowInFolder()); | 1649 EXPECT_FALSE(item->CanShowInFolder()); |
| 1651 EXPECT_TRUE(item->CanOpenDownload()); | 1650 EXPECT_TRUE(item->CanOpenDownload()); |
| 1652 } | 1651 } |
| 1653 | 1652 |
| 1654 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { | 1653 TEST_F(DownloadItemTest, EnabledActionsForCancelledDownload) { |
| 1655 DownloadItemImpl* item = CreateDownloadItem(); | 1654 DownloadItemImpl* item = CreateDownloadItem(); |
| 1656 MockDownloadFile* download_file = | 1655 MockDownloadFile* download_file = |
| 1657 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1656 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 1658 | 1657 |
| 1659 EXPECT_CALL(*download_file, Cancel()); | 1658 EXPECT_CALL(*download_file, Cancel()); |
| 1660 item->Cancel(true); | 1659 item->Cancel(true); |
| 1661 RunAllPendingInMessageLoops(); | 1660 RunAllBlockingPoolTasksUntilIdle(); |
| 1662 | 1661 |
| 1663 ASSERT_EQ(DownloadItem::CANCELLED, item->GetState()); | 1662 ASSERT_EQ(DownloadItem::CANCELLED, item->GetState()); |
| 1664 EXPECT_FALSE(item->CanShowInFolder()); | 1663 EXPECT_FALSE(item->CanShowInFolder()); |
| 1665 EXPECT_FALSE(item->CanOpenDownload()); | 1664 EXPECT_FALSE(item->CanOpenDownload()); |
| 1666 } | 1665 } |
| 1667 | 1666 |
| 1668 // Test various aspects of the delegate completion blocker. | 1667 // Test various aspects of the delegate completion blocker. |
| 1669 | 1668 |
| 1670 // Just allowing completion. | 1669 // Just allowing completion. |
| 1671 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { | 1670 TEST_F(DownloadItemTest, CompleteDelegate_ReturnTrue) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 1686 // Make sure the download can complete. | 1685 // Make sure the download can complete. |
| 1687 EXPECT_CALL(*download_file, | 1686 EXPECT_CALL(*download_file, |
| 1688 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1687 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1689 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1688 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1690 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1689 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1691 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1690 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 1692 .WillOnce(Return(true)); | 1691 .WillOnce(Return(true)); |
| 1693 EXPECT_CALL(*download_file, FullPath()) | 1692 EXPECT_CALL(*download_file, FullPath()) |
| 1694 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1693 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1695 EXPECT_CALL(*download_file, Detach()); | 1694 EXPECT_CALL(*download_file, Detach()); |
| 1696 RunAllPendingInMessageLoops(); | 1695 RunAllBlockingPoolTasksUntilIdle(); |
| 1697 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1696 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1698 } | 1697 } |
| 1699 | 1698 |
| 1700 // Just delaying completion. | 1699 // Just delaying completion. |
| 1701 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { | 1700 TEST_F(DownloadItemTest, CompleteDelegate_BlockOnce) { |
| 1702 // Test to confirm that if we have a callback that returns true, | 1701 // Test to confirm that if we have a callback that returns true, |
| 1703 // we complete immediately. | 1702 // we complete immediately. |
| 1704 DownloadItemImpl* item = CreateDownloadItem(); | 1703 DownloadItemImpl* item = CreateDownloadItem(); |
| 1705 MockDownloadFile* download_file = | 1704 MockDownloadFile* download_file = |
| 1706 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1705 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 1726 // Make sure the download can complete. | 1725 // Make sure the download can complete. |
| 1727 EXPECT_CALL(*download_file, | 1726 EXPECT_CALL(*download_file, |
| 1728 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1727 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1729 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1728 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1730 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1729 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1731 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1730 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 1732 .WillOnce(Return(true)); | 1731 .WillOnce(Return(true)); |
| 1733 EXPECT_CALL(*download_file, FullPath()) | 1732 EXPECT_CALL(*download_file, FullPath()) |
| 1734 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1733 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1735 EXPECT_CALL(*download_file, Detach()); | 1734 EXPECT_CALL(*download_file, Detach()); |
| 1736 RunAllPendingInMessageLoops(); | 1735 RunAllBlockingPoolTasksUntilIdle(); |
| 1737 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1736 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1738 } | 1737 } |
| 1739 | 1738 |
| 1740 // Delay and set danger. | 1739 // Delay and set danger. |
| 1741 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { | 1740 TEST_F(DownloadItemTest, CompleteDelegate_SetDanger) { |
| 1742 // Test to confirm that if we have a callback that returns true, | 1741 // Test to confirm that if we have a callback that returns true, |
| 1743 // we complete immediately. | 1742 // we complete immediately. |
| 1744 DownloadItemImpl* item = CreateDownloadItem(); | 1743 DownloadItemImpl* item = CreateDownloadItem(); |
| 1745 MockDownloadFile* download_file = | 1744 MockDownloadFile* download_file = |
| 1746 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1745 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. | 1768 // Make sure the download doesn't complete until we've validated it. |
| 1770 EXPECT_CALL(*download_file, | 1769 EXPECT_CALL(*download_file, |
| 1771 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1770 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1772 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1771 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1773 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1772 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1774 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1773 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 1775 .WillOnce(Return(true)); | 1774 .WillOnce(Return(true)); |
| 1776 EXPECT_CALL(*download_file, FullPath()) | 1775 EXPECT_CALL(*download_file, FullPath()) |
| 1777 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1776 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1778 EXPECT_CALL(*download_file, Detach()); | 1777 EXPECT_CALL(*download_file, Detach()); |
| 1779 RunAllPendingInMessageLoops(); | 1778 RunAllBlockingPoolTasksUntilIdle(); |
| 1780 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1779 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1781 EXPECT_TRUE(item->IsDangerous()); | 1780 EXPECT_TRUE(item->IsDangerous()); |
| 1782 | 1781 |
| 1783 item->ValidateDangerousDownload(); | 1782 item->ValidateDangerousDownload(); |
| 1784 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); | 1783 EXPECT_EQ(DOWNLOAD_DANGER_TYPE_USER_VALIDATED, item->GetDangerType()); |
| 1785 RunAllPendingInMessageLoops(); | 1784 RunAllBlockingPoolTasksUntilIdle(); |
| 1786 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1785 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1787 } | 1786 } |
| 1788 | 1787 |
| 1789 // Just delaying completion twice. | 1788 // Just delaying completion twice. |
| 1790 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { | 1789 TEST_F(DownloadItemTest, CompleteDelegate_BlockTwice) { |
| 1791 // Test to confirm that if we have a callback that returns true, | 1790 // Test to confirm that if we have a callback that returns true, |
| 1792 // we complete immediately. | 1791 // we complete immediately. |
| 1793 DownloadItemImpl* item = CreateDownloadItem(); | 1792 DownloadItemImpl* item = CreateDownloadItem(); |
| 1794 MockDownloadFile* download_file = | 1793 MockDownloadFile* download_file = |
| 1795 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1794 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 1822 // Make sure the download can complete. | 1821 // Make sure the download can complete. |
| 1823 EXPECT_CALL(*download_file, | 1822 EXPECT_CALL(*download_file, |
| 1824 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) | 1823 RenameAndAnnotate(base::FilePath(kDummyTargetPath), _, _, _, _)) |
| 1825 .WillOnce(ScheduleRenameAndAnnotateCallback( | 1824 .WillOnce(ScheduleRenameAndAnnotateCallback( |
| 1826 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); | 1825 DOWNLOAD_INTERRUPT_REASON_NONE, base::FilePath(kDummyTargetPath))); |
| 1827 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 1826 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 1828 .WillOnce(Return(true)); | 1827 .WillOnce(Return(true)); |
| 1829 EXPECT_CALL(*download_file, FullPath()) | 1828 EXPECT_CALL(*download_file, FullPath()) |
| 1830 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 1829 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 1831 EXPECT_CALL(*download_file, Detach()); | 1830 EXPECT_CALL(*download_file, Detach()); |
| 1832 RunAllPendingInMessageLoops(); | 1831 RunAllBlockingPoolTasksUntilIdle(); |
| 1833 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); | 1832 EXPECT_EQ(DownloadItem::COMPLETE, item->GetState()); |
| 1834 } | 1833 } |
| 1835 | 1834 |
| 1836 TEST_F(DownloadItemTest, StealDangerousDownloadAndDiscard) { | 1835 TEST_F(DownloadItemTest, StealDangerousDownloadAndDiscard) { |
| 1837 DownloadItemImpl* item = CreateDownloadItem(); | 1836 DownloadItemImpl* item = CreateDownloadItem(); |
| 1838 MockDownloadFile* download_file = | 1837 MockDownloadFile* download_file = |
| 1839 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1838 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1840 ASSERT_TRUE(item->IsDangerous()); | 1839 ASSERT_TRUE(item->IsDangerous()); |
| 1841 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); | 1840 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); |
| 1842 base::FilePath returned_path; | 1841 base::FilePath returned_path; |
| 1843 | 1842 |
| 1844 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); | 1843 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); |
| 1845 EXPECT_CALL(*download_file, Detach()); | 1844 EXPECT_CALL(*download_file, Detach()); |
| 1846 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); | 1845 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
| 1847 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); | 1846 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1848 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 1847 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 1849 item->StealDangerousDownload( | 1848 item->StealDangerousDownload( |
| 1850 true, // delete_file_after_feedback | 1849 true, // delete_file_after_feedback |
| 1851 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, | 1850 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1852 weak_ptr_factory.GetWeakPtr(), | 1851 weak_ptr_factory.GetWeakPtr(), |
| 1853 base::Unretained(&returned_path))); | 1852 base::Unretained(&returned_path))); |
| 1854 RunAllPendingInMessageLoops(); | 1853 RunAllBlockingPoolTasksUntilIdle(); |
| 1855 EXPECT_EQ(full_path, returned_path); | 1854 EXPECT_EQ(full_path, returned_path); |
| 1856 } | 1855 } |
| 1857 | 1856 |
| 1858 TEST_F(DownloadItemTest, StealDangerousDownloadAndKeep) { | 1857 TEST_F(DownloadItemTest, StealDangerousDownloadAndKeep) { |
| 1859 DownloadItemImpl* item = CreateDownloadItem(); | 1858 DownloadItemImpl* item = CreateDownloadItem(); |
| 1860 MockDownloadFile* download_file = | 1859 MockDownloadFile* download_file = |
| 1861 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1860 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1862 ASSERT_TRUE(item->IsDangerous()); | 1861 ASSERT_TRUE(item->IsDangerous()); |
| 1863 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); | 1862 base::FilePath full_path(FILE_PATH_LITERAL("foo.txt")); |
| 1864 base::FilePath returned_path; | 1863 base::FilePath returned_path; |
| 1865 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); | 1864 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); |
| 1866 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); | 1865 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1867 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 1866 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 1868 item->StealDangerousDownload( | 1867 item->StealDangerousDownload( |
| 1869 false, // delete_file_after_feedback | 1868 false, // delete_file_after_feedback |
| 1870 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, | 1869 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1871 weak_ptr_factory.GetWeakPtr(), | 1870 weak_ptr_factory.GetWeakPtr(), |
| 1872 base::Unretained(&returned_path))); | 1871 base::Unretained(&returned_path))); |
| 1873 RunAllPendingInMessageLoops(); | 1872 RunAllBlockingPoolTasksUntilIdle(); |
| 1874 EXPECT_NE(full_path, returned_path); | 1873 EXPECT_NE(full_path, returned_path); |
| 1875 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); | 1874 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 1876 } | 1875 } |
| 1877 | 1876 |
| 1878 TEST_F(DownloadItemTest, StealInterruptedContinuableDangerousDownload) { | 1877 TEST_F(DownloadItemTest, StealInterruptedContinuableDangerousDownload) { |
| 1879 base::FilePath returned_path; | 1878 base::FilePath returned_path; |
| 1880 DownloadItemImpl* item = CreateDownloadItem(); | 1879 DownloadItemImpl* item = CreateDownloadItem(); |
| 1881 MockDownloadFile* download_file = | 1880 MockDownloadFile* download_file = |
| 1882 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1881 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1883 base::FilePath full_path = item->GetFullPath(); | 1882 base::FilePath full_path = item->GetFullPath(); |
| 1884 EXPECT_FALSE(full_path.empty()); | 1883 EXPECT_FALSE(full_path.empty()); |
| 1885 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); | 1884 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); |
| 1886 EXPECT_CALL(*download_file, Detach()); | 1885 EXPECT_CALL(*download_file, Detach()); |
| 1887 item->DestinationObserverAsWeakPtr()->DestinationError( | 1886 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1888 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1, | 1887 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1, |
| 1889 std::unique_ptr<crypto::SecureHash>()); | 1888 std::unique_ptr<crypto::SecureHash>()); |
| 1890 ASSERT_TRUE(item->IsDangerous()); | 1889 ASSERT_TRUE(item->IsDangerous()); |
| 1891 | 1890 |
| 1892 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); | 1891 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
| 1893 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); | 1892 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1894 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 1893 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 1895 item->StealDangerousDownload( | 1894 item->StealDangerousDownload( |
| 1896 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, | 1895 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1897 weak_ptr_factory.GetWeakPtr(), | 1896 weak_ptr_factory.GetWeakPtr(), |
| 1898 base::Unretained(&returned_path))); | 1897 base::Unretained(&returned_path))); |
| 1899 RunAllPendingInMessageLoops(); | 1898 RunAllBlockingPoolTasksUntilIdle(); |
| 1900 EXPECT_EQ(full_path, returned_path); | 1899 EXPECT_EQ(full_path, returned_path); |
| 1901 } | 1900 } |
| 1902 | 1901 |
| 1903 TEST_F(DownloadItemTest, StealInterruptedNonContinuableDangerousDownload) { | 1902 TEST_F(DownloadItemTest, StealInterruptedNonContinuableDangerousDownload) { |
| 1904 base::FilePath returned_path; | 1903 base::FilePath returned_path; |
| 1905 DownloadItemImpl* item = CreateDownloadItem(); | 1904 DownloadItemImpl* item = CreateDownloadItem(); |
| 1906 MockDownloadFile* download_file = | 1905 MockDownloadFile* download_file = |
| 1907 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 1906 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 1908 EXPECT_CALL(*download_file, Cancel()); | 1907 EXPECT_CALL(*download_file, Cancel()); |
| 1909 item->DestinationObserverAsWeakPtr()->DestinationError( | 1908 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 1910 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1, | 1909 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1, |
| 1911 std::unique_ptr<crypto::SecureHash>()); | 1910 std::unique_ptr<crypto::SecureHash>()); |
| 1912 ASSERT_TRUE(item->IsDangerous()); | 1911 ASSERT_TRUE(item->IsDangerous()); |
| 1913 | 1912 |
| 1914 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); | 1913 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
| 1915 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); | 1914 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); |
| 1916 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); | 1915 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); |
| 1917 item->StealDangerousDownload( | 1916 item->StealDangerousDownload( |
| 1918 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, | 1917 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, |
| 1919 weak_ptr_factory.GetWeakPtr(), | 1918 weak_ptr_factory.GetWeakPtr(), |
| 1920 base::Unretained(&returned_path))); | 1919 base::Unretained(&returned_path))); |
| 1921 RunAllPendingInMessageLoops(); | 1920 RunAllBlockingPoolTasksUntilIdle(); |
| 1922 EXPECT_TRUE(returned_path.empty()); | 1921 EXPECT_TRUE(returned_path.empty()); |
| 1923 } | 1922 } |
| 1924 | 1923 |
| 1925 namespace { | 1924 namespace { |
| 1926 | 1925 |
| 1927 // The DownloadItemDestinationUpdateRaceTest fixture (defined below) is used to | 1926 // The DownloadItemDestinationUpdateRaceTest fixture (defined below) is used to |
| 1928 // test for race conditions between download destination events received via the | 1927 // test for race conditions between download destination events received via the |
| 1929 // DownloadDestinationObserver interface, and the target determination logic. | 1928 // DownloadDestinationObserver interface, and the target determination logic. |
| 1930 // | 1929 // |
| 1931 // The general control flow for DownloadItemImpl looks like this: | 1930 // 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(_, _, _, _)) | 2167 EXPECT_CALL(*file_, Initialize(_, _, _, _)) |
| 2169 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 2168 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
| 2170 ScheduleClosure(download_start_loop.QuitClosure()))); | 2169 ScheduleClosure(download_start_loop.QuitClosure()))); |
| 2171 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); | 2170 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
| 2172 download_start_loop.Run(); | 2171 download_start_loop.Run(); |
| 2173 | 2172 |
| 2174 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 2173 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
| 2175 item_->DestinationObserverAsWeakPtr(); | 2174 item_->DestinationObserverAsWeakPtr(); |
| 2176 | 2175 |
| 2177 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 2176 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
| 2178 RunAllPendingInMessageLoops(); | 2177 RunAllBlockingPoolTasksUntilIdle(); |
| 2179 | 2178 |
| 2180 base::RunLoop initialize_completion_loop; | 2179 base::RunLoop initialize_completion_loop; |
| 2181 DownloadTargetCallback target_callback; | 2180 DownloadTargetCallback target_callback; |
| 2182 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) | 2181 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) |
| 2183 .WillOnce( | 2182 .WillOnce( |
| 2184 DoAll(SaveArg<1>(&target_callback), | 2183 DoAll(SaveArg<1>(&target_callback), |
| 2185 ScheduleClosure(initialize_completion_loop.QuitClosure()))); | 2184 ScheduleClosure(initialize_completion_loop.QuitClosure()))); |
| 2186 ScheduleObservations(PostInitializeFileObservations(), destination_observer); | 2185 ScheduleObservations(PostInitializeFileObservations(), destination_observer); |
| 2187 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); | 2186 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2188 initialize_completion_loop.Run(); | 2187 initialize_completion_loop.Run(); |
| 2189 | 2188 |
| 2190 RunAllPendingInMessageLoops(); | 2189 RunAllBlockingPoolTasksUntilIdle(); |
| 2191 | 2190 |
| 2192 ASSERT_FALSE(target_callback.is_null()); | 2191 ASSERT_FALSE(target_callback.is_null()); |
| 2193 ScheduleObservations(PostTargetDeterminationObservations(), | 2192 ScheduleObservations(PostTargetDeterminationObservations(), |
| 2194 destination_observer); | 2193 destination_observer); |
| 2195 target_callback.Run(base::FilePath(), | 2194 target_callback.Run(base::FilePath(), |
| 2196 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 2195 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 2197 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath(), | 2196 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath(), |
| 2198 DOWNLOAD_INTERRUPT_REASON_NONE); | 2197 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2199 EXPECT_EQ(DownloadItem::CANCELLED, item_->GetState()); | 2198 EXPECT_EQ(DownloadItem::CANCELLED, item_->GetState()); |
| 2200 RunAllPendingInMessageLoops(); | 2199 RunAllBlockingPoolTasksUntilIdle(); |
| 2201 } | 2200 } |
| 2202 | 2201 |
| 2203 // Run through the DII workflow, but the intermediate rename fails. | 2202 // Run through the DII workflow, but the intermediate rename fails. |
| 2204 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameFails) { | 2203 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameFails) { |
| 2205 // Expect that the download file and the request will be cancelled as a | 2204 // Expect that the download file and the request will be cancelled as a |
| 2206 // result. | 2205 // result. |
| 2207 EXPECT_CALL(*file_, Cancel()); | 2206 EXPECT_CALL(*file_, Cancel()); |
| 2208 EXPECT_CALL(*request_handle_, CancelRequest(_)); | 2207 EXPECT_CALL(*request_handle_, CancelRequest(_)); |
| 2209 | 2208 |
| 2210 // Intermediate rename loop is not used immediately, but let's set up the | 2209 // 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(_, _, _, _)) | 2220 EXPECT_CALL(*file_, Initialize(_, _, _, _)) |
| 2222 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 2221 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
| 2223 ScheduleClosure(download_start_loop.QuitClosure()))); | 2222 ScheduleClosure(download_start_loop.QuitClosure()))); |
| 2224 | 2223 |
| 2225 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); | 2224 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
| 2226 download_start_loop.Run(); | 2225 download_start_loop.Run(); |
| 2227 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 2226 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
| 2228 item_->DestinationObserverAsWeakPtr(); | 2227 item_->DestinationObserverAsWeakPtr(); |
| 2229 | 2228 |
| 2230 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 2229 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
| 2231 RunAllPendingInMessageLoops(); | 2230 RunAllBlockingPoolTasksUntilIdle(); |
| 2232 | 2231 |
| 2233 base::RunLoop initialize_completion_loop; | 2232 base::RunLoop initialize_completion_loop; |
| 2234 DownloadTargetCallback target_callback; | 2233 DownloadTargetCallback target_callback; |
| 2235 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) | 2234 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) |
| 2236 .WillOnce( | 2235 .WillOnce( |
| 2237 DoAll(SaveArg<1>(&target_callback), | 2236 DoAll(SaveArg<1>(&target_callback), |
| 2238 ScheduleClosure(initialize_completion_loop.QuitClosure()))); | 2237 ScheduleClosure(initialize_completion_loop.QuitClosure()))); |
| 2239 ScheduleObservations(PostInitializeFileObservations(), destination_observer); | 2238 ScheduleObservations(PostInitializeFileObservations(), destination_observer); |
| 2240 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); | 2239 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2241 initialize_completion_loop.Run(); | 2240 initialize_completion_loop.Run(); |
| 2242 | 2241 |
| 2243 RunAllPendingInMessageLoops(); | 2242 RunAllBlockingPoolTasksUntilIdle(); |
| 2244 ASSERT_FALSE(target_callback.is_null()); | 2243 ASSERT_FALSE(target_callback.is_null()); |
| 2245 | 2244 |
| 2246 ScheduleObservations(PostTargetDeterminationObservations(), | 2245 ScheduleObservations(PostTargetDeterminationObservations(), |
| 2247 destination_observer); | 2246 destination_observer); |
| 2248 target_callback.Run(base::FilePath(kDummyTargetPath), | 2247 target_callback.Run(base::FilePath(kDummyTargetPath), |
| 2249 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 2248 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 2250 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2249 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 2251 base::FilePath(kDummyIntermediatePath), | 2250 base::FilePath(kDummyIntermediatePath), |
| 2252 DOWNLOAD_INTERRUPT_REASON_NONE); | 2251 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2253 | 2252 |
| 2254 intermediate_rename_loop.Run(); | 2253 intermediate_rename_loop.Run(); |
| 2255 ASSERT_FALSE(intermediate_rename_callback.is_null()); | 2254 ASSERT_FALSE(intermediate_rename_callback.is_null()); |
| 2256 | 2255 |
| 2257 ScheduleObservations(PostIntermediateRenameObservations(), | 2256 ScheduleObservations(PostIntermediateRenameObservations(), |
| 2258 destination_observer); | 2257 destination_observer); |
| 2259 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, | 2258 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, |
| 2260 base::FilePath()); | 2259 base::FilePath()); |
| 2261 RunAllPendingInMessageLoops(); | 2260 RunAllBlockingPoolTasksUntilIdle(); |
| 2262 | 2261 |
| 2263 EXPECT_EQ(DownloadItem::INTERRUPTED, item_->GetState()); | 2262 EXPECT_EQ(DownloadItem::INTERRUPTED, item_->GetState()); |
| 2264 } | 2263 } |
| 2265 | 2264 |
| 2266 // Run through the DII workflow. Download file initialization, target | 2265 // Run through the DII workflow. Download file initialization, target |
| 2267 // determination and intermediate rename all succeed. | 2266 // determination and intermediate rename all succeed. |
| 2268 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameSucceeds) { | 2267 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameSucceeds) { |
| 2269 // We expect either that the download will fail (in which case the request and | 2268 // 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 | 2269 // 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 | 2270 // DownloadFile will Detach()). It depends on the list of observations that |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 2291 EXPECT_CALL(*file_, Initialize(_, _, _, _)) | 2290 EXPECT_CALL(*file_, Initialize(_, _, _, _)) |
| 2292 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), | 2291 .WillOnce(DoAll(SaveArg<0>(&initialize_callback), |
| 2293 ScheduleClosure(download_start_loop.QuitClosure()))); | 2292 ScheduleClosure(download_start_loop.QuitClosure()))); |
| 2294 | 2293 |
| 2295 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); | 2294 item_->Start(std::move(file_), std::move(request_handle_), *create_info()); |
| 2296 download_start_loop.Run(); | 2295 download_start_loop.Run(); |
| 2297 base::WeakPtr<DownloadDestinationObserver> destination_observer = | 2296 base::WeakPtr<DownloadDestinationObserver> destination_observer = |
| 2298 item_->DestinationObserverAsWeakPtr(); | 2297 item_->DestinationObserverAsWeakPtr(); |
| 2299 | 2298 |
| 2300 ScheduleObservations(PreInitializeFileObservations(), destination_observer); | 2299 ScheduleObservations(PreInitializeFileObservations(), destination_observer); |
| 2301 RunAllPendingInMessageLoops(); | 2300 RunAllBlockingPoolTasksUntilIdle(); |
| 2302 | 2301 |
| 2303 base::RunLoop initialize_completion_loop; | 2302 base::RunLoop initialize_completion_loop; |
| 2304 DownloadTargetCallback target_callback; | 2303 DownloadTargetCallback target_callback; |
| 2305 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) | 2304 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)) |
| 2306 .WillOnce( | 2305 .WillOnce( |
| 2307 DoAll(SaveArg<1>(&target_callback), | 2306 DoAll(SaveArg<1>(&target_callback), |
| 2308 ScheduleClosure(initialize_completion_loop.QuitClosure()))); | 2307 ScheduleClosure(initialize_completion_loop.QuitClosure()))); |
| 2309 ScheduleObservations(PostInitializeFileObservations(), destination_observer); | 2308 ScheduleObservations(PostInitializeFileObservations(), destination_observer); |
| 2310 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); | 2309 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2311 initialize_completion_loop.Run(); | 2310 initialize_completion_loop.Run(); |
| 2312 | 2311 |
| 2313 RunAllPendingInMessageLoops(); | 2312 RunAllBlockingPoolTasksUntilIdle(); |
| 2314 ASSERT_FALSE(target_callback.is_null()); | 2313 ASSERT_FALSE(target_callback.is_null()); |
| 2315 | 2314 |
| 2316 ScheduleObservations(PostTargetDeterminationObservations(), | 2315 ScheduleObservations(PostTargetDeterminationObservations(), |
| 2317 destination_observer); | 2316 destination_observer); |
| 2318 target_callback.Run(base::FilePath(kDummyTargetPath), | 2317 target_callback.Run(base::FilePath(kDummyTargetPath), |
| 2319 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 2318 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 2320 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 2319 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 2321 base::FilePath(kDummyIntermediatePath), | 2320 base::FilePath(kDummyIntermediatePath), |
| 2322 DOWNLOAD_INTERRUPT_REASON_NONE); | 2321 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 2323 | 2322 |
| 2324 intermediate_rename_loop.Run(); | 2323 intermediate_rename_loop.Run(); |
| 2325 ASSERT_FALSE(intermediate_rename_callback.is_null()); | 2324 ASSERT_FALSE(intermediate_rename_callback.is_null()); |
| 2326 | 2325 |
| 2327 // This may or may not be called, depending on whether there are any errors in | 2326 // This may or may not be called, depending on whether there are any errors in |
| 2328 // our action list. | 2327 // our action list. |
| 2329 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) | 2328 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) |
| 2330 .Times(::testing::AnyNumber()); | 2329 .Times(::testing::AnyNumber()); |
| 2331 | 2330 |
| 2332 ScheduleObservations(PostIntermediateRenameObservations(), | 2331 ScheduleObservations(PostIntermediateRenameObservations(), |
| 2333 destination_observer); | 2332 destination_observer); |
| 2334 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE, | 2333 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 2335 base::FilePath(kDummyIntermediatePath)); | 2334 base::FilePath(kDummyIntermediatePath)); |
| 2336 RunAllPendingInMessageLoops(); | 2335 RunAllBlockingPoolTasksUntilIdle(); |
| 2337 | 2336 |
| 2338 // The state of the download depends on the observer events that were played | 2337 // 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 | 2338 // 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 | 2339 // 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 | 2340 // were correct. On Release builds, tests are expected to run to completion |
| 2342 // without crashing on success. | 2341 // without crashing on success. |
| 2343 EXPECT_TRUE(item_->GetState() == DownloadItem::IN_PROGRESS || | 2342 EXPECT_TRUE(item_->GetState() == DownloadItem::IN_PROGRESS || |
| 2344 item_->GetState() == DownloadItem::INTERRUPTED); | 2343 item_->GetState() == DownloadItem::INTERRUPTED); |
| 2345 if (item_->GetState() == DownloadItem::INTERRUPTED) | 2344 if (item_->GetState() == DownloadItem::INTERRUPTED) |
| 2346 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item_->GetLastReason()); | 2345 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item_->GetLastReason()); |
| 2347 | 2346 |
| 2348 item_->Cancel(true); | 2347 item_->Cancel(true); |
| 2349 RunAllPendingInMessageLoops(); | 2348 RunAllBlockingPoolTasksUntilIdle(); |
| 2350 } | 2349 } |
| 2351 | 2350 |
| 2352 TEST(MockDownloadItem, Compiles) { | 2351 TEST(MockDownloadItem, Compiles) { |
| 2353 MockDownloadItem mock_item; | 2352 MockDownloadItem mock_item; |
| 2354 } | 2353 } |
| 2355 | 2354 |
| 2356 } // namespace content | 2355 } // namespace content |
| OLD | NEW |