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

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

Issue 1525753002: Test both download resumption code paths. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: A couple of renames. Created 5 years 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // This file contains download browser tests that are known to be runnable 5 // This file contains download browser tests that are known to be runnable
6 // in a pure content context. Over time tests should be migrated here. 6 // in a pure content context. Over time tests should be migrated here.
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 636
637 // Check the contents. 637 // Check the contents.
638 EXPECT_EQ(value, file_contents); 638 EXPECT_EQ(value, file_contents);
639 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) 639 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0)
640 return false; 640 return false;
641 641
642 return true; 642 return true;
643 } 643 }
644 644
645 // Start a download and return the item. 645 // Start a download and return the item.
646 DownloadItem* StartDownloadAndReturnItem(GURL url) { 646 DownloadItem* StartDownloadAndReturnItem(Shell* shell, GURL url) {
647 scoped_ptr<DownloadCreateObserver> observer( 647 scoped_ptr<DownloadCreateObserver> observer(
648 new DownloadCreateObserver(DownloadManagerForShell(shell()))); 648 new DownloadCreateObserver(DownloadManagerForShell(shell)));
649 shell()->LoadURL(url); 649 shell->LoadURL(url);
650 return observer->WaitForFinished(); 650 return observer->WaitForFinished();
651 } 651 }
652 652
653 static void ReadAndVerifyFileContents(int seed, 653 static void ReadAndVerifyFileContents(int seed,
654 int64_t expected_size, 654 int64_t expected_size,
655 const base::FilePath& path) { 655 const base::FilePath& path) {
656 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); 656 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
657 ASSERT_TRUE(file.IsValid()); 657 ASSERT_TRUE(file.IsValid());
658 int64_t file_length = file.GetLength(); 658 int64_t file_length = file.GetLength();
659 ASSERT_EQ(expected_size, file_length); 659 ASSERT_EQ(expected_size, file_length);
(...skipping 23 matching lines...) Expand all
683 *result = false; 683 *result = false;
684 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 684 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
685 base::MessageLoop::QuitWhenIdleClosure()); 685 base::MessageLoop::QuitWhenIdleClosure());
686 } 686 }
687 687
688 // Location of the downloads directory for these tests 688 // Location of the downloads directory for these tests
689 base::ScopedTempDir downloads_directory_; 689 base::ScopedTempDir downloads_directory_;
690 scoped_ptr<TestShellDownloadManagerDelegate> test_delegate_; 690 scoped_ptr<TestShellDownloadManagerDelegate> test_delegate_;
691 }; 691 };
692 692
693 // Parameters for DownloadResumptionContentTest.
694 enum class DownloadResumptionTestType {
695 RESUME_WITH_RENDERER, // Resume() is called while the originating WebContents
696 // is still alive.
697 RESUME_WITHOUT_RENDERER // Resume() is called after the originating
698 // WebContents has been deleted.
699 };
700
701 // Parameterized test for download resumption. Tests using this fixure will be
702 // run once with RESUME_WITH_RENDERER and once with RESUME_WITHOUT_RENDERER.
703 // Use initiator_shell_for_resumption() to retrieve the Shell object that should
704 // be used as the originator for the initial download. Prior to calling
705 // Resume(), call PrepareToResume() which will cause the originating Shell to be
706 // deleted if the test parameter is RESUME_WITHOUT_RENDERER.
707 class DownloadResumptionContentTest
708 : public DownloadContentTest,
709 public ::testing::WithParamInterface<DownloadResumptionTestType> {
710 public:
711 void SetUpOnMainThread() override {
712 base::CommandLine::ForCurrentProcess()->AppendSwitch(
713 switches::kEnableDownloadResumption);
714 DownloadContentTest::SetUpOnMainThread();
715
716 if (GetParam() == DownloadResumptionTestType::RESUME_WITHOUT_RENDERER)
717 initiator_shell_for_resumption_ = CreateBrowser();
718 else
719 initiator_shell_for_resumption_ = shell();
720
721 ASSERT_EQ(DownloadManagerForShell(shell()),
722 DownloadManagerForShell(initiator_shell_for_resumption()));
723 }
724
725 // Shell to use for initiating a download. Only valid *before*
726 // PrepareToResume() is called.
727 Shell* initiator_shell_for_resumption() const {
728 DCHECK(initiator_shell_for_resumption_);
729 return initiator_shell_for_resumption_;
730 }
731
732 // Should be called once before calling DownloadItem::Resume() on an
733 // interrupted download. This may cause initiator_shell_for_resumption() to
734 // become invalidated.
735 void PrepareToResume() {
736 if (GetParam() == DownloadResumptionTestType::RESUME_WITH_RENDERER)
737 return;
738 DCHECK_NE(initiator_shell_for_resumption(), shell());
739 DCHECK(initiator_shell_for_resumption());
740 initiator_shell_for_resumption_->Close();
741 initiator_shell_for_resumption_ = nullptr;
742 }
743
744 private:
745 Shell* initiator_shell_for_resumption_ = nullptr;
746 };
747
748 INSTANTIATE_TEST_CASE_P(
749 _,
750 DownloadResumptionContentTest,
751 ::testing::Values(DownloadResumptionTestType::RESUME_WITH_RENDERER,
752 DownloadResumptionTestType::RESUME_WITHOUT_RENDERER));
753
693 } // namespace 754 } // namespace
694 755
695 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { 756 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) {
696 SetupEnsureNoPendingDownloads(); 757 SetupEnsureNoPendingDownloads();
697 758
698 // Create a download, wait until it's started, and confirm 759 // Create a download, wait until it's started, and confirm
699 // we're in the expected state. 760 // we're in the expected state.
700 DownloadItem* download = StartDownloadAndReturnItem( 761 DownloadItem* download = StartDownloadAndReturnItem(
701 GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); 762 shell(), GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl));
702 ASSERT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 763 ASSERT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
703 764
704 // Cancel the download and wait for download system quiesce. 765 // Cancel the download and wait for download system quiesce.
705 download->Cancel(true); 766 download->Cancel(true);
706 scoped_refptr<DownloadTestFlushObserver> flush_observer( 767 scoped_refptr<DownloadTestFlushObserver> flush_observer(
707 new DownloadTestFlushObserver(DownloadManagerForShell(shell()))); 768 new DownloadTestFlushObserver(DownloadManagerForShell(shell())));
708 flush_observer->WaitForFlush(); 769 flush_observer->WaitForFlush();
709 770
710 // Get the important info from other threads and check it. 771 // Get the important info from other threads and check it.
711 EXPECT_TRUE(EnsureNoPendingDownloads()); 772 EXPECT_TRUE(EnsureNoPendingDownloads());
712 } 773 }
713 774
714 // Check that downloading multiple (in this case, 2) files does not result in 775 // Check that downloading multiple (in this case, 2) files does not result in
715 // corrupted files. 776 // corrupted files.
716 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { 777 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) {
717 SetupEnsureNoPendingDownloads(); 778 SetupEnsureNoPendingDownloads();
718 779
719 // Create a download, wait until it's started, and confirm 780 // Create a download, wait until it's started, and confirm
720 // we're in the expected state. 781 // we're in the expected state.
721 DownloadItem* download1 = StartDownloadAndReturnItem( 782 DownloadItem* download1 = StartDownloadAndReturnItem(
722 GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); 783 shell(), GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl));
723 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState()); 784 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState());
724 785
725 // Start the second download and wait until it's done. 786 // Start the second download and wait until it's done.
726 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("download-test.lib")); 787 GURL url(net::URLRequestMockHTTPJob::GetMockUrl("download-test.lib"));
727 DownloadItem* download2 = StartDownloadAndReturnItem(url); 788 DownloadItem* download2 = StartDownloadAndReturnItem(shell(), url);
728 WaitForCompletion(download2); 789 WaitForCompletion(download2);
729 790
730 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState()); 791 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState());
731 ASSERT_EQ(DownloadItem::COMPLETE, download2->GetState()); 792 ASSERT_EQ(DownloadItem::COMPLETE, download2->GetState());
732 793
733 // Allow the first request to finish. 794 // Allow the first request to finish.
734 scoped_ptr<DownloadTestObserver> observer2(CreateWaiter(shell(), 1)); 795 scoped_ptr<DownloadTestObserver> observer2(CreateWaiter(shell(), 1));
735 NavigateToURL(shell(), 796 NavigateToURL(shell(),
736 GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl)); 797 GURL(net::URLRequestSlowDownloadJob::kFinishDownloadUrl));
737 observer2->WaitForFinished(); // Wait for the third request. 798 observer2->WaitForFinished(); // Wait for the third request.
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 #define MAYBE_ShutdownInProgress DISABLED_ShutdownInProgress 944 #define MAYBE_ShutdownInProgress DISABLED_ShutdownInProgress
884 #else 945 #else
885 #define MAYBE_ShutdownInProgress ShutdownInProgress 946 #define MAYBE_ShutdownInProgress ShutdownInProgress
886 #endif 947 #endif
887 948
888 // Try to shutdown with a download in progress to make sure shutdown path 949 // Try to shutdown with a download in progress to make sure shutdown path
889 // works properly. 950 // works properly.
890 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MAYBE_ShutdownInProgress) { 951 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MAYBE_ShutdownInProgress) {
891 // Create a download that won't complete. 952 // Create a download that won't complete.
892 DownloadItem* download = StartDownloadAndReturnItem( 953 DownloadItem* download = StartDownloadAndReturnItem(
893 GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); 954 shell(), GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl));
894 955
895 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 956 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
896 957
897 // Shutdown the download manager and make sure we get the right 958 // Shutdown the download manager and make sure we get the right
898 // notifications in the right order. 959 // notifications in the right order.
899 StrictMock<MockDownloadItemObserver> item_observer; 960 StrictMock<MockDownloadItemObserver> item_observer;
900 download->AddObserver(&item_observer); 961 download->AddObserver(&item_observer);
901 MockDownloadManagerObserver manager_observer( 962 MockDownloadManagerObserver manager_observer(
902 DownloadManagerForShell(shell())); 963 DownloadManagerForShell(shell()));
903 // Don't care about ModelChanged() events. 964 // Don't care about ModelChanged() events.
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 MockDownloadItemObserver observer; 1044 MockDownloadItemObserver observer;
984 items[0]->AddObserver(&observer); 1045 items[0]->AddObserver(&observer);
985 EXPECT_CALL(observer, OnDownloadDestroyed(items[0])); 1046 EXPECT_CALL(observer, OnDownloadDestroyed(items[0]));
986 1047
987 // Shutdown the download manager. Mostly this is confirming a lack of 1048 // Shutdown the download manager. Mostly this is confirming a lack of
988 // crashes. 1049 // crashes.
989 DownloadManagerForShell(shell())->Shutdown(); 1050 DownloadManagerForShell(shell())->Shutdown();
990 } 1051 }
991 1052
992 // Test resumption with a response that contains strong validators. 1053 // Test resumption with a response that contains strong validators.
993 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_WithStrongValidators) { 1054 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, StrongValidators) {
994 base::CommandLine::ForCurrentProcess()->AppendSwitch(
995 switches::kEnableDownloadResumption);
996
997 TestDownloadRequestHandler request_handler; 1055 TestDownloadRequestHandler request_handler;
998 TestDownloadRequestHandler::Parameters parameters = 1056 TestDownloadRequestHandler::Parameters parameters =
999 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); 1057 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1000 const TestDownloadRequestHandler::InjectedError interruption = 1058 const TestDownloadRequestHandler::InjectedError interruption =
1001 parameters.injected_errors.front(); 1059 parameters.injected_errors.front();
1002 request_handler.StartServing(parameters); 1060 request_handler.StartServing(parameters);
1003 1061
1004 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); 1062 DownloadItem* download = StartDownloadAndReturnItem(
1063 initiator_shell_for_resumption(), request_handler.url());
1005 WaitForInterrupt(download); 1064 WaitForInterrupt(download);
1006 1065
1007 ASSERT_EQ(interruption.offset, download->GetReceivedBytes()); 1066 ASSERT_EQ(interruption.offset, download->GetReceivedBytes());
1008 ASSERT_EQ(parameters.size, download->GetTotalBytes()); 1067 ASSERT_EQ(parameters.size, download->GetTotalBytes());
1009 1068
1069 PrepareToResume();
1010 download->Resume(); 1070 download->Resume();
1011 WaitForCompletion(download); 1071 WaitForCompletion(download);
1012 1072
1013 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); 1073 ASSERT_EQ(parameters.size, download->GetReceivedBytes());
1014 ASSERT_EQ(parameters.size, download->GetTotalBytes()); 1074 ASSERT_EQ(parameters.size, download->GetTotalBytes());
1015 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents( 1075 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents(
1016 parameters.pattern_generator_seed, parameters.size, 1076 parameters.pattern_generator_seed, parameters.size,
1017 download->GetTargetFilePath())); 1077 download->GetTargetFilePath()));
1018 1078
1019 // Characterization risk: The next portion of the test examines the requests 1079 // Characterization risk: The next portion of the test examines the requests
(...skipping 22 matching lines...) Expand all
1042 net::HttpRequestHeaders::kRange, &value)); 1102 net::HttpRequestHeaders::kRange, &value));
1043 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset), 1103 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset),
1044 value); 1104 value);
1045 } 1105 }
1046 1106
1047 // A partial resumption results in an HTTP 200 response. I.e. the server ignored 1107 // A partial resumption results in an HTTP 200 response. I.e. the server ignored
1048 // the range request and sent the entire resource instead. For If-Range requests 1108 // the range request and sent the entire resource instead. For If-Range requests
1049 // (as opposed to If-Match), the behavior for a precondition failure is also to 1109 // (as opposed to If-Match), the behavior for a precondition failure is also to
1050 // respond with a 200. So this test case covers both validation failure and 1110 // respond with a 200. So this test case covers both validation failure and
1051 // ignoring the range request. 1111 // ignoring the range request.
1052 IN_PROC_BROWSER_TEST_F(DownloadContentTest, 1112 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest,
1053 Resume_RestartIfNotPartialResponse) { 1113 RestartIfNotPartialResponse) {
1054 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1055 switches::kEnableDownloadResumption);
1056 const int kOriginalPatternGeneratorSeed = 1; 1114 const int kOriginalPatternGeneratorSeed = 1;
1057 const int kNewPatternGeneratorSeed = 2; 1115 const int kNewPatternGeneratorSeed = 2;
1058 1116
1059 TestDownloadRequestHandler::Parameters parameters = 1117 TestDownloadRequestHandler::Parameters parameters =
1060 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); 1118 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1061 parameters.pattern_generator_seed = kOriginalPatternGeneratorSeed; 1119 parameters.pattern_generator_seed = kOriginalPatternGeneratorSeed;
1062 const TestDownloadRequestHandler::InjectedError interruption = 1120 const TestDownloadRequestHandler::InjectedError interruption =
1063 parameters.injected_errors.front(); 1121 parameters.injected_errors.front();
1064 1122
1065 TestDownloadRequestHandler request_handler; 1123 TestDownloadRequestHandler request_handler;
1066 request_handler.StartServing(parameters); 1124 request_handler.StartServing(parameters);
1067 1125
1068 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); 1126 DownloadItem* download = StartDownloadAndReturnItem(
1127 initiator_shell_for_resumption(), request_handler.url());
1069 WaitForInterrupt(download); 1128 WaitForInterrupt(download);
1070 1129
1071 ASSERT_EQ(interruption.offset, download->GetReceivedBytes()); 1130 ASSERT_EQ(interruption.offset, download->GetReceivedBytes());
1072 ASSERT_EQ(parameters.size, download->GetTotalBytes()); 1131 ASSERT_EQ(parameters.size, download->GetTotalBytes());
1073 1132
1074 parameters = TestDownloadRequestHandler::Parameters(); 1133 parameters = TestDownloadRequestHandler::Parameters();
1075 parameters.support_byte_ranges = false; 1134 parameters.support_byte_ranges = false;
1076 parameters.pattern_generator_seed = kNewPatternGeneratorSeed; 1135 parameters.pattern_generator_seed = kNewPatternGeneratorSeed;
1077 request_handler.StartServing(parameters); 1136 request_handler.StartServing(parameters);
1078 1137
1138 PrepareToResume();
1079 download->Resume(); 1139 download->Resume();
1080 WaitForCompletion(download); 1140 WaitForCompletion(download);
1081 1141
1082 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); 1142 ASSERT_EQ(parameters.size, download->GetReceivedBytes());
1083 ASSERT_EQ(parameters.size, download->GetTotalBytes()); 1143 ASSERT_EQ(parameters.size, download->GetTotalBytes());
1084 ASSERT_NO_FATAL_FAILURE( 1144 ASSERT_NO_FATAL_FAILURE(
1085 ReadAndVerifyFileContents(kNewPatternGeneratorSeed, parameters.size, 1145 ReadAndVerifyFileContents(kNewPatternGeneratorSeed, parameters.size,
1086 download->GetTargetFilePath())); 1146 download->GetTargetFilePath()));
1087 1147
1088 // When the downloads system sees the full response, it should accept the 1148 // When the downloads system sees the full response, it should accept the
(...skipping 17 matching lines...) Expand all
1106 net::HttpRequestHeaders::kIfRange, &value)); 1166 net::HttpRequestHeaders::kIfRange, &value));
1107 EXPECT_EQ(parameters.etag, value); 1167 EXPECT_EQ(parameters.etag, value);
1108 1168
1109 ASSERT_TRUE(requests[1].request_headers.GetHeader( 1169 ASSERT_TRUE(requests[1].request_headers.GetHeader(
1110 net::HttpRequestHeaders::kRange, &value)); 1170 net::HttpRequestHeaders::kRange, &value));
1111 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset), 1171 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset),
1112 value); 1172 value);
1113 } 1173 }
1114 1174
1115 // Confirm we restart if we don't have a verifier. 1175 // Confirm we restart if we don't have a verifier.
1116 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_RestartIfNoETag) { 1176 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, RestartIfNoETag) {
1117 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1118 switches::kEnableDownloadResumption);
1119 const int kOriginalPatternGeneratorSeed = 1; 1177 const int kOriginalPatternGeneratorSeed = 1;
1120 const int kNewPatternGeneratorSeed = 2; 1178 const int kNewPatternGeneratorSeed = 2;
1121 1179
1122 TestDownloadRequestHandler::Parameters parameters = 1180 TestDownloadRequestHandler::Parameters parameters =
1123 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); 1181 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1124 ASSERT_EQ(1u, parameters.injected_errors.size()); 1182 ASSERT_EQ(1u, parameters.injected_errors.size());
1125 parameters.etag.clear(); 1183 parameters.etag.clear();
1126 parameters.pattern_generator_seed = kOriginalPatternGeneratorSeed; 1184 parameters.pattern_generator_seed = kOriginalPatternGeneratorSeed;
1127 1185
1128 TestDownloadRequestHandler request_handler; 1186 TestDownloadRequestHandler request_handler;
1129 request_handler.StartServing(parameters); 1187 request_handler.StartServing(parameters);
1130 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); 1188 DownloadItem* download = StartDownloadAndReturnItem(
1189 initiator_shell_for_resumption(), request_handler.url());
1131 WaitForInterrupt(download); 1190 WaitForInterrupt(download);
1132 1191
1133 parameters.pattern_generator_seed = kNewPatternGeneratorSeed; 1192 parameters.pattern_generator_seed = kNewPatternGeneratorSeed;
1134 parameters.ClearInjectedErrors(); 1193 parameters.ClearInjectedErrors();
1135 request_handler.StartServing(parameters); 1194 request_handler.StartServing(parameters);
1136 1195
1196 PrepareToResume();
1137 download->Resume(); 1197 download->Resume();
1138 WaitForCompletion(download); 1198 WaitForCompletion(download);
1139 1199
1140 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); 1200 ASSERT_EQ(parameters.size, download->GetReceivedBytes());
1141 ASSERT_EQ(parameters.size, download->GetTotalBytes()); 1201 ASSERT_EQ(parameters.size, download->GetTotalBytes());
1142 ASSERT_NO_FATAL_FAILURE( 1202 ASSERT_NO_FATAL_FAILURE(
1143 ReadAndVerifyFileContents(kNewPatternGeneratorSeed, parameters.size, 1203 ReadAndVerifyFileContents(kNewPatternGeneratorSeed, parameters.size,
1144 download->GetTargetFilePath())); 1204 download->GetTargetFilePath()));
1145 1205
1146 TestDownloadRequestHandler::CompletedRequests requests; 1206 TestDownloadRequestHandler::CompletedRequests requests;
1147 request_handler.GetCompletedRequestInfo(&requests); 1207 request_handler.GetCompletedRequestInfo(&requests);
1148 1208
1149 // Neither If-Range nor Range headers should be present in the second request. 1209 // Neither If-Range nor Range headers should be present in the second request.
1150 ASSERT_EQ(2u, requests.size()); 1210 ASSERT_EQ(2u, requests.size());
1151 std::string value; 1211 std::string value;
1152 EXPECT_FALSE(requests[1].request_headers.GetHeader( 1212 EXPECT_FALSE(requests[1].request_headers.GetHeader(
1153 net::HttpRequestHeaders::kIfRange, &value)); 1213 net::HttpRequestHeaders::kIfRange, &value));
1154 EXPECT_FALSE(requests[1].request_headers.GetHeader( 1214 EXPECT_FALSE(requests[1].request_headers.GetHeader(
1155 net::HttpRequestHeaders::kRange, &value)); 1215 net::HttpRequestHeaders::kRange, &value));
1156 } 1216 }
1157 1217
1158 // Partial file goes missing before the download is resumed. The download should 1218 // Partial file goes missing before the download is resumed. The download should
1159 // restart. 1219 // restart.
1160 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_RestartIfNoPartialFile) { 1220 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, RestartIfNoPartialFile) {
1161 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1162 switches::kEnableDownloadResumption);
1163 TestDownloadRequestHandler::Parameters parameters = 1221 TestDownloadRequestHandler::Parameters parameters =
1164 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); 1222 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1165 1223
1166 TestDownloadRequestHandler request_handler; 1224 TestDownloadRequestHandler request_handler;
1167 request_handler.StartServing(parameters); 1225 request_handler.StartServing(parameters);
1168 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); 1226 DownloadItem* download = StartDownloadAndReturnItem(
1227 initiator_shell_for_resumption(), request_handler.url());
1169 WaitForInterrupt(download); 1228 WaitForInterrupt(download);
1170 1229
1171 // Delete the intermediate file. 1230 // Delete the intermediate file.
1172 ASSERT_TRUE(base::PathExists(download->GetFullPath())); 1231 ASSERT_TRUE(base::PathExists(download->GetFullPath()));
1173 ASSERT_TRUE(base::DeleteFile(download->GetFullPath(), false)); 1232 ASSERT_TRUE(base::DeleteFile(download->GetFullPath(), false));
1174 1233
1175 parameters.ClearInjectedErrors(); 1234 parameters.ClearInjectedErrors();
1176 request_handler.StartServing(parameters); 1235 request_handler.StartServing(parameters);
1177 1236
1237 PrepareToResume();
1178 download->Resume(); 1238 download->Resume();
1179 WaitForCompletion(download); 1239 WaitForCompletion(download);
1180 1240
1181 ASSERT_EQ(parameters.size, download->GetReceivedBytes()); 1241 ASSERT_EQ(parameters.size, download->GetReceivedBytes());
1182 ASSERT_EQ(parameters.size, download->GetTotalBytes()); 1242 ASSERT_EQ(parameters.size, download->GetTotalBytes());
1183 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents( 1243 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents(
1184 parameters.pattern_generator_seed, parameters.size, 1244 parameters.pattern_generator_seed, parameters.size,
1185 download->GetTargetFilePath())); 1245 download->GetTargetFilePath()));
1186 } 1246 }
1187 1247
1188 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Resume_RecoverFromInitFileError) { 1248 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest,
1189 base::CommandLine::ForCurrentProcess()->AppendSwitch( 1249 RecoverFromInitFileError) {
1190 switches::kEnableDownloadResumption);
1191 TestDownloadRequestHandler request_handler; 1250 TestDownloadRequestHandler request_handler;
1192 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); 1251 request_handler.StartServing(TestDownloadRequestHandler::Parameters());
1193 1252
1194 // Setup the error injector. 1253 // Setup the error injector.
1195 scoped_refptr<TestFileErrorInjector> injector( 1254 scoped_refptr<TestFileErrorInjector> injector(TestFileErrorInjector::Create(
1196 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); 1255 DownloadManagerForShell(initiator_shell_for_resumption())));
1197 1256
1198 const TestFileErrorInjector::FileErrorInfo err = { 1257 const TestFileErrorInjector::FileErrorInfo err = {
1199 request_handler.url().spec(), 1258 request_handler.url().spec(),
1200 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, 0, 1259 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, 0,
1201 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE}; 1260 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE};
1202 injector->AddError(err); 1261 injector->AddError(err);
1203 injector->InjectErrors(); 1262 injector->InjectErrors();
1204 1263
1205 // Start and watch for interrupt. 1264 // Start and watch for interrupt.
1206 DownloadItem* download(StartDownloadAndReturnItem(request_handler.url())); 1265 DownloadItem* download(StartDownloadAndReturnItem(
1266 initiator_shell_for_resumption(), request_handler.url()));
1207 WaitForInterrupt(download); 1267 WaitForInterrupt(download);
1208 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); 1268 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState());
1209 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, 1269 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE,
1210 download->GetLastReason()); 1270 download->GetLastReason());
1211 EXPECT_EQ(0, download->GetReceivedBytes()); 1271 EXPECT_EQ(0, download->GetReceivedBytes());
1212 EXPECT_TRUE(download->GetFullPath().empty()); 1272 EXPECT_TRUE(download->GetFullPath().empty());
1213 EXPECT_TRUE(download->GetTargetFilePath().empty()); 1273 EXPECT_TRUE(download->GetTargetFilePath().empty());
1214 1274
1215 // We need to make sure that any cross-thread downloads communication has 1275 // We need to make sure that any cross-thread downloads communication has
1216 // quiesced before clearing and injecting the new errors, as the 1276 // quiesced before clearing and injecting the new errors, as the
1217 // InjectErrors() routine alters the currently in use download file 1277 // InjectErrors() routine alters the currently in use download file
1218 // factory, which is a file thread object. 1278 // factory, which is a file thread object.
1219 RunAllPendingInMessageLoop(BrowserThread::FILE); 1279 RunAllPendingInMessageLoop(BrowserThread::FILE);
1220 RunAllPendingInMessageLoop(); 1280 RunAllPendingInMessageLoop();
1221 1281
1222 // Clear the old errors list. 1282 // Clear the old errors list.
1223 injector->ClearErrors(); 1283 injector->ClearErrors();
1224 injector->InjectErrors(); 1284 injector->InjectErrors();
1225 1285
1226 // Resume and watch completion. 1286 // Resume and watch completion.
1287 PrepareToResume();
1227 download->Resume(); 1288 download->Resume();
1228 WaitForCompletion(download); 1289 WaitForCompletion(download);
1229 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); 1290 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE);
1230 } 1291 }
1231 1292
1232 IN_PROC_BROWSER_TEST_F(DownloadContentTest, 1293 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest,
1233 Resume_RecoverFromIntermediateFileRenameError) { 1294 RecoverFromIntermediateFileRenameError) {
1234 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1235 switches::kEnableDownloadResumption);
1236 TestDownloadRequestHandler request_handler; 1295 TestDownloadRequestHandler request_handler;
1237 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); 1296 request_handler.StartServing(TestDownloadRequestHandler::Parameters());
1238 1297
1239 // Setup the error injector. 1298 // Setup the error injector.
1240 scoped_refptr<TestFileErrorInjector> injector( 1299 scoped_refptr<TestFileErrorInjector> injector(TestFileErrorInjector::Create(
1241 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); 1300 DownloadManagerForShell(initiator_shell_for_resumption())));
1242 1301
1243 const TestFileErrorInjector::FileErrorInfo err = { 1302 const TestFileErrorInjector::FileErrorInfo err = {
1244 request_handler.url().spec(), 1303 request_handler.url().spec(),
1245 TestFileErrorInjector::FILE_OPERATION_RENAME_UNIQUIFY, 0, 1304 TestFileErrorInjector::FILE_OPERATION_RENAME_UNIQUIFY, 0,
1246 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE}; 1305 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE};
1247 injector->AddError(err); 1306 injector->AddError(err);
1248 injector->InjectErrors(); 1307 injector->InjectErrors();
1249 1308
1250 // Start and watch for interrupt. 1309 // Start and watch for interrupt.
1251 DownloadItem* download(StartDownloadAndReturnItem(request_handler.url())); 1310 DownloadItem* download(StartDownloadAndReturnItem(
1311 initiator_shell_for_resumption(), request_handler.url()));
1252 WaitForInterrupt(download); 1312 WaitForInterrupt(download);
1253 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); 1313 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState());
1254 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, 1314 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE,
1255 download->GetLastReason()); 1315 download->GetLastReason());
1256 EXPECT_TRUE(download->GetFullPath().empty()); 1316 EXPECT_TRUE(download->GetFullPath().empty());
1257 // Target path will have been set after file name determination. GetFullPath() 1317 // Target path will have been set after file name determination. GetFullPath()
1258 // being empty is sufficient to signal that filename determination needs to be 1318 // being empty is sufficient to signal that filename determination needs to be
1259 // redone. 1319 // redone.
1260 EXPECT_FALSE(download->GetTargetFilePath().empty()); 1320 EXPECT_FALSE(download->GetTargetFilePath().empty());
1261 1321
1262 // We need to make sure that any cross-thread downloads communication has 1322 // We need to make sure that any cross-thread downloads communication has
1263 // quiesced before clearing and injecting the new errors, as the 1323 // quiesced before clearing and injecting the new errors, as the
1264 // InjectErrors() routine alters the currently in use download file 1324 // InjectErrors() routine alters the currently in use download file
1265 // factory, which is a file thread object. 1325 // factory, which is a file thread object.
1266 RunAllPendingInMessageLoop(BrowserThread::FILE); 1326 RunAllPendingInMessageLoop(BrowserThread::FILE);
1267 RunAllPendingInMessageLoop(); 1327 RunAllPendingInMessageLoop();
1268 1328
1269 // Clear the old errors list. 1329 // Clear the old errors list.
1270 injector->ClearErrors(); 1330 injector->ClearErrors();
1271 injector->InjectErrors(); 1331 injector->InjectErrors();
1272 1332
1333 PrepareToResume();
1273 download->Resume(); 1334 download->Resume();
1274 WaitForCompletion(download); 1335 WaitForCompletion(download);
1275 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); 1336 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE);
1276 } 1337 }
1277 1338
1278 IN_PROC_BROWSER_TEST_F(DownloadContentTest, 1339 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest,
1279 Resume_RecoverFromFinalRenameError) { 1340 RecoverFromFinalRenameError) {
1280 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1281 switches::kEnableDownloadResumption);
1282 TestDownloadRequestHandler request_handler; 1341 TestDownloadRequestHandler request_handler;
1283 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); 1342 request_handler.StartServing(TestDownloadRequestHandler::Parameters());
1284 1343
1285 // Setup the error injector. 1344 // Setup the error injector.
1286 scoped_refptr<TestFileErrorInjector> injector( 1345 scoped_refptr<TestFileErrorInjector> injector(TestFileErrorInjector::Create(
1287 TestFileErrorInjector::Create(DownloadManagerForShell(shell()))); 1346 DownloadManagerForShell(initiator_shell_for_resumption())));
1288 1347
1289 DownloadManagerForShell(shell())->RemoveAllDownloads(); 1348 DownloadManagerForShell(initiator_shell_for_resumption())
1349 ->RemoveAllDownloads();
1290 TestFileErrorInjector::FileErrorInfo err = { 1350 TestFileErrorInjector::FileErrorInfo err = {
1291 request_handler.url().spec(), 1351 request_handler.url().spec(),
1292 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE, 0, 1352 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE, 0,
1293 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE}; 1353 DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE};
1294 injector->AddError(err); 1354 injector->AddError(err);
1295 injector->InjectErrors(); 1355 injector->InjectErrors();
1296 1356
1297 // Start and watch for interrupt. 1357 // Start and watch for interrupt.
1298 DownloadItem* download(StartDownloadAndReturnItem(request_handler.url())); 1358 DownloadItem* download(StartDownloadAndReturnItem(
1359 initiator_shell_for_resumption(), request_handler.url()));
1299 WaitForInterrupt(download); 1360 WaitForInterrupt(download);
1300 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); 1361 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState());
1301 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, 1362 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE,
1302 download->GetLastReason()); 1363 download->GetLastReason());
1303 EXPECT_TRUE(download->GetFullPath().empty()); 1364 EXPECT_TRUE(download->GetFullPath().empty());
1304 // Target path should still be intact. 1365 // Target path should still be intact.
1305 EXPECT_FALSE(download->GetTargetFilePath().empty()); 1366 EXPECT_FALSE(download->GetTargetFilePath().empty());
1306 1367
1307 // We need to make sure that any cross-thread downloads communication has 1368 // We need to make sure that any cross-thread downloads communication has
1308 // quiesced before clearing and injecting the new errors, as the 1369 // quiesced before clearing and injecting the new errors, as the
1309 // InjectErrors() routine alters the currently in use download file 1370 // InjectErrors() routine alters the currently in use download file
1310 // factory, which is a file thread object. 1371 // factory, which is a file thread object.
1311 RunAllPendingInMessageLoop(BrowserThread::FILE); 1372 RunAllPendingInMessageLoop(BrowserThread::FILE);
1312 RunAllPendingInMessageLoop(); 1373 RunAllPendingInMessageLoop();
1313 1374
1314 // Clear the old errors list. 1375 // Clear the old errors list.
1315 injector->ClearErrors(); 1376 injector->ClearErrors();
1316 injector->InjectErrors(); 1377 injector->InjectErrors();
1317 1378
1379 PrepareToResume();
1318 download->Resume(); 1380 download->Resume();
1319 WaitForCompletion(download); 1381 WaitForCompletion(download);
1320 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); 1382 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE);
1321 } 1383 }
1322 1384
1323 // An interrupted download should remove the intermediate file when it is 1385 // An interrupted download should remove the intermediate file when it is
1324 // cancelled. 1386 // cancelled.
1325 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelInterruptedDownload) { 1387 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest,
1326 base::CommandLine::ForCurrentProcess()->AppendSwitch( 1388 CancelInterruptedDownload) {
1327 switches::kEnableDownloadResumption);
1328 TestDownloadRequestHandler request_handler; 1389 TestDownloadRequestHandler request_handler;
1329 request_handler.StartServing( 1390 request_handler.StartServing(
1330 TestDownloadRequestHandler::Parameters::WithSingleInterruption()); 1391 TestDownloadRequestHandler::Parameters::WithSingleInterruption());
1331 1392
1332 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); 1393 DownloadItem* download = StartDownloadAndReturnItem(
1394 initiator_shell_for_resumption(), request_handler.url());
1333 WaitForInterrupt(download); 1395 WaitForInterrupt(download);
1334 1396
1335 base::FilePath intermediate_path = download->GetFullPath(); 1397 base::FilePath intermediate_path = download->GetFullPath();
1336 ASSERT_FALSE(intermediate_path.empty()); 1398 ASSERT_FALSE(intermediate_path.empty());
1337 ASSERT_TRUE(base::PathExists(intermediate_path)); 1399 ASSERT_TRUE(base::PathExists(intermediate_path));
1338 1400
1339 download->Cancel(true /* user_cancel */); 1401 download->Cancel(true /* user_cancel */);
1340 RunAllPendingInMessageLoop(BrowserThread::FILE); 1402 RunAllPendingInMessageLoop(BrowserThread::FILE);
1341 RunAllPendingInMessageLoop(); 1403 RunAllPendingInMessageLoop();
1342 1404
1343 // The intermediate file should now be gone. 1405 // The intermediate file should now be gone.
1344 EXPECT_FALSE(base::PathExists(intermediate_path)); 1406 EXPECT_FALSE(base::PathExists(intermediate_path));
1345 EXPECT_TRUE(download->GetFullPath().empty()); 1407 EXPECT_TRUE(download->GetFullPath().empty());
1346 } 1408 }
1347 1409
1348 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveInterruptedDownload) { 1410 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest,
1349 base::CommandLine::ForCurrentProcess()->AppendSwitch( 1411 RemoveInterruptedDownload) {
1350 switches::kEnableDownloadResumption);
1351 TestDownloadRequestHandler request_handler; 1412 TestDownloadRequestHandler request_handler;
1352 request_handler.StartServing( 1413 request_handler.StartServing(
1353 TestDownloadRequestHandler::Parameters::WithSingleInterruption()); 1414 TestDownloadRequestHandler::Parameters::WithSingleInterruption());
1354 1415
1355 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); 1416 DownloadItem* download = StartDownloadAndReturnItem(
1417 initiator_shell_for_resumption(), request_handler.url());
1356 WaitForInterrupt(download); 1418 WaitForInterrupt(download);
1357 1419
1358 base::FilePath intermediate_path = download->GetFullPath(); 1420 base::FilePath intermediate_path = download->GetFullPath();
1359 ASSERT_FALSE(intermediate_path.empty()); 1421 ASSERT_FALSE(intermediate_path.empty());
1360 ASSERT_TRUE(base::PathExists(intermediate_path)); 1422 ASSERT_TRUE(base::PathExists(intermediate_path));
1361 1423
1362 download->Remove(); 1424 download->Remove();
1363 RunAllPendingInMessageLoop(BrowserThread::FILE); 1425 RunAllPendingInMessageLoop(BrowserThread::FILE);
1364 RunAllPendingInMessageLoop(); 1426 RunAllPendingInMessageLoop();
1365 1427
1366 // The intermediate file should now be gone. 1428 // The intermediate file should now be gone.
1367 EXPECT_FALSE(base::PathExists(intermediate_path)); 1429 EXPECT_FALSE(base::PathExists(intermediate_path));
1368 } 1430 }
1369 1431
1370 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveCompletedDownload) { 1432 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveCompletedDownload) {
1371 // A completed download shouldn't delete the downloaded file when it is 1433 // A completed download shouldn't delete the downloaded file when it is
1372 // removed. 1434 // removed.
1373 TestDownloadRequestHandler request_handler; 1435 TestDownloadRequestHandler request_handler;
1374 request_handler.StartServing(TestDownloadRequestHandler::Parameters()); 1436 request_handler.StartServing(TestDownloadRequestHandler::Parameters());
1375 scoped_ptr<DownloadTestObserver> completion_observer( 1437 scoped_ptr<DownloadTestObserver> completion_observer(
1376 CreateWaiter(shell(), 1)); 1438 CreateWaiter(shell(), 1));
1377 DownloadItem* download(StartDownloadAndReturnItem(request_handler.url())); 1439 DownloadItem* download(
1440 StartDownloadAndReturnItem(shell(), request_handler.url()));
1378 completion_observer->WaitForFinished(); 1441 completion_observer->WaitForFinished();
1379 1442
1380 // The target path should exist. 1443 // The target path should exist.
1381 base::FilePath target_path(download->GetTargetFilePath()); 1444 base::FilePath target_path(download->GetTargetFilePath());
1382 EXPECT_TRUE(base::PathExists(target_path)); 1445 EXPECT_TRUE(base::PathExists(target_path));
1383 download->Remove(); 1446 download->Remove();
1384 RunAllPendingInMessageLoop(BrowserThread::FILE); 1447 RunAllPendingInMessageLoop(BrowserThread::FILE);
1385 RunAllPendingInMessageLoop(); 1448 RunAllPendingInMessageLoop();
1386 1449
1387 // The file should still exist. 1450 // The file should still exist.
1388 EXPECT_TRUE(base::PathExists(target_path)); 1451 EXPECT_TRUE(base::PathExists(target_path));
1389 } 1452 }
1390 1453
1391 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { 1454 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, RemoveResumingDownload) {
1392 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1393 switches::kEnableDownloadResumption);
1394 TestDownloadRequestHandler::Parameters parameters = 1455 TestDownloadRequestHandler::Parameters parameters =
1395 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); 1456 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1396 TestDownloadRequestHandler request_handler; 1457 TestDownloadRequestHandler request_handler;
1397 request_handler.StartServing(parameters); 1458 request_handler.StartServing(parameters);
1398 1459
1399 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); 1460 DownloadItem* download = StartDownloadAndReturnItem(
1461 initiator_shell_for_resumption(), request_handler.url());
1400 WaitForInterrupt(download); 1462 WaitForInterrupt(download);
1401 1463
1402 base::FilePath intermediate_path(download->GetFullPath()); 1464 base::FilePath intermediate_path(download->GetFullPath());
1403 ASSERT_FALSE(intermediate_path.empty()); 1465 ASSERT_FALSE(intermediate_path.empty());
1404 EXPECT_TRUE(base::PathExists(intermediate_path)); 1466 EXPECT_TRUE(base::PathExists(intermediate_path));
1405 1467
1406 // Resume and remove download. We expect only a single OnDownloadCreated() 1468 // Resume and remove download. We expect only a single OnDownloadCreated()
1407 // call, and that's for the second download created below. 1469 // call, and that's for the second download created below.
1408 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); 1470 MockDownloadManagerObserver dm_observer(
1471 DownloadManagerForShell(initiator_shell_for_resumption()));
1409 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); 1472 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1);
1410 1473
1411 TestRequestStartHandler request_start_handler; 1474 TestRequestStartHandler request_start_handler;
1412 parameters.on_start_handler = request_start_handler.GetOnStartHandler(); 1475 parameters.on_start_handler = request_start_handler.GetOnStartHandler();
1413 request_handler.StartServing(parameters); 1476 request_handler.StartServing(parameters);
1414 1477
1478 PrepareToResume();
1415 download->Resume(); 1479 download->Resume();
1416 request_start_handler.WaitForCallback(); 1480 request_start_handler.WaitForCallback();
1417 1481
1418 // At this point, the download resumption request has been sent out, but the 1482 // At this point, the download resumption request has been sent out, but the
1419 // reponse hasn't been received yet. 1483 // reponse hasn't been received yet.
1420 download->Remove(); 1484 download->Remove();
1421 1485
1422 request_start_handler.RespondWith(std::string(), net::OK); 1486 request_start_handler.RespondWith(std::string(), net::OK);
1423 1487
1424 // The intermediate file should now be gone. 1488 // The intermediate file should now be gone.
1425 RunAllPendingInMessageLoop(BrowserThread::FILE); 1489 RunAllPendingInMessageLoop(BrowserThread::FILE);
1426 RunAllPendingInMessageLoop(); 1490 RunAllPendingInMessageLoop();
1427 EXPECT_FALSE(base::PathExists(intermediate_path)); 1491 EXPECT_FALSE(base::PathExists(intermediate_path));
1428 1492
1429 parameters.ClearInjectedErrors(); 1493 parameters.ClearInjectedErrors();
1430 parameters.on_start_handler.Reset(); 1494 parameters.on_start_handler.Reset();
1431 request_handler.StartServing(parameters); 1495 request_handler.StartServing(parameters);
1432 1496
1433 // Start the second download and wait until it's done. This exercises the 1497 // Start the second download and wait until it's done. This exercises the
1434 // entire downloads stack and effectively flushes all of our worker threads. 1498 // entire downloads stack and effectively flushes all of our worker threads.
1435 // We are testing whether the URL request created in the previous 1499 // We are testing whether the URL request created in the previous
1436 // DownloadItem::Resume() call reulted in a new download or not. 1500 // DownloadItem::Resume() call reulted in a new download or not.
1437 NavigateToURLAndWaitForDownload(shell(), request_handler.url(), 1501 NavigateToURLAndWaitForDownload(shell(), request_handler.url(),
1438 DownloadItem::COMPLETE); 1502 DownloadItem::COMPLETE);
1439 EXPECT_TRUE(EnsureNoPendingDownloads()); 1503 EXPECT_TRUE(EnsureNoPendingDownloads());
1440 } 1504 }
1441 1505
1442 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumingDownload) { 1506 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, CancelResumingDownload) {
1443 base::CommandLine::ForCurrentProcess()->AppendSwitch(
1444 switches::kEnableDownloadResumption);
1445 TestDownloadRequestHandler::Parameters parameters = 1507 TestDownloadRequestHandler::Parameters parameters =
1446 TestDownloadRequestHandler::Parameters::WithSingleInterruption(); 1508 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1447 TestDownloadRequestHandler request_handler; 1509 TestDownloadRequestHandler request_handler;
1448 request_handler.StartServing(parameters); 1510 request_handler.StartServing(parameters);
1449 1511
1450 DownloadItem* download = StartDownloadAndReturnItem(request_handler.url()); 1512 DownloadItem* download = StartDownloadAndReturnItem(
1513 initiator_shell_for_resumption(), request_handler.url());
1451 WaitForInterrupt(download); 1514 WaitForInterrupt(download);
1452 1515
1453 base::FilePath intermediate_path(download->GetFullPath()); 1516 base::FilePath intermediate_path(download->GetFullPath());
1454 ASSERT_FALSE(intermediate_path.empty()); 1517 ASSERT_FALSE(intermediate_path.empty());
1455 EXPECT_TRUE(base::PathExists(intermediate_path)); 1518 EXPECT_TRUE(base::PathExists(intermediate_path));
1456 1519
1457 // Resume and remove download. We expect only a single OnDownloadCreated() 1520 // Resume and remove download. We expect only a single OnDownloadCreated()
1458 // call, and that's for the second download created below. 1521 // call, and that's for the second download created below.
1459 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); 1522 MockDownloadManagerObserver dm_observer(
1523 DownloadManagerForShell(initiator_shell_for_resumption()));
1460 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); 1524 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1);
1461 1525
1462 TestRequestStartHandler request_start_handler; 1526 TestRequestStartHandler request_start_handler;
1463 parameters.on_start_handler = request_start_handler.GetOnStartHandler(); 1527 parameters.on_start_handler = request_start_handler.GetOnStartHandler();
1464 request_handler.StartServing(parameters); 1528 request_handler.StartServing(parameters);
1465 1529
1530 PrepareToResume();
1466 download->Resume(); 1531 download->Resume();
1467 request_start_handler.WaitForCallback(); 1532 request_start_handler.WaitForCallback();
1468 1533
1469 // At this point, the download item has initiated a network request for the 1534 // At this point, the download item has initiated a network request for the
1470 // resumption attempt, but hasn't received a response yet. 1535 // resumption attempt, but hasn't received a response yet.
1471 download->Cancel(true /* user_cancel */); 1536 download->Cancel(true /* user_cancel */);
1472 1537
1473 request_start_handler.RespondWith(std::string(), net::OK); 1538 request_start_handler.RespondWith(std::string(), net::OK);
1474 1539
1475 // The intermediate file should now be gone. 1540 // The intermediate file should now be gone.
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1647 1712
1648 std::vector<DownloadItem*> downloads; 1713 std::vector<DownloadItem*> downloads;
1649 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); 1714 DownloadManagerForShell(shell())->GetAllDownloads(&downloads);
1650 ASSERT_EQ(1u, downloads.size()); 1715 ASSERT_EQ(1u, downloads.size());
1651 1716
1652 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"), 1717 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"),
1653 downloads[0]->GetTargetFilePath().BaseName().value()); 1718 downloads[0]->GetTargetFilePath().BaseName().value());
1654 } 1719 }
1655 1720
1656 } // namespace content 1721 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698