OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/i18n/number_formatting.h" | 10 #include "base/i18n/number_formatting.h" |
11 #include "base/i18n/rtl.h" | 11 #include "base/i18n/rtl.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/string16.h" | 14 #include "base/string16.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 18 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
19 #include "chrome/browser/download/download_item_model.h" | 19 #include "chrome/browser/download/download_item_model.h" |
20 #include "chrome/browser/download/download_prefs.h" | 20 #include "chrome/browser/download/download_prefs.h" |
21 #include "chrome/browser/download/download_util.h" | 21 #include "chrome/browser/download/download_util.h" |
22 #include "chrome/browser/prefs/pref_service.h" | 22 #include "chrome/browser/prefs/pref_service.h" |
23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
24 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
25 #include "content/browser/download/download_buffer.h" | 25 #include "content/browser/download/download_buffer.h" |
26 #include "content/browser/download/download_create_info.h" | 26 #include "content/browser/download/download_create_info.h" |
27 #include "content/browser/download/download_file.h" | 27 #include "content/browser/download/download_file.h" |
28 #include "content/browser/download/download_file_manager.h" | 28 #include "content/browser/download/download_file_manager.h" |
| 29 #include "content/browser/download/download_id_factory.h" |
29 #include "content/browser/download/download_item.h" | 30 #include "content/browser/download/download_item.h" |
30 #include "content/browser/download/download_manager.h" | 31 #include "content/browser/download/download_manager.h" |
31 #include "content/browser/download/download_request_handle.h" | 32 #include "content/browser/download/download_request_handle.h" |
32 #include "content/browser/download/download_status_updater.h" | 33 #include "content/browser/download/download_status_updater.h" |
33 #include "content/browser/download/interrupt_reasons.h" | 34 #include "content/browser/download/interrupt_reasons.h" |
34 #include "content/browser/download/mock_download_manager.h" | 35 #include "content/browser/download/mock_download_manager.h" |
35 #include "content/test/test_browser_thread.h" | 36 #include "content/test/test_browser_thread.h" |
36 #include "grit/generated_resources.h" | 37 #include "grit/generated_resources.h" |
37 #include "net/base/io_buffer.h" | 38 #include "net/base/io_buffer.h" |
38 #include "net/base/mock_file_stream.h" | 39 #include "net/base/mock_file_stream.h" |
39 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
40 #include "testing/gmock_mutant.h" | 41 #include "testing/gmock_mutant.h" |
41 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
42 #include "ui/base/l10n/l10n_util.h" | 43 #include "ui/base/l10n/l10n_util.h" |
43 #include "ui/base/text/bytes_formatting.h" | 44 #include "ui/base/text/bytes_formatting.h" |
44 | 45 |
| 46 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
| 47 |
45 class DownloadManagerTest : public testing::Test { | 48 class DownloadManagerTest : public testing::Test { |
46 public: | 49 public: |
47 static const char* kTestData; | 50 static const char* kTestData; |
48 static const size_t kTestDataLen; | 51 static const size_t kTestDataLen; |
49 | 52 |
50 DownloadManagerTest() | 53 DownloadManagerTest() |
51 : profile_(new TestingProfile()), | 54 : profile_(new TestingProfile()), |
52 download_manager_delegate_(new ChromeDownloadManagerDelegate( | 55 download_manager_delegate_(new ChromeDownloadManagerDelegate( |
53 profile_.get())), | 56 profile_.get())), |
| 57 id_factory_(new DownloadIdFactory(kValidIdDomain)), |
54 download_manager_(new MockDownloadManager( | 58 download_manager_(new MockDownloadManager( |
55 download_manager_delegate_, &download_status_updater_)), | 59 download_manager_delegate_, |
| 60 id_factory_, |
| 61 &download_status_updater_)), |
56 ui_thread_(BrowserThread::UI, &message_loop_), | 62 ui_thread_(BrowserThread::UI, &message_loop_), |
57 file_thread_(BrowserThread::FILE, &message_loop_), | 63 file_thread_(BrowserThread::FILE, &message_loop_), |
58 download_buffer_(new content::DownloadBuffer) { | 64 download_buffer_(new content::DownloadBuffer) { |
59 download_manager_->Init(profile_.get()); | 65 download_manager_->Init(profile_.get()); |
60 download_manager_delegate_->SetDownloadManager(download_manager_); | 66 download_manager_delegate_->SetDownloadManager(download_manager_); |
61 } | 67 } |
62 | 68 |
63 ~DownloadManagerTest() { | 69 ~DownloadManagerTest() { |
64 download_manager_->Shutdown(); | 70 download_manager_->Shutdown(); |
65 // profile_ must outlive download_manager_, so we explicitly delete | 71 // profile_ must outlive download_manager_, so we explicitly delete |
66 // download_manager_ first. | 72 // download_manager_ first. |
67 download_manager_ = NULL; | 73 download_manager_ = NULL; |
68 download_manager_delegate_ = NULL; | 74 download_manager_delegate_ = NULL; |
69 profile_.reset(NULL); | 75 profile_.reset(NULL); |
70 message_loop_.RunAllPending(); | 76 message_loop_.RunAllPending(); |
71 } | 77 } |
72 | 78 |
73 void AddDownloadToFileManager(int id, DownloadFile* download_file) { | 79 void AddDownloadToFileManager(int id, DownloadFile* download_file) { |
74 file_manager()->downloads_[DownloadId(download_manager_.get(), id)] = | 80 file_manager()->downloads_[DownloadId(kValidIdDomain, id)] = |
75 download_file; | 81 download_file; |
76 } | 82 } |
77 | 83 |
78 void OnResponseCompleted(int32 download_id, int64 size, | 84 void OnResponseCompleted(int32 download_id, int64 size, |
79 const std::string& hash) { | 85 const std::string& hash) { |
80 download_manager_->OnResponseCompleted(download_id, size, hash); | 86 download_manager_->OnResponseCompleted(download_id, size, hash); |
81 } | 87 } |
82 | 88 |
83 void FileSelected(const FilePath& path, void* params) { | 89 void FileSelected(const FilePath& path, void* params) { |
84 download_manager_->FileSelected(path, params); | 90 download_manager_->FileSelected(path, params); |
85 } | 91 } |
86 | 92 |
87 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { | 93 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { |
88 download_manager_->ContinueDownloadWithPath(download, path); | 94 download_manager_->ContinueDownloadWithPath(download, path); |
89 } | 95 } |
90 | 96 |
91 void UpdateData(int32 id, const char* data, size_t length) { | 97 void UpdateData(int32 id, const char* data, size_t length) { |
92 // We are passing ownership of this buffer to the download file manager. | 98 // We are passing ownership of this buffer to the download file manager. |
93 net::IOBuffer* io_buffer = new net::IOBuffer(length); | 99 net::IOBuffer* io_buffer = new net::IOBuffer(length); |
94 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. | 100 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. |
95 io_buffer->AddRef(); | 101 io_buffer->AddRef(); |
96 memcpy(io_buffer->data(), data, length); | 102 memcpy(io_buffer->data(), data, length); |
97 | 103 |
98 download_buffer_->AddData(io_buffer, length); | 104 download_buffer_->AddData(io_buffer, length); |
99 | 105 |
100 BrowserThread::PostTask( | 106 BrowserThread::PostTask( |
101 BrowserThread::FILE, FROM_HERE, | 107 BrowserThread::FILE, FROM_HERE, |
102 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), | 108 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), |
103 DownloadId(download_manager_.get(), id), download_buffer_)); | 109 DownloadId(kValidIdDomain, id), download_buffer_)); |
104 | 110 |
105 message_loop_.RunAllPending(); | 111 message_loop_.RunAllPending(); |
106 } | 112 } |
107 | 113 |
108 void OnDownloadInterrupted(int32 download_id, int64 size, | 114 void OnDownloadInterrupted(int32 download_id, int64 size, |
109 InterruptReason reason) { | 115 InterruptReason reason) { |
110 download_manager_->OnDownloadInterrupted(download_id, size, reason); | 116 download_manager_->OnDownloadInterrupted(download_id, size, reason); |
111 } | 117 } |
112 | 118 |
113 // Get the download item with ID |id|. | 119 // Get the download item with ID |id|. |
114 DownloadItem* GetActiveDownloadItem(int32 id) { | 120 DownloadItem* GetActiveDownloadItem(int32 id) { |
115 if (ContainsKey(download_manager_->active_downloads_, id)) | 121 if (ContainsKey(download_manager_->active_downloads_, id)) |
116 return download_manager_->active_downloads_[id]; | 122 return download_manager_->active_downloads_[id]; |
117 return NULL; | 123 return NULL; |
118 } | 124 } |
119 | 125 |
120 protected: | 126 protected: |
121 DownloadStatusUpdater download_status_updater_; | 127 DownloadStatusUpdater download_status_updater_; |
122 scoped_ptr<TestingProfile> profile_; | 128 scoped_ptr<TestingProfile> profile_; |
123 scoped_refptr<ChromeDownloadManagerDelegate> download_manager_delegate_; | 129 scoped_refptr<ChromeDownloadManagerDelegate> download_manager_delegate_; |
| 130 scoped_refptr<DownloadIdFactory> id_factory_; |
124 scoped_refptr<DownloadManager> download_manager_; | 131 scoped_refptr<DownloadManager> download_manager_; |
125 scoped_refptr<DownloadFileManager> file_manager_; | 132 scoped_refptr<DownloadFileManager> file_manager_; |
126 MessageLoopForUI message_loop_; | 133 MessageLoopForUI message_loop_; |
127 content::TestBrowserThread ui_thread_; | 134 content::TestBrowserThread ui_thread_; |
128 content::TestBrowserThread file_thread_; | 135 content::TestBrowserThread file_thread_; |
129 scoped_refptr<content::DownloadBuffer> download_buffer_; | 136 scoped_refptr<content::DownloadBuffer> download_buffer_; |
130 | 137 |
131 DownloadFileManager* file_manager() { | 138 DownloadFileManager* file_manager() { |
132 if (!file_manager_) { | 139 if (!file_manager_) { |
133 file_manager_ = new DownloadFileManager(NULL); | 140 file_manager_ = new DownloadFileManager(NULL); |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 383 |
377 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kStartDownloadCases); ++i) { | 384 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kStartDownloadCases); ++i) { |
378 prefs->SetBoolean(prefs::kPromptForDownload, | 385 prefs->SetBoolean(prefs::kPromptForDownload, |
379 kStartDownloadCases[i].prompt_for_download); | 386 kStartDownloadCases[i].prompt_for_download); |
380 | 387 |
381 SelectFileObserver observer(download_manager_); | 388 SelectFileObserver observer(download_manager_); |
382 // Normally, the download system takes ownership of info, and is | 389 // Normally, the download system takes ownership of info, and is |
383 // responsible for deleting it. In these unit tests, however, we | 390 // responsible for deleting it. In these unit tests, however, we |
384 // don't call the function that deletes it, so we do so ourselves. | 391 // don't call the function that deletes it, so we do so ourselves. |
385 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 392 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
386 info->download_id = static_cast<int>(i); | 393 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); |
387 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; | 394 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; |
388 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); | 395 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); |
389 info->mime_type = kStartDownloadCases[i].mime_type; | 396 info->mime_type = kStartDownloadCases[i].mime_type; |
390 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 397 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
391 | 398 |
392 DownloadFile* download_file( | 399 DownloadFile* download_file( |
393 new DownloadFile(info.get(), DownloadRequestHandle(), | 400 new DownloadFile(info.get(), DownloadRequestHandle(), |
394 download_manager_)); | 401 download_manager_)); |
395 AddDownloadToFileManager(info->download_id, download_file); | 402 AddDownloadToFileManager(info->download_id.local(), download_file); |
396 download_file->Initialize(false); | 403 download_file->Initialize(false); |
397 download_manager_->StartDownload(info->download_id); | 404 download_manager_->StartDownload(info->download_id.local()); |
398 message_loop_.RunAllPending(); | 405 message_loop_.RunAllPending(); |
399 | 406 |
400 // SelectFileObserver will have recorded any attempt to open the | 407 // SelectFileObserver will have recorded any attempt to open the |
401 // select file dialog. | 408 // select file dialog. |
402 // Note that DownloadManager::FileSelectionCanceled() is never called. | 409 // Note that DownloadManager::FileSelectionCanceled() is never called. |
403 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, | 410 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, |
404 observer.ShowedFileDialogForId(i)); | 411 observer.ShowedFileDialogForId(i)); |
405 } | 412 } |
406 } | 413 } |
407 | 414 |
408 TEST_F(DownloadManagerTest, DownloadRenameTest) { | 415 TEST_F(DownloadManagerTest, DownloadRenameTest) { |
409 using ::testing::_; | 416 using ::testing::_; |
410 using ::testing::CreateFunctor; | 417 using ::testing::CreateFunctor; |
411 using ::testing::Invoke; | 418 using ::testing::Invoke; |
412 using ::testing::Return; | 419 using ::testing::Return; |
413 | 420 |
414 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { | 421 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { |
415 // Normally, the download system takes ownership of info, and is | 422 // Normally, the download system takes ownership of info, and is |
416 // responsible for deleting it. In these unit tests, however, we | 423 // responsible for deleting it. In these unit tests, however, we |
417 // don't call the function that deletes it, so we do so ourselves. | 424 // don't call the function that deletes it, so we do so ourselves. |
418 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 425 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
419 info->download_id = static_cast<int>(i); | 426 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); |
420 info->prompt_user_for_save_location = false; | 427 info->prompt_user_for_save_location = false; |
421 info->url_chain.push_back(GURL()); | 428 info->url_chain.push_back(GURL()); |
422 const FilePath new_path(kDownloadRenameCases[i].suggested_path); | 429 const FilePath new_path(kDownloadRenameCases[i].suggested_path); |
423 | 430 |
424 MockDownloadFile* download_file( | 431 MockDownloadFile* download_file( |
425 new MockDownloadFile(info.get(), download_manager_)); | 432 new MockDownloadFile(info.get(), download_manager_)); |
426 AddDownloadToFileManager(info->download_id, download_file); | 433 AddDownloadToFileManager(info->download_id.local(), download_file); |
427 | 434 |
428 // |download_file| is owned by DownloadFileManager. | 435 // |download_file| is owned by DownloadFileManager. |
429 ::testing::Mock::AllowLeak(download_file); | 436 ::testing::Mock::AllowLeak(download_file); |
430 EXPECT_CALL(*download_file, Destructed()).Times(1); | 437 EXPECT_CALL(*download_file, Destructed()).Times(1); |
431 | 438 |
432 if (kDownloadRenameCases[i].expected_rename_count == 1) { | 439 if (kDownloadRenameCases[i].expected_rename_count == 1) { |
433 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(net::OK)); | 440 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(net::OK)); |
434 } else { | 441 } else { |
435 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); | 442 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); |
436 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); | 443 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 TEST_F(DownloadManagerTest, DownloadInterruptTest) { | 479 TEST_F(DownloadManagerTest, DownloadInterruptTest) { |
473 using ::testing::_; | 480 using ::testing::_; |
474 using ::testing::CreateFunctor; | 481 using ::testing::CreateFunctor; |
475 using ::testing::Invoke; | 482 using ::testing::Invoke; |
476 using ::testing::Return; | 483 using ::testing::Return; |
477 | 484 |
478 // Normally, the download system takes ownership of info, and is | 485 // Normally, the download system takes ownership of info, and is |
479 // responsible for deleting it. In these unit tests, however, we | 486 // responsible for deleting it. In these unit tests, however, we |
480 // don't call the function that deletes it, so we do so ourselves. | 487 // don't call the function that deletes it, so we do so ourselves. |
481 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 488 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
482 info->download_id = static_cast<int>(0); | 489 info->download_id = DownloadId(kValidIdDomain, 0); |
483 info->prompt_user_for_save_location = false; | 490 info->prompt_user_for_save_location = false; |
484 info->url_chain.push_back(GURL()); | 491 info->url_chain.push_back(GURL()); |
485 info->total_bytes = static_cast<int64>(kTestDataLen); | 492 info->total_bytes = static_cast<int64>(kTestDataLen); |
486 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); | 493 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
487 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); | 494 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
488 | 495 |
489 MockDownloadFile* download_file( | 496 MockDownloadFile* download_file( |
490 new MockDownloadFile(info.get(), download_manager_)); | 497 new MockDownloadFile(info.get(), download_manager_)); |
491 AddDownloadToFileManager(info->download_id, download_file); | 498 AddDownloadToFileManager(info->download_id.local(), download_file); |
492 | 499 |
493 // |download_file| is owned by DownloadFileManager. | 500 // |download_file| is owned by DownloadFileManager. |
494 ::testing::Mock::AllowLeak(download_file); | 501 ::testing::Mock::AllowLeak(download_file); |
495 EXPECT_CALL(*download_file, Destructed()).Times(1); | 502 EXPECT_CALL(*download_file, Destructed()).Times(1); |
496 | 503 |
497 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(net::OK)); | 504 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(net::OK)); |
498 | 505 |
499 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 506 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
500 | 507 |
501 DownloadItem* download = GetActiveDownloadItem(0); | 508 DownloadItem* download = GetActiveDownloadItem(0); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 // This file stream will be used, until the first rename occurs. | 569 // This file stream will be used, until the first rename occurs. |
563 net::testing::MockFileStream* mock_stream = new net::testing::MockFileStream; | 570 net::testing::MockFileStream* mock_stream = new net::testing::MockFileStream; |
564 ASSERT_EQ(0, mock_stream->Open( | 571 ASSERT_EQ(0, mock_stream->Open( |
565 path, | 572 path, |
566 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); | 573 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); |
567 | 574 |
568 // Normally, the download system takes ownership of info, and is | 575 // Normally, the download system takes ownership of info, and is |
569 // responsible for deleting it. In these unit tests, however, we | 576 // responsible for deleting it. In these unit tests, however, we |
570 // don't call the function that deletes it, so we do so ourselves. | 577 // don't call the function that deletes it, so we do so ourselves. |
571 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 578 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
572 int32 id = 0; | 579 static const int32 local_id = 0; |
573 info->download_id = id; | 580 info->download_id = DownloadId(kValidIdDomain, local_id); |
574 info->prompt_user_for_save_location = false; | 581 info->prompt_user_for_save_location = false; |
575 info->url_chain.push_back(GURL()); | 582 info->url_chain.push_back(GURL()); |
576 info->total_bytes = static_cast<int64>(kTestDataLen * 3); | 583 info->total_bytes = static_cast<int64>(kTestDataLen * 3); |
577 info->save_info.file_path = path; | 584 info->save_info.file_path = path; |
578 | 585 |
579 // Create a download file that we can insert errors into. | 586 // Create a download file that we can insert errors into. |
580 DownloadFileWithMockStream* download_file(new DownloadFileWithMockStream( | 587 DownloadFileWithMockStream* download_file(new DownloadFileWithMockStream( |
581 info.get(), download_manager_, mock_stream)); | 588 info.get(), download_manager_, mock_stream)); |
582 AddDownloadToFileManager(id, download_file); | 589 AddDownloadToFileManager(local_id, download_file); |
583 | 590 |
584 // |download_file| is owned by DownloadFileManager. | 591 // |download_file| is owned by DownloadFileManager. |
585 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 592 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
586 | 593 |
587 DownloadItem* download = GetActiveDownloadItem(0); | 594 DownloadItem* download = GetActiveDownloadItem(0); |
588 ASSERT_TRUE(download != NULL); | 595 ASSERT_TRUE(download != NULL); |
589 // This will keep track of what should be displayed on the shelf. | 596 // This will keep track of what should be displayed on the shelf. |
590 scoped_ptr<DownloadItemModel> download_item_model( | 597 scoped_ptr<DownloadItemModel> download_item_model( |
591 new DownloadItemModel(download)); | 598 new DownloadItemModel(download)); |
592 | 599 |
593 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); | 600 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
594 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 601 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
595 | 602 |
596 // Add some data before finalizing the file name. | 603 // Add some data before finalizing the file name. |
597 UpdateData(id, kTestData, kTestDataLen); | 604 UpdateData(local_id, kTestData, kTestDataLen); |
598 | 605 |
599 // Finalize the file name. | 606 // Finalize the file name. |
600 ContinueDownloadWithPath(download, path); | 607 ContinueDownloadWithPath(download, path); |
601 message_loop_.RunAllPending(); | 608 message_loop_.RunAllPending(); |
602 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 609 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
603 | 610 |
604 // Add more data. | 611 // Add more data. |
605 UpdateData(id, kTestData, kTestDataLen); | 612 UpdateData(local_id, kTestData, kTestDataLen); |
606 | 613 |
607 // Add more data, but an error occurs. | 614 // Add more data, but an error occurs. |
608 download_file->SetForcedError(net::ERR_FAILED); | 615 download_file->SetForcedError(net::ERR_FAILED); |
609 UpdateData(id, kTestData, kTestDataLen); | 616 UpdateData(local_id, kTestData, kTestDataLen); |
610 | 617 |
611 // Check the state. The download should have been interrupted. | 618 // Check the state. The download should have been interrupted. |
612 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 619 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
613 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 620 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
614 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); | 621 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
615 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); | 622 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
616 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 623 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
617 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 624 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
618 EXPECT_TRUE(observer->was_updated()); | 625 EXPECT_TRUE(observer->was_updated()); |
619 EXPECT_FALSE(observer->was_opened()); | 626 EXPECT_FALSE(observer->was_opened()); |
(...skipping 21 matching lines...) Expand all Loading... |
641 TEST_F(DownloadManagerTest, DownloadCancelTest) { | 648 TEST_F(DownloadManagerTest, DownloadCancelTest) { |
642 using ::testing::_; | 649 using ::testing::_; |
643 using ::testing::CreateFunctor; | 650 using ::testing::CreateFunctor; |
644 using ::testing::Invoke; | 651 using ::testing::Invoke; |
645 using ::testing::Return; | 652 using ::testing::Return; |
646 | 653 |
647 // Normally, the download system takes ownership of info, and is | 654 // Normally, the download system takes ownership of info, and is |
648 // responsible for deleting it. In these unit tests, however, we | 655 // responsible for deleting it. In these unit tests, however, we |
649 // don't call the function that deletes it, so we do so ourselves. | 656 // don't call the function that deletes it, so we do so ourselves. |
650 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 657 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
651 info->download_id = static_cast<int>(0); | 658 info->download_id = DownloadId(kValidIdDomain, 0); |
652 info->prompt_user_for_save_location = false; | 659 info->prompt_user_for_save_location = false; |
653 info->url_chain.push_back(GURL()); | 660 info->url_chain.push_back(GURL()); |
654 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); | 661 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
655 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); | 662 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
656 | 663 |
657 MockDownloadFile* download_file( | 664 MockDownloadFile* download_file( |
658 new MockDownloadFile(info.get(), download_manager_)); | 665 new MockDownloadFile(info.get(), download_manager_)); |
659 AddDownloadToFileManager(info->download_id, download_file); | 666 AddDownloadToFileManager(info->download_id.local(), download_file); |
660 | 667 |
661 // |download_file| is owned by DownloadFileManager. | 668 // |download_file| is owned by DownloadFileManager. |
662 ::testing::Mock::AllowLeak(download_file); | 669 ::testing::Mock::AllowLeak(download_file); |
663 EXPECT_CALL(*download_file, Destructed()).Times(1); | 670 EXPECT_CALL(*download_file, Destructed()).Times(1); |
664 | 671 |
665 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(net::OK)); | 672 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(net::OK)); |
666 | 673 |
667 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 674 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
668 | 675 |
669 DownloadItem* download = GetActiveDownloadItem(0); | 676 DownloadItem* download = GetActiveDownloadItem(0); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 // which we will override. | 732 // which we will override. |
726 int uniquifier = DownloadFile::GetUniquePathNumber(new_path); | 733 int uniquifier = DownloadFile::GetUniquePathNumber(new_path); |
727 FilePath unique_new_path = new_path; | 734 FilePath unique_new_path = new_path; |
728 EXPECT_NE(0, uniquifier); | 735 EXPECT_NE(0, uniquifier); |
729 DownloadFile::AppendNumberToPath(&unique_new_path, uniquifier); | 736 DownloadFile::AppendNumberToPath(&unique_new_path, uniquifier); |
730 | 737 |
731 // Normally, the download system takes ownership of info, and is | 738 // Normally, the download system takes ownership of info, and is |
732 // responsible for deleting it. In these unit tests, however, we | 739 // responsible for deleting it. In these unit tests, however, we |
733 // don't call the function that deletes it, so we do so ourselves. | 740 // don't call the function that deletes it, so we do so ourselves. |
734 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 741 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
735 info->download_id = static_cast<int>(0); | 742 info->download_id = DownloadId(kValidIdDomain, 0); |
736 info->prompt_user_for_save_location = true; | 743 info->prompt_user_for_save_location = true; |
737 info->url_chain.push_back(GURL()); | 744 info->url_chain.push_back(GURL()); |
738 | 745 |
739 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 746 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
740 | 747 |
741 DownloadItem* download = GetActiveDownloadItem(0); | 748 DownloadItem* download = GetActiveDownloadItem(0); |
742 ASSERT_TRUE(download != NULL); | 749 ASSERT_TRUE(download != NULL); |
743 scoped_ptr<DownloadItemModel> download_item_model( | 750 scoped_ptr<DownloadItemModel> download_item_model( |
744 new DownloadItemModel(download)); | 751 new DownloadItemModel(download)); |
745 | 752 |
746 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); | 753 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
747 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 754 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
748 | 755 |
749 // Create and initialize the download file. We're bypassing the first part | 756 // Create and initialize the download file. We're bypassing the first part |
750 // of the download process and skipping to the part after the final file | 757 // of the download process and skipping to the part after the final file |
751 // name has been chosen, so we need to initialize the download file | 758 // name has been chosen, so we need to initialize the download file |
752 // properly. | 759 // properly. |
753 DownloadFile* download_file( | 760 DownloadFile* download_file( |
754 new DownloadFile(info.get(), DownloadRequestHandle(), download_manager_)); | 761 new DownloadFile(info.get(), DownloadRequestHandle(), download_manager_)); |
755 download_file->Rename(cr_path); | 762 download_file->Rename(cr_path); |
756 // This creates the .crdownload version of the file. | 763 // This creates the .crdownload version of the file. |
757 download_file->Initialize(false); | 764 download_file->Initialize(false); |
758 // |download_file| is owned by DownloadFileManager. | 765 // |download_file| is owned by DownloadFileManager. |
759 AddDownloadToFileManager(info->download_id, download_file); | 766 AddDownloadToFileManager(info->download_id.local(), download_file); |
760 | 767 |
761 ContinueDownloadWithPath(download, new_path); | 768 ContinueDownloadWithPath(download, new_path); |
762 message_loop_.RunAllPending(); | 769 message_loop_.RunAllPending(); |
763 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 770 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
764 | 771 |
765 download_file->AppendDataToFile(kTestData, kTestDataLen); | 772 download_file->AppendDataToFile(kTestData, kTestDataLen); |
766 | 773 |
767 // Finish the download. | 774 // Finish the download. |
768 OnResponseCompleted(0, kTestDataLen, ""); | 775 OnResponseCompleted(0, kTestDataLen, ""); |
769 message_loop_.RunAllPending(); | 776 message_loop_.RunAllPending(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 | 808 |
802 // File names we're using. | 809 // File names we're using. |
803 const FilePath new_path(temp_dir_.path().AppendASCII("foo.txt")); | 810 const FilePath new_path(temp_dir_.path().AppendASCII("foo.txt")); |
804 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); | 811 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
805 EXPECT_FALSE(file_util::PathExists(new_path)); | 812 EXPECT_FALSE(file_util::PathExists(new_path)); |
806 | 813 |
807 // Normally, the download system takes ownership of info, and is | 814 // Normally, the download system takes ownership of info, and is |
808 // responsible for deleting it. In these unit tests, however, we | 815 // responsible for deleting it. In these unit tests, however, we |
809 // don't call the function that deletes it, so we do so ourselves. | 816 // don't call the function that deletes it, so we do so ourselves. |
810 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 817 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
811 info->download_id = static_cast<int>(0); | 818 info->download_id = DownloadId(kValidIdDomain, 0); |
812 info->prompt_user_for_save_location = true; | 819 info->prompt_user_for_save_location = true; |
813 info->url_chain.push_back(GURL()); | 820 info->url_chain.push_back(GURL()); |
814 | 821 |
815 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 822 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
816 | 823 |
817 DownloadItem* download = GetActiveDownloadItem(0); | 824 DownloadItem* download = GetActiveDownloadItem(0); |
818 ASSERT_TRUE(download != NULL); | 825 ASSERT_TRUE(download != NULL); |
819 scoped_ptr<DownloadItemModel> download_item_model( | 826 scoped_ptr<DownloadItemModel> download_item_model( |
820 new DownloadItemModel(download)); | 827 new DownloadItemModel(download)); |
821 | 828 |
822 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); | 829 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
823 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 830 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
824 | 831 |
825 // Create and initialize the download file. We're bypassing the first part | 832 // Create and initialize the download file. We're bypassing the first part |
826 // of the download process and skipping to the part after the final file | 833 // of the download process and skipping to the part after the final file |
827 // name has been chosen, so we need to initialize the download file | 834 // name has been chosen, so we need to initialize the download file |
828 // properly. | 835 // properly. |
829 DownloadFile* download_file( | 836 DownloadFile* download_file( |
830 new DownloadFile(info.get(), DownloadRequestHandle(), download_manager_)); | 837 new DownloadFile(info.get(), DownloadRequestHandle(), download_manager_)); |
831 download_file->Rename(cr_path); | 838 download_file->Rename(cr_path); |
832 // This creates the .crdownload version of the file. | 839 // This creates the .crdownload version of the file. |
833 download_file->Initialize(false); | 840 download_file->Initialize(false); |
834 // |download_file| is owned by DownloadFileManager. | 841 // |download_file| is owned by DownloadFileManager. |
835 AddDownloadToFileManager(info->download_id, download_file); | 842 AddDownloadToFileManager(info->download_id.local(), download_file); |
836 | 843 |
837 ContinueDownloadWithPath(download, new_path); | 844 ContinueDownloadWithPath(download, new_path); |
838 message_loop_.RunAllPending(); | 845 message_loop_.RunAllPending(); |
839 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 846 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
840 | 847 |
841 download_file->AppendDataToFile(kTestData, kTestDataLen); | 848 download_file->AppendDataToFile(kTestData, kTestDataLen); |
842 | 849 |
843 // Finish the download. | 850 // Finish the download. |
844 OnResponseCompleted(0, kTestDataLen, ""); | 851 OnResponseCompleted(0, kTestDataLen, ""); |
845 message_loop_.RunAllPending(); | 852 message_loop_.RunAllPending(); |
(...skipping 27 matching lines...) Expand all Loading... |
873 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 880 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
874 EXPECT_TRUE(observer->was_updated()); | 881 EXPECT_TRUE(observer->was_updated()); |
875 EXPECT_FALSE(observer->was_opened()); | 882 EXPECT_FALSE(observer->was_opened()); |
876 EXPECT_TRUE(download->file_externally_removed()); | 883 EXPECT_TRUE(download->file_externally_removed()); |
877 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 884 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
878 EXPECT_EQ(download_item_model->GetStatusText(), | 885 EXPECT_EQ(download_item_model->GetStatusText(), |
879 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); | 886 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
880 | 887 |
881 EXPECT_FALSE(file_util::PathExists(new_path)); | 888 EXPECT_FALSE(file_util::PathExists(new_path)); |
882 } | 889 } |
OLD | NEW |