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

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

Issue 8399001: Use a DownloadRequestHandle pointer in construction to allow mocking for tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to TOT. 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
« no previous file with comments | « no previous file | content/browser/download/download_file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 176
177 protected: 177 protected:
178 // This version creates a |MockFileStream| instead of a |FileStream|. 178 // This version creates a |MockFileStream| instead of a |FileStream|.
179 virtual void CreateFileStream() OVERRIDE; 179 virtual void CreateFileStream() OVERRIDE;
180 }; 180 };
181 181
182 DownloadFileWithMockStream::DownloadFileWithMockStream( 182 DownloadFileWithMockStream::DownloadFileWithMockStream(
183 DownloadCreateInfo* info, 183 DownloadCreateInfo* info,
184 DownloadManager* manager, 184 DownloadManager* manager,
185 net::testing::MockFileStream* stream) 185 net::testing::MockFileStream* stream)
186 : DownloadFile(info, DownloadRequestHandle(), manager) { 186 : DownloadFile(info, new DownloadRequestHandle(), manager) {
187 DCHECK(file_stream_ == NULL); 187 DCHECK(file_stream_ == NULL);
188 file_stream_.reset(stream); 188 file_stream_.reset(stream);
189 } 189 }
190 190
191 void DownloadFileWithMockStream::SetForcedError(int error) 191 void DownloadFileWithMockStream::SetForcedError(int error)
192 { 192 {
193 // |file_stream_| can only be set in the constructor and in 193 // |file_stream_| can only be set in the constructor and in
194 // CreateFileStream(), both of which insure that it is a |MockFileStream|. 194 // CreateFileStream(), both of which insure that it is a |MockFileStream|.
195 net::testing::MockFileStream* mock_stream = 195 net::testing::MockFileStream* mock_stream =
196 static_cast<net::testing::MockFileStream *>(file_stream_.get()); 196 static_cast<net::testing::MockFileStream *>(file_stream_.get());
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // Dangerous download, download finishes AFTER file name determined. 274 // Dangerous download, download finishes AFTER file name determined.
275 // Needs to be renamed only once. 275 // Needs to be renamed only once.
276 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, 276 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, },
277 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, 277 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, },
278 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, 278 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, },
279 }; 279 };
280 280
281 class MockDownloadFile : public DownloadFile { 281 class MockDownloadFile : public DownloadFile {
282 public: 282 public:
283 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) 283 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager)
284 : DownloadFile(info, DownloadRequestHandle(), manager), 284 : DownloadFile(info, new DownloadRequestHandle(), manager),
285 renamed_count_(0) { } 285 renamed_count_(0) { }
286 virtual ~MockDownloadFile() { Destructed(); } 286 virtual ~MockDownloadFile() { Destructed(); }
287 MOCK_METHOD1(Rename, net::Error(const FilePath&)); 287 MOCK_METHOD1(Rename, net::Error(const FilePath&));
288 MOCK_METHOD0(Destructed, void()); 288 MOCK_METHOD0(Destructed, void());
289 289
290 net::Error TestMultipleRename( 290 net::Error TestMultipleRename(
291 int expected_count, const FilePath& expected, 291 int expected_count, const FilePath& expected,
292 const FilePath& path) { 292 const FilePath& path) {
293 ++renamed_count_; 293 ++renamed_count_;
294 EXPECT_EQ(expected_count, renamed_count_); 294 EXPECT_EQ(expected_count, renamed_count_);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 // responsible for deleting it. In these unit tests, however, we 390 // responsible for deleting it. In these unit tests, however, we
391 // 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.
392 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 392 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
393 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); 393 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i));
394 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; 394 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
395 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); 395 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
396 info->mime_type = kStartDownloadCases[i].mime_type; 396 info->mime_type = kStartDownloadCases[i].mime_type;
397 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 397 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
398 398
399 DownloadFile* download_file( 399 DownloadFile* download_file(
400 new DownloadFile(info.get(), DownloadRequestHandle(), 400 new DownloadFile(info.get(), new DownloadRequestHandle(),
401 download_manager_)); 401 download_manager_));
402 AddDownloadToFileManager(info->download_id.local(), download_file); 402 AddDownloadToFileManager(info->download_id.local(), download_file);
403 download_file->Initialize(false); 403 download_file->Initialize(false);
404 download_manager_->StartDownload(info->download_id.local()); 404 download_manager_->StartDownload(info->download_id.local());
405 message_loop_.RunAllPending(); 405 message_loop_.RunAllPending();
406 406
407 // SelectFileObserver will have recorded any attempt to open the 407 // SelectFileObserver will have recorded any attempt to open the
408 // select file dialog. 408 // select file dialog.
409 // Note that DownloadManager::FileSelectionCanceled() is never called. 409 // Note that DownloadManager::FileSelectionCanceled() is never called.
410 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, 410 EXPECT_EQ(kStartDownloadCases[i].expected_save_as,
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 new DownloadItemModel(download)); 751 new DownloadItemModel(download));
752 752
753 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 753 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
754 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 754 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
755 755
756 // 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
757 // 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
758 // 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
759 // properly. 759 // properly.
760 DownloadFile* download_file( 760 DownloadFile* download_file(
761 new DownloadFile(info.get(), DownloadRequestHandle(), download_manager_)); 761 new DownloadFile(info.get(), new DownloadRequestHandle(),
762 download_manager_));
762 download_file->Rename(cr_path); 763 download_file->Rename(cr_path);
763 // This creates the .crdownload version of the file. 764 // This creates the .crdownload version of the file.
764 download_file->Initialize(false); 765 download_file->Initialize(false);
765 // |download_file| is owned by DownloadFileManager. 766 // |download_file| is owned by DownloadFileManager.
766 AddDownloadToFileManager(info->download_id.local(), download_file); 767 AddDownloadToFileManager(info->download_id.local(), download_file);
767 768
768 ContinueDownloadWithPath(download, new_path); 769 ContinueDownloadWithPath(download, new_path);
769 message_loop_.RunAllPending(); 770 message_loop_.RunAllPending();
770 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 771 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
771 772
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
827 new DownloadItemModel(download)); 828 new DownloadItemModel(download));
828 829
829 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 830 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
830 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 831 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
831 832
832 // Create and initialize the download file. We're bypassing the first part 833 // Create and initialize the download file. We're bypassing the first part
833 // of the download process and skipping to the part after the final file 834 // of the download process and skipping to the part after the final file
834 // name has been chosen, so we need to initialize the download file 835 // name has been chosen, so we need to initialize the download file
835 // properly. 836 // properly.
836 DownloadFile* download_file( 837 DownloadFile* download_file(
837 new DownloadFile(info.get(), DownloadRequestHandle(), download_manager_)); 838 new DownloadFile(info.get(), new DownloadRequestHandle(),
839 download_manager_));
838 download_file->Rename(cr_path); 840 download_file->Rename(cr_path);
839 // This creates the .crdownload version of the file. 841 // This creates the .crdownload version of the file.
840 download_file->Initialize(false); 842 download_file->Initialize(false);
841 // |download_file| is owned by DownloadFileManager. 843 // |download_file| is owned by DownloadFileManager.
842 AddDownloadToFileManager(info->download_id.local(), download_file); 844 AddDownloadToFileManager(info->download_id.local(), download_file);
843 845
844 ContinueDownloadWithPath(download, new_path); 846 ContinueDownloadWithPath(download, new_path);
845 message_loop_.RunAllPending(); 847 message_loop_.RunAllPending();
846 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 848 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
847 849
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 882 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
881 EXPECT_TRUE(observer->was_updated()); 883 EXPECT_TRUE(observer->was_updated());
882 EXPECT_FALSE(observer->was_opened()); 884 EXPECT_FALSE(observer->was_opened());
883 EXPECT_TRUE(download->file_externally_removed()); 885 EXPECT_TRUE(download->file_externally_removed());
884 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); 886 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
885 EXPECT_EQ(download_item_model->GetStatusText(), 887 EXPECT_EQ(download_item_model->GetStatusText(),
886 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); 888 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED));
887 889
888 EXPECT_FALSE(file_util::PathExists(new_path)); 890 EXPECT_FALSE(file_util::PathExists(new_path));
889 } 891 }
OLDNEW
« no previous file with comments | « no previous file | content/browser/download/download_file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698