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

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

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r168573 Created 8 years, 1 month 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 | Annotate | Revision Log
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 "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 27 matching lines...) Expand all
38 MOCK_METHOD2(ShouldOpenDownload, 38 MOCK_METHOD2(ShouldOpenDownload,
39 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); 39 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&));
40 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); 40 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&));
41 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); 41 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*));
42 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); 42 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*());
43 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); 43 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*));
44 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl*)); 44 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl*));
45 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl*)); 45 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl*));
46 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); 46 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*));
47 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); 47 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*));
48 MOCK_METHOD1(DownloadRenamedToIntermediateName, 48 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*));
49 void(DownloadItemImpl*));
50 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl*));
51 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); 49 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*));
52 }; 50 };
53 51
54 class MockRequestHandle : public DownloadRequestHandleInterface { 52 class MockRequestHandle : public DownloadRequestHandleInterface {
55 public: 53 public:
56 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); 54 MOCK_CONST_METHOD0(GetWebContents, WebContents*());
57 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); 55 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*());
58 MOCK_CONST_METHOD0(PauseRequest, void()); 56 MOCK_CONST_METHOD0(PauseRequest, void());
59 MOCK_CONST_METHOD0(ResumeRequest, void()); 57 MOCK_CONST_METHOD0(ResumeRequest, void());
60 MOCK_CONST_METHOD0(CancelRequest, void()); 58 MOCK_CONST_METHOD0(CancelRequest, void());
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 MockDownloadFile* mock_download_file(new MockDownloadFile); 436 MockDownloadFile* mock_download_file(new MockDownloadFile);
439 scoped_ptr<DownloadFile> download_file(mock_download_file); 437 scoped_ptr<DownloadFile> download_file(mock_download_file);
440 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 438 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
441 EXPECT_CALL(*mock_download_file, Initialize(_)); 439 EXPECT_CALL(*mock_download_file, Initialize(_));
442 item->Start(download_file.Pass()); 440 item->Start(download_file.Pass());
443 441
444 CleanupItem(item, mock_download_file); 442 CleanupItem(item, mock_download_file);
445 } 443 }
446 444
447 // Test that the delegate is invoked after the download file is renamed. 445 // Test that the delegate is invoked after the download file is renamed.
448 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the
449 // download is renamed to the intermediate name.
450 // Delegate::DownloadRenamedToFinalName() should be invoked after the final
451 // rename.
452 TEST_F(DownloadItemTest, CallbackAfterRename) { 446 TEST_F(DownloadItemTest, CallbackAfterRename) {
453 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 447 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
454 DownloadItemImplDelegate::DownloadTargetCallback callback; 448 DownloadItemImplDelegate::DownloadTargetCallback callback;
455 MockDownloadFile* download_file = 449 MockDownloadFile* download_file =
456 AddDownloadFileToDownloadItem(item, &callback); 450 AddDownloadFileToDownloadItem(item, &callback);
457 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); 451 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar"));
458 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 452 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
459 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); 453 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y"));
460 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) 454 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _))
461 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); 455 .WillOnce(ScheduleRenameCallback(new_intermediate_path));
456 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item))
457 .Times(1);
462 458
463 // DownloadItemImpl should invoke this callback on the delegate once the
464 // download is renamed to the intermediate name. Also check that GetFullPath()
465 // returns the intermediate path at the time of the call.
466 EXPECT_CALL(*mock_delegate(),
467 DownloadRenamedToIntermediateName(
468 AllOf(item,
469 Property(&DownloadItem::GetFullPath,
470 new_intermediate_path))));
471 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 459 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
472 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 460 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
473 RunAllPendingInMessageLoops(); 461 RunAllPendingInMessageLoops();
474 // All the callbacks should have happened by now. 462 // All the callbacks should have happened by now.
475 ::testing::Mock::VerifyAndClearExpectations(download_file); 463 ::testing::Mock::VerifyAndClearExpectations(download_file);
476 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); 464 ::testing::Mock::VerifyAndClearExpectations(mock_delegate());
477 465
478 item->OnAllDataSaved(""); 466 item->OnAllDataSaved("");
479 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) 467 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _))
480 .WillOnce(ScheduleRenameCallback(final_path)); 468 .WillOnce(ScheduleRenameCallback(final_path));
481 // DownloadItemImpl should invoke this callback on the delegate after the
482 // final rename has completed. Also check that GetFullPath() and
483 // GetTargetFilePath() return the final path at the time of the call.
484 EXPECT_CALL(*mock_delegate(),
485 DownloadRenamedToFinalName(
486 AllOf(item,
487 Property(&DownloadItem::GetFullPath, final_path),
488 Property(&DownloadItem::GetTargetFilePath,
489 final_path))));
490 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); 469 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item));
491 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) 470 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _))
492 .WillOnce(Return(true)); 471 .WillOnce(Return(true));
493 EXPECT_CALL(*download_file, Detach()); 472 EXPECT_CALL(*download_file, Detach());
494 item->SetIsPersisted();
495 item->MaybeCompleteDownload(); 473 item->MaybeCompleteDownload();
496 RunAllPendingInMessageLoops(); 474 RunAllPendingInMessageLoops();
497 ::testing::Mock::VerifyAndClearExpectations(download_file); 475 ::testing::Mock::VerifyAndClearExpectations(download_file);
498 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); 476 ::testing::Mock::VerifyAndClearExpectations(mock_delegate());
499 } 477 }
500 478
501 TEST_F(DownloadItemTest, Interrupted) { 479 TEST_F(DownloadItemTest, Interrupted) {
502 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 480 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
503 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); 481 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL);
504 482
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 EXPECT_EQ("livebeef", item->GetHash()); 593 EXPECT_EQ("livebeef", item->GetHash());
616 EXPECT_EQ("", item->GetHashState()); 594 EXPECT_EQ("", item->GetHashState());
617 EXPECT_TRUE(item->AllDataSaved()); 595 EXPECT_TRUE(item->AllDataSaved());
618 } 596 }
619 597
620 TEST(MockDownloadItem, Compiles) { 598 TEST(MockDownloadItem, Compiles) {
621 MockDownloadItem mock_item; 599 MockDownloadItem mock_item;
622 } 600 }
623 601
624 } // namespace content 602 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698