| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 57 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
| 58 MOCK_CONST_METHOD0(PauseRequest, void()); | 58 MOCK_CONST_METHOD0(PauseRequest, void()); |
| 59 MOCK_CONST_METHOD0(ResumeRequest, void()); | 59 MOCK_CONST_METHOD0(ResumeRequest, void()); |
| 60 MOCK_CONST_METHOD0(CancelRequest, void()); | 60 MOCK_CONST_METHOD0(CancelRequest, void()); |
| 61 MOCK_CONST_METHOD0(DebugString, std::string()); | 61 MOCK_CONST_METHOD0(DebugString, std::string()); |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on | 64 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on |
| 65 // the UI thread. Should only be used as the action for | 65 // the UI thread. Should only be used as the action for |
| 66 // MockDownloadFile::Rename as follows: | 66 // MockDownloadFile::Rename as follows: |
| 67 // EXPECT_CALL(download_file, Rename(_,_,_)) | 67 // EXPECT_CALL(download_file, Rename*(_,_)) |
| 68 // .WillOnce(ScheduleRenameCallback(new_path)); | 68 // .WillOnce(ScheduleRenameCallback(new_path)); |
| 69 ACTION_P(ScheduleRenameCallback, new_path) { | 69 ACTION_P(ScheduleRenameCallback, new_path) { |
| 70 BrowserThread::PostTask( | 70 BrowserThread::PostTask( |
| 71 BrowserThread::UI, FROM_HERE, | 71 BrowserThread::UI, FROM_HERE, |
| 72 base::Bind(arg2, DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); | 72 base::Bind(arg1, DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); |
| 73 } | |
| 74 | |
| 75 // Schedules a task to invoke the input closure on | |
| 76 // the UI thread. Should only be used as the action for | |
| 77 // MockDownloadFile::Detach as follows: | |
| 78 // EXPECT_CALL(download_file, Detach(_)) | |
| 79 // .WillOnce(ScheduleDetachCallback())); | |
| 80 ACTION(ScheduleDetachCallback) { | |
| 81 BrowserThread::PostTask( | |
| 82 BrowserThread::UI, FROM_HERE, | |
| 83 base::Bind(arg0, DOWNLOAD_INTERRUPT_REASON_NONE)); | |
| 84 } | 73 } |
| 85 | 74 |
| 86 } // namespace | 75 } // namespace |
| 87 | 76 |
| 88 class DownloadItemTest : public testing::Test { | 77 class DownloadItemTest : public testing::Test { |
| 89 public: | 78 public: |
| 90 class MockObserver : public DownloadItem::Observer { | 79 class MockObserver : public DownloadItem::Observer { |
| 91 public: | 80 public: |
| 92 explicit MockObserver(DownloadItem* item) | 81 explicit MockObserver(DownloadItem* item) |
| 93 : item_(item), | 82 : item_(item), |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 // not before. | 378 // not before. |
| 390 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { | 379 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { |
| 391 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 380 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 392 DownloadItemImplDelegate::DownloadTargetCallback callback; | 381 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 393 MockDownloadFile* download_file = | 382 MockDownloadFile* download_file = |
| 394 AddDownloadFileToDownloadItem(item, &callback); | 383 AddDownloadFileToDownloadItem(item, &callback); |
| 395 MockObserver observer(item); | 384 MockObserver observer(item); |
| 396 FilePath target_path(kDummyPath); | 385 FilePath target_path(kDummyPath); |
| 397 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 386 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 398 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); | 387 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); |
| 399 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _)) | 388 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 400 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 389 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
| 401 | 390 |
| 402 // Currently, a notification would be generated if the danger type is anything | 391 // Currently, a notification would be generated if the danger type is anything |
| 403 // other than NOT_DANGEROUS. | 392 // other than NOT_DANGEROUS. |
| 404 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 393 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 405 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 394 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 406 EXPECT_FALSE(observer.CheckUpdated()); | 395 EXPECT_FALSE(observer.CheckUpdated()); |
| 407 RunAllPendingInMessageLoops(); | 396 RunAllPendingInMessageLoops(); |
| 408 EXPECT_TRUE(observer.CheckUpdated()); | 397 EXPECT_TRUE(observer.CheckUpdated()); |
| 409 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 398 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 424 | 413 |
| 425 TEST_F(DownloadItemTest, DisplayName) { | 414 TEST_F(DownloadItemTest, DisplayName) { |
| 426 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 415 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 427 DownloadItemImplDelegate::DownloadTargetCallback callback; | 416 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 428 MockDownloadFile* download_file = | 417 MockDownloadFile* download_file = |
| 429 AddDownloadFileToDownloadItem(item, &callback); | 418 AddDownloadFileToDownloadItem(item, &callback); |
| 430 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 419 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 431 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 420 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 432 EXPECT_EQ(FILE_PATH_LITERAL(""), | 421 EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 433 item->GetFileNameToReportUser().value()); | 422 item->GetFileNameToReportUser().value()); |
| 434 EXPECT_CALL(*download_file, Rename(_, false, _)) | 423 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) |
| 435 .WillOnce(ScheduleRenameCallback(intermediate_path)); | 424 .WillOnce(ScheduleRenameCallback(intermediate_path)); |
| 436 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 425 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 437 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 426 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 438 RunAllPendingInMessageLoops(); | 427 RunAllPendingInMessageLoops(); |
| 439 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 428 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
| 440 item->GetFileNameToReportUser().value()); | 429 item->GetFileNameToReportUser().value()); |
| 441 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 430 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
| 442 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 431 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
| 443 item->GetFileNameToReportUser().value()); | 432 item->GetFileNameToReportUser().value()); |
| 444 CleanupItem(item, download_file); | 433 CleanupItem(item, download_file); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 461 // Delegate::DownloadRenamedToFinalName() should be invoked after the final | 450 // Delegate::DownloadRenamedToFinalName() should be invoked after the final |
| 462 // rename. | 451 // rename. |
| 463 TEST_F(DownloadItemTest, CallbackAfterRename) { | 452 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 464 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 453 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 465 DownloadItemImplDelegate::DownloadTargetCallback callback; | 454 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 466 MockDownloadFile* download_file = | 455 MockDownloadFile* download_file = |
| 467 AddDownloadFileToDownloadItem(item, &callback); | 456 AddDownloadFileToDownloadItem(item, &callback); |
| 468 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 457 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 469 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 458 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 470 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 459 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
| 471 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _)) | 460 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
| 472 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 461 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
| 473 | 462 |
| 474 // DownloadItemImpl should invoke this callback on the delegate once the | 463 // DownloadItemImpl should invoke this callback on the delegate once the |
| 475 // download is renamed to the intermediate name. Also check that GetFullPath() | 464 // download is renamed to the intermediate name. Also check that GetFullPath() |
| 476 // returns the intermediate path at the time of the call. | 465 // returns the intermediate path at the time of the call. |
| 477 EXPECT_CALL(*mock_delegate(), | 466 EXPECT_CALL(*mock_delegate(), |
| 478 DownloadRenamedToIntermediateName( | 467 DownloadRenamedToIntermediateName( |
| 479 AllOf(item, | 468 AllOf(item, |
| 480 Property(&DownloadItem::GetFullPath, | 469 Property(&DownloadItem::GetFullPath, |
| 481 new_intermediate_path)))); | 470 new_intermediate_path)))); |
| 482 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 471 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 483 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 472 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 484 RunAllPendingInMessageLoops(); | 473 RunAllPendingInMessageLoops(); |
| 485 // All the callbacks should have happened by now. | 474 // All the callbacks should have happened by now. |
| 486 ::testing::Mock::VerifyAndClearExpectations(download_file); | 475 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 487 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 476 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 488 | 477 |
| 489 item->OnAllDataSaved(""); | 478 item->OnAllDataSaved(""); |
| 490 EXPECT_CALL(*download_file, Rename(final_path, true, _)) | 479 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) |
| 491 .WillOnce(ScheduleRenameCallback(final_path)); | 480 .WillOnce(ScheduleRenameCallback(final_path)); |
| 492 // DownloadItemImpl should invoke this callback on the delegate after the | 481 // DownloadItemImpl should invoke this callback on the delegate after the |
| 493 // final rename has completed. Also check that GetFullPath() and | 482 // final rename has completed. Also check that GetFullPath() and |
| 494 // GetTargetFilePath() return the final path at the time of the call. | 483 // GetTargetFilePath() return the final path at the time of the call. |
| 495 EXPECT_CALL(*mock_delegate(), | 484 EXPECT_CALL(*mock_delegate(), |
| 496 DownloadRenamedToFinalName( | 485 DownloadRenamedToFinalName( |
| 497 AllOf(item, | 486 AllOf(item, |
| 498 Property(&DownloadItem::GetFullPath, final_path), | 487 Property(&DownloadItem::GetFullPath, final_path), |
| 499 Property(&DownloadItem::GetTargetFilePath, | 488 Property(&DownloadItem::GetTargetFilePath, |
| 500 final_path)))); | 489 final_path)))); |
| 501 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); | 490 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); |
| 502 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | 491 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) |
| 503 .WillOnce(Return(true)); | 492 .WillOnce(Return(true)); |
| 504 EXPECT_CALL(*download_file, Detach(_)) | 493 EXPECT_CALL(*download_file, Detach()); |
| 505 .WillOnce(ScheduleDetachCallback()); | |
| 506 item->SetIsPersisted(); | 494 item->SetIsPersisted(); |
| 507 item->MaybeCompleteDownload(); | 495 item->MaybeCompleteDownload(); |
| 508 RunAllPendingInMessageLoops(); | 496 RunAllPendingInMessageLoops(); |
| 509 ::testing::Mock::VerifyAndClearExpectations(download_file); | 497 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 510 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 498 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 511 } | 499 } |
| 512 | 500 |
| 513 TEST_F(DownloadItemTest, Interrupted) { | 501 TEST_F(DownloadItemTest, Interrupted) { |
| 514 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 502 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 515 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 503 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 EXPECT_EQ("livebeef", item->GetHash()); | 615 EXPECT_EQ("livebeef", item->GetHash()); |
| 628 EXPECT_EQ("", item->GetHashState()); | 616 EXPECT_EQ("", item->GetHashState()); |
| 629 EXPECT_TRUE(item->AllDataSaved()); | 617 EXPECT_TRUE(item->AllDataSaved()); |
| 630 } | 618 } |
| 631 | 619 |
| 632 TEST(MockDownloadItem, Compiles) { | 620 TEST(MockDownloadItem, Compiles) { |
| 633 MockDownloadItem mock_item; | 621 MockDownloadItem mock_item; |
| 634 } | 622 } |
| 635 | 623 |
| 636 } // namespace content | 624 } // namespace content |
| OLD | NEW |