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