| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
| 8 #include "content/browser/download/byte_stream.h" | 8 #include "content/browser/download/byte_stream.h" |
| 9 #include "content/browser/download/download_create_info.h" | 9 #include "content/browser/download/download_create_info.h" |
| 10 #include "content/browser/download/download_file_factory.h" | 10 #include "content/browser/download/download_file_factory.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 using ::testing::SaveArg; | 27 using ::testing::SaveArg; |
| 28 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 31 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 class MockDelegate : public DownloadItemImplDelegate { | 34 class MockDelegate : public DownloadItemImplDelegate { |
| 35 public: | 35 public: |
| 36 MOCK_METHOD2(DetermineDownloadTarget, void( | 36 MOCK_METHOD2(DetermineDownloadTarget, void( |
| 37 DownloadItemImpl* download, const DownloadTargetCallback& callback)); | 37 DownloadItemImpl* download, const DownloadTargetCallback&)); |
| 38 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download)); | 38 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl*)); |
| 39 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); | 39 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); |
| 40 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); | 40 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); |
| 41 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); | 41 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); |
| 42 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl* download)); | 42 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); |
| 43 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); | 43 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl*)); |
| 44 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); | 44 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl*)); |
| 45 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); | 45 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); |
| 46 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); | 46 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); |
| 47 MOCK_METHOD1(DownloadRenamedToIntermediateName, | 47 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); |
| 48 void(DownloadItemImpl* download)); | 48 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); |
| 49 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); | |
| 50 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); | |
| 51 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE { | |
| 52 return file_manager_; | |
| 53 } | |
| 54 private: | |
| 55 DownloadFileManager* file_manager_; | |
| 56 }; | 49 }; |
| 57 | 50 |
| 58 class MockRequestHandle : public DownloadRequestHandleInterface { | 51 class MockRequestHandle : public DownloadRequestHandleInterface { |
| 59 public: | 52 public: |
| 60 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); | 53 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); |
| 61 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 54 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
| 62 MOCK_CONST_METHOD0(PauseRequest, void()); | 55 MOCK_CONST_METHOD0(PauseRequest, void()); |
| 63 MOCK_CONST_METHOD0(ResumeRequest, void()); | 56 MOCK_CONST_METHOD0(ResumeRequest, void()); |
| 64 MOCK_CONST_METHOD0(CancelRequest, void()); | 57 MOCK_CONST_METHOD0(CancelRequest, void()); |
| 65 MOCK_CONST_METHOD0(DebugString, std::string()); | 58 MOCK_CONST_METHOD0(DebugString, std::string()); |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 MockDownloadFile* mock_download_file(new MockDownloadFile); | 445 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 453 scoped_ptr<DownloadFile> download_file(mock_download_file); | 446 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 454 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 447 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 455 EXPECT_CALL(*mock_download_file, Initialize(_)); | 448 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 456 item->Start(download_file.Pass()); | 449 item->Start(download_file.Pass()); |
| 457 | 450 |
| 458 CleanupItem(item, mock_download_file); | 451 CleanupItem(item, mock_download_file); |
| 459 } | 452 } |
| 460 | 453 |
| 461 // Test that the delegate is invoked after the download file is renamed. | 454 // Test that the delegate is invoked after the download file is renamed. |
| 462 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the | |
| 463 // download is renamed to the intermediate name. | |
| 464 // Delegate::DownloadRenamedToFinalName() should be invoked after the final | |
| 465 // rename. | |
| 466 TEST_F(DownloadItemTest, CallbackAfterRename) { | 455 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 467 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 456 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 468 DownloadItemImplDelegate::DownloadTargetCallback callback; | 457 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 469 MockDownloadFile* download_file = | 458 MockDownloadFile* download_file = |
| 470 AddDownloadFileToDownloadItem(item, &callback); | 459 AddDownloadFileToDownloadItem(item, &callback); |
| 471 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 460 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 472 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 461 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 473 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 462 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
| 474 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _)) | 463 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _)) |
| 475 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 464 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
| 465 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) |
| 466 .Times(1); |
| 476 | 467 |
| 477 // DownloadItemImpl should invoke this callback on the delegate once the | |
| 478 // download is renamed to the intermediate name. Also check that GetFullPath() | |
| 479 // returns the intermediate path at the time of the call. | |
| 480 EXPECT_CALL(*mock_delegate(), | |
| 481 DownloadRenamedToIntermediateName( | |
| 482 AllOf(item, | |
| 483 Property(&DownloadItem::GetFullPath, | |
| 484 new_intermediate_path)))); | |
| 485 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 468 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 486 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 469 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 487 RunAllPendingInMessageLoops(); | 470 RunAllPendingInMessageLoops(); |
| 488 // All the callbacks should have happened by now. | 471 // All the callbacks should have happened by now. |
| 489 ::testing::Mock::VerifyAndClearExpectations(download_file); | 472 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 490 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 473 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 491 | 474 |
| 492 item->OnAllDataSaved(""); | 475 item->OnAllDataSaved(""); |
| 493 EXPECT_CALL(*download_file, Rename(final_path, true, _)) | 476 EXPECT_CALL(*download_file, Rename(final_path, true, _)) |
| 494 .WillOnce(ScheduleRenameCallback(final_path)); | 477 .WillOnce(ScheduleRenameCallback(final_path)); |
| 495 // DownloadItemImpl should invoke this callback on the delegate after the | |
| 496 // final rename has completed. Also check that GetFullPath() and | |
| 497 // GetTargetFilePath() return the final path at the time of the call. | |
| 498 EXPECT_CALL(*mock_delegate(), | |
| 499 DownloadRenamedToFinalName( | |
| 500 AllOf(item, | |
| 501 Property(&DownloadItem::GetFullPath, final_path), | |
| 502 Property(&DownloadItem::GetTargetFilePath, | |
| 503 final_path)))); | |
| 504 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); | 478 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); |
| 505 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) | 479 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) |
| 506 .WillOnce(Return(true)); | 480 .WillOnce(Return(true)); |
| 507 EXPECT_CALL(*download_file, Detach(_)) | 481 EXPECT_CALL(*download_file, Detach(_)) |
| 508 .WillOnce(ScheduleDetachCallback()); | 482 .WillOnce(ScheduleDetachCallback()); |
| 509 item->SetIsPersisted(); | |
| 510 item->MaybeCompleteDownload(); | 483 item->MaybeCompleteDownload(); |
| 511 RunAllPendingInMessageLoops(); | 484 RunAllPendingInMessageLoops(); |
| 512 ::testing::Mock::VerifyAndClearExpectations(download_file); | 485 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 513 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 486 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 514 } | 487 } |
| 515 | 488 |
| 516 TEST_F(DownloadItemTest, Interrupted) { | 489 TEST_F(DownloadItemTest, Interrupted) { |
| 517 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 490 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 518 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 491 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 519 | 492 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 EXPECT_EQ("livebeef", item->GetHash()); | 603 EXPECT_EQ("livebeef", item->GetHash()); |
| 631 EXPECT_EQ("", item->GetHashState()); | 604 EXPECT_EQ("", item->GetHashState()); |
| 632 EXPECT_TRUE(item->AllDataSaved()); | 605 EXPECT_TRUE(item->AllDataSaved()); |
| 633 } | 606 } |
| 634 | 607 |
| 635 TEST(MockDownloadItem, Compiles) { | 608 TEST(MockDownloadItem, Compiles) { |
| 636 MockDownloadItem mock_item; | 609 MockDownloadItem mock_item; |
| 637 } | 610 } |
| 638 | 611 |
| 639 } // namespace content | 612 } // namespace content |
| OLD | NEW |