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

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

Issue 2082343002: Remove calls to deprecated MessageLoop methods in content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR Created 4 years, 5 months 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
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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 void FinishStream(DownloadInterruptReason interrupt_reason, 279 void FinishStream(DownloadInterruptReason interrupt_reason,
280 bool check_observer, 280 bool check_observer,
281 const std::string& expected_hash) { 281 const std::string& expected_hash) {
282 ::testing::Sequence s1; 282 ::testing::Sequence s1;
283 SetupFinishStream(interrupt_reason, s1); 283 SetupFinishStream(interrupt_reason, s1);
284 sink_callback_.Run(); 284 sink_callback_.Run();
285 VerifyStreamAndSize(); 285 VerifyStreamAndSize();
286 if (check_observer) { 286 if (check_observer) {
287 EXPECT_CALL(*(observer_.get()), 287 EXPECT_CALL(*(observer_.get()),
288 MockDestinationCompleted(_, expected_hash)); 288 MockDestinationCompleted(_, expected_hash));
289 loop_.RunUntilIdle(); 289 base::RunLoop().RunUntilIdle();
290 ::testing::Mock::VerifyAndClearExpectations(observer_.get()); 290 ::testing::Mock::VerifyAndClearExpectations(observer_.get());
291 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _)) 291 EXPECT_CALL(*(observer_.get()), DestinationUpdate(_, _))
292 .Times(AnyNumber()) 292 .Times(AnyNumber())
293 .WillRepeatedly( 293 .WillRepeatedly(
294 Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo)); 294 Invoke(this, &DownloadFileTest::SetUpdateDownloadInfo));
295 } 295 }
296 } 296 }
297 297
298 DownloadInterruptReason RenameAndUniquify( 298 DownloadInterruptReason RenameAndUniquify(
299 const base::FilePath& full_path, 299 const base::FilePath& full_path,
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 InvokeSelectedRenameMethod(path_3, &output_path)); 481 InvokeSelectedRenameMethod(path_3, &output_path));
482 renamed_path = download_file_->FullPath(); 482 renamed_path = download_file_->FullPath();
483 EXPECT_EQ(path_3, renamed_path); 483 EXPECT_EQ(path_3, renamed_path);
484 EXPECT_EQ(path_3, output_path); 484 EXPECT_EQ(path_3, output_path);
485 485
486 // Check the files. 486 // Check the files.
487 EXPECT_FALSE(base::PathExists(path_2)); 487 EXPECT_FALSE(base::PathExists(path_2));
488 EXPECT_TRUE(base::PathExists(path_3)); 488 EXPECT_TRUE(base::PathExists(path_3));
489 489
490 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kDataHash); 490 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kDataHash);
491 loop_.RunUntilIdle(); 491 base::RunLoop().RunUntilIdle();
492 492
493 // Rename the file after downloading all the data and closing the file. 493 // Rename the file after downloading all the data and closing the file.
494 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 494 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
495 InvokeSelectedRenameMethod(path_4, &output_path)); 495 InvokeSelectedRenameMethod(path_4, &output_path));
496 renamed_path = download_file_->FullPath(); 496 renamed_path = download_file_->FullPath();
497 EXPECT_EQ(path_4, renamed_path); 497 EXPECT_EQ(path_4, renamed_path);
498 EXPECT_EQ(path_4, output_path); 498 EXPECT_EQ(path_4, output_path);
499 499
500 // Check the files. 500 // Check the files.
501 EXPECT_FALSE(base::PathExists(path_3)); 501 EXPECT_FALSE(base::PathExists(path_3));
(...skipping 20 matching lines...) Expand all
522 base::FilePath new_path; 522 base::FilePath new_path;
523 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 523 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
524 RenameAndAnnotate(path_1, &new_path)); 524 RenameAndAnnotate(path_1, &new_path));
525 EXPECT_EQ(path_1.value(), new_path.value()); 525 EXPECT_EQ(path_1.value(), new_path.value());
526 526
527 std::string file_contents; 527 std::string file_contents;
528 ASSERT_TRUE(base::ReadFileToString(new_path, &file_contents)); 528 ASSERT_TRUE(base::ReadFileToString(new_path, &file_contents));
529 EXPECT_NE(std::string(file_data), file_contents); 529 EXPECT_NE(std::string(file_data), file_contents);
530 530
531 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash); 531 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash);
532 loop_.RunUntilIdle(); 532 base::RunLoop().RunUntilIdle();
533 DestroyDownloadFile(0); 533 DestroyDownloadFile(0);
534 } 534 }
535 535
536 // Test to make sure the rename uniquifies if we aren't overwriting 536 // Test to make sure the rename uniquifies if we aren't overwriting
537 // and there's a file where we're aiming. As above, not a 537 // and there's a file where we're aiming. As above, not a
538 // DownloadFileTestWithRename test because this only applies to 538 // DownloadFileTestWithRename test because this only applies to
539 // RenameAndUniquify(). 539 // RenameAndUniquify().
540 TEST_F(DownloadFileTest, RenameUniquifies) { 540 TEST_F(DownloadFileTest, RenameUniquifies) {
541 ASSERT_TRUE(CreateDownloadFile(0, true)); 541 ASSERT_TRUE(CreateDownloadFile(0, true));
542 base::FilePath initial_path(download_file_->FullPath()); 542 base::FilePath initial_path(download_file_->FullPath());
543 EXPECT_TRUE(base::PathExists(initial_path)); 543 EXPECT_TRUE(base::PathExists(initial_path));
544 base::FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); 544 base::FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1"));
545 base::FilePath path_1_suffixed(path_1.InsertBeforeExtensionASCII(" (1)")); 545 base::FilePath path_1_suffixed(path_1.InsertBeforeExtensionASCII(" (1)"));
546 546
547 ASSERT_FALSE(base::PathExists(path_1)); 547 ASSERT_FALSE(base::PathExists(path_1));
548 static const char file_data[] = "xyzzy"; 548 static const char file_data[] = "xyzzy";
549 ASSERT_EQ(static_cast<int>(sizeof(file_data)), 549 ASSERT_EQ(static_cast<int>(sizeof(file_data)),
550 base::WriteFile(path_1, file_data, sizeof(file_data))); 550 base::WriteFile(path_1, file_data, sizeof(file_data)));
551 ASSERT_TRUE(base::PathExists(path_1)); 551 ASSERT_TRUE(base::PathExists(path_1));
552 552
553 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, RenameAndUniquify(path_1, NULL)); 553 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, RenameAndUniquify(path_1, NULL));
554 EXPECT_TRUE(base::PathExists(path_1_suffixed)); 554 EXPECT_TRUE(base::PathExists(path_1_suffixed));
555 555
556 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash); 556 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash);
557 loop_.RunUntilIdle(); 557 base::RunLoop().RunUntilIdle();
558 DestroyDownloadFile(0); 558 DestroyDownloadFile(0);
559 } 559 }
560 560
561 // Test that RenameAndUniquify doesn't try to uniquify in the case where the 561 // Test that RenameAndUniquify doesn't try to uniquify in the case where the
562 // target filename is the same as the current filename. 562 // target filename is the same as the current filename.
563 TEST_F(DownloadFileTest, RenameRecognizesSelfConflict) { 563 TEST_F(DownloadFileTest, RenameRecognizesSelfConflict) {
564 ASSERT_TRUE(CreateDownloadFile(0, true)); 564 ASSERT_TRUE(CreateDownloadFile(0, true));
565 base::FilePath initial_path(download_file_->FullPath()); 565 base::FilePath initial_path(download_file_->FullPath());
566 EXPECT_TRUE(base::PathExists(initial_path)); 566 EXPECT_TRUE(base::PathExists(initial_path));
567 567
568 base::FilePath new_path; 568 base::FilePath new_path;
569 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 569 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
570 RenameAndUniquify(initial_path, &new_path)); 570 RenameAndUniquify(initial_path, &new_path));
571 EXPECT_TRUE(base::PathExists(initial_path)); 571 EXPECT_TRUE(base::PathExists(initial_path));
572 572
573 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash); 573 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash);
574 loop_.RunUntilIdle(); 574 base::RunLoop().RunUntilIdle();
575 DestroyDownloadFile(0); 575 DestroyDownloadFile(0);
576 EXPECT_EQ(initial_path.value(), new_path.value()); 576 EXPECT_EQ(initial_path.value(), new_path.value());
577 } 577 }
578 578
579 // Test to make sure we get the proper error on failure. 579 // Test to make sure we get the proper error on failure.
580 TEST_P(DownloadFileTestWithRename, RenameError) { 580 TEST_P(DownloadFileTestWithRename, RenameError) {
581 ASSERT_TRUE(CreateDownloadFile(0, true)); 581 ASSERT_TRUE(CreateDownloadFile(0, true));
582 base::FilePath initial_path(download_file_->FullPath()); 582 base::FilePath initial_path(download_file_->FullPath());
583 583
584 // Create a subdirectory. 584 // Create a subdirectory.
(...skipping 14 matching lines...) Expand all
599 base::FilePermissionRestorer restorer(target_dir); 599 base::FilePermissionRestorer restorer(target_dir);
600 ASSERT_TRUE(base::MakeFileUnwritable(target_dir)); 600 ASSERT_TRUE(base::MakeFileUnwritable(target_dir));
601 601
602 // Expect nulling out of further processing. 602 // Expect nulling out of further processing.
603 EXPECT_CALL(*input_stream_, RegisterCallback(IsNullCallback())); 603 EXPECT_CALL(*input_stream_, RegisterCallback(IsNullCallback()));
604 ExpectPermissionError(InvokeSelectedRenameMethod(target_path, NULL)); 604 ExpectPermissionError(InvokeSelectedRenameMethod(target_path, NULL));
605 EXPECT_FALSE(base::PathExists(target_path_suffixed)); 605 EXPECT_FALSE(base::PathExists(target_path_suffixed));
606 } 606 }
607 607
608 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash); 608 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash);
609 loop_.RunUntilIdle(); 609 base::RunLoop().RunUntilIdle();
610 DestroyDownloadFile(0); 610 DestroyDownloadFile(0);
611 } 611 }
612 612
613 namespace { 613 namespace {
614 614
615 void TestRenameCompletionCallback(const base::Closure& closure, 615 void TestRenameCompletionCallback(const base::Closure& closure,
616 bool* did_run_callback, 616 bool* did_run_callback,
617 DownloadInterruptReason interrupt_reason, 617 DownloadInterruptReason interrupt_reason,
618 const base::FilePath& new_path) { 618 const base::FilePath& new_path) {
619 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason); 619 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, interrupt_reason);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 FROM_HERE, second_failing_run.QuitClosure()); 692 FROM_HERE, second_failing_run.QuitClosure());
693 second_failing_run.Run(); 693 second_failing_run.Run();
694 EXPECT_FALSE(did_run_callback); 694 EXPECT_FALSE(did_run_callback);
695 } 695 }
696 696
697 // This time the QuitClosure from succeeding_run should get executed. 697 // This time the QuitClosure from succeeding_run should get executed.
698 succeeding_run.Run(); 698 succeeding_run.Run();
699 EXPECT_TRUE(did_run_callback); 699 EXPECT_TRUE(did_run_callback);
700 700
701 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash); 701 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash);
702 loop_.RunUntilIdle(); 702 base::RunLoop().RunUntilIdle();
703 DestroyDownloadFile(0); 703 DestroyDownloadFile(0);
704 } 704 }
705 705
706 // Various tests of the StreamActive method. 706 // Various tests of the StreamActive method.
707 TEST_F(DownloadFileTest, StreamEmptySuccess) { 707 TEST_F(DownloadFileTest, StreamEmptySuccess) {
708 ASSERT_TRUE(CreateDownloadFile(0, true)); 708 ASSERT_TRUE(CreateDownloadFile(0, true));
709 base::FilePath initial_path(download_file_->FullPath()); 709 base::FilePath initial_path(download_file_->FullPath());
710 EXPECT_TRUE(base::PathExists(initial_path)); 710 EXPECT_TRUE(base::PathExists(initial_path));
711 711
712 // Test that calling the sink_callback_ on an empty stream shouldn't 712 // Test that calling the sink_callback_ on an empty stream shouldn't
713 // do anything. 713 // do anything.
714 AppendDataToFile(NULL, 0); 714 AppendDataToFile(NULL, 0);
715 715
716 // Finish the download this way and make sure we see it on the observer. 716 // Finish the download this way and make sure we see it on the observer.
717 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash); 717 FinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, true, kEmptyHash);
718 loop_.RunUntilIdle(); 718 base::RunLoop().RunUntilIdle();
719 719
720 DestroyDownloadFile(0); 720 DestroyDownloadFile(0);
721 } 721 }
722 722
723 TEST_F(DownloadFileTest, StreamEmptyError) { 723 TEST_F(DownloadFileTest, StreamEmptyError) {
724 ASSERT_TRUE(CreateDownloadFile(0, true)); 724 ASSERT_TRUE(CreateDownloadFile(0, true));
725 base::FilePath initial_path(download_file_->FullPath()); 725 base::FilePath initial_path(download_file_->FullPath());
726 EXPECT_TRUE(base::PathExists(initial_path)); 726 EXPECT_TRUE(base::PathExists(initial_path));
727 727
728 // Finish the download in error and make sure we see it on the 728 // Finish the download in error and make sure we see it on the
729 // observer. 729 // observer.
730 EXPECT_CALL( 730 EXPECT_CALL(
731 *(observer_.get()), 731 *(observer_.get()),
732 MockDestinationError( 732 MockDestinationError(
733 DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, 0, kEmptyHash)) 733 DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, 0, kEmptyHash))
734 .WillOnce(InvokeWithoutArgs( 734 .WillOnce(InvokeWithoutArgs(
735 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); 735 this, &DownloadFileTest::ConfirmUpdateDownloadInfo));
736 736
737 // If this next EXPECT_CALL fails flakily, it's probably a real failure. 737 // If this next EXPECT_CALL fails flakily, it's probably a real failure.
738 // We'll be getting a stream of UpdateDownload calls from the timer, and 738 // We'll be getting a stream of UpdateDownload calls from the timer, and
739 // the last one may have the correct information even if the failure 739 // the last one may have the correct information even if the failure
740 // doesn't produce an update, as the timer update may have triggered at the 740 // doesn't produce an update, as the timer update may have triggered at the
741 // same time. 741 // same time.
742 EXPECT_CALL(*(observer_.get()), CurrentUpdateStatus(0, _)); 742 EXPECT_CALL(*(observer_.get()), CurrentUpdateStatus(0, _));
743 743
744 FinishStream( 744 FinishStream(
745 DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false, kEmptyHash); 745 DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, false, kEmptyHash);
746 746
747 loop_.RunUntilIdle(); 747 base::RunLoop().RunUntilIdle();
748 748
749 DestroyDownloadFile(0); 749 DestroyDownloadFile(0);
750 } 750 }
751 751
752 TEST_F(DownloadFileTest, StreamNonEmptySuccess) { 752 TEST_F(DownloadFileTest, StreamNonEmptySuccess) {
753 ASSERT_TRUE(CreateDownloadFile(0, true)); 753 ASSERT_TRUE(CreateDownloadFile(0, true));
754 base::FilePath initial_path(download_file_->FullPath()); 754 base::FilePath initial_path(download_file_->FullPath());
755 EXPECT_TRUE(base::PathExists(initial_path)); 755 EXPECT_TRUE(base::PathExists(initial_path));
756 756
757 const char* chunks1[] = { kTestData1, kTestData2 }; 757 const char* chunks1[] = { kTestData1, kTestData2 };
758 ::testing::Sequence s1; 758 ::testing::Sequence s1;
759 SetupDataAppend(chunks1, 2, s1); 759 SetupDataAppend(chunks1, 2, s1);
760 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, s1); 760 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NONE, s1);
761 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _)); 761 EXPECT_CALL(*(observer_.get()), MockDestinationCompleted(_, _));
762 sink_callback_.Run(); 762 sink_callback_.Run();
763 VerifyStreamAndSize(); 763 VerifyStreamAndSize();
764 loop_.RunUntilIdle(); 764 base::RunLoop().RunUntilIdle();
765 DestroyDownloadFile(0); 765 DestroyDownloadFile(0);
766 } 766 }
767 767
768 TEST_F(DownloadFileTest, StreamNonEmptyError) { 768 TEST_F(DownloadFileTest, StreamNonEmptyError) {
769 ASSERT_TRUE(CreateDownloadFile(0, true)); 769 ASSERT_TRUE(CreateDownloadFile(0, true));
770 base::FilePath initial_path(download_file_->FullPath()); 770 base::FilePath initial_path(download_file_->FullPath());
771 EXPECT_TRUE(base::PathExists(initial_path)); 771 EXPECT_TRUE(base::PathExists(initial_path));
772 772
773 const char* chunks1[] = { kTestData1, kTestData2 }; 773 const char* chunks1[] = { kTestData1, kTestData2 };
774 ::testing::Sequence s1; 774 ::testing::Sequence s1;
775 SetupDataAppend(chunks1, 2, s1); 775 SetupDataAppend(chunks1, 2, s1);
776 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, s1); 776 SetupFinishStream(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, s1);
777 777
778 EXPECT_CALL(*(observer_.get()), 778 EXPECT_CALL(*(observer_.get()),
779 MockDestinationError( 779 MockDestinationError(
780 DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, _, _)) 780 DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, _, _))
781 .WillOnce(InvokeWithoutArgs( 781 .WillOnce(InvokeWithoutArgs(
782 this, &DownloadFileTest::ConfirmUpdateDownloadInfo)); 782 this, &DownloadFileTest::ConfirmUpdateDownloadInfo));
783 783
784 // If this next EXPECT_CALL fails flakily, it's probably a real failure. 784 // If this next EXPECT_CALL fails flakily, it's probably a real failure.
785 // We'll be getting a stream of UpdateDownload calls from the timer, and 785 // We'll be getting a stream of UpdateDownload calls from the timer, and
786 // the last one may have the correct information even if the failure 786 // the last one may have the correct information even if the failure
787 // doesn't produce an update, as the timer update may have triggered at the 787 // doesn't produce an update, as the timer update may have triggered at the
788 // same time. 788 // same time.
789 EXPECT_CALL(*(observer_.get()), 789 EXPECT_CALL(*(observer_.get()),
790 CurrentUpdateStatus(strlen(kTestData1) + strlen(kTestData2), _)); 790 CurrentUpdateStatus(strlen(kTestData1) + strlen(kTestData2), _));
791 791
792 sink_callback_.Run(); 792 sink_callback_.Run();
793 loop_.RunUntilIdle(); 793 base::RunLoop().RunUntilIdle();
794 VerifyStreamAndSize(); 794 VerifyStreamAndSize();
795 DestroyDownloadFile(0); 795 DestroyDownloadFile(0);
796 } 796 }
797 797
798 } // namespace content 798 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/download_browsertest.cc ('k') | content/browser/fileapi/file_system_dir_url_request_job_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698