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

Side by Side Diff: chrome/browser/download/download_manager_unittest.cc

Issue 8404049: Added member data to classes to support download resumption. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup, and added fields to DownloadPersistentStoreInfo. Created 9 years, 1 month 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) 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"
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 107
108 BrowserThread::PostTask( 108 BrowserThread::PostTask(
109 BrowserThread::FILE, FROM_HERE, 109 BrowserThread::FILE, FROM_HERE,
110 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), 110 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(),
111 DownloadId(kValidIdDomain, id), download_buffer_)); 111 DownloadId(kValidIdDomain, id), download_buffer_));
112 112
113 message_loop_.RunAllPending(); 113 message_loop_.RunAllPending();
114 } 114 }
115 115
116 void OnDownloadInterrupted(int32 download_id, int64 size, 116 void OnDownloadInterrupted(int32 download_id, int64 size,
117 const std::string partial_hash,
117 InterruptReason reason) { 118 InterruptReason reason) {
118 download_manager_->OnDownloadInterrupted(download_id, size, reason); 119 download_manager_->OnDownloadInterrupted(download_id, size,
120 partial_hash, reason);
119 } 121 }
120 122
121 // Get the download item with ID |id|. 123 // Get the download item with ID |id|.
122 DownloadItem* GetActiveDownloadItem(int32 id) { 124 DownloadItem* GetActiveDownloadItem(int32 id) {
123 if (ContainsKey(download_manager_->active_downloads_, id)) 125 if (ContainsKey(download_manager_->active_downloads_, id))
124 return download_manager_->active_downloads_[id]; 126 return download_manager_->active_downloads_[id];
125 return NULL; 127 return NULL;
126 } 128 }
127 129
128 protected: 130 protected:
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 180
179 protected: 181 protected:
180 // This version creates a |MockFileStream| instead of a |FileStream|. 182 // This version creates a |MockFileStream| instead of a |FileStream|.
181 virtual void CreateFileStream() OVERRIDE; 183 virtual void CreateFileStream() OVERRIDE;
182 }; 184 };
183 185
184 DownloadFileWithMockStream::DownloadFileWithMockStream( 186 DownloadFileWithMockStream::DownloadFileWithMockStream(
185 DownloadCreateInfo* info, 187 DownloadCreateInfo* info,
186 DownloadManager* manager, 188 DownloadManager* manager,
187 net::testing::MockFileStream* stream) 189 net::testing::MockFileStream* stream)
188 : DownloadFile(info, new DownloadRequestHandle(), manager) { 190 : DownloadFile(info, new DownloadRequestHandle(), manager, "") {
189 DCHECK(file_stream_ == NULL); 191 DCHECK(file_stream_ == NULL);
190 file_stream_.reset(stream); 192 file_stream_.reset(stream);
191 } 193 }
192 194
193 void DownloadFileWithMockStream::SetForcedError(int error) 195 void DownloadFileWithMockStream::SetForcedError(int error)
194 { 196 {
195 // |file_stream_| can only be set in the constructor and in 197 // |file_stream_| can only be set in the constructor and in
196 // CreateFileStream(), both of which insure that it is a |MockFileStream|. 198 // CreateFileStream(), both of which insure that it is a |MockFileStream|.
197 net::testing::MockFileStream* mock_stream = 199 net::testing::MockFileStream* mock_stream =
198 static_cast<net::testing::MockFileStream *>(file_stream_.get()); 200 static_cast<net::testing::MockFileStream *>(file_stream_.get());
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 // Dangerous download, download finishes AFTER file name determined. 278 // Dangerous download, download finishes AFTER file name determined.
277 // Needs to be renamed only once. 279 // Needs to be renamed only once.
278 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, 280 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, },
279 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, 281 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, },
280 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, 282 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, },
281 }; 283 };
282 284
283 class MockDownloadFile : public DownloadFile { 285 class MockDownloadFile : public DownloadFile {
284 public: 286 public:
285 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) 287 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager)
286 : DownloadFile(info, new DownloadRequestHandle(), manager), 288 : DownloadFile(info, new DownloadRequestHandle(), manager, ""),
287 renamed_count_(0) { } 289 renamed_count_(0) { }
288 virtual ~MockDownloadFile() { Destructed(); } 290 virtual ~MockDownloadFile() { Destructed(); }
289 MOCK_METHOD1(Rename, net::Error(const FilePath&)); 291 MOCK_METHOD1(Rename, net::Error(const FilePath&));
290 MOCK_METHOD0(Destructed, void()); 292 MOCK_METHOD0(Destructed, void());
291 293
292 net::Error TestMultipleRename( 294 net::Error TestMultipleRename(
293 int expected_count, const FilePath& expected, 295 int expected_count, const FilePath& expected,
294 const FilePath& path) { 296 const FilePath& path) {
295 ++renamed_count_; 297 ++renamed_count_;
296 EXPECT_EQ(expected_count, renamed_count_); 298 EXPECT_EQ(expected_count, renamed_count_);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 // don't call the function that deletes it, so we do so ourselves. 395 // don't call the function that deletes it, so we do so ourselves.
394 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 396 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
395 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); 397 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i));
396 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; 398 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
397 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); 399 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
398 info->mime_type = kStartDownloadCases[i].mime_type; 400 info->mime_type = kStartDownloadCases[i].mime_type;
399 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 401 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
400 402
401 DownloadFile* download_file( 403 DownloadFile* download_file(
402 new DownloadFile(info.get(), new DownloadRequestHandle(), 404 new DownloadFile(info.get(), new DownloadRequestHandle(),
403 download_manager_)); 405 download_manager_, ""));
404 AddDownloadToFileManager(info->download_id.local(), download_file); 406 AddDownloadToFileManager(info->download_id.local(), download_file);
405 download_file->Initialize(false); 407 download_file->Initialize(false);
406 download_manager_->StartDownload(info->download_id.local()); 408 download_manager_->StartDownload(info->download_id.local());
407 message_loop_.RunAllPending(); 409 message_loop_.RunAllPending();
408 410
409 // SelectFileObserver will have recorded any attempt to open the 411 // SelectFileObserver will have recorded any attempt to open the
410 // select file dialog. 412 // select file dialog.
411 // Note that DownloadManager::FileSelectionCanceled() is never called. 413 // Note that DownloadManager::FileSelectionCanceled() is never called.
412 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, 414 EXPECT_EQ(kStartDownloadCases[i].expected_save_as,
413 observer.ShowedFileDialogForId(i)); 415 observer.ShowedFileDialogForId(i));
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 517 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
516 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 518 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
517 519
518 download_file->AppendDataToFile(kTestData, kTestDataLen); 520 download_file->AppendDataToFile(kTestData, kTestDataLen);
519 521
520 ContinueDownloadWithPath(download, new_path); 522 ContinueDownloadWithPath(download, new_path);
521 message_loop_.RunAllPending(); 523 message_loop_.RunAllPending();
522 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 524 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
523 525
524 int64 error_size = 3; 526 int64 error_size = 3;
525 OnDownloadInterrupted(0, error_size, 527 OnDownloadInterrupted(0, error_size, "",
526 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 528 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
527 message_loop_.RunAllPending(); 529 message_loop_.RunAllPending();
528 530
529 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 531 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
530 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 532 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
531 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); 533 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED));
532 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); 534 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE));
533 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 535 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
534 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 536 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
535 EXPECT_TRUE(observer->was_updated()); 537 EXPECT_TRUE(observer->was_updated());
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 756
755 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 757 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
756 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 758 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
757 759
758 // Create and initialize the download file. We're bypassing the first part 760 // Create and initialize the download file. We're bypassing the first part
759 // of the download process and skipping to the part after the final file 761 // of the download process and skipping to the part after the final file
760 // name has been chosen, so we need to initialize the download file 762 // name has been chosen, so we need to initialize the download file
761 // properly. 763 // properly.
762 DownloadFile* download_file( 764 DownloadFile* download_file(
763 new DownloadFile(info.get(), new DownloadRequestHandle(), 765 new DownloadFile(info.get(), new DownloadRequestHandle(),
764 download_manager_)); 766 download_manager_, ""));
765 download_file->Rename(cr_path); 767 download_file->Rename(cr_path);
766 // This creates the .crdownload version of the file. 768 // This creates the .crdownload version of the file.
767 download_file->Initialize(false); 769 download_file->Initialize(false);
768 // |download_file| is owned by DownloadFileManager. 770 // |download_file| is owned by DownloadFileManager.
769 AddDownloadToFileManager(info->download_id.local(), download_file); 771 AddDownloadToFileManager(info->download_id.local(), download_file);
770 772
771 ContinueDownloadWithPath(download, new_path); 773 ContinueDownloadWithPath(download, new_path);
772 message_loop_.RunAllPending(); 774 message_loop_.RunAllPending();
773 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 775 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
774 776
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 833
832 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 834 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
833 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 835 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
834 836
835 // Create and initialize the download file. We're bypassing the first part 837 // Create and initialize the download file. We're bypassing the first part
836 // of the download process and skipping to the part after the final file 838 // of the download process and skipping to the part after the final file
837 // name has been chosen, so we need to initialize the download file 839 // name has been chosen, so we need to initialize the download file
838 // properly. 840 // properly.
839 DownloadFile* download_file( 841 DownloadFile* download_file(
840 new DownloadFile(info.get(), new DownloadRequestHandle(), 842 new DownloadFile(info.get(), new DownloadRequestHandle(),
841 download_manager_)); 843 download_manager_, ""));
842 download_file->Rename(cr_path); 844 download_file->Rename(cr_path);
843 // This creates the .crdownload version of the file. 845 // This creates the .crdownload version of the file.
844 download_file->Initialize(false); 846 download_file->Initialize(false);
845 // |download_file| is owned by DownloadFileManager. 847 // |download_file| is owned by DownloadFileManager.
846 AddDownloadToFileManager(info->download_id.local(), download_file); 848 AddDownloadToFileManager(info->download_id.local(), download_file);
847 849
848 ContinueDownloadWithPath(download, new_path); 850 ContinueDownloadWithPath(download, new_path);
849 message_loop_.RunAllPending(); 851 message_loop_.RunAllPending();
850 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 852 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
851 853
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 886 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
885 EXPECT_TRUE(observer->was_updated()); 887 EXPECT_TRUE(observer->was_updated());
886 EXPECT_FALSE(observer->was_opened()); 888 EXPECT_FALSE(observer->was_opened());
887 EXPECT_TRUE(download->file_externally_removed()); 889 EXPECT_TRUE(download->file_externally_removed());
888 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); 890 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
889 EXPECT_EQ(download_item_model->GetStatusText(), 891 EXPECT_EQ(download_item_model->GetStatusText(),
890 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); 892 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED));
891 893
892 EXPECT_FALSE(file_util::PathExists(new_path)); 894 EXPECT_FALSE(file_util::PathExists(new_path));
893 } 895 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698