OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/download/download_file_manager.h" | 5 #include "content/browser/download/download_file_manager.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "content/browser/browser_thread_impl.h" | 12 #include "content/browser/browser_thread_impl.h" |
13 #include "content/browser/download/download_buffer.h" | |
14 #include "content/browser/download/download_create_info.h" | 13 #include "content/browser/download/download_create_info.h" |
15 #include "content/browser/download/download_interrupt_reasons_impl.h" | 14 #include "content/browser/download/download_interrupt_reasons_impl.h" |
16 #include "content/browser/download/download_request_handle.h" | 15 #include "content/browser/download/download_request_handle.h" |
17 #include "content/browser/download/mock_download_file.h" | 16 #include "content/browser/download/mock_download_file.h" |
18 #include "content/public/browser/download_id.h" | 17 #include "content/public/browser/download_id.h" |
19 #include "content/test/mock_download_manager.h" | 18 #include "content/test/mock_download_manager.h" |
20 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
21 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 static const int32 kDummyDownloadId; | 119 static const int32 kDummyDownloadId; |
121 static const int32 kDummyDownloadId2; | 120 static const int32 kDummyDownloadId2; |
122 static const int kDummyChildId; | 121 static const int kDummyChildId; |
123 static const int kDummyRequestId; | 122 static const int kDummyRequestId; |
124 | 123 |
125 // We need a UI |BrowserThread| in order to destruct |download_manager_|, | 124 // We need a UI |BrowserThread| in order to destruct |download_manager_|, |
126 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, | 125 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, |
127 // calling Release() on |download_manager_| won't ever result in its | 126 // calling Release() on |download_manager_| won't ever result in its |
128 // destructor being called and we get a leak. | 127 // destructor being called and we get a leak. |
129 DownloadFileManagerTest() | 128 DownloadFileManagerTest() |
130 : download_buffer_(new content::DownloadBuffer()), | 129 : ui_thread_(BrowserThread::UI, &loop_), |
131 ui_thread_(BrowserThread::UI, &loop_), | |
132 file_thread_(BrowserThread::FILE, &loop_) { | 130 file_thread_(BrowserThread::FILE, &loop_) { |
133 } | 131 } |
134 | 132 |
135 ~DownloadFileManagerTest() { | 133 ~DownloadFileManagerTest() { |
136 } | 134 } |
137 | 135 |
138 virtual void SetUp() { | 136 virtual void SetUp() { |
139 download_manager_ = new MockDownloadManager(); | 137 download_manager_ = new MockDownloadManager(); |
140 request_handle_.reset(new MockDownloadRequestHandle(download_manager_)); | 138 request_handle_.reset(new MockDownloadRequestHandle(download_manager_)); |
141 download_file_factory_ = new MockDownloadFileFactory; | 139 download_file_factory_ = new MockDownloadFileFactory; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 .Times(AtLeast(1)) | 187 .Times(AtLeast(1)) |
190 .WillRepeatedly(Return(false)); | 188 .WillRepeatedly(Return(false)); |
191 EXPECT_CALL(*download_manager_, StartDownload(id.local())); | 189 EXPECT_CALL(*download_manager_, StartDownload(id.local())); |
192 | 190 |
193 download_file_manager_->StartDownload(info, *request_handle_); | 191 download_file_manager_->StartDownload(info, *request_handle_); |
194 ProcessAllPendingMessages(); | 192 ProcessAllPendingMessages(); |
195 | 193 |
196 ClearExpectations(id); | 194 ClearExpectations(id); |
197 } | 195 } |
198 | 196 |
199 // Creates a new |net::IOBuffer| of size |length| with |data|, and attaches | |
200 // it to the download buffer. | |
201 bool UpdateBuffer(const char* data, size_t length) { | |
202 // We are passing ownership of this buffer to the download file manager. | |
203 // NOTE: we are padding io_buffer with one extra character so that the | |
204 // mock testing framework can treat it as a NULL-delimited string. | |
205 net::IOBuffer* io_buffer = new net::IOBuffer(length + 1); | |
206 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. | |
207 io_buffer->AddRef(); | |
208 memcpy(io_buffer->data(), data, length); | |
209 io_buffer->data()[length] = 0; | |
210 | |
211 download_buffer_->AddData(io_buffer, length); | |
212 | |
213 return true; | |
214 } | |
215 | |
216 // Updates the download file. | |
217 // |id| is the download ID of the download file. | |
218 // |data| is the buffer containing the data. | |
219 // |length| is the length of the data buffer. | |
220 // |error_to_insert| is the error to simulate. For no error, use net::OK. | |
221 void UpdateDownload(const DownloadId& id, | |
222 const char* data, | |
223 size_t length, | |
224 net::Error error_to_insert) { | |
225 // Expected call sequence: | |
226 // Create DownloadBuffer | |
227 // UpdateDownload | |
228 // GetDownloadFile | |
229 // DownloadFile::AppendDataToFile | |
230 // | |
231 // On error: | |
232 // DownloadFile::GetDownloadManager | |
233 // DownloadFile::BytesSoFar | |
234 // CancelDownload | |
235 // Process one message in the message loop | |
236 // DownloadManager::OnDownloadInterrupted | |
237 EXPECT_TRUE(UpdateBuffer(data, length)); | |
238 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | |
239 ASSERT_TRUE(file != NULL); | |
240 byte_count_[id] += length; | |
241 | |
242 // Make sure our comparison string is NULL-terminated. | |
243 char* expected_data = new char[length + 1]; | |
244 memcpy(expected_data, data, length); | |
245 expected_data[length] = 0; | |
246 | |
247 EXPECT_CALL(*file, AppendDataToFile(StrEq(expected_data), length)) | |
248 .Times(1) | |
249 .WillOnce(Return(error_to_insert)); | |
250 | |
251 if (error_to_insert != net::OK) { | |
252 EXPECT_CALL(*file, GetDownloadManager()) | |
253 .Times(AtLeast(1)); | |
254 EXPECT_CALL(*file, BytesSoFar()) | |
255 .Times(AtLeast(1)) | |
256 .WillRepeatedly(Return(byte_count_[id])); | |
257 EXPECT_CALL(*file, GetHashState()) | |
258 .Times(AtLeast(1)); | |
259 EXPECT_CALL(*file, Cancel()); | |
260 } | |
261 | |
262 download_file_manager_->UpdateDownload(id, download_buffer_.get()); | |
263 | |
264 if (error_to_insert != net::OK) { | |
265 EXPECT_CALL(*download_manager_, | |
266 OnDownloadInterrupted( | |
267 id.local(), | |
268 byte_count_[id], | |
269 "", | |
270 content::ConvertNetErrorToInterruptReason( | |
271 error_to_insert, content::DOWNLOAD_INTERRUPT_FROM_DISK))); | |
272 | |
273 ProcessAllPendingMessages(); | |
274 ++error_count_[id]; | |
275 } | |
276 | |
277 ClearExpectations(id); | |
278 delete [] expected_data; | |
279 } | |
280 | |
281 // Renames the download file. | 197 // Renames the download file. |
282 // |id| is the download ID of the download file. | 198 // |id| is the download ID of the download file. |
283 // |new_path| is the new file path. | 199 // |new_path| is the new file path. |
284 // |unique_path| is the actual path that the download file will be | 200 // |unique_path| is the actual path that the download file will be |
285 // renamed to. If there is an existing file at | 201 // renamed to. If there is an existing file at |
286 // |new_path| and |replace| is false, then |new_path| | 202 // |new_path| and |replace| is false, then |new_path| |
287 // will be uniquified. | 203 // will be uniquified. |
288 // |rename_error| is the error to inject. For no error, use net::OK. | 204 // |rename_error| is the error to inject. For no error, use net::OK. |
289 // |state| whether we are renaming an in-progress download or a | 205 // |state| whether we are renaming an in-progress download or a |
290 // completed download. | 206 // completed download. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { | 304 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
389 EXPECT_CALL(*file, GetHash(_)) | 305 EXPECT_CALL(*file, GetHash(_)) |
390 .WillOnce(Return(false)); | 306 .WillOnce(Return(false)); |
391 } else { | 307 } else { |
392 EXPECT_CALL(*file, GetHashState()); | 308 EXPECT_CALL(*file, GetHashState()); |
393 } | 309 } |
394 EXPECT_CALL(*file, BytesSoFar()) | 310 EXPECT_CALL(*file, BytesSoFar()) |
395 .Times(AtLeast(1)) | 311 .Times(AtLeast(1)) |
396 .WillRepeatedly(Return(byte_count_[id])); | 312 .WillRepeatedly(Return(byte_count_[id])); |
397 | 313 |
398 download_file_manager_->OnResponseCompleted(id, reason, security_string); | |
399 | |
400 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { | 314 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
401 EXPECT_CALL(*download_manager_, | 315 EXPECT_CALL(*download_manager_, |
402 OnResponseCompleted(id.local(), byte_count_[id], "")) | 316 OnResponseCompleted(id.local(), byte_count_[id], "")) |
403 .Times(1); | 317 .Times(1); |
404 } else { | 318 } else { |
405 EXPECT_EQ(0, error_count_[id]); | 319 EXPECT_EQ(0, error_count_[id]); |
406 | 320 |
407 EXPECT_CALL(*download_manager_, | 321 EXPECT_CALL(*download_manager_, |
408 OnDownloadInterrupted(id.local(), | 322 OnDownloadInterrupted(id.local(), |
409 byte_count_[id], | 323 byte_count_[id], |
(...skipping 25 matching lines...) Expand all Loading... |
435 download_file_manager_->CancelDownload(id); | 349 download_file_manager_->CancelDownload(id); |
436 | 350 |
437 EXPECT_TRUE(NULL == download_file_manager_->GetDownloadFile(id)); | 351 EXPECT_TRUE(NULL == download_file_manager_->GetDownloadFile(id)); |
438 } | 352 } |
439 | 353 |
440 protected: | 354 protected: |
441 scoped_refptr<MockDownloadManager> download_manager_; | 355 scoped_refptr<MockDownloadManager> download_manager_; |
442 scoped_ptr<MockDownloadRequestHandle> request_handle_; | 356 scoped_ptr<MockDownloadRequestHandle> request_handle_; |
443 MockDownloadFileFactory* download_file_factory_; | 357 MockDownloadFileFactory* download_file_factory_; |
444 scoped_refptr<DownloadFileManager> download_file_manager_; | 358 scoped_refptr<DownloadFileManager> download_file_manager_; |
445 scoped_refptr<content::DownloadBuffer> download_buffer_; | |
446 | 359 |
447 // Per-download statistics. | 360 // Per-download statistics. |
448 std::map<DownloadId, int64> byte_count_; | 361 std::map<DownloadId, int64> byte_count_; |
449 std::map<DownloadId, int> error_count_; | 362 std::map<DownloadId, int> error_count_; |
450 | 363 |
451 private: | 364 private: |
452 MessageLoop loop_; | 365 MessageLoop loop_; |
453 | 366 |
454 // UI thread. | 367 // UI thread. |
455 BrowserThreadImpl ui_thread_; | 368 BrowserThreadImpl ui_thread_; |
(...skipping 24 matching lines...) Expand all Loading... |
480 CleanUp(dummy_id); | 393 CleanUp(dummy_id); |
481 } | 394 } |
482 | 395 |
483 TEST_F(DownloadFileManagerTest, CancelBeforeFinished) { | 396 TEST_F(DownloadFileManagerTest, CancelBeforeFinished) { |
484 // Same as StartDownload, at first. | 397 // Same as StartDownload, at first. |
485 DownloadCreateInfo* info = new DownloadCreateInfo; | 398 DownloadCreateInfo* info = new DownloadCreateInfo; |
486 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 399 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
487 | 400 |
488 StartDownload(info, dummy_id); | 401 StartDownload(info, dummy_id); |
489 | 402 |
490 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
491 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
492 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
493 | |
494 CleanUp(dummy_id); | 403 CleanUp(dummy_id); |
495 } | 404 } |
496 | 405 |
497 TEST_F(DownloadFileManagerTest, DownloadWithError) { | 406 TEST_F(DownloadFileManagerTest, DownloadWithError) { |
498 // Same as StartDownload, at first. | 407 // Same as StartDownload, at first. |
499 DownloadCreateInfo* info = new DownloadCreateInfo; | 408 DownloadCreateInfo* info = new DownloadCreateInfo; |
500 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 409 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
501 | 410 |
502 StartDownload(info, dummy_id); | 411 StartDownload(info, dummy_id); |
503 | 412 |
504 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
505 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), | |
506 net::ERR_FILE_NO_SPACE); | |
507 } | 413 } |
508 | 414 |
509 TEST_F(DownloadFileManagerTest, CompleteDownload) { | 415 TEST_F(DownloadFileManagerTest, CompleteDownload) { |
510 // Same as StartDownload, at first. | 416 // Same as StartDownload, at first. |
511 DownloadCreateInfo* info = new DownloadCreateInfo; | 417 DownloadCreateInfo* info = new DownloadCreateInfo; |
512 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 418 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
513 | 419 |
514 StartDownload(info, dummy_id); | 420 StartDownload(info, dummy_id); |
515 | 421 |
516 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
517 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
518 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
519 | |
520 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 422 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
521 | 423 |
522 CleanUp(dummy_id); | 424 CleanUp(dummy_id); |
523 } | 425 } |
524 | 426 |
525 TEST_F(DownloadFileManagerTest, CompleteDownloadWithError) { | 427 TEST_F(DownloadFileManagerTest, CompleteDownloadWithError) { |
526 // Same as StartDownload, at first. | 428 // Same as StartDownload, at first. |
527 DownloadCreateInfo* info = new DownloadCreateInfo; | 429 DownloadCreateInfo* info = new DownloadCreateInfo; |
528 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 430 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
529 | 431 |
530 StartDownload(info, dummy_id); | 432 StartDownload(info, dummy_id); |
531 | 433 |
532 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
533 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
534 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
535 | |
536 OnResponseCompleted(dummy_id, | 434 OnResponseCompleted(dummy_id, |
537 content::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED, | 435 content::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED, |
538 ""); | 436 ""); |
539 | 437 |
540 CleanUp(dummy_id); | 438 CleanUp(dummy_id); |
541 } | 439 } |
542 | 440 |
543 TEST_F(DownloadFileManagerTest, RenameInProgress) { | 441 TEST_F(DownloadFileManagerTest, RenameInProgress) { |
544 // Same as StartDownload, at first. | 442 // Same as StartDownload, at first. |
545 DownloadCreateInfo* info = new DownloadCreateInfo; | 443 DownloadCreateInfo* info = new DownloadCreateInfo; |
546 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 444 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
547 ScopedTempDir download_dir; | 445 ScopedTempDir download_dir; |
548 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 446 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
549 | 447 |
550 StartDownload(info, dummy_id); | 448 StartDownload(info, dummy_id); |
551 | 449 |
552 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
553 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
554 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 450 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
555 RenameFile(dummy_id, foo, foo, net::OK, IN_PROGRESS, OVERWRITE); | 451 RenameFile(dummy_id, foo, foo, net::OK, IN_PROGRESS, OVERWRITE); |
556 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
557 | 452 |
558 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 453 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
559 | 454 |
560 CleanUp(dummy_id); | 455 CleanUp(dummy_id); |
561 } | 456 } |
562 | 457 |
563 TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { | 458 TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { |
564 // Same as StartDownload, at first. | 459 // Same as StartDownload, at first. |
565 DownloadCreateInfo* info = new DownloadCreateInfo; | 460 DownloadCreateInfo* info = new DownloadCreateInfo; |
566 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 461 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
567 ScopedTempDir download_dir; | 462 ScopedTempDir download_dir; |
568 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 463 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
569 | 464 |
570 StartDownload(info, dummy_id); | 465 StartDownload(info, dummy_id); |
571 | 466 |
572 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
573 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
574 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 467 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
575 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, | 468 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, |
576 IN_PROGRESS, OVERWRITE); | 469 IN_PROGRESS, OVERWRITE); |
577 | 470 |
578 CleanUp(dummy_id); | 471 CleanUp(dummy_id); |
579 } | 472 } |
580 | 473 |
581 TEST_F(DownloadFileManagerTest, RenameCompleting) { | 474 TEST_F(DownloadFileManagerTest, RenameCompleting) { |
582 // Same as StartDownload, at first. | 475 // Same as StartDownload, at first. |
583 DownloadCreateInfo* info = new DownloadCreateInfo; | 476 DownloadCreateInfo* info = new DownloadCreateInfo; |
584 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 477 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
585 ScopedTempDir download_dir; | 478 ScopedTempDir download_dir; |
586 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 479 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
587 | 480 |
588 StartDownload(info, dummy_id); | 481 StartDownload(info, dummy_id); |
589 | 482 |
590 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
591 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
592 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
593 | |
594 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 483 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
595 | 484 |
596 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 485 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
597 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | 486 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); |
598 | 487 |
599 CleanUp(dummy_id); | 488 CleanUp(dummy_id); |
600 } | 489 } |
601 | 490 |
602 TEST_F(DownloadFileManagerTest, RenameCompletingWithUniquification) { | 491 TEST_F(DownloadFileManagerTest, RenameCompletingWithUniquification) { |
603 // Same as StartDownload, at first. | 492 // Same as StartDownload, at first. |
604 DownloadCreateInfo* info = new DownloadCreateInfo; | 493 DownloadCreateInfo* info = new DownloadCreateInfo; |
605 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 494 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
606 ScopedTempDir download_dir; | 495 ScopedTempDir download_dir; |
607 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 496 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
608 | 497 |
609 StartDownload(info, dummy_id); | 498 StartDownload(info, dummy_id); |
610 | 499 |
611 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
612 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
613 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
614 | |
615 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 500 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
616 | 501 |
617 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 502 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
618 FilePath unique_foo(foo.InsertBeforeExtension(FILE_PATH_LITERAL(" (1)"))); | 503 FilePath unique_foo(foo.InsertBeforeExtension(FILE_PATH_LITERAL(" (1)"))); |
619 // Create a file at |foo|. Since we are specifying DONT_OVERWRITE, | 504 // Create a file at |foo|. Since we are specifying DONT_OVERWRITE, |
620 // RenameCompletingDownloadFile() should pick "foo (1).txt" instead of | 505 // RenameCompletingDownloadFile() should pick "foo (1).txt" instead of |
621 // overwriting this file. | 506 // overwriting this file. |
622 ASSERT_EQ(0, file_util::WriteFile(foo, "", 0)); | 507 ASSERT_EQ(0, file_util::WriteFile(foo, "", 0)); |
623 RenameFile(dummy_id, foo, unique_foo, net::OK, COMPLETE, DONT_OVERWRITE); | 508 RenameFile(dummy_id, foo, unique_foo, net::OK, COMPLETE, DONT_OVERWRITE); |
624 | 509 |
625 CleanUp(dummy_id); | 510 CleanUp(dummy_id); |
626 } | 511 } |
627 | 512 |
628 TEST_F(DownloadFileManagerTest, RenameCompletingWithError) { | 513 TEST_F(DownloadFileManagerTest, RenameCompletingWithError) { |
629 // Same as StartDownload, at first. | 514 // Same as StartDownload, at first. |
630 DownloadCreateInfo* info = new DownloadCreateInfo; | 515 DownloadCreateInfo* info = new DownloadCreateInfo; |
631 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 516 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
632 ScopedTempDir download_dir; | 517 ScopedTempDir download_dir; |
633 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 518 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
634 | 519 |
635 StartDownload(info, dummy_id); | 520 StartDownload(info, dummy_id); |
636 | 521 |
637 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
638 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
639 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
640 | |
641 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 522 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
642 | 523 |
643 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 524 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
644 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, | 525 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, |
645 COMPLETE, OVERWRITE); | 526 COMPLETE, OVERWRITE); |
646 | 527 |
647 CleanUp(dummy_id); | 528 CleanUp(dummy_id); |
648 } | 529 } |
649 | 530 |
650 TEST_F(DownloadFileManagerTest, RenameTwice) { | 531 TEST_F(DownloadFileManagerTest, RenameTwice) { |
651 // Same as StartDownload, at first. | 532 // Same as StartDownload, at first. |
652 DownloadCreateInfo* info = new DownloadCreateInfo; | 533 DownloadCreateInfo* info = new DownloadCreateInfo; |
653 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 534 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
654 ScopedTempDir download_dir; | 535 ScopedTempDir download_dir; |
655 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 536 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
656 | 537 |
657 StartDownload(info, dummy_id); | 538 StartDownload(info, dummy_id); |
658 | 539 |
659 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
660 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
661 FilePath crfoo(download_dir.path().Append( | 540 FilePath crfoo(download_dir.path().Append( |
662 FILE_PATH_LITERAL("foo.txt.crdownload"))); | 541 FILE_PATH_LITERAL("foo.txt.crdownload"))); |
663 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); | 542 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); |
664 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
665 | 543 |
666 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 544 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
667 | 545 |
668 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 546 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
669 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | 547 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); |
670 | 548 |
671 CleanUp(dummy_id); | 549 CleanUp(dummy_id); |
672 } | 550 } |
673 | 551 |
674 TEST_F(DownloadFileManagerTest, TwoDownloads) { | 552 TEST_F(DownloadFileManagerTest, TwoDownloads) { |
675 // Same as StartDownload, at first. | 553 // Same as StartDownload, at first. |
676 DownloadCreateInfo* info = new DownloadCreateInfo; | 554 DownloadCreateInfo* info = new DownloadCreateInfo; |
677 DownloadCreateInfo* info2 = new DownloadCreateInfo; | 555 DownloadCreateInfo* info2 = new DownloadCreateInfo; |
678 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 556 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
679 DownloadId dummy_id2(download_manager_.get(), kDummyDownloadId2); | 557 DownloadId dummy_id2(download_manager_.get(), kDummyDownloadId2); |
680 ScopedTempDir download_dir; | 558 ScopedTempDir download_dir; |
681 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 559 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
682 | 560 |
683 StartDownload(info, dummy_id); | 561 StartDownload(info, dummy_id); |
684 | 562 |
685 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
686 | 563 |
687 StartDownload(info2, dummy_id2); | 564 StartDownload(info2, dummy_id2); |
688 | 565 |
689 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
690 | |
691 UpdateDownload(dummy_id2, kTestData4, strlen(kTestData4), net::OK); | |
692 FilePath crbar(download_dir.path().Append( | 566 FilePath crbar(download_dir.path().Append( |
693 FILE_PATH_LITERAL("bar.txt.crdownload"))); | 567 FILE_PATH_LITERAL("bar.txt.crdownload"))); |
694 RenameFile(dummy_id2, crbar, crbar, net::OK, IN_PROGRESS, OVERWRITE); | 568 RenameFile(dummy_id2, crbar, crbar, net::OK, IN_PROGRESS, OVERWRITE); |
695 | 569 |
696 FilePath crfoo(download_dir.path().Append( | 570 FilePath crfoo(download_dir.path().Append( |
697 FILE_PATH_LITERAL("foo.txt.crdownload"))); | 571 FILE_PATH_LITERAL("foo.txt.crdownload"))); |
698 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); | 572 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); |
699 | 573 |
700 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
701 | |
702 UpdateDownload(dummy_id2, kTestData5, strlen(kTestData5), net::OK); | |
703 UpdateDownload(dummy_id2, kTestData6, strlen(kTestData6), net::OK); | |
704 | |
705 OnResponseCompleted(dummy_id2, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 574 OnResponseCompleted(dummy_id2, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
706 | 575 |
707 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 576 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
708 | 577 |
709 FilePath bar(download_dir.path().Append(FILE_PATH_LITERAL("bar.txt"))); | 578 FilePath bar(download_dir.path().Append(FILE_PATH_LITERAL("bar.txt"))); |
710 RenameFile(dummy_id2, bar, bar, net::OK, COMPLETE, OVERWRITE); | 579 RenameFile(dummy_id2, bar, bar, net::OK, COMPLETE, OVERWRITE); |
711 | 580 |
712 CleanUp(dummy_id2); | 581 CleanUp(dummy_id2); |
713 | 582 |
714 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 583 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
715 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | 584 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); |
716 | 585 |
717 CleanUp(dummy_id); | 586 CleanUp(dummy_id); |
718 } | 587 } |
719 | 588 |
720 | 589 |
721 // TODO(ahendrickson) -- A test for updating progress. | 590 // TODO(ahendrickson) -- A test for updating progress. |
722 // Expected call sequence: | 591 // Expected call sequence: |
723 // UpdateInProgressDownloads | 592 // UpdateInProgressDownloads |
724 // DownloadFile::GetDownloadFile | 593 // DownloadFile::GetDownloadFile |
725 // Process one message in the message loop | 594 // Process one message in the message loop |
726 // DownloadManager::UpdateDownload | 595 // DownloadManager::UpdateDownload |
727 | 596 |
728 // TODO(ahendrickson) -- A test for download manager shutdown. | 597 // TODO(ahendrickson) -- A test for download manager shutdown. |
729 // Expected call sequence: | 598 // Expected call sequence: |
730 // OnDownloadManagerShutdown | 599 // OnDownloadManagerShutdown |
731 // DownloadFile::GetDownloadManager | 600 // DownloadFile::GetDownloadManager |
732 // DownloadFile::CancelDownloadRequest | 601 // DownloadFile::CancelDownloadRequest |
733 // DownloadFile::~DownloadFile | 602 // DownloadFile::~DownloadFile |
OLD | NEW |