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

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

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 4 years, 10 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 | 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 // 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 <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <utility> 10 #include <utility>
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) { 111 static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) {
112 // We're in a content_browsertest; we know that the DownloadManager 112 // We're in a content_browsertest; we know that the DownloadManager
113 // is a DownloadManagerImpl. 113 // is a DownloadManagerImpl.
114 return static_cast<DownloadManagerImpl*>( 114 return static_cast<DownloadManagerImpl*>(
115 BrowserContext::GetDownloadManager( 115 BrowserContext::GetDownloadManager(
116 shell->web_contents()->GetBrowserContext())); 116 shell->web_contents()->GetBrowserContext()));
117 } 117 }
118 118
119 class DownloadFileWithDelay : public DownloadFileImpl { 119 class DownloadFileWithDelay : public DownloadFileImpl {
120 public: 120 public:
121 DownloadFileWithDelay( 121 DownloadFileWithDelay(const DownloadSaveInfo& save_info,
122 scoped_ptr<DownloadSaveInfo> save_info, 122 const base::FilePath& default_download_directory,
123 const base::FilePath& default_download_directory, 123 const GURL& url,
124 const GURL& url, 124 const GURL& referrer_url,
125 const GURL& referrer_url, 125 bool calculate_hash,
126 bool calculate_hash, 126 base::File file,
127 scoped_ptr<ByteStreamReader> stream, 127 scoped_ptr<ByteStreamReader> stream,
128 const net::BoundNetLog& bound_net_log, 128 const net::BoundNetLog& bound_net_log,
129 scoped_ptr<PowerSaveBlocker> power_save_blocker, 129 scoped_ptr<PowerSaveBlocker> power_save_blocker,
130 base::WeakPtr<DownloadDestinationObserver> observer, 130 base::WeakPtr<DownloadDestinationObserver> observer,
131 base::WeakPtr<DownloadFileWithDelayFactory> owner); 131 base::WeakPtr<DownloadFileWithDelayFactory> owner);
132 132
133 ~DownloadFileWithDelay() override; 133 ~DownloadFileWithDelay() override;
134 134
135 // Wraps DownloadFileImpl::Rename* and intercepts the return callback, 135 // Wraps DownloadFileImpl::Rename* and intercepts the return callback,
136 // storing it in the factory that produced this object for later 136 // storing it in the factory that produced this object for later
137 // retrieval. 137 // retrieval.
138 void RenameAndUniquify(const base::FilePath& full_path, 138 void RenameAndUniquify(const base::FilePath& full_path,
139 const RenameCompletionCallback& callback) override; 139 const RenameCompletionCallback& callback) override;
140 void RenameAndAnnotate(const base::FilePath& full_path, 140 void RenameAndAnnotate(const base::FilePath& full_path,
141 const RenameCompletionCallback& callback) override; 141 const RenameCompletionCallback& callback) override;
(...skipping 16 matching lines...) Expand all
158 }; 158 };
159 159
160 // All routines on this class must be called on the UI thread. 160 // All routines on this class must be called on the UI thread.
161 class DownloadFileWithDelayFactory : public DownloadFileFactory { 161 class DownloadFileWithDelayFactory : public DownloadFileFactory {
162 public: 162 public:
163 DownloadFileWithDelayFactory(); 163 DownloadFileWithDelayFactory();
164 ~DownloadFileWithDelayFactory() override; 164 ~DownloadFileWithDelayFactory() override;
165 165
166 // DownloadFileFactory interface. 166 // DownloadFileFactory interface.
167 DownloadFile* CreateFile( 167 DownloadFile* CreateFile(
168 scoped_ptr<DownloadSaveInfo> save_info, 168 const DownloadSaveInfo& save_info,
169 const base::FilePath& default_download_directory, 169 const base::FilePath& default_download_directory,
170 const GURL& url, 170 const GURL& url,
171 const GURL& referrer_url, 171 const GURL& referrer_url,
172 bool calculate_hash, 172 bool calculate_hash,
173 base::File file,
173 scoped_ptr<ByteStreamReader> stream, 174 scoped_ptr<ByteStreamReader> stream,
174 const net::BoundNetLog& bound_net_log, 175 const net::BoundNetLog& bound_net_log,
175 base::WeakPtr<DownloadDestinationObserver> observer) override; 176 base::WeakPtr<DownloadDestinationObserver> observer) override;
176 177
177 void AddRenameCallback(base::Closure callback); 178 void AddRenameCallback(base::Closure callback);
178 void GetAllRenameCallbacks(std::vector<base::Closure>* results); 179 void GetAllRenameCallbacks(std::vector<base::Closure>* results);
179 180
180 // Do not return until GetAllRenameCallbacks() will return a non-empty list. 181 // Do not return until GetAllRenameCallbacks() will return a non-empty list.
181 void WaitForSomeCallback(); 182 void WaitForSomeCallback();
182 183
183 private: 184 private:
184 std::vector<base::Closure> rename_callbacks_; 185 std::vector<base::Closure> rename_callbacks_;
185 bool waiting_; 186 bool waiting_;
186 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; 187 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_;
187 188
188 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); 189 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory);
189 }; 190 };
190 191
191 DownloadFileWithDelay::DownloadFileWithDelay( 192 DownloadFileWithDelay::DownloadFileWithDelay(
192 scoped_ptr<DownloadSaveInfo> save_info, 193 const DownloadSaveInfo& save_info,
193 const base::FilePath& default_download_directory, 194 const base::FilePath& default_download_directory,
194 const GURL& url, 195 const GURL& url,
195 const GURL& referrer_url, 196 const GURL& referrer_url,
196 bool calculate_hash, 197 bool calculate_hash,
198 base::File file,
197 scoped_ptr<ByteStreamReader> stream, 199 scoped_ptr<ByteStreamReader> stream,
198 const net::BoundNetLog& bound_net_log, 200 const net::BoundNetLog& bound_net_log,
199 scoped_ptr<PowerSaveBlocker> power_save_blocker, 201 scoped_ptr<PowerSaveBlocker> power_save_blocker,
200 base::WeakPtr<DownloadDestinationObserver> observer, 202 base::WeakPtr<DownloadDestinationObserver> observer,
201 base::WeakPtr<DownloadFileWithDelayFactory> owner) 203 base::WeakPtr<DownloadFileWithDelayFactory> owner)
202 : DownloadFileImpl(std::move(save_info), 204 : DownloadFileImpl(save_info,
203 default_download_directory, 205 default_download_directory,
204 url, 206 url,
205 referrer_url, 207 referrer_url,
206 calculate_hash, 208 calculate_hash,
209 std::move(file),
207 std::move(stream), 210 std::move(stream),
208 bound_net_log, 211 bound_net_log,
209 observer), 212 observer),
210 owner_(owner) {} 213 owner_(owner) {}
211 214
212 DownloadFileWithDelay::~DownloadFileWithDelay() {} 215 DownloadFileWithDelay::~DownloadFileWithDelay() {}
213 216
214 void DownloadFileWithDelay::RenameAndUniquify( 217 void DownloadFileWithDelay::RenameAndUniquify(
215 const base::FilePath& full_path, 218 const base::FilePath& full_path,
216 const RenameCompletionCallback& callback) { 219 const RenameCompletionCallback& callback) {
(...skipping 23 matching lines...) Expand all
240 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); 243 factory->AddRenameCallback(base::Bind(original_callback, reason, path));
241 } 244 }
242 245
243 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() 246 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory()
244 : waiting_(false), 247 : waiting_(false),
245 weak_ptr_factory_(this) {} 248 weak_ptr_factory_(this) {}
246 249
247 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} 250 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {}
248 251
249 DownloadFile* DownloadFileWithDelayFactory::CreateFile( 252 DownloadFile* DownloadFileWithDelayFactory::CreateFile(
250 scoped_ptr<DownloadSaveInfo> save_info, 253 const DownloadSaveInfo& save_info,
251 const base::FilePath& default_download_directory, 254 const base::FilePath& default_download_directory,
252 const GURL& url, 255 const GURL& url,
253 const GURL& referrer_url, 256 const GURL& referrer_url,
254 bool calculate_hash, 257 bool calculate_hash,
258 base::File file,
255 scoped_ptr<ByteStreamReader> stream, 259 scoped_ptr<ByteStreamReader> stream,
256 const net::BoundNetLog& bound_net_log, 260 const net::BoundNetLog& bound_net_log,
257 base::WeakPtr<DownloadDestinationObserver> observer) { 261 base::WeakPtr<DownloadDestinationObserver> observer) {
258 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create( 262 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create(
259 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 263 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
260 PowerSaveBlocker::kReasonOther, "Download in progress")); 264 PowerSaveBlocker::kReasonOther, "Download in progress"));
261 return new DownloadFileWithDelay( 265 return new DownloadFileWithDelay(
262 std::move(save_info), default_download_directory, url, referrer_url, 266 save_info, default_download_directory, url, referrer_url, calculate_hash,
263 calculate_hash, std::move(stream), bound_net_log, std::move(psb), 267 std::move(file), std::move(stream), bound_net_log, std::move(psb),
264 observer, weak_ptr_factory_.GetWeakPtr()); 268 observer, weak_ptr_factory_.GetWeakPtr());
265 } 269 }
266 270
267 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { 271 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) {
268 DCHECK_CURRENTLY_ON(BrowserThread::UI); 272 DCHECK_CURRENTLY_ON(BrowserThread::UI);
269 rename_callbacks_.push_back(callback); 273 rename_callbacks_.push_back(callback);
270 if (waiting_) 274 if (waiting_)
271 base::MessageLoopForUI::current()->QuitWhenIdle(); 275 base::MessageLoopForUI::current()->QuitWhenIdle();
272 } 276 }
273 277
274 void DownloadFileWithDelayFactory::GetAllRenameCallbacks( 278 void DownloadFileWithDelayFactory::GetAllRenameCallbacks(
275 std::vector<base::Closure>* results) { 279 std::vector<base::Closure>* results) {
276 DCHECK_CURRENTLY_ON(BrowserThread::UI); 280 DCHECK_CURRENTLY_ON(BrowserThread::UI);
277 results->swap(rename_callbacks_); 281 results->swap(rename_callbacks_);
278 } 282 }
279 283
280 void DownloadFileWithDelayFactory::WaitForSomeCallback() { 284 void DownloadFileWithDelayFactory::WaitForSomeCallback() {
281 DCHECK_CURRENTLY_ON(BrowserThread::UI); 285 DCHECK_CURRENTLY_ON(BrowserThread::UI);
282 286
283 if (rename_callbacks_.empty()) { 287 if (rename_callbacks_.empty()) {
284 waiting_ = true; 288 waiting_ = true;
285 RunMessageLoop(); 289 RunMessageLoop();
286 waiting_ = false; 290 waiting_ = false;
287 } 291 }
288 } 292 }
289 293
290 class CountingDownloadFile : public DownloadFileImpl { 294 class CountingDownloadFile : public DownloadFileImpl {
291 public: 295 public:
292 CountingDownloadFile(scoped_ptr<DownloadSaveInfo> save_info, 296 CountingDownloadFile(const DownloadSaveInfo& save_info,
293 const base::FilePath& default_downloads_directory, 297 const base::FilePath& default_downloads_directory,
294 const GURL& url, 298 const GURL& url,
295 const GURL& referrer_url, 299 const GURL& referrer_url,
296 bool calculate_hash, 300 bool calculate_hash,
301 base::File file,
297 scoped_ptr<ByteStreamReader> stream, 302 scoped_ptr<ByteStreamReader> stream,
298 const net::BoundNetLog& bound_net_log, 303 const net::BoundNetLog& bound_net_log,
299 scoped_ptr<PowerSaveBlocker> power_save_blocker, 304 scoped_ptr<PowerSaveBlocker> power_save_blocker,
300 base::WeakPtr<DownloadDestinationObserver> observer) 305 base::WeakPtr<DownloadDestinationObserver> observer)
301 : DownloadFileImpl(std::move(save_info), 306 : DownloadFileImpl(save_info,
302 default_downloads_directory, 307 default_downloads_directory,
303 url, 308 url,
304 referrer_url, 309 referrer_url,
305 calculate_hash, 310 calculate_hash,
311 std::move(file),
306 std::move(stream), 312 std::move(stream),
307 bound_net_log, 313 bound_net_log,
308 observer) {} 314 observer) {}
309 315
310 ~CountingDownloadFile() override { 316 ~CountingDownloadFile() override {
311 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 317 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
312 active_files_--; 318 active_files_--;
313 } 319 }
314 320
315 void Initialize(const InitializeCallback& callback) override { 321 void Initialize(const InitializeCallback& callback) override {
(...skipping 26 matching lines...) Expand all
342 348
343 int CountingDownloadFile::active_files_ = 0; 349 int CountingDownloadFile::active_files_ = 0;
344 350
345 class CountingDownloadFileFactory : public DownloadFileFactory { 351 class CountingDownloadFileFactory : public DownloadFileFactory {
346 public: 352 public:
347 CountingDownloadFileFactory() {} 353 CountingDownloadFileFactory() {}
348 ~CountingDownloadFileFactory() override {} 354 ~CountingDownloadFileFactory() override {}
349 355
350 // DownloadFileFactory interface. 356 // DownloadFileFactory interface.
351 DownloadFile* CreateFile( 357 DownloadFile* CreateFile(
352 scoped_ptr<DownloadSaveInfo> save_info, 358 const DownloadSaveInfo& save_info,
353 const base::FilePath& default_downloads_directory, 359 const base::FilePath& default_downloads_directory,
354 const GURL& url, 360 const GURL& url,
355 const GURL& referrer_url, 361 const GURL& referrer_url,
356 bool calculate_hash, 362 bool calculate_hash,
363 base::File file,
357 scoped_ptr<ByteStreamReader> stream, 364 scoped_ptr<ByteStreamReader> stream,
358 const net::BoundNetLog& bound_net_log, 365 const net::BoundNetLog& bound_net_log,
359 base::WeakPtr<DownloadDestinationObserver> observer) override { 366 base::WeakPtr<DownloadDestinationObserver> observer) override {
360 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create( 367 scoped_ptr<PowerSaveBlocker> psb(PowerSaveBlocker::Create(
361 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, 368 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
362 PowerSaveBlocker::kReasonOther, "Download in progress")); 369 PowerSaveBlocker::kReasonOther, "Download in progress"));
363 return new CountingDownloadFile( 370 return new CountingDownloadFile(save_info, default_downloads_directory, url,
364 std::move(save_info), default_downloads_directory, url, referrer_url, 371 referrer_url, calculate_hash,
365 calculate_hash, std::move(stream), bound_net_log, std::move(psb), 372 std::move(file), std::move(stream),
366 observer); 373 bound_net_log, std::move(psb), observer);
367 } 374 }
368 }; 375 };
369 376
370 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { 377 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate {
371 public: 378 public:
372 TestShellDownloadManagerDelegate() 379 TestShellDownloadManagerDelegate()
373 : delay_download_open_(false) {} 380 : delay_download_open_(false) {}
374 ~TestShellDownloadManagerDelegate() override {} 381 ~TestShellDownloadManagerDelegate() override {}
375 382
376 bool ShouldOpenDownload( 383 bool ShouldOpenDownload(
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 ASSERT_TRUE(requests[1].request_headers.GetHeader( 1123 ASSERT_TRUE(requests[1].request_headers.GetHeader(
1117 net::HttpRequestHeaders::kIfRange, &value)); 1124 net::HttpRequestHeaders::kIfRange, &value));
1118 EXPECT_EQ(parameters.etag, value); 1125 EXPECT_EQ(parameters.etag, value);
1119 1126
1120 ASSERT_TRUE(requests[1].request_headers.GetHeader( 1127 ASSERT_TRUE(requests[1].request_headers.GetHeader(
1121 net::HttpRequestHeaders::kRange, &value)); 1128 net::HttpRequestHeaders::kRange, &value));
1122 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset), 1129 EXPECT_EQ(base::StringPrintf("bytes=%" PRId64 "-", interruption.offset),
1123 value); 1130 value);
1124 } 1131 }
1125 1132
1133 // Resumption should only attempt to contact the final URL if the download has a
1134 // URL chain.
1135 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, RedirectBeforeResume) {
1136 TestDownloadRequestHandler request_handler_1(
1137 GURL("http://example.com/first-url"));
1138 request_handler_1.StartServingStaticResponse(
1139 "HTTP/1.1 302 Redirect\r\n"
1140 "Location: http://example.com/second-url\r\n"
1141 "\r\n");
1142
1143 TestDownloadRequestHandler request_handler_2(
1144 GURL("http://example.com/second-url"));
1145 request_handler_2.StartServingStaticResponse(
1146 "HTTP/1.1 302 Redirect\r\n"
1147 "Location: http://example.com/third-url\r\n"
1148 "\r\n");
1149
1150 TestDownloadRequestHandler request_handler_3(
1151 GURL("http://example.com/third-url"));
1152 request_handler_3.StartServingStaticResponse(
1153 "HTTP/1.1 302 Redirect\r\n"
1154 "Location: http://example.com/download\r\n"
1155 "\r\n");
1156
1157 TestDownloadRequestHandler resumable_request_handler(
1158 GURL("http://example.com/download"));
1159 TestDownloadRequestHandler::Parameters parameters =
1160 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1161 resumable_request_handler.StartServing(parameters);
1162
1163 DownloadItem* download = StartDownloadAndReturnItem(
1164 initiator_shell_for_resumption(), request_handler_1.url());
1165 WaitForInterrupt(download);
1166
1167 EXPECT_EQ(4u, download->GetUrlChain().size());
1168 EXPECT_EQ(request_handler_1.url(), download->GetOriginalUrl());
1169 EXPECT_EQ(resumable_request_handler.url(), download->GetURL());
1170
1171 // Now that the download is interrupted, make all intermediate servers return
1172 // a 404. The only way a resumption request would succeed if the resumption
1173 // request is sent to the final server in the chain.
1174 const char k404Response[] = "HTTP/1.1 404 Not found\r\n\r\n";
1175 request_handler_1.StartServingStaticResponse(k404Response);
1176 request_handler_2.StartServingStaticResponse(k404Response);
1177 request_handler_3.StartServingStaticResponse(k404Response);
1178
1179 PrepareToResume();
1180 download->Resume();
1181 WaitForCompletion(download);
1182
1183 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents(
1184 parameters.pattern_generator_seed, parameters.size,
1185 download->GetTargetFilePath()));
1186 }
1187
1188 // If the server response for the resumption request specifies a bad range (i.e.
1189 // not the range that was requested or an invalid or missing Content-Range
1190 // header), then the download should be marked as interrupted again without
1191 // discarding the partial state.
1192 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, BadRangeHeader) {
1193 TestDownloadRequestHandler request_handler;
1194 TestDownloadRequestHandler::Parameters parameters =
1195 TestDownloadRequestHandler::Parameters::WithSingleInterruption();
1196 request_handler.StartServing(parameters);
1197
1198 DownloadItem* download = StartDownloadAndReturnItem(
1199 initiator_shell_for_resumption(), request_handler.url());
1200 WaitForInterrupt(download);
1201
1202 // Upon resumption, the server starts responding with a bad range header.
1203 request_handler.StartServingStaticResponse(
1204 "HTTP/1.1 206 Partial Content\r\n"
1205 "Content-Range: bytes 1000000-2000000/3000000\r\n"
1206 "\r\n");
1207 PrepareToResume();
1208 download->Resume();
1209 WaitForInterrupt(download);
1210 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT,
1211 download->GetLastReason());
1212
1213 // Or this time, the server sends a response with an invalid Content-Range
1214 // header.
1215 request_handler.StartServingStaticResponse(
1216 "HTTP/1.1 206 Partial Content\r\n"
1217 "Content-Range: ooga-booga-booga-booga\r\n"
1218 "\r\n");
1219 download->Resume();
1220 WaitForInterrupt(download);
1221 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT,
1222 download->GetLastReason());
1223
1224 // Or no Content-Range header at all.
1225 request_handler.StartServingStaticResponse(
1226 "HTTP/1.1 206 Partial Content\r\n"
1227 "Some-Headers: ooga-booga-booga-booga\r\n"
1228 "\r\n");
1229 download->Resume();
1230 WaitForInterrupt(download);
1231 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT,
1232 download->GetLastReason());
1233
1234 // Back to the original request handler. Resumption should now succeed, and
1235 // use the partial data it had prior to the first interruption.
1236 request_handler.StartServing(parameters);
1237 download->Resume();
1238 WaitForCompletion(download);
1239
1240 ASSERT_EQ(parameters.size, download->GetReceivedBytes());
1241 ASSERT_EQ(parameters.size, download->GetTotalBytes());
1242 ASSERT_NO_FATAL_FAILURE(ReadAndVerifyFileContents(
1243 parameters.pattern_generator_seed, parameters.size,
1244 download->GetTargetFilePath()));
1245
1246 // Characterization risk: The next portion of the test examines the requests
1247 // that were sent out while downloading our resource. These requests
1248 // correspond to the requests that were generated by the browser and the
1249 // downloads system and may change as implementation details change.
1250 TestDownloadRequestHandler::CompletedRequests requests;
1251 request_handler.GetCompletedRequestInfo(&requests);
1252
1253 ASSERT_EQ(5u, requests.size());
1254
1255 // None of the request should have transferred the entire resource.
1256 EXPECT_GT(parameters.size, requests[0].transferred_byte_count);
1257 EXPECT_EQ(0, requests[1].transferred_byte_count);
1258 EXPECT_EQ(0, requests[2].transferred_byte_count);
1259 EXPECT_EQ(0, requests[3].transferred_byte_count);
1260 EXPECT_GT(parameters.size, requests[4].transferred_byte_count);
1261 }
1262
1126 // A partial resumption results in an HTTP 200 response. I.e. the server ignored 1263 // A partial resumption results in an HTTP 200 response. I.e. the server ignored
1127 // the range request and sent the entire resource instead. For If-Range requests 1264 // the range request and sent the entire resource instead. For If-Range requests
1128 // (as opposed to If-Match), the behavior for a precondition failure is also to 1265 // (as opposed to If-Match), the behavior for a precondition failure is also to
1129 // respond with a 200. So this test case covers both validation failure and 1266 // respond with a 200. So this test case covers both validation failure and
1130 // ignoring the range request. 1267 // ignoring the range request.
1131 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest, 1268 IN_PROC_BROWSER_TEST_P(DownloadResumptionContentTest,
1132 RestartIfNotPartialResponse) { 1269 RestartIfNotPartialResponse) {
1133 const int kOriginalPatternGeneratorSeed = 1; 1270 const int kOriginalPatternGeneratorSeed = 1;
1134 const int kNewPatternGeneratorSeed = 2; 1271 const int kNewPatternGeneratorSeed = 2;
1135 1272
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
1282 1419
1283 // Start and watch for interrupt. 1420 // Start and watch for interrupt.
1284 DownloadItem* download(StartDownloadAndReturnItem( 1421 DownloadItem* download(StartDownloadAndReturnItem(
1285 initiator_shell_for_resumption(), request_handler.url())); 1422 initiator_shell_for_resumption(), request_handler.url()));
1286 WaitForInterrupt(download); 1423 WaitForInterrupt(download);
1287 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState()); 1424 ASSERT_EQ(DownloadItem::INTERRUPTED, download->GetState());
1288 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE, 1425 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE,
1289 download->GetLastReason()); 1426 download->GetLastReason());
1290 EXPECT_EQ(0, download->GetReceivedBytes()); 1427 EXPECT_EQ(0, download->GetReceivedBytes());
1291 EXPECT_TRUE(download->GetFullPath().empty()); 1428 EXPECT_TRUE(download->GetFullPath().empty());
1292 EXPECT_TRUE(download->GetTargetFilePath().empty()); 1429 EXPECT_FALSE(download->GetTargetFilePath().empty());
1293 1430
1294 // We need to make sure that any cross-thread downloads communication has 1431 // We need to make sure that any cross-thread downloads communication has
1295 // quiesced before clearing and injecting the new errors, as the 1432 // quiesced before clearing and injecting the new errors, as the
1296 // InjectErrors() routine alters the currently in use download file 1433 // InjectErrors() routine alters the currently in use download file
1297 // factory, which is a file thread object. 1434 // factory, which is a file thread object.
1298 RunAllPendingInMessageLoop(BrowserThread::FILE); 1435 RunAllPendingInMessageLoop(BrowserThread::FILE);
1299 RunAllPendingInMessageLoop(); 1436 RunAllPendingInMessageLoop();
1300 1437
1301 // Clear the old errors list. 1438 // Clear the old errors list.
1302 injector->ClearErrors(); 1439 injector->ClearErrors();
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after
1827 std::vector<DownloadItem*> downloads; 1964 std::vector<DownloadItem*> downloads;
1828 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); 1965 DownloadManagerForShell(shell())->GetAllDownloads(&downloads);
1829 ASSERT_EQ(1u, downloads.size()); 1966 ASSERT_EQ(1u, downloads.size());
1830 1967
1831 EXPECT_EQ(FILE_PATH_LITERAL("suggested-filename"), 1968 EXPECT_EQ(FILE_PATH_LITERAL("suggested-filename"),
1832 downloads[0]->GetTargetFilePath().BaseName().value()); 1969 downloads[0]->GetTargetFilePath().BaseName().value());
1833 ASSERT_TRUE(origin_one.ShutdownAndWaitUntilComplete()); 1970 ASSERT_TRUE(origin_one.ShutdownAndWaitUntilComplete());
1834 ASSERT_TRUE(origin_two.ShutdownAndWaitUntilComplete()); 1971 ASSERT_TRUE(origin_two.ShutdownAndWaitUntilComplete());
1835 } 1972 }
1836 1973
1974 // A request for a non-existent resource should still result in a DownloadItem
1975 // that's created in an interrupted state.
1976 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeServerError) {
1977 ASSERT_TRUE(embedded_test_server()->Start());
1978
1979 GURL download_url =
1980 embedded_test_server()->GetURL("/download/does-not-exist");
1981 GURL document_url = embedded_test_server()->GetURL(
1982 std::string("/download/download-attribute.html?target=") +
1983 download_url.spec());
1984
1985 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url);
1986 WaitForInterrupt(download);
1987
1988 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT,
1989 download->GetLastReason());
1990 }
1991
1992 namespace {
1993
1994 void ErrorReturningRequestHandler(
1995 const net::HttpRequestHeaders& headers,
1996 const TestDownloadRequestHandler::OnStartResponseCallback& callback) {
1997 callback.Run(std::string(), net::ERR_INTERNET_DISCONNECTED);
1998 }
1999
2000 } // namespace
2001
2002 // A request that fails before it gets a response from the server should also
2003 // result in a DownloadItem that's created in an interrupted state.
2004 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeNetworkError) {
2005 ASSERT_TRUE(embedded_test_server()->Start());
2006 TestDownloadRequestHandler request_handler;
2007 TestDownloadRequestHandler::Parameters parameters;
2008
2009 parameters.on_start_handler = base::Bind(&ErrorReturningRequestHandler);
2010 request_handler.StartServing(parameters);
2011
2012 GURL document_url = embedded_test_server()->GetURL(
2013 std::string("/download/download-attribute.html?target=") +
2014 request_handler.url().spec());
2015 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url);
2016 WaitForInterrupt(download);
2017
2018 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED,
2019 download->GetLastReason());
2020 }
2021
2022 // A request that fails due to it being rejected by policy should result ina
2023 // DownloadItem that's created in an interrupted state.
2024 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadAttributeInvalidURL) {
2025 ASSERT_TRUE(embedded_test_server()->Start());
2026
2027 GURL document_url = embedded_test_server()->GetURL(
2028 "/download/download-attribute.html?target=about:version");
2029 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url);
2030 WaitForInterrupt(download);
2031
2032 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST,
2033 download->GetLastReason());
2034 }
2035
1837 // The file empty.bin is served with a MIME type of application/octet-stream. 2036 // The file empty.bin is served with a MIME type of application/octet-stream.
1838 // The content body is empty. Make sure this case is handled properly and we 2037 // The content body is empty. Make sure this case is handled properly and we
1839 // don't regress on http://crbug.com/320394. 2038 // don't regress on http://crbug.com/320394.
1840 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadGZipWithNoContent) { 2039 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadGZipWithNoContent) {
1841 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("empty.bin"); 2040 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("empty.bin");
1842 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE); 2041 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE);
1843 // That's it. This should work without crashing. 2042 // That's it. This should work without crashing.
1844 } 2043 }
1845 2044
1846 // Make sure that sniffed MIME types are correctly passed through to the 2045 // Make sure that sniffed MIME types are correctly passed through to the
1847 // download item. 2046 // download item.
1848 IN_PROC_BROWSER_TEST_F(DownloadContentTest, SniffedMimeType) { 2047 IN_PROC_BROWSER_TEST_F(DownloadContentTest, SniffedMimeType) {
1849 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.gz"); 2048 GURL url = net::URLRequestMockHTTPJob::GetMockUrl("gzip-content.gz");
1850 DownloadItem* item = StartDownloadAndReturnItem(shell(), url); 2049 DownloadItem* item = StartDownloadAndReturnItem(shell(), url);
1851 WaitForCompletion(item); 2050 WaitForCompletion(item);
1852 2051
1853 EXPECT_STREQ("application/x-gzip", item->GetMimeType().c_str()); 2052 EXPECT_STREQ("application/x-gzip", item->GetMimeType().c_str());
1854 EXPECT_TRUE(item->GetOriginalMimeType().empty()); 2053 EXPECT_TRUE(item->GetOriginalMimeType().empty());
1855 } 2054 }
1856 2055
1857 IN_PROC_BROWSER_TEST_F(DownloadContentTest, Spam) { 2056 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DuplicateContentDisposition) {
1858 ASSERT_TRUE(embedded_test_server()->Start()); 2057 ASSERT_TRUE(embedded_test_server()->Start());
1859 2058
2059 // double-content-disposition.txt is served with two Content-Disposition
2060 // headers, both of which are identical.
1860 NavigateToURLAndWaitForDownload( 2061 NavigateToURLAndWaitForDownload(
1861 shell(), 2062 shell(),
1862 embedded_test_server()->GetURL( 2063 embedded_test_server()->GetURL(
1863 "/download/double-content-disposition.txt"), 2064 "/download/double-content-disposition.txt"),
1864 DownloadItem::COMPLETE); 2065 DownloadItem::COMPLETE);
1865 2066
1866 std::vector<DownloadItem*> downloads; 2067 std::vector<DownloadItem*> downloads;
1867 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); 2068 DownloadManagerForShell(shell())->GetAllDownloads(&downloads);
1868 ASSERT_EQ(1u, downloads.size()); 2069 ASSERT_EQ(1u, downloads.size());
1869 2070
1870 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"), 2071 EXPECT_EQ(FILE_PATH_LITERAL("Jumboshrimp.txt"),
1871 downloads[0]->GetTargetFilePath().BaseName().value()); 2072 downloads[0]->GetTargetFilePath().BaseName().value());
1872 } 2073 }
1873 2074
1874 } // namespace content 2075 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698