Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: content/browser/download/download_item_impl_unittest.cc

Issue 2453633006: [downloads] Move platform specific code out of DownloadTargetDeterminer. (Closed)
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <iterator> 10 #include <iterator>
10 #include <map> 11 #include <map>
11 #include <memory> 12 #include <memory>
12 #include <queue> 13 #include <queue>
13 #include <utility> 14 #include <utility>
14 15
15 #include "base/callback.h" 16 #include "base/callback.h"
17 #include "base/callback_helpers.h"
16 #include "base/feature_list.h" 18 #include "base/feature_list.h"
17 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
18 #include "base/message_loop/message_loop.h" 20 #include "base/message_loop/message_loop.h"
19 #include "base/run_loop.h" 21 #include "base/run_loop.h"
20 #include "base/threading/thread.h" 22 #include "base/threading/thread.h"
21 #include "content/browser/byte_stream.h" 23 #include "content/browser/byte_stream.h"
22 #include "content/browser/download/download_create_info.h" 24 #include "content/browser/download/download_create_info.h"
23 #include "content/browser/download/download_destination_observer.h" 25 #include "content/browser/download/download_destination_observer.h"
24 #include "content/browser/download/download_file_factory.h" 26 #include "content/browser/download/download_file_factory.h"
25 #include "content/browser/download/download_item_impl_delegate.h" 27 #include "content/browser/download/download_item_impl_delegate.h"
26 #include "content/browser/download/download_request_handle.h" 28 #include "content/browser/download/download_request_handle.h"
27 #include "content/browser/download/mock_download_file.h" 29 #include "content/browser/download/mock_download_file.h"
28 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/download_interrupt_reasons.h" 31 #include "content/public/browser/download_interrupt_reasons.h"
30 #include "content/public/browser/download_url_parameters.h" 32 #include "content/public/browser/download_url_parameters.h"
31 #include "content/public/common/content_features.h" 33 #include "content/public/common/content_features.h"
32 #include "content/public/test/mock_download_item.h" 34 #include "content/public/test/mock_download_item.h"
33 #include "content/public/test/test_browser_context.h" 35 #include "content/public/test/test_browser_context.h"
34 #include "content/public/test/test_browser_thread_bundle.h" 36 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "content/public/test/web_contents_tester.h" 37 #include "content/public/test/web_contents_tester.h"
36 #include "crypto/secure_hash.h" 38 #include "crypto/secure_hash.h"
37 #include "testing/gmock/include/gmock/gmock.h" 39 #include "testing/gmock/include/gmock/gmock.h"
38 #include "testing/gtest/include/gtest/gtest.h" 40 #include "testing/gtest/include/gtest/gtest.h"
39 41
40 using ::testing::DoAll; 42 using ::testing::DoAll;
43 using ::testing::Invoke;
44 using ::testing::InvokeWithoutArgs;
41 using ::testing::NiceMock; 45 using ::testing::NiceMock;
42 using ::testing::Property; 46 using ::testing::Property;
43 using ::testing::Return; 47 using ::testing::Return;
44 using ::testing::ReturnRefOfCopy; 48 using ::testing::ReturnRefOfCopy;
45 using ::testing::SaveArg; 49 using ::testing::SaveArg;
46 using ::testing::StrictMock; 50 using ::testing::StrictMock;
47 using ::testing::WithArg; 51 using ::testing::WithArg;
48 using ::testing::_; 52 using ::testing::_;
49 53
50 const int kDownloadChunkSize = 1000; 54 const int kDownloadChunkSize = 1000;
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 // called. 282 // called.
279 DownloadItemImpl* CreateDownloadItem() { 283 DownloadItemImpl* CreateDownloadItem() {
280 create_info_->download_id = ++next_download_id_; 284 create_info_->download_id = ++next_download_id_;
281 DownloadItemImpl* download = 285 DownloadItemImpl* download =
282 new DownloadItemImpl(&delegate_, create_info_->download_id, 286 new DownloadItemImpl(&delegate_, create_info_->download_id,
283 *create_info_, net::NetLogWithSource()); 287 *create_info_, net::NetLogWithSource());
284 allocated_downloads_[download] = base::WrapUnique(download); 288 allocated_downloads_[download] = base::WrapUnique(download);
285 return download; 289 return download;
286 } 290 }
287 291
288 // Add DownloadFile to DownloadItem. Set |callback| to nullptr if a download 292 // Add DownloadFile to DownloadItem.
289 // target determination is not expected.
290 MockDownloadFile* CallDownloadItemStart( 293 MockDownloadFile* CallDownloadItemStart(
291 DownloadItemImpl* item, 294 DownloadItemImpl* item,
292 DownloadItemImplDelegate::DownloadTargetCallback* callback) { 295 DownloadItemImplDelegate::DownloadTargetCallback* callback) {
293 MockDownloadFile* mock_download_file = nullptr; 296 MockDownloadFile* mock_download_file = nullptr;
294 std::unique_ptr<DownloadFile> download_file; 297 std::unique_ptr<DownloadFile> download_file;
295 if (callback) { 298 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
296 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 299 .WillOnce(SaveArg<1>(callback));
297 .WillOnce(SaveArg<1>(callback));
298 } else {
299 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)).Times(0);
300 }
301 300
302 // Only create a DownloadFile if the request was successful. 301 // Only create a DownloadFile if the request was successful.
303 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) { 302 if (create_info_->result == DOWNLOAD_INTERRUPT_REASON_NONE) {
304 mock_download_file = new StrictMock<MockDownloadFile>; 303 mock_download_file = new StrictMock<MockDownloadFile>;
305 download_file.reset(mock_download_file); 304 download_file.reset(mock_download_file);
306 EXPECT_CALL(*mock_download_file, Initialize(_)) 305 EXPECT_CALL(*mock_download_file, Initialize(_))
307 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE)); 306 .WillOnce(ScheduleCallbackWithParam(DOWNLOAD_INTERRUPT_REASON_NONE));
308 EXPECT_CALL(*mock_download_file, FullPath()) 307 EXPECT_CALL(*mock_download_file, FullPath())
309 .WillRepeatedly(ReturnRefOfCopy(base::FilePath())); 308 .WillRepeatedly(ReturnRefOfCopy(base::FilePath()));
310 } 309 }
311 310
312 std::unique_ptr<MockRequestHandle> request_handle( 311 std::unique_ptr<MockRequestHandle> request_handle =
313 new NiceMock<MockRequestHandle>); 312 base::MakeUnique<NiceMock<MockRequestHandle>>();
314 item->Start(std::move(download_file), std::move(request_handle), 313 item->Start(std::move(download_file), std::move(request_handle),
315 *create_info_); 314 *create_info_);
316 RunAllPendingInMessageLoops(); 315 RunAllPendingInMessageLoops();
317 316
318 // So that we don't have a function writing to a stack variable 317 // So that we don't have a function writing to a stack variable
319 // lying around if the above failed. 318 // lying around if the above failed.
320 mock_delegate()->VerifyAndClearExpectations(); 319 mock_delegate()->VerifyAndClearExpectations();
321 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) 320 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_))
322 .WillRepeatedly(Return()); 321 .WillRepeatedly(Return());
323 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) 322 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_))
(...skipping 12 matching lines...) Expand all
336 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 335 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
337 EXPECT_TRUE(item->GetTargetFilePath().empty()); 336 EXPECT_TRUE(item->GetTargetFilePath().empty());
338 DownloadItemImplDelegate::DownloadTargetCallback callback; 337 DownloadItemImplDelegate::DownloadTargetCallback callback;
339 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 338 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
340 base::FilePath target_path(kDummyTargetPath); 339 base::FilePath target_path(kDummyTargetPath);
341 base::FilePath intermediate_path(kDummyIntermediatePath); 340 base::FilePath intermediate_path(kDummyIntermediatePath);
342 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 341 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
343 .WillOnce(ScheduleRenameAndUniquifyCallback( 342 .WillOnce(ScheduleRenameAndUniquifyCallback(
344 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); 343 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
345 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 344 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
346 danger_type, intermediate_path); 345 danger_type, intermediate_path,
346 DOWNLOAD_INTERRUPT_REASON_NONE);
347 RunAllPendingInMessageLoops(); 347 RunAllPendingInMessageLoops();
348 return download_file; 348 return download_file;
349 } 349 }
350 350
351 void DoDestinationComplete(DownloadItemImpl* item, 351 void DoDestinationComplete(DownloadItemImpl* item,
352 MockDownloadFile* download_file) { 352 MockDownloadFile* download_file) {
353 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) 353 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _))
354 .WillOnce(Return(true)); 354 .WillOnce(Return(true));
355 base::FilePath final_path(kDummyTargetPath); 355 base::FilePath final_path(kDummyTargetPath);
356 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _)) 356 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _, _, _, _))
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 469 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
470 } 470 }
471 471
472 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { 472 TEST_F(DownloadItemTest, NotificationAfterInterrupted) {
473 DownloadItemImpl* item = CreateDownloadItem(); 473 DownloadItemImpl* item = CreateDownloadItem();
474 MockDownloadFile* download_file = 474 MockDownloadFile* download_file =
475 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 475 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
476 EXPECT_CALL(*download_file, Cancel()); 476 EXPECT_CALL(*download_file, Cancel());
477 TestDownloadItemObserver observer(item); 477 TestDownloadItemObserver observer(item);
478 478
479 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) 479 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(0);
480 .Times(0);
481 480
482 item->DestinationObserverAsWeakPtr()->DestinationError( 481 item->DestinationObserverAsWeakPtr()->DestinationError(
483 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, 482 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0,
484 std::unique_ptr<crypto::SecureHash>()); 483 std::unique_ptr<crypto::SecureHash>());
485 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 484 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
486 } 485 }
487 486
488 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { 487 TEST_F(DownloadItemTest, NotificationAfterDestroyed) {
489 DownloadItemImpl* item = CreateDownloadItem(); 488 DownloadItemImpl* item = CreateDownloadItem();
490 TestDownloadItemObserver observer(item); 489 TestDownloadItemObserver observer(item);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); 569 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x"));
571 base::FilePath new_intermediate_path( 570 base::FilePath new_intermediate_path(
572 target_path.InsertBeforeExtensionASCII("y")); 571 target_path.InsertBeforeExtensionASCII("y"));
573 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 572 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
574 .WillOnce(ScheduleRenameAndUniquifyCallback( 573 .WillOnce(ScheduleRenameAndUniquifyCallback(
575 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); 574 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
576 575
577 // Currently, a notification would be generated if the danger type is anything 576 // Currently, a notification would be generated if the danger type is anything
578 // other than NOT_DANGEROUS. 577 // other than NOT_DANGEROUS.
579 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 578 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
580 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 579 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
580 DOWNLOAD_INTERRUPT_REASON_NONE);
581 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 581 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
582 RunAllPendingInMessageLoops(); 582 RunAllPendingInMessageLoops();
583 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 583 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
584 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 584 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
585 585
586 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 586 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
587 } 587 }
588 588
589 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { 589 TEST_F(DownloadItemTest, NotificationAfterTogglePause) {
590 DownloadItemImpl* item = CreateDownloadItem(); 590 DownloadItemImpl* item = CreateDownloadItem();
(...skipping 15 matching lines...) Expand all
606 606
607 item->Resume(); 607 item->Resume();
608 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 608 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
609 609
610 RunAllPendingInMessageLoops(); 610 RunAllPendingInMessageLoops();
611 611
612 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); 612 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
613 } 613 }
614 614
615 // Test that a download is resumed automatcially after a continuable interrupt. 615 // Test that a download is resumed automatcially after a continuable interrupt.
616 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { 616 TEST_F(DownloadItemTest, AutomaticResumption_Continue) {
617 DownloadItemImpl* item = CreateDownloadItem(); 617 DownloadItemImpl* item = CreateDownloadItem();
618 TestDownloadItemObserver observer(item); 618 TestDownloadItemObserver observer(item);
619 MockDownloadFile* download_file = 619 MockDownloadFile* download_file =
620 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 620 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
621 621
622 // Interrupt the download, using a continuable interrupt. 622 // Interrupt the download using a continuable interrupt after writing a single
623 // byte. An intermediate file with data shouldn't be discarding after a
624 // continuable interrupt.
625
626 // The DownloadFile should be detached without discarding.
623 EXPECT_CALL(*download_file, FullPath()) 627 EXPECT_CALL(*download_file, FullPath())
624 .WillOnce(ReturnRefOfCopy(base::FilePath())); 628 .WillOnce(ReturnRefOfCopy(base::FilePath()));
625 EXPECT_CALL(*download_file, Detach()); 629 EXPECT_CALL(*download_file, Detach());
626 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)).Times(1); 630
631 // Resumption attempt should pass the intermediate file along.
632 EXPECT_CALL(*mock_delegate(),
633 MockResumeInterruptedDownload(
634 AllOf(Property(&DownloadUrlParameters::file_path,
635 Property(&base::FilePath::value,
636 kDummyIntermediatePath)),
637 Property(&DownloadUrlParameters::offset, 1)),
638 _));
639
627 item->DestinationObserverAsWeakPtr()->DestinationError( 640 item->DestinationObserverAsWeakPtr()->DestinationError(
628 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, 641 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1,
629 std::unique_ptr<crypto::SecureHash>()); 642 std::unique_ptr<crypto::SecureHash>());
630 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 643 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
631 // Since the download is resumed automatically, the interrupt count doesn't 644 // Since the download is resumed automatically, the interrupt count doesn't
632 // increase. 645 // increase.
633 ASSERT_EQ(0, observer.interrupt_count()); 646 ASSERT_EQ(0, observer.interrupt_count());
634 647
635 // Test expectations verify that ResumeInterruptedDownload() is called (by way 648 // Test expectations verify that ResumeInterruptedDownload() is called (by way
636 // of MockResumeInterruptedDownload) after the download is interrupted. But 649 // of MockResumeInterruptedDownload) after the download is interrupted. But
637 // the mock doesn't follow through with the resumption. 650 // the mock doesn't follow through with the resumption.
638 // ResumeInterruptedDownload() being called is sufficient for verifying that 651 // ResumeInterruptedDownload() being called is sufficient for verifying that
639 // the automatic resumption was triggered. 652 // the automatic resumption was triggered.
640 RunAllPendingInMessageLoops(); 653 RunAllPendingInMessageLoops();
641 654
642 // The download item is currently in RESUMING_INTERNAL state, which maps to 655 // The download item is currently in RESUMING_INTERNAL state, which maps to
643 // IN_PROGRESS. 656 // IN_PROGRESS.
644 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 657 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
645 } 658 }
646 659
647 // Test that automatic resumption doesn't happen after a non-continuable 660 // Automatic resumption should restart and discard the intermediate file if the
648 // interrupt. 661 // interrupt reason requires it.
649 TEST_F(DownloadItemTest, RestartAfterInterrupted) { 662 TEST_F(DownloadItemTest, AutomaticResumption_Restart) {
650 DownloadItemImpl* item = CreateDownloadItem(); 663 DownloadItemImpl* item = CreateDownloadItem();
651 TestDownloadItemObserver observer(item); 664 TestDownloadItemObserver observer(item);
652 MockDownloadFile* download_file = 665 MockDownloadFile* download_file =
653 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 666 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
654 667
655 // Interrupt the download, using a restartable interrupt. 668 // Interrupt the download, using a restartable interrupt.
656 EXPECT_CALL(*download_file, Cancel()); 669 EXPECT_CALL(*download_file, Cancel());
670 EXPECT_EQ(kDummyIntermediatePath, item->GetFullPath().value());
671
672 // Resumption attempt should have discarded intermediate file.
673 EXPECT_CALL(*mock_delegate(),
674 MockResumeInterruptedDownload(
675 Property(&DownloadUrlParameters::file_path,
676 Property(&base::FilePath::empty, true)),
677 _));
678
679 item->DestinationObserverAsWeakPtr()->DestinationError(
680 DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE, 1,
681 std::unique_ptr<crypto::SecureHash>());
682 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
683
684 // Since the download is resumed automatically, the interrupt count doesn't
685 // increase.
686 ASSERT_EQ(0, observer.interrupt_count());
687 RunAllPendingInMessageLoops();
688
689 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
690 }
691
692 // Test that automatic resumption doesn't happen after an interrupt that
693 // requires user action to resolve.
694 TEST_F(DownloadItemTest, AutomaticResumption_NeedsUserAction) {
695 DownloadItemImpl* item = CreateDownloadItem();
696 TestDownloadItemObserver observer(item);
697 MockDownloadFile* download_file =
698 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
699
700 // Interrupt the download, using a restartable interrupt.
701 EXPECT_CALL(*download_file, Cancel());
657 item->DestinationObserverAsWeakPtr()->DestinationError( 702 item->DestinationObserverAsWeakPtr()->DestinationError(
658 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, 703 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1,
659 std::unique_ptr<crypto::SecureHash>()); 704 std::unique_ptr<crypto::SecureHash>());
660 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 705 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
661 // Should not try to auto-resume. 706 // Should not try to auto-resume.
662 ASSERT_EQ(1, observer.interrupt_count()); 707 ASSERT_EQ(1, observer.interrupt_count());
663 ASSERT_EQ(0, observer.resume_count()); 708 ASSERT_EQ(0, observer.resume_count());
664 RunAllPendingInMessageLoops(); 709 RunAllPendingInMessageLoops();
665 710
666 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 711 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
667 } 712 }
668 713
(...skipping 19 matching lines...) Expand all
688 RunAllPendingInMessageLoops(); 733 RunAllPendingInMessageLoops();
689 734
690 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); 735 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated());
691 // Should not try to auto-resume. 736 // Should not try to auto-resume.
692 ASSERT_EQ(1, observer.interrupt_count()); 737 ASSERT_EQ(1, observer.interrupt_count());
693 ASSERT_EQ(0, observer.resume_count()); 738 ASSERT_EQ(0, observer.resume_count());
694 739
695 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 740 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
696 } 741 }
697 742
698 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { 743 TEST_F(DownloadItemTest, AutomaticResumption_AttemptLimit) {
699 DownloadItemImpl* item = CreateDownloadItem(); 744 DownloadItemImpl* item = CreateDownloadItem();
700 base::WeakPtr<DownloadDestinationObserver> as_observer( 745 base::WeakPtr<DownloadDestinationObserver> as_observer(
701 item->DestinationObserverAsWeakPtr()); 746 item->DestinationObserverAsWeakPtr());
702 TestDownloadItemObserver observer(item); 747 TestDownloadItemObserver observer(item);
703 MockDownloadFile* mock_download_file(nullptr); 748 MockDownloadFile* mock_download_file_ref = nullptr;
704 std::unique_ptr<DownloadFile> download_file; 749 std::unique_ptr<MockDownloadFile> mock_download_file;
705 MockRequestHandle* mock_request_handle(nullptr); 750 std::unique_ptr<MockRequestHandle> mock_request_handle;
706 std::unique_ptr<DownloadRequestHandleInterface> request_handle;
707 DownloadItemImplDelegate::DownloadTargetCallback callback; 751 DownloadItemImplDelegate::DownloadTargetCallback callback;
708 752
709 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 753 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
710 .WillRepeatedly(SaveArg<1>(&callback)); 754 .WillRepeatedly(SaveArg<1>(&callback));
711 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)) 755
756 // All attempts at resumption should pass along the intermediate file.
757 EXPECT_CALL(*mock_delegate(),
758 MockResumeInterruptedDownload(
759 AllOf(Property(&DownloadUrlParameters::file_path,
760 Property(&base::FilePath::value,
761 kDummyIntermediatePath)),
762 Property(&DownloadUrlParameters::offset, 1)),
763 _))
712 .Times(DownloadItemImpl::kMaxAutoResumeAttempts); 764 .Times(DownloadItemImpl::kMaxAutoResumeAttempts);
713 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { 765 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) {
714 SCOPED_TRACE(::testing::Message() << "Iteration " << i); 766 SCOPED_TRACE(::testing::Message() << "Iteration " << i);
715 767
716 mock_download_file = new NiceMock<MockDownloadFile>; 768 mock_download_file = base::MakeUnique<NiceMock<MockDownloadFile>>();
717 download_file.reset(mock_download_file); 769 mock_download_file_ref = mock_download_file.get();
718 mock_request_handle = new NiceMock<MockRequestHandle>; 770 mock_request_handle = base::MakeUnique<NiceMock<MockRequestHandle>>();
719 request_handle.reset(mock_request_handle);
720 771
721 ON_CALL(*mock_download_file, FullPath()) 772 ON_CALL(*mock_download_file_ref, FullPath())
722 .WillByDefault(ReturnRefOfCopy(base::FilePath())); 773 .WillByDefault(ReturnRefOfCopy(base::FilePath()));
723 774
724 // Copied key parts of DoIntermediateRename & CallDownloadItemStart 775 // Copied key parts of DoIntermediateRename & CallDownloadItemStart
725 // to allow for holding onto the request handle. 776 // to allow for holding onto the request handle.
726 item->Start(std::move(download_file), std::move(request_handle), 777 item->Start(std::move(mock_download_file), std::move(mock_request_handle),
727 *create_info()); 778 *create_info());
728 RunAllPendingInMessageLoops(); 779 RunAllPendingInMessageLoops();
729 780
730 base::FilePath target_path(kDummyTargetPath); 781 base::FilePath target_path(kDummyTargetPath);
731 base::FilePath intermediate_path(kDummyIntermediatePath); 782 base::FilePath intermediate_path(kDummyIntermediatePath);
732 if (i == 0) { 783
733 // RenameAndUniquify is only called the first time. In all the subsequent 784 // Target of RenameAndUniquify is always the intermediate path.
734 // iterations, the intermediate file already has the correct name, hence 785 ON_CALL(*mock_download_file_ref, RenameAndUniquify(_, _))
735 // no rename is necessary. 786 .WillByDefault(ScheduleRenameAndUniquifyCallback(
736 EXPECT_CALL(*mock_download_file, RenameAndUniquify(intermediate_path, _)) 787 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
737 .WillOnce(ScheduleRenameAndUniquifyCallback( 788
738 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); 789 // RenameAndUniquify is only called the first time. In all the subsequent
739 } 790 // iterations, the intermediate file already has the correct name, hence no
740 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 791 // rename is necessary.
741 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 792 EXPECT_CALL(*mock_download_file_ref, RenameAndUniquify(_, _)).Times(i == 0);
793
794 ASSERT_FALSE(callback.is_null());
795 base::ResetAndReturn(&callback).Run(
796 target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
797 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
798 DOWNLOAD_INTERRUPT_REASON_NONE);
742 RunAllPendingInMessageLoops(); 799 RunAllPendingInMessageLoops();
743 800
744 // Use a continuable interrupt. 801 // Use a continuable interrupt.
802 EXPECT_CALL(*mock_download_file_ref, Cancel()).Times(0);
745 item->DestinationObserverAsWeakPtr()->DestinationError( 803 item->DestinationObserverAsWeakPtr()->DestinationError(
746 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, 804 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1,
747 std::unique_ptr<crypto::SecureHash>()); 805 std::unique_ptr<crypto::SecureHash>());
748 806
749 RunAllPendingInMessageLoops(); 807 RunAllPendingInMessageLoops();
750 ::testing::Mock::VerifyAndClearExpectations(mock_download_file); 808 ::testing::Mock::VerifyAndClearExpectations(mock_download_file_ref);
751 } 809 }
752 810
753 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 811 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
754 EXPECT_EQ(1, observer.interrupt_count()); 812 EXPECT_EQ(1, observer.interrupt_count());
755 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED); 813 CleanupItem(item, nullptr, DownloadItem::INTERRUPTED);
756 } 814 }
757 815
758 // If the download attempts to resume and the resumption request fails, the 816 // If the download attempts to resume and the resumption request fails, the
759 // subsequent Start() call shouldn't update the origin state (URL redirect 817 // subsequent Start() call shouldn't update the origin state (URL redirect
760 // chains, Content-Disposition, download URL, etc..) 818 // chains, Content-Disposition, download URL, etc..)
(...skipping 11 matching lines...) Expand all
772 830
773 DownloadItemImpl* item = CreateDownloadItem(); 831 DownloadItemImpl* item = CreateDownloadItem();
774 MockDownloadFile* download_file = 832 MockDownloadFile* download_file =
775 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 833 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
776 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); 834 EXPECT_EQ(kContentDisposition, item->GetContentDisposition());
777 EXPECT_EQ(kFirstETag, item->GetETag()); 835 EXPECT_EQ(kFirstETag, item->GetETag());
778 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); 836 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
779 EXPECT_EQ(kFirstURL, item->GetURL().spec()); 837 EXPECT_EQ(kFirstURL, item->GetURL().spec());
780 EXPECT_EQ(kMimeType, item->GetMimeType()); 838 EXPECT_EQ(kMimeType, item->GetMimeType());
781 839
782 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_, _)); 840 EXPECT_CALL(*mock_delegate(),
841 MockResumeInterruptedDownload(
842 AllOf(Property(&DownloadUrlParameters::file_path,
843 Property(&base::FilePath::value,
844 kDummyIntermediatePath)),
845 Property(&DownloadUrlParameters::offset, 1)),
846 _));
783 EXPECT_CALL(*download_file, Detach()); 847 EXPECT_CALL(*download_file, Detach());
784 item->DestinationObserverAsWeakPtr()->DestinationError( 848 item->DestinationObserverAsWeakPtr()->DestinationError(
785 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, 849 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1,
786 std::unique_ptr<crypto::SecureHash>()); 850 std::unique_ptr<crypto::SecureHash>());
787 RunAllPendingInMessageLoops(); 851 RunAllPendingInMessageLoops();
788 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 852 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
789 853
790 // Now change the create info. The changes should not cause the DownloadItem 854 // Now change the create info. The changes should not cause the DownloadItem
791 // to be updated. 855 // to be updated.
792 const char kSecondContentDisposition[] = "attachment; filename=bar"; 856 const char kSecondContentDisposition[] = "attachment; filename=bar";
793 const char kSecondETag[] = "123"; 857 const char kSecondETag[] = "123";
794 const char kSecondLastModified[] = "Today"; 858 const char kSecondLastModified[] = "Today";
795 const char kSecondURL[] = "http://example.com/another-download"; 859 const char kSecondURL[] = "http://example.com/another-download";
796 const char kSecondMimeType[] = "text/html"; 860 const char kSecondMimeType[] = "text/html";
797 create_info()->content_disposition = kSecondContentDisposition; 861 create_info()->content_disposition = kSecondContentDisposition;
798 create_info()->etag = kSecondETag; 862 create_info()->etag = kSecondETag;
799 create_info()->last_modified = kSecondLastModified; 863 create_info()->last_modified = kSecondLastModified;
800 create_info()->url_chain.clear(); 864 create_info()->url_chain.clear();
801 create_info()->url_chain.push_back(GURL(kSecondURL)); 865 create_info()->url_chain.push_back(GURL(kSecondURL));
802 create_info()->mime_type = kSecondMimeType; 866 create_info()->mime_type = kSecondMimeType;
803 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; 867 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED;
868 create_info()->save_info->file_path = base::FilePath(kDummyIntermediatePath);
869 create_info()->save_info->offset = 1;
804 870
805 // The following ends up calling DownloadItem::Start(), but shouldn't result 871 // Calling Start() with a response indicating failure shouldn't cause a target
806 // in an origin update. 872 // update, nor should it result in discarding the intermediate file.
807 download_file = CallDownloadItemStart(item, nullptr); 873 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
874 download_file = CallDownloadItemStart(item, &target_callback);
875 ASSERT_FALSE(target_callback.is_null());
876 target_callback.Run(base::FilePath(kDummyTargetPath),
877 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
878 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
879 base::FilePath(kDummyIntermediatePath),
880 DOWNLOAD_INTERRUPT_REASON_NONE);
881 RunAllPendingInMessageLoops();
808 882
809 EXPECT_EQ(kContentDisposition, item->GetContentDisposition()); 883 EXPECT_EQ(kContentDisposition, item->GetContentDisposition());
810 EXPECT_EQ(kFirstETag, item->GetETag()); 884 EXPECT_EQ(kFirstETag, item->GetETag());
811 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime()); 885 EXPECT_EQ(kFirstLastModified, item->GetLastModifiedTime());
812 EXPECT_EQ(kFirstURL, item->GetURL().spec()); 886 EXPECT_EQ(kFirstURL, item->GetURL().spec());
813 EXPECT_EQ(kMimeType, item->GetMimeType()); 887 EXPECT_EQ(kMimeType, item->GetMimeType());
814 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 888 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
815 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); 889 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason());
890 EXPECT_EQ(kDummyIntermediatePath, item->GetFullPath().value());
891 EXPECT_EQ(1, item->GetReceivedBytes());
816 } 892 }
817 893
818 // If the download resumption request succeeds, the origin state should be 894 // If the download resumption request succeeds, the origin state should be
819 // updated 895 // updated
820 TEST_F(DownloadItemTest, SucceededResumptionUpdatesOriginState) { 896 TEST_F(DownloadItemTest, SucceededResumptionUpdatesOriginState) {
821 const char kContentDisposition[] = "attachment; filename=foo"; 897 const char kContentDisposition[] = "attachment; filename=foo";
822 const char kFirstETag[] = "ABC"; 898 const char kFirstETag[] = "ABC";
823 const char kFirstLastModified[] = "Yesterday"; 899 const char kFirstLastModified[] = "Yesterday";
824 const char kFirstURL[] = "http://www.example.com/download"; 900 const char kFirstURL[] = "http://www.example.com/download";
825 const char kMimeType[] = "text/css"; 901 const char kMimeType[] = "text/css";
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 EXPECT_EQ(kSecondContentDisposition, item->GetContentDisposition()); 936 EXPECT_EQ(kSecondContentDisposition, item->GetContentDisposition());
861 EXPECT_EQ(kSecondETag, item->GetETag()); 937 EXPECT_EQ(kSecondETag, item->GetETag());
862 EXPECT_EQ(kSecondLastModified, item->GetLastModifiedTime()); 938 EXPECT_EQ(kSecondLastModified, item->GetLastModifiedTime());
863 EXPECT_EQ(kSecondURL, item->GetURL().spec()); 939 EXPECT_EQ(kSecondURL, item->GetURL().spec());
864 EXPECT_EQ(kSecondMimeType, item->GetMimeType()); 940 EXPECT_EQ(kSecondMimeType, item->GetMimeType());
865 941
866 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 942 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
867 } 943 }
868 944
869 // Test that resumption uses the final URL in a URL chain when resuming. 945 // Test that resumption uses the final URL in a URL chain when resuming.
870 TEST_F(DownloadItemTest, ResumeUsingFinalURL) { 946 TEST_F(DownloadItemTest, ResumeUsesFinalURL) {
871 create_info()->save_info->prompt_for_save_location = false; 947 create_info()->save_info->prompt_for_save_location = false;
872 create_info()->url_chain.clear(); 948 create_info()->url_chain.clear();
873 create_info()->url_chain.push_back(GURL("http://example.com/a")); 949 create_info()->url_chain.push_back(GURL("http://example.com/a"));
874 create_info()->url_chain.push_back(GURL("http://example.com/b")); 950 create_info()->url_chain.push_back(GURL("http://example.com/b"));
875 create_info()->url_chain.push_back(GURL("http://example.com/c")); 951 create_info()->url_chain.push_back(GURL("http://example.com/c"));
876 952
877 DownloadItemImpl* item = CreateDownloadItem(); 953 DownloadItemImpl* item = CreateDownloadItem();
878 TestDownloadItemObserver observer(item); 954 TestDownloadItemObserver observer(item);
879 MockDownloadFile* download_file = 955 MockDownloadFile* download_file =
880 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 956 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
881 957
882 // Interrupt the download, using a continuable interrupt. 958 // Interrupt the download, using a continuable interrupt.
883 EXPECT_CALL(*download_file, FullPath()) 959 EXPECT_CALL(*download_file, FullPath())
884 .WillOnce(ReturnRefOfCopy(base::FilePath())); 960 .WillOnce(ReturnRefOfCopy(base::FilePath()));
885 EXPECT_CALL(*download_file, Detach()); 961 EXPECT_CALL(*download_file, Detach());
886 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload( 962 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(
887 Property(&DownloadUrlParameters::url, 963 Property(&DownloadUrlParameters::url,
888 GURL("http://example.com/c")), 964 GURL("http://example.com/c")),
889 _)) 965 _))
890 .Times(1); 966 .Times(1);
891 item->DestinationObserverAsWeakPtr()->DestinationError( 967 item->DestinationObserverAsWeakPtr()->DestinationError(
892 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 0, 968 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR, 1,
893 std::unique_ptr<crypto::SecureHash>()); 969 std::unique_ptr<crypto::SecureHash>());
894 970
895 // Test expectations verify that ResumeInterruptedDownload() is called (by way 971 // Test expectations verify that ResumeInterruptedDownload() is called (by way
896 // of MockResumeInterruptedDownload) after the download is interrupted. But 972 // of MockResumeInterruptedDownload) after the download is interrupted. But
897 // the mock doesn't follow through with the resumption. 973 // the mock doesn't follow through with the resumption.
898 // ResumeInterruptedDownload() being called is sufficient for verifying that 974 // ResumeInterruptedDownload() being called is sufficient for verifying that
899 // the resumption was triggered. 975 // the resumption was triggered.
900 RunAllPendingInMessageLoops(); 976 RunAllPendingInMessageLoops();
901 977
902 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS. 978 // The download is currently in RESUMING_INTERNAL, which maps to IN_PROGRESS.
903 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS); 979 CleanupItem(item, nullptr, DownloadItem::IN_PROGRESS);
904 } 980 }
905 981
906 TEST_F(DownloadItemTest, DisplayName) { 982 TEST_F(DownloadItemTest, DisplayName) {
907 DownloadItemImpl* item = CreateDownloadItem(); 983 DownloadItemImpl* item = CreateDownloadItem();
908 DownloadItemImplDelegate::DownloadTargetCallback callback; 984 DownloadItemImplDelegate::DownloadTargetCallback callback;
909 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 985 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
910 base::FilePath target_path( 986 base::FilePath target_path(
911 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 987 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
912 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); 988 base::FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x"));
913 EXPECT_EQ(FILE_PATH_LITERAL(""), 989 EXPECT_EQ(FILE_PATH_LITERAL(""),
914 item->GetFileNameToReportUser().value()); 990 item->GetFileNameToReportUser().value());
915 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) 991 EXPECT_CALL(*download_file, RenameAndUniquify(_, _))
916 .WillOnce(ScheduleRenameAndUniquifyCallback( 992 .WillOnce(ScheduleRenameAndUniquifyCallback(
917 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path)); 993 DOWNLOAD_INTERRUPT_REASON_NONE, intermediate_path));
918 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 994 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
919 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 995 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
996 DOWNLOAD_INTERRUPT_REASON_NONE);
920 RunAllPendingInMessageLoops(); 997 RunAllPendingInMessageLoops();
921 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), 998 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"),
922 item->GetFileNameToReportUser().value()); 999 item->GetFileNameToReportUser().value());
923 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name"))); 1000 item->SetDisplayName(base::FilePath(FILE_PATH_LITERAL("new.name")));
924 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), 1001 EXPECT_EQ(FILE_PATH_LITERAL("new.name"),
925 item->GetFileNameToReportUser().value()); 1002 item->GetFileNameToReportUser().value());
926 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 1003 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
927 } 1004 }
928 1005
929 // Test to make sure that Start method calls DF initialize properly. 1006 // Test to make sure that Start method calls DF initialize properly.
930 TEST_F(DownloadItemTest, Start) { 1007 TEST_F(DownloadItemTest, Start) {
931 MockDownloadFile* mock_download_file(new MockDownloadFile); 1008 MockDownloadFile* mock_download_file(new MockDownloadFile);
932 std::unique_ptr<DownloadFile> download_file(mock_download_file); 1009 std::unique_ptr<DownloadFile> download_file(mock_download_file);
933 DownloadItemImpl* item = CreateDownloadItem(); 1010 DownloadItemImpl* item = CreateDownloadItem();
934 EXPECT_CALL(*mock_download_file, Initialize(_)); 1011 EXPECT_CALL(*mock_download_file, Initialize(_));
935 std::unique_ptr<DownloadRequestHandleInterface> request_handle( 1012 std::unique_ptr<DownloadRequestHandleInterface> request_handle(
936 new NiceMock<MockRequestHandle>); 1013 new NiceMock<MockRequestHandle>);
937 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); 1014 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _));
938 item->Start(std::move(download_file), std::move(request_handle), 1015 item->Start(std::move(download_file), std::move(request_handle),
939 *create_info()); 1016 *create_info());
940 RunAllPendingInMessageLoops(); 1017 RunAllPendingInMessageLoops();
941 1018
942 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); 1019 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
943 } 1020 }
944 1021
945 // Download file and the request should be cancelled as a result of download 1022 // Download file and the request should be cancelled as a result of download
946 // file initialization failing. 1023 // file initialization failing.
947 TEST_F(DownloadItemTest, InitDownloadFileFails) { 1024 TEST_F(DownloadItemTest, InitDownloadFileFails) {
948 DownloadItemImpl* item = CreateDownloadItem(); 1025 DownloadItemImpl* item = CreateDownloadItem();
949 std::unique_ptr<MockDownloadFile> file(new MockDownloadFile()); 1026 std::unique_ptr<MockDownloadFile> file = base::MakeUnique<MockDownloadFile>();
950 std::unique_ptr<MockRequestHandle> request_handle(new MockRequestHandle()); 1027 std::unique_ptr<MockRequestHandle> request_handle =
1028 base::MakeUnique<MockRequestHandle>();
1029
951 EXPECT_CALL(*file, Cancel()); 1030 EXPECT_CALL(*file, Cancel());
952 EXPECT_CALL(*request_handle, CancelRequest()); 1031 EXPECT_CALL(*request_handle, CancelRequest());
953 EXPECT_CALL(*file, Initialize(_)) 1032 EXPECT_CALL(*file, Initialize(_))
954 .WillOnce(ScheduleCallbackWithParam( 1033 .WillOnce(ScheduleCallbackWithParam(
955 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED)); 1034 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED));
956 1035
957 base::RunLoop start_download_loop; 1036 base::RunLoop start_download_loop;
958 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback; 1037 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback;
959 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 1038 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
960 .WillOnce(DoAll(SaveArg<1>(&download_target_callback), 1039 .WillOnce(DoAll(SaveArg<1>(&download_target_callback),
961 ScheduleClosure(start_download_loop.QuitClosure()))); 1040 ScheduleClosure(start_download_loop.QuitClosure())));
962 1041
963 item->Start(std::move(file), std::move(request_handle), *create_info()); 1042 item->Start(std::move(file), std::move(request_handle), *create_info());
964 start_download_loop.Run(); 1043 start_download_loop.Run();
965 1044
966 download_target_callback.Run(base::FilePath(kDummyTargetPath), 1045 download_target_callback.Run(base::FilePath(kDummyTargetPath),
967 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1046 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
968 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 1047 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
969 base::FilePath(kDummyIntermediatePath)); 1048 base::FilePath(kDummyIntermediatePath),
1049 DOWNLOAD_INTERRUPT_REASON_NONE);
970 RunAllPendingInMessageLoops(); 1050 RunAllPendingInMessageLoops();
971 1051
972 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1052 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
973 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, 1053 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED,
974 item->GetLastReason()); 1054 item->GetLastReason());
1055 EXPECT_FALSE(item->GetTargetFilePath().empty());
1056 EXPECT_TRUE(item->GetFullPath().empty());
975 } 1057 }
976 1058
977 // Handling of downloads initiated via a failed request. In this case, Start() 1059 // Handling of downloads initiated via a failed request. In this case, Start()
978 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason. 1060 // will get called with a DownloadCreateInfo with a non-zero interrupt_reason.
979 TEST_F(DownloadItemTest, StartFailedDownload) { 1061 TEST_F(DownloadItemTest, StartFailedDownload) {
980 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED; 1062 create_info()->result = DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED;
981 DownloadItemImpl* item = CreateDownloadItem(); 1063 DownloadItemImpl* item = CreateDownloadItem();
982 1064
983 // DownloadFile and DownloadRequestHandleInterface objects aren't created for 1065 // DownloadFile and DownloadRequestHandleInterface objects aren't created for
984 // failed downloads. 1066 // failed downloads.
985 std::unique_ptr<DownloadFile> null_download_file; 1067 std::unique_ptr<DownloadFile> null_download_file;
986 std::unique_ptr<DownloadRequestHandleInterface> null_request_handle; 1068 std::unique_ptr<DownloadRequestHandleInterface> null_request_handle;
987 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback; 1069 DownloadItemImplDelegate::DownloadTargetCallback download_target_callback;
988 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 1070 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
989 .WillOnce(SaveArg<1>(&download_target_callback)); 1071 .WillOnce(SaveArg<1>(&download_target_callback));
990 item->Start(std::move(null_download_file), std::move(null_request_handle), 1072 item->Start(std::move(null_download_file), std::move(null_request_handle),
991 *create_info()); 1073 *create_info());
992 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1074 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
993 RunAllPendingInMessageLoops(); 1075 RunAllPendingInMessageLoops();
994 1076
995 // The DownloadItemImpl should attempt to determine a target path even if the 1077 // The DownloadItemImpl should attempt to determine a target path even if the
996 // download was interrupted. 1078 // download was interrupted.
997 ASSERT_FALSE(download_target_callback.is_null()); 1079 ASSERT_FALSE(download_target_callback.is_null());
998 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1080 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
999 base::FilePath target_path(FILE_PATH_LITERAL("foo")); 1081 base::FilePath target_path(FILE_PATH_LITERAL("foo"));
1000 download_target_callback.Run(target_path, 1082 download_target_callback.Run(target_path,
1001 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1083 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1002 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path); 1084 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path,
1085 DOWNLOAD_INTERRUPT_REASON_NONE);
1003 RunAllPendingInMessageLoops(); 1086 RunAllPendingInMessageLoops();
1004 1087
1005 EXPECT_EQ(target_path, item->GetTargetFilePath()); 1088 EXPECT_EQ(target_path, item->GetTargetFilePath());
1006 CleanupItem(item, NULL, DownloadItem::INTERRUPTED); 1089 CleanupItem(item, NULL, DownloadItem::INTERRUPTED);
1007 } 1090 }
1008 1091
1009 // Test that the delegate is invoked after the download file is renamed. 1092 // Test that the delegate is invoked after the download file is renamed.
1010 TEST_F(DownloadItemTest, CallbackAfterRename) { 1093 TEST_F(DownloadItemTest, CallbackAfterRename) {
1011 DownloadItemImpl* item = CreateDownloadItem(); 1094 DownloadItemImpl* item = CreateDownloadItem();
1012 DownloadItemImplDelegate::DownloadTargetCallback callback; 1095 DownloadItemImplDelegate::DownloadTargetCallback callback;
1013 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1096 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1014 base::FilePath final_path( 1097 base::FilePath final_path(
1015 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1098 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
1016 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1099 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1017 base::FilePath new_intermediate_path( 1100 base::FilePath new_intermediate_path(
1018 final_path.InsertBeforeExtensionASCII("y")); 1101 final_path.InsertBeforeExtensionASCII("y"));
1019 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1102 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1020 .WillOnce(ScheduleRenameAndUniquifyCallback( 1103 .WillOnce(ScheduleRenameAndUniquifyCallback(
1021 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); 1104 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
1022 1105
1023 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1106 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1024 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1107 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1108 DOWNLOAD_INTERRUPT_REASON_NONE);
1025 RunAllPendingInMessageLoops(); 1109 RunAllPendingInMessageLoops();
1026 // All the callbacks should have happened by now. 1110 // All the callbacks should have happened by now.
1027 ::testing::Mock::VerifyAndClearExpectations(download_file); 1111 ::testing::Mock::VerifyAndClearExpectations(download_file);
1028 mock_delegate()->VerifyAndClearExpectations(); 1112 mock_delegate()->VerifyAndClearExpectations();
1029 1113
1030 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _)) 1114 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(item, _))
1031 .WillOnce(Return(true)); 1115 .WillOnce(Return(true));
1032 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _)) 1116 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _, _, _, _))
1033 .WillOnce(ScheduleRenameAndAnnotateCallback( 1117 .WillOnce(ScheduleRenameAndAnnotateCallback(
1034 DOWNLOAD_INTERRUPT_REASON_NONE, final_path)); 1118 DOWNLOAD_INTERRUPT_REASON_NONE, final_path));
(...skipping 18 matching lines...) Expand all
1053 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1137 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1054 base::FilePath new_intermediate_path( 1138 base::FilePath new_intermediate_path(
1055 final_path.InsertBeforeExtensionASCII("y")); 1139 final_path.InsertBeforeExtensionASCII("y"));
1056 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1140 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1057 .WillOnce(ScheduleRenameAndUniquifyCallback( 1141 .WillOnce(ScheduleRenameAndUniquifyCallback(
1058 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); 1142 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path));
1059 EXPECT_CALL(*download_file, Cancel()) 1143 EXPECT_CALL(*download_file, Cancel())
1060 .Times(1); 1144 .Times(1);
1061 1145
1062 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1146 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1063 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1147 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1148 DOWNLOAD_INTERRUPT_REASON_NONE);
1064 RunAllPendingInMessageLoops(); 1149 RunAllPendingInMessageLoops();
1065 // All the callbacks should have happened by now. 1150 // All the callbacks should have happened by now.
1066 ::testing::Mock::VerifyAndClearExpectations(download_file); 1151 ::testing::Mock::VerifyAndClearExpectations(download_file);
1067 mock_delegate()->VerifyAndClearExpectations(); 1152 mock_delegate()->VerifyAndClearExpectations();
1068 } 1153 }
1069 1154
1070 TEST_F(DownloadItemTest, Interrupted) { 1155 TEST_F(DownloadItemTest, Interrupted) {
1071 DownloadItemImpl* item = CreateDownloadItem(); 1156 DownloadItemImpl* item = CreateDownloadItem();
1072 MockDownloadFile* download_file = 1157 MockDownloadFile* download_file =
1073 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1158 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1190 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1106 base::FilePath new_intermediate_path( 1191 base::FilePath new_intermediate_path(
1107 final_path.InsertBeforeExtensionASCII("y")); 1192 final_path.InsertBeforeExtensionASCII("y"));
1108 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1193 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1109 .WillOnce(ScheduleRenameAndUniquifyCallback( 1194 .WillOnce(ScheduleRenameAndUniquifyCallback(
1110 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); 1195 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
1111 EXPECT_CALL(*download_file, Cancel()) 1196 EXPECT_CALL(*download_file, Cancel())
1112 .Times(1); 1197 .Times(1);
1113 1198
1114 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1199 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1115 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1200 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1201 DOWNLOAD_INTERRUPT_REASON_NONE);
1116 RunAllPendingInMessageLoops(); 1202 RunAllPendingInMessageLoops();
1117 // All the callbacks should have happened by now. 1203 // All the callbacks should have happened by now.
1118 ::testing::Mock::VerifyAndClearExpectations(download_file); 1204 ::testing::Mock::VerifyAndClearExpectations(download_file);
1119 mock_delegate()->VerifyAndClearExpectations(); 1205 mock_delegate()->VerifyAndClearExpectations();
1120 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1206 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1121 EXPECT_TRUE(item->GetFullPath().empty()); 1207 EXPECT_TRUE(item->GetFullPath().empty());
1122 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1208 EXPECT_EQ(final_path, item->GetTargetFilePath());
1123 } 1209 }
1124 1210
1125 // As above. But if the download can be resumed by continuing, then the 1211 // As above. But if the download can be resumed by continuing, then the
1126 // intermediate path should be retained when the download is interrupted after 1212 // intermediate path should be retained when the download is interrupted after
1127 // the intermediate rename succeeds. 1213 // the intermediate rename succeeds.
1128 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Continue) { 1214 TEST_F(DownloadItemTest, InterruptedBeforeIntermediateRename_Continue) {
1129 DownloadItemImpl* item = CreateDownloadItem(); 1215 DownloadItemImpl* item = CreateDownloadItem();
1130 DownloadItemImplDelegate::DownloadTargetCallback callback; 1216 DownloadItemImplDelegate::DownloadTargetCallback callback;
1131 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback); 1217 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1218
1219 // Write some data and interrupt with NETWORK_FAILED. The download shouldn't
1220 // transition to INTERRUPTED until the destination callback has been invoked.
1132 item->DestinationObserverAsWeakPtr()->DestinationError( 1221 item->DestinationObserverAsWeakPtr()->DestinationError(
1133 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 0, 1222 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1,
1134 std::unique_ptr<crypto::SecureHash>()); 1223 std::unique_ptr<crypto::SecureHash>());
1135 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1224 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1136 1225
1137 base::FilePath final_path( 1226 base::FilePath final_path(
1138 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar")); 1227 base::FilePath(kDummyTargetPath).AppendASCII("foo.bar"));
1139 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1228 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1140 base::FilePath new_intermediate_path( 1229 base::FilePath new_intermediate_path(
1141 final_path.InsertBeforeExtensionASCII("y")); 1230 final_path.InsertBeforeExtensionASCII("y"));
1142 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1231 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1143 .WillOnce(ScheduleRenameAndUniquifyCallback( 1232 .WillOnce(ScheduleRenameAndUniquifyCallback(
1144 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path)); 1233 DOWNLOAD_INTERRUPT_REASON_NONE, new_intermediate_path));
1145 EXPECT_CALL(*download_file, FullPath()) 1234 EXPECT_CALL(*download_file, FullPath())
1146 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path))); 1235 .WillOnce(ReturnRefOfCopy(base::FilePath(new_intermediate_path)));
1147 EXPECT_CALL(*download_file, Detach()); 1236 EXPECT_CALL(*download_file, Detach());
1148 1237
1149 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1238 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1150 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1239 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1240 DOWNLOAD_INTERRUPT_REASON_NONE);
1151 RunAllPendingInMessageLoops(); 1241 RunAllPendingInMessageLoops();
1152 // All the callbacks should have happened by now. 1242 // All the callbacks should have happened by now.
1153 ::testing::Mock::VerifyAndClearExpectations(download_file); 1243 ::testing::Mock::VerifyAndClearExpectations(download_file);
1154 mock_delegate()->VerifyAndClearExpectations(); 1244 mock_delegate()->VerifyAndClearExpectations();
1155 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1245 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1156 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 1246 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
1157 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1247 EXPECT_EQ(final_path, item->GetTargetFilePath());
1158 } 1248 }
1159 1249
1160 // As above. If the intermediate rename fails, then the interrupt reason should 1250 // As above. If the intermediate rename fails, then the interrupt reason should
(...skipping 12 matching lines...) Expand all
1173 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 1263 base::FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
1174 base::FilePath new_intermediate_path( 1264 base::FilePath new_intermediate_path(
1175 final_path.InsertBeforeExtensionASCII("y")); 1265 final_path.InsertBeforeExtensionASCII("y"));
1176 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 1266 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
1177 .WillOnce(ScheduleRenameAndUniquifyCallback( 1267 .WillOnce(ScheduleRenameAndUniquifyCallback(
1178 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path)); 1268 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, new_intermediate_path));
1179 EXPECT_CALL(*download_file, Cancel()) 1269 EXPECT_CALL(*download_file, Cancel())
1180 .Times(1); 1270 .Times(1);
1181 1271
1182 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1272 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1183 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 1273 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path,
1274 DOWNLOAD_INTERRUPT_REASON_NONE);
1184 RunAllPendingInMessageLoops(); 1275 RunAllPendingInMessageLoops();
1185 // All the callbacks should have happened by now. 1276 // All the callbacks should have happened by now.
1186 ::testing::Mock::VerifyAndClearExpectations(download_file); 1277 ::testing::Mock::VerifyAndClearExpectations(download_file);
1187 mock_delegate()->VerifyAndClearExpectations(); 1278 mock_delegate()->VerifyAndClearExpectations();
1188 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1279 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1189 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason()); 1280 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason());
1190 EXPECT_TRUE(item->GetFullPath().empty()); 1281 EXPECT_TRUE(item->GetFullPath().empty());
1191 EXPECT_EQ(final_path, item->GetTargetFilePath()); 1282 EXPECT_EQ(final_path, item->GetTargetFilePath());
1192 } 1283 }
1193 1284
1194 TEST_F(DownloadItemTest, Canceled) { 1285 TEST_F(DownloadItemTest, Canceled) {
1195 DownloadItemImpl* item = CreateDownloadItem(); 1286 DownloadItemImpl* item = CreateDownloadItem();
1196 DownloadItemImplDelegate::DownloadTargetCallback target_callback; 1287 DownloadItemImplDelegate::DownloadTargetCallback target_callback;
1197 MockDownloadFile* download_file = 1288 MockDownloadFile* download_file =
1198 CallDownloadItemStart(item, &target_callback); 1289 CallDownloadItemStart(item, &target_callback);
1199 1290
1200 // Confirm cancel sets state properly. 1291 // Confirm cancel sets state properly.
1201 EXPECT_CALL(*download_file, Cancel()); 1292 EXPECT_CALL(*download_file, Cancel());
1202 item->Cancel(true); 1293 item->Cancel(true);
1203 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); 1294 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
1204 } 1295 }
1205 1296
1297 TEST_F(DownloadItemTest, DownloadTargetDetermined_Cancel) {
1298 DownloadItemImpl* item = CreateDownloadItem();
1299 DownloadItemImplDelegate::DownloadTargetCallback callback;
1300 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1301
1302 EXPECT_CALL(*download_file, Cancel());
1303 callback.Run(base::FilePath(FILE_PATH_LITERAL("foo")),
1304 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1305 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1306 base::FilePath(FILE_PATH_LITERAL("bar")),
1307 DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
1308 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
1309 }
1310
1311 TEST_F(DownloadItemTest, DownloadTargetDetermined_CancelWithEmptyName) {
1312 DownloadItemImpl* item = CreateDownloadItem();
1313 DownloadItemImplDelegate::DownloadTargetCallback callback;
1314 MockDownloadFile* download_file = CallDownloadItemStart(item, &callback);
1315
1316 EXPECT_CALL(*download_file, Cancel());
1317 callback.Run(base::FilePath(), DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1318 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath(),
1319 DOWNLOAD_INTERRUPT_REASON_NONE);
1320 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
1321 }
1322
1206 TEST_F(DownloadItemTest, FileRemoved) { 1323 TEST_F(DownloadItemTest, FileRemoved) {
1207 DownloadItemImpl* item = CreateDownloadItem(); 1324 DownloadItemImpl* item = CreateDownloadItem();
1208 1325
1209 EXPECT_FALSE(item->GetFileExternallyRemoved()); 1326 EXPECT_FALSE(item->GetFileExternallyRemoved());
1210 item->OnDownloadedFileRemoved(); 1327 item->OnDownloadedFileRemoved();
1211 EXPECT_TRUE(item->GetFileExternallyRemoved()); 1328 EXPECT_TRUE(item->GetFileExternallyRemoved());
1212 } 1329 }
1213 1330
1214 TEST_F(DownloadItemTest, DestinationUpdate) { 1331 TEST_F(DownloadItemTest, DestinationUpdate) {
1215 DownloadItemImpl* item = CreateDownloadItem(); 1332 DownloadItemImpl* item = CreateDownloadItem();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1256 1373
1257 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1374 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1258 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); 1375 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason());
1259 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 1376 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
1260 1377
1261 std::unique_ptr<crypto::SecureHash> hash( 1378 std::unique_ptr<crypto::SecureHash> hash(
1262 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); 1379 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
1263 hash->Update(kTestData1, sizeof(kTestData1)); 1380 hash->Update(kTestData1, sizeof(kTestData1));
1264 1381
1265 EXPECT_CALL(*download_file, Detach()); 1382 EXPECT_CALL(*download_file, Detach());
1266 as_observer->DestinationError( 1383 as_observer->DestinationError(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1,
1267 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 0, std::move(hash)); 1384 std::move(hash));
1268 mock_delegate()->VerifyAndClearExpectations(); 1385 mock_delegate()->VerifyAndClearExpectations();
1269 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1386 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1270 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1387 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1271 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason()); 1388 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, item->GetLastReason());
1272 EXPECT_EQ( 1389 EXPECT_EQ(
1273 std::string(std::begin(kHashOfTestData1), std::end(kHashOfTestData1)), 1390 std::string(std::begin(kHashOfTestData1), std::end(kHashOfTestData1)),
1274 item->GetHash()); 1391 item->GetHash());
1275 } 1392 }
1393
1276 TEST_F(DownloadItemTest, DestinationError_RestartRequired) { 1394 TEST_F(DownloadItemTest, DestinationError_RestartRequired) {
1277 DownloadItemImpl* item = CreateDownloadItem(); 1395 DownloadItemImpl* item = CreateDownloadItem();
1278 MockDownloadFile* download_file = 1396 MockDownloadFile* download_file =
1279 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 1397 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
1280 base::WeakPtr<DownloadDestinationObserver> as_observer( 1398 base::WeakPtr<DownloadDestinationObserver> as_observer(
1281 item->DestinationObserverAsWeakPtr()); 1399 item->DestinationObserverAsWeakPtr());
1282 TestDownloadItemObserver observer(item); 1400 TestDownloadItemObserver observer(item);
1283 1401
1284 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 1402 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState());
1285 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); 1403 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason());
1286 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); 1404 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated());
1287 1405
1288 std::unique_ptr<crypto::SecureHash> hash( 1406 std::unique_ptr<crypto::SecureHash> hash(
1289 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); 1407 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
1290 hash->Update(kTestData1, sizeof(kTestData1)); 1408 hash->Update(kTestData1, sizeof(kTestData1));
1291 1409
1292 EXPECT_CALL(*download_file, Cancel()); 1410 EXPECT_CALL(*download_file, Cancel());
1293 as_observer->DestinationError( 1411 as_observer->DestinationError(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1,
1294 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, std::move(hash)); 1412 std::move(hash));
1295 mock_delegate()->VerifyAndClearExpectations(); 1413 mock_delegate()->VerifyAndClearExpectations();
1296 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); 1414 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated());
1297 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 1415 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
1298 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason()); 1416 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, item->GetLastReason());
1299 EXPECT_EQ(std::string(), item->GetHash()); 1417 EXPECT_EQ(std::string(), item->GetHash());
1300 } 1418 }
1301 1419
1302 TEST_F(DownloadItemTest, DestinationCompleted) { 1420 TEST_F(DownloadItemTest, DestinationCompleted) {
1303 DownloadItemImpl* item = CreateDownloadItem(); 1421 DownloadItemImpl* item = CreateDownloadItem();
1304 MockDownloadFile* download_file = 1422 MockDownloadFile* download_file =
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
1636 item->StealDangerousDownload( 1754 item->StealDangerousDownload(
1637 false, // delete_file_after_feedback 1755 false, // delete_file_after_feedback
1638 base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1756 base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1639 weak_ptr_factory.GetWeakPtr(), 1757 weak_ptr_factory.GetWeakPtr(),
1640 base::Unretained(&returned_path))); 1758 base::Unretained(&returned_path)));
1641 RunAllPendingInMessageLoops(); 1759 RunAllPendingInMessageLoops();
1642 EXPECT_NE(full_path, returned_path); 1760 EXPECT_NE(full_path, returned_path);
1643 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); 1761 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
1644 } 1762 }
1645 1763
1646 TEST_F(DownloadItemTest, StealInterruptedDangerousDownload) { 1764 TEST_F(DownloadItemTest, StealInterruptedContinuableDangerousDownload) {
1647 base::FilePath returned_path; 1765 base::FilePath returned_path;
1648 DownloadItemImpl* item = CreateDownloadItem(); 1766 DownloadItemImpl* item = CreateDownloadItem();
1649 MockDownloadFile* download_file = 1767 MockDownloadFile* download_file =
1650 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1768 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1651 base::FilePath full_path = item->GetFullPath(); 1769 base::FilePath full_path = item->GetFullPath();
1652 EXPECT_FALSE(full_path.empty()); 1770 EXPECT_FALSE(full_path.empty());
1653 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path)); 1771 EXPECT_CALL(*download_file, FullPath()).WillOnce(ReturnRefOfCopy(full_path));
1654 EXPECT_CALL(*download_file, Detach()); 1772 EXPECT_CALL(*download_file, Detach());
1655 item->DestinationObserverAsWeakPtr()->DestinationError( 1773 item->DestinationObserverAsWeakPtr()->DestinationError(
1656 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 0, 1774 DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, 1,
1657 std::unique_ptr<crypto::SecureHash>()); 1775 std::unique_ptr<crypto::SecureHash>());
1658 ASSERT_TRUE(item->IsDangerous()); 1776 ASSERT_TRUE(item->IsDangerous());
1659 1777
1660 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1778 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1661 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1779 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1662 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); 1780 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>());
1663 item->StealDangerousDownload( 1781 item->StealDangerousDownload(
1664 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1782 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1665 weak_ptr_factory.GetWeakPtr(), 1783 weak_ptr_factory.GetWeakPtr(),
1666 base::Unretained(&returned_path))); 1784 base::Unretained(&returned_path)));
1667 RunAllPendingInMessageLoops(); 1785 RunAllPendingInMessageLoops();
1668 EXPECT_EQ(full_path, returned_path); 1786 EXPECT_EQ(full_path, returned_path);
1669 } 1787 }
1670 1788
1671 TEST_F(DownloadItemTest, StealInterruptedNonResumableDangerousDownload) { 1789 TEST_F(DownloadItemTest, StealInterruptedNonContinuableDangerousDownload) {
1672 base::FilePath returned_path; 1790 base::FilePath returned_path;
1673 DownloadItemImpl* item = CreateDownloadItem(); 1791 DownloadItemImpl* item = CreateDownloadItem();
1674 MockDownloadFile* download_file = 1792 MockDownloadFile* download_file =
1675 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 1793 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
1676 EXPECT_CALL(*download_file, Cancel()); 1794 EXPECT_CALL(*download_file, Cancel());
1677 item->DestinationObserverAsWeakPtr()->DestinationError( 1795 item->DestinationObserverAsWeakPtr()->DestinationError(
1678 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 0, 1796 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 1,
1679 std::unique_ptr<crypto::SecureHash>()); 1797 std::unique_ptr<crypto::SecureHash>());
1680 ASSERT_TRUE(item->IsDangerous()); 1798 ASSERT_TRUE(item->IsDangerous());
1681 1799
1682 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); 1800 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_));
1683 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this); 1801 base::WeakPtrFactory<DownloadItemTest> weak_ptr_factory(this);
1684 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>()); 1802 item->OnAllDataSaved(0, std::unique_ptr<crypto::SecureHash>());
1685 item->StealDangerousDownload( 1803 item->StealDangerousDownload(
1686 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired, 1804 true, base::Bind(&DownloadItemTest::OnDownloadFileAcquired,
1687 weak_ptr_factory.GetWeakPtr(), 1805 weak_ptr_factory.GetWeakPtr(),
1688 base::Unretained(&returned_path))); 1806 base::Unretained(&returned_path)));
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1955 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE); 2073 initialize_callback.Run(DOWNLOAD_INTERRUPT_REASON_NONE);
1956 initialize_completion_loop.Run(); 2074 initialize_completion_loop.Run();
1957 2075
1958 RunAllPendingInMessageLoops(); 2076 RunAllPendingInMessageLoops();
1959 2077
1960 ASSERT_FALSE(target_callback.is_null()); 2078 ASSERT_FALSE(target_callback.is_null());
1961 ScheduleObservations(PostTargetDeterminationObservations(), 2079 ScheduleObservations(PostTargetDeterminationObservations(),
1962 destination_observer); 2080 destination_observer);
1963 target_callback.Run(base::FilePath(), 2081 target_callback.Run(base::FilePath(),
1964 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 2082 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1965 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath()); 2083 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, base::FilePath(),
2084 DOWNLOAD_INTERRUPT_REASON_NONE);
1966 EXPECT_EQ(DownloadItem::CANCELLED, item_->GetState()); 2085 EXPECT_EQ(DownloadItem::CANCELLED, item_->GetState());
1967 RunAllPendingInMessageLoops(); 2086 RunAllPendingInMessageLoops();
1968 } 2087 }
1969 2088
1970 // Run through the DII workflow, but the intermediate rename fails. 2089 // Run through the DII workflow, but the intermediate rename fails.
1971 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameFails) { 2090 TEST_P(DownloadItemDestinationUpdateRaceTest, IntermediateRenameFails) {
1972 // Expect that the download file and the request will be cancelled as a 2091 // Expect that the download file and the request will be cancelled as a
1973 // result. 2092 // result.
1974 EXPECT_CALL(*file_, Cancel()); 2093 EXPECT_CALL(*file_, Cancel());
1975 EXPECT_CALL(*request_handle_, CancelRequest()); 2094 EXPECT_CALL(*request_handle_, CancelRequest());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2008 initialize_completion_loop.Run(); 2127 initialize_completion_loop.Run();
2009 2128
2010 RunAllPendingInMessageLoops(); 2129 RunAllPendingInMessageLoops();
2011 ASSERT_FALSE(target_callback.is_null()); 2130 ASSERT_FALSE(target_callback.is_null());
2012 2131
2013 ScheduleObservations(PostTargetDeterminationObservations(), 2132 ScheduleObservations(PostTargetDeterminationObservations(),
2014 destination_observer); 2133 destination_observer);
2015 target_callback.Run(base::FilePath(kDummyTargetPath), 2134 target_callback.Run(base::FilePath(kDummyTargetPath),
2016 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 2135 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2017 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 2136 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2018 base::FilePath(kDummyIntermediatePath)); 2137 base::FilePath(kDummyIntermediatePath),
2138 DOWNLOAD_INTERRUPT_REASON_NONE);
2019 2139
2020 intermediate_rename_loop.Run(); 2140 intermediate_rename_loop.Run();
2021 ASSERT_FALSE(intermediate_rename_callback.is_null()); 2141 ASSERT_FALSE(intermediate_rename_callback.is_null());
2022 2142
2023 ScheduleObservations(PostIntermediateRenameObservations(), 2143 ScheduleObservations(PostIntermediateRenameObservations(),
2024 destination_observer); 2144 destination_observer);
2025 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, 2145 intermediate_rename_callback.Run(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED,
2026 base::FilePath()); 2146 base::FilePath());
2027 RunAllPendingInMessageLoops(); 2147 RunAllPendingInMessageLoops();
2028 2148
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
2077 initialize_completion_loop.Run(); 2197 initialize_completion_loop.Run();
2078 2198
2079 RunAllPendingInMessageLoops(); 2199 RunAllPendingInMessageLoops();
2080 ASSERT_FALSE(target_callback.is_null()); 2200 ASSERT_FALSE(target_callback.is_null());
2081 2201
2082 ScheduleObservations(PostTargetDeterminationObservations(), 2202 ScheduleObservations(PostTargetDeterminationObservations(),
2083 destination_observer); 2203 destination_observer);
2084 target_callback.Run(base::FilePath(kDummyTargetPath), 2204 target_callback.Run(base::FilePath(kDummyTargetPath),
2085 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 2205 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2086 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 2206 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2087 base::FilePath(kDummyIntermediatePath)); 2207 base::FilePath(kDummyIntermediatePath),
2208 DOWNLOAD_INTERRUPT_REASON_NONE);
2088 2209
2089 intermediate_rename_loop.Run(); 2210 intermediate_rename_loop.Run();
2090 ASSERT_FALSE(intermediate_rename_callback.is_null()); 2211 ASSERT_FALSE(intermediate_rename_callback.is_null());
2091 2212
2092 // This may or may not be called, depending on whether there are any errors in 2213 // This may or may not be called, depending on whether there are any errors in
2093 // our action list. 2214 // our action list.
2094 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _)) 2215 EXPECT_CALL(*mock_delegate(), ShouldCompleteDownload(_, _))
2095 .Times(::testing::AnyNumber()); 2216 .Times(::testing::AnyNumber());
2096 2217
2097 ScheduleObservations(PostIntermediateRenameObservations(), 2218 ScheduleObservations(PostIntermediateRenameObservations(),
(...skipping 14 matching lines...) Expand all
2112 2233
2113 item_->Cancel(true); 2234 item_->Cancel(true);
2114 RunAllPendingInMessageLoops(); 2235 RunAllPendingInMessageLoops();
2115 } 2236 }
2116 2237
2117 TEST(MockDownloadItem, Compiles) { 2238 TEST(MockDownloadItem, Compiles) {
2118 MockDownloadItem mock_item; 2239 MockDownloadItem mock_item;
2119 } 2240 }
2120 2241
2121 } // namespace content 2242 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl_delegate.cc ('k') | content/browser/download/download_manager_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698