| 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 <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 namespace { | 53 namespace { |
| 54 | 54 |
| 55 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { | 55 class MockDownloadManagerDelegate : public content::DownloadManagerDelegate { |
| 56 public: | 56 public: |
| 57 MockDownloadManagerDelegate(); | 57 MockDownloadManagerDelegate(); |
| 58 virtual ~MockDownloadManagerDelegate(); | 58 virtual ~MockDownloadManagerDelegate(); |
| 59 | 59 |
| 60 MOCK_METHOD0(Shutdown, void()); | 60 MOCK_METHOD0(Shutdown, void()); |
| 61 MOCK_METHOD0(GetNextId, content::DownloadId()); | 61 MOCK_METHOD0(GetNextId, content::DownloadId()); |
| 62 MOCK_METHOD1(ShouldStartDownload, bool(int32)); | 62 MOCK_METHOD1(ShouldStartDownload, bool(int32)); |
| 63 MOCK_METHOD1(ChooseDownloadPath, void(DownloadItem*)); | |
| 64 MOCK_METHOD1(GetIntermediatePath, FilePath(const DownloadItem&)); | 63 MOCK_METHOD1(GetIntermediatePath, FilePath(const DownloadItem&)); |
| 65 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*()); | 64 MOCK_METHOD0(GetAlternativeWebContentsToNotifyForDownload, WebContents*()); |
| 66 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); | 65 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); |
| 67 MOCK_METHOD2(ShouldCompleteDownload, bool( | 66 MOCK_METHOD2(ShouldCompleteDownload, bool( |
| 68 DownloadItem*, const base::Closure&)); | 67 DownloadItem*, const base::Closure&)); |
| 69 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem*)); | 68 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem*)); |
| 70 MOCK_METHOD0(GenerateFileHash, bool()); | 69 MOCK_METHOD0(GenerateFileHash, bool()); |
| 71 MOCK_METHOD1(AddItemToPersistentStore, void(DownloadItem*)); | 70 MOCK_METHOD1(AddItemToPersistentStore, void(DownloadItem*)); |
| 72 MOCK_METHOD1(UpdateItemInPersistentStore, void(DownloadItem*)); | 71 MOCK_METHOD1(UpdateItemInPersistentStore, void(DownloadItem*)); |
| 73 MOCK_METHOD2(UpdatePathForItemInPersistentStore, | 72 MOCK_METHOD2(UpdatePathForItemInPersistentStore, |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) | 448 EXPECT_CALL(GetMockDownloadManagerDelegate(), GenerateFileHash()) |
| 450 .WillOnce(Return(true)); | 449 .WillOnce(Return(true)); |
| 451 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile( | 450 EXPECT_CALL(GetMockDownloadFileManager(), MockCreateDownloadFile( |
| 452 info.get(), static_cast<content::ByteStreamReader*>(NULL), | 451 info.get(), static_cast<content::ByteStreamReader*>(NULL), |
| 453 download_manager_.get(), true, _, _)); | 452 download_manager_.get(), true, _, _)); |
| 454 | 453 |
| 455 download_manager_->StartDownload(info.Pass(), stream.Pass()); | 454 download_manager_->StartDownload(info.Pass(), stream.Pass()); |
| 456 EXPECT_TRUE(GetActiveDownloadItem(local_id)); | 455 EXPECT_TRUE(GetActiveDownloadItem(local_id)); |
| 457 } | 456 } |
| 458 | 457 |
| 459 // Does the DownloadManager prompt when requested? | |
| 460 TEST_F(DownloadManagerTest, RestartDownload) { | |
| 461 // Put a mock we have a handle to on the download manager. | |
| 462 content::MockDownloadItem& item(AddItemToManager()); | |
| 463 int download_id = item.GetId(); | |
| 464 | |
| 465 // Confirm we're internally consistent. | |
| 466 EXPECT_EQ(&item, GetActiveDownloadItem(download_id)); | |
| 467 | |
| 468 ScopedTempDir download_dir; | |
| 469 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | |
| 470 FilePath expected_path(download_dir.path().Append( | |
| 471 FILE_PATH_LITERAL("location"))); | |
| 472 | |
| 473 EXPECT_CALL(item, GetTargetDisposition()) | |
| 474 .WillOnce(Return(DownloadItem::TARGET_DISPOSITION_PROMPT)); | |
| 475 EXPECT_CALL(GetMockDownloadManagerDelegate(), ChooseDownloadPath(&item)); | |
| 476 download_manager_->RestartDownload(download_id); | |
| 477 | |
| 478 // The alternative pathway goes straight to OnTargetPathAvailable, | |
| 479 // so it more naturally belongs below. | |
| 480 } | |
| 481 | |
| 482 // Do the results of GetIntermediatePath get passed through to the | 458 // Do the results of GetIntermediatePath get passed through to the |
| 483 // download? Note that this path is tested from RestartDownload | 459 // download? Note that this path is tested from RestartDownload |
| 484 // to test the non-prompting path in RestartDownload as well. | 460 // to test the non-prompting path in RestartDownload as well. |
| 485 TEST_F(DownloadManagerTest, OnTargetPathAvailable) { | 461 TEST_F(DownloadManagerTest, OnTargetPathAvailable) { |
| 486 // Put a mock we have a handle to on the download manager. | 462 // Put a mock we have a handle to on the download manager. |
| 487 content::MockDownloadItem& item(AddItemToManager()); | 463 content::MockDownloadItem& item(AddItemToManager()); |
| 488 | 464 |
| 489 ScopedTempDir download_dir; | 465 ScopedTempDir download_dir; |
| 490 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 466 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 491 FilePath target_path(download_dir.path().Append( | 467 FilePath target_path(download_dir.path().Append( |
| 492 FILE_PATH_LITERAL("location"))); | 468 FILE_PATH_LITERAL("location"))); |
| 493 FilePath intermediate_path(download_dir.path().Append( | 469 FilePath intermediate_path(download_dir.path().Append( |
| 494 FILE_PATH_LITERAL("location.crdownload"))); | 470 FILE_PATH_LITERAL("location.crdownload"))); |
| 495 | 471 |
| 496 EXPECT_CALL(item, GetTargetDisposition()) | |
| 497 .WillOnce(Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | |
| 498 EXPECT_CALL(GetMockDownloadManagerDelegate(), | 472 EXPECT_CALL(GetMockDownloadManagerDelegate(), |
| 499 GetIntermediatePath(Ref(item))) | 473 GetIntermediatePath(Ref(item))) |
| 500 .WillOnce(Return(intermediate_path)); | 474 .WillOnce(Return(intermediate_path)); |
| 501 // Finesse DCHECK with WillRepeatedly. | 475 // Finesse DCHECK with WillRepeatedly. |
| 502 EXPECT_CALL(item, GetTargetFilePath()) | 476 EXPECT_CALL(item, GetTargetFilePath()) |
| 503 .WillRepeatedly(ReturnRef(target_path)); | 477 .WillRepeatedly(ReturnRef(target_path)); |
| 504 EXPECT_CALL(item, OnIntermediatePathDetermined( | 478 EXPECT_CALL(item, OnIntermediatePathDetermined( |
| 505 &GetMockDownloadFileManager(), intermediate_path)); | 479 &GetMockDownloadFileManager(), intermediate_path)); |
| 506 download_manager_->RestartDownload(item.GetId()); | 480 download_manager_->RestartDownload(item.GetId()); |
| 507 } | 481 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 UpdateItemInPersistentStore(&item)); | 535 UpdateItemInPersistentStore(&item)); |
| 562 EXPECT_CALL(item, GetState()) | 536 EXPECT_CALL(item, GetState()) |
| 563 .WillRepeatedly(Return(DownloadItem::CANCELLED)); | 537 .WillRepeatedly(Return(DownloadItem::CANCELLED)); |
| 564 EXPECT_CALL(item, GetDbHandle()) | 538 EXPECT_CALL(item, GetDbHandle()) |
| 565 .WillRepeatedly(Return(db_handle)); | 539 .WillRepeatedly(Return(db_handle)); |
| 566 | 540 |
| 567 EXPECT_CALL(item, OffThreadCancel(&GetMockDownloadFileManager())); | 541 EXPECT_CALL(item, OffThreadCancel(&GetMockDownloadFileManager())); |
| 568 download_manager_->DownloadStopped(&item); | 542 download_manager_->DownloadStopped(&item); |
| 569 EXPECT_EQ(NULL, GetActiveDownloadItem(download_id)); | 543 EXPECT_EQ(NULL, GetActiveDownloadItem(download_id)); |
| 570 } | 544 } |
| OLD | NEW |