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

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

Issue 8372034: Created an interface for DownloadFile, for use in unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaned up download file interface and unit test class. 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"
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_impl.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_id_factory.h"
30 #include "content/browser/download/download_item.h" 30 #include "content/browser/download/download_item.h"
31 #include "content/browser/download/download_manager.h" 31 #include "content/browser/download/download_manager.h"
32 #include "content/browser/download/download_request_handle.h" 32 #include "content/browser/download/download_request_handle.h"
33 #include "content/browser/download/download_status_updater.h" 33 #include "content/browser/download/download_status_updater.h"
34 #include "content/browser/download/interrupt_reasons.h" 34 #include "content/browser/download/interrupt_reasons.h"
35 #include "content/browser/download/mock_download_manager.h" 35 #include "content/browser/download/mock_download_manager.h"
36 #include "content/test/test_browser_thread.h" 36 #include "content/test/test_browser_thread.h"
37 #include "grit/generated_resources.h" 37 #include "grit/generated_resources.h"
38 #include "net/base/io_buffer.h" 38 #include "net/base/io_buffer.h"
39 #include "net/base/mock_file_stream.h"
40 #include "testing/gmock/include/gmock/gmock.h" 39 #include "testing/gmock/include/gmock/gmock.h"
41 #include "testing/gmock_mutant.h" 40 #include "testing/gmock_mutant.h"
42 #include "testing/gtest/include/gtest/gtest.h" 41 #include "testing/gtest/include/gtest/gtest.h"
43 #include "ui/base/l10n/l10n_util.h" 42 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/base/text/bytes_formatting.h" 43 #include "ui/base/text/bytes_formatting.h"
45 44
46 using content::BrowserThread; 45 using content::BrowserThread;
47 46
48 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; 47 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain";
49 48
50 class DownloadManagerTest : public testing::Test { 49 class DownloadManagerTest : public testing::Test {
51 public: 50 public:
52 static const char* kTestData; 51 static const char* kTestData;
53 static const size_t kTestDataLen; 52 static const size_t kTestDataLen;
54 53
55 DownloadManagerTest() 54 DownloadManagerTest()
56 : profile_(new TestingProfile()), 55 : profile_(new TestingProfile()),
57 download_manager_delegate_(new ChromeDownloadManagerDelegate( 56 download_manager_delegate_(new ChromeDownloadManagerDelegate(
58 profile_.get())), 57 profile_.get())),
59 id_factory_(new DownloadIdFactory(kValidIdDomain)), 58 id_factory_(new DownloadIdFactory(kValidIdDomain)),
60 download_manager_(new MockDownloadManager( 59 download_manager_(new DownloadManager(
Randy Smith (Not in Mondays) 2011/11/10 21:58:43 Why this change here? I thought that was the othe
ahendrickson 2011/11/13 00:50:21 Ah, yes, this is a result of splitting a CL in two
61 download_manager_delegate_, 60 download_manager_delegate_,
62 id_factory_, 61 id_factory_,
63 &download_status_updater_)), 62 &download_status_updater_)),
64 ui_thread_(BrowserThread::UI, &message_loop_), 63 ui_thread_(BrowserThread::UI, &message_loop_),
65 file_thread_(BrowserThread::FILE, &message_loop_), 64 file_thread_(BrowserThread::FILE, &message_loop_),
66 download_buffer_(new content::DownloadBuffer) { 65 download_buffer_(new content::DownloadBuffer) {
67 download_manager_->Init(profile_.get()); 66 download_manager_->Init(profile_.get());
68 download_manager_delegate_->SetDownloadManager(download_manager_); 67 download_manager_delegate_->SetDownloadManager(download_manager_);
69 } 68 }
70 69
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 safety_state != DownloadItem::SAFE : safety_state == DownloadItem::SAFE; 155 safety_state != DownloadItem::SAFE : safety_state == DownloadItem::SAFE;
157 } 156 }
158 157
159 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 158 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
160 }; 159 };
161 160
162 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; 161 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad";
163 const size_t DownloadManagerTest::kTestDataLen = 162 const size_t DownloadManagerTest::kTestDataLen =
164 strlen(DownloadManagerTest::kTestData); 163 strlen(DownloadManagerTest::kTestData);
165 164
166 // A DownloadFile that we can inject errors into. Uses MockFileStream. 165 // A DownloadFile that we can inject errors into.
167 // Note: This can't be in an anonymous namespace because it must be declared 166 class DownloadFileWithErrors : public DownloadFileImpl {
Randy Smith (Not in Mondays) 2011/11/10 21:58:43 Just noting that in the context of non-test code I
168 // as a friend of |DownloadFile| in order to access its private members.
169 class DownloadFileWithMockStream : public DownloadFile {
170 public: 167 public:
171 DownloadFileWithMockStream(DownloadCreateInfo* info, 168 DownloadFileWithErrors(DownloadCreateInfo* info, DownloadManager* manager);
172 DownloadManager* manager, 169 virtual ~DownloadFileWithErrors() {}
173 net::testing::MockFileStream* stream);
174 170
175 virtual ~DownloadFileWithMockStream() {} 171 // BaseFile delegated functions.
172 virtual net::Error Initialize(bool calculate_hash);
173 virtual net::Error AppendDataToFile(const char* data, size_t data_len);
174 virtual net::Error Rename(const FilePath& full_path);
176 175
177 void SetForcedError(int error); 176 void set_forced_error(net::Error error) { forced_error_ = error; }
177 void clear_forced_error() { forced_error_ = net::OK; }
178 net::Error forced_error() const { return forced_error_; }
178 179
179 protected: 180 private:
180 // This version creates a |MockFileStream| instead of a |FileStream|. 181 net::Error ReturnError(net::Error function_error) {
181 virtual void CreateFileStream() OVERRIDE; 182 if (forced_error_ != net::OK) {
183 net::Error ret = forced_error_;
184 clear_forced_error();
185 return ret;
186 }
187
188 return function_error;
189 }
190
191 net::Error forced_error_;
182 }; 192 };
183 193
184 DownloadFileWithMockStream::DownloadFileWithMockStream( 194 DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info,
185 DownloadCreateInfo* info, 195 DownloadManager* manager)
186 DownloadManager* manager, 196 : DownloadFileImpl(info, new DownloadRequestHandle(), manager),
187 net::testing::MockFileStream* stream) 197 forced_error_(net::OK) {
188 : DownloadFile(info, new DownloadRequestHandle(), manager) {
189 DCHECK(file_stream_ == NULL);
190 file_stream_.reset(stream);
191 } 198 }
192 199
193 void DownloadFileWithMockStream::SetForcedError(int error) 200 net::Error DownloadFileWithErrors::Initialize(bool calculate_hash) {
194 { 201 return ReturnError(DownloadFileImpl::Initialize(calculate_hash));
195 // |file_stream_| can only be set in the constructor and in
196 // CreateFileStream(), both of which insure that it is a |MockFileStream|.
197 net::testing::MockFileStream* mock_stream =
198 static_cast<net::testing::MockFileStream *>(file_stream_.get());
199 mock_stream->set_forced_error(error);
200 } 202 }
201 void DownloadFileWithMockStream::CreateFileStream() { 203
202 file_stream_.reset(new net::testing::MockFileStream); 204 net::Error DownloadFileWithErrors::AppendDataToFile(const char* data,
205 size_t data_len) {
206 return ReturnError(DownloadFileImpl::AppendDataToFile(data, data_len));
207 }
208
209 net::Error DownloadFileWithErrors::Rename(const FilePath& full_path) {
210 return ReturnError(DownloadFileImpl::Rename(full_path));
203 } 211 }
204 212
205 namespace { 213 namespace {
206 214
207 const struct { 215 const struct {
208 const char* url; 216 const char* url;
209 const char* mime_type; 217 const char* mime_type;
210 bool save_as; 218 bool save_as;
211 bool prompt_for_download; 219 bool prompt_for_download;
212 bool expected_save_as; 220 bool expected_save_as;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 // Safe download, download finishes AFTER file name determined. 281 // Safe download, download finishes AFTER file name determined.
274 // Needs to be renamed twice. 282 // Needs to be renamed twice.
275 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, }, 283 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, },
276 // Dangerous download, download finishes AFTER file name determined. 284 // Dangerous download, download finishes AFTER file name determined.
277 // Needs to be renamed only once. 285 // Needs to be renamed only once.
278 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, 286 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, },
279 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, 287 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, },
280 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, 288 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, },
281 }; 289 };
282 290
283 class MockDownloadFile : public DownloadFile { 291 class GMockDownloadFile : public DownloadFileImpl {
284 public: 292 public:
285 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) 293 GMockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager)
286 : DownloadFile(info, new DownloadRequestHandle(), manager), 294 : DownloadFileImpl(info, new DownloadRequestHandle(), manager),
287 renamed_count_(0) { } 295 renamed_count_(0) { }
288 virtual ~MockDownloadFile() { Destructed(); } 296 virtual ~GMockDownloadFile() { Destructed(); }
289 MOCK_METHOD1(Rename, net::Error(const FilePath&)); 297 MOCK_METHOD1(Rename, net::Error(const FilePath&));
290 MOCK_METHOD0(Destructed, void()); 298 MOCK_METHOD0(Destructed, void());
291 299
292 net::Error TestMultipleRename( 300 net::Error TestMultipleRename(
293 int expected_count, const FilePath& expected, 301 int expected_count, const FilePath& expected,
294 const FilePath& path) { 302 const FilePath& path) {
295 ++renamed_count_; 303 ++renamed_count_;
296 EXPECT_EQ(expected_count, renamed_count_); 304 EXPECT_EQ(expected_count, renamed_count_);
297 EXPECT_EQ(expected.value(), path.value()); 305 EXPECT_EQ(expected.value(), path.value());
298 return net::OK; 306 return net::OK;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 // responsible for deleting it. In these unit tests, however, we 400 // responsible for deleting it. In these unit tests, however, we
393 // don't call the function that deletes it, so we do so ourselves. 401 // don't call the function that deletes it, so we do so ourselves.
394 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 402 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
395 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); 403 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i));
396 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; 404 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
397 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); 405 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
398 info->mime_type = kStartDownloadCases[i].mime_type; 406 info->mime_type = kStartDownloadCases[i].mime_type;
399 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 407 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
400 408
401 DownloadFile* download_file( 409 DownloadFile* download_file(
402 new DownloadFile(info.get(), new DownloadRequestHandle(), 410 new DownloadFileImpl(info.get(), new DownloadRequestHandle(),
403 download_manager_)); 411 download_manager_));
404 AddDownloadToFileManager(info->download_id.local(), download_file); 412 AddDownloadToFileManager(info->download_id.local(), download_file);
405 download_file->Initialize(false); 413 download_file->Initialize(false);
406 download_manager_->StartDownload(info->download_id.local()); 414 download_manager_->StartDownload(info->download_id.local());
407 message_loop_.RunAllPending(); 415 message_loop_.RunAllPending();
408 416
409 // SelectFileObserver will have recorded any attempt to open the 417 // SelectFileObserver will have recorded any attempt to open the
410 // select file dialog. 418 // select file dialog.
411 // Note that DownloadManager::FileSelectionCanceled() is never called. 419 // Note that DownloadManager::FileSelectionCanceled() is never called.
412 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, 420 EXPECT_EQ(kStartDownloadCases[i].expected_save_as,
413 observer.ShowedFileDialogForId(i)); 421 observer.ShowedFileDialogForId(i));
414 } 422 }
415 } 423 }
416 424
417 TEST_F(DownloadManagerTest, DownloadRenameTest) { 425 TEST_F(DownloadManagerTest, DownloadRenameTest) {
418 using ::testing::_; 426 using ::testing::_;
419 using ::testing::CreateFunctor; 427 using ::testing::CreateFunctor;
420 using ::testing::Invoke; 428 using ::testing::Invoke;
421 using ::testing::Return; 429 using ::testing::Return;
422 430
423 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { 431 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) {
424 // Normally, the download system takes ownership of info, and is 432 // Normally, the download system takes ownership of info, and is
425 // responsible for deleting it. In these unit tests, however, we 433 // responsible for deleting it. In these unit tests, however, we
426 // don't call the function that deletes it, so we do so ourselves. 434 // don't call the function that deletes it, so we do so ourselves.
427 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 435 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
428 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); 436 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i));
429 info->prompt_user_for_save_location = false; 437 info->prompt_user_for_save_location = false;
430 info->url_chain.push_back(GURL()); 438 info->url_chain.push_back(GURL());
431 const FilePath new_path(kDownloadRenameCases[i].suggested_path); 439 const FilePath new_path(kDownloadRenameCases[i].suggested_path);
432 440
433 MockDownloadFile* download_file( 441 GMockDownloadFile* download_file(
434 new MockDownloadFile(info.get(), download_manager_)); 442 new GMockDownloadFile(info.get(), download_manager_));
435 AddDownloadToFileManager(info->download_id.local(), download_file); 443 AddDownloadToFileManager(info->download_id.local(), download_file);
436 444
437 // |download_file| is owned by DownloadFileManager. 445 // |download_file| is owned by DownloadFileManager.
438 ::testing::Mock::AllowLeak(download_file); 446 ::testing::Mock::AllowLeak(download_file);
439 EXPECT_CALL(*download_file, Destructed()).Times(1); 447 EXPECT_CALL(*download_file, Destructed()).Times(1);
440 448
441 if (kDownloadRenameCases[i].expected_rename_count == 1) { 449 if (kDownloadRenameCases[i].expected_rename_count == 1) {
442 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(net::OK)); 450 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(net::OK));
443 } else { 451 } else {
444 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); 452 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count);
445 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); 453 FilePath crdownload(download_util::GetCrDownloadPath(new_path));
446 EXPECT_CALL(*download_file, Rename(_)) 454 EXPECT_CALL(*download_file, Rename(_))
447 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( 455 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor(
448 download_file, &MockDownloadFile::TestMultipleRename, 456 download_file, &GMockDownloadFile::TestMultipleRename,
449 1, crdownload)))) 457 1, crdownload))))
450 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( 458 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor(
451 download_file, &MockDownloadFile::TestMultipleRename, 459 download_file, &GMockDownloadFile::TestMultipleRename,
452 2, new_path)))); 460 2, new_path))));
453 } 461 }
454 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 462 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
455 DownloadItem* download = GetActiveDownloadItem(i); 463 DownloadItem* download = GetActiveDownloadItem(i);
456 ASSERT_TRUE(download != NULL); 464 ASSERT_TRUE(download != NULL);
457 if (kDownloadRenameCases[i].is_dangerous_file) 465 if (kDownloadRenameCases[i].is_dangerous_file)
458 download->MarkFileDangerous(); 466 download->MarkFileDangerous();
459 if (kDownloadRenameCases[i].is_dangerous_url) 467 if (kDownloadRenameCases[i].is_dangerous_url)
460 download->MarkUrlDangerous(); 468 download->MarkUrlDangerous();
461 469
(...skipping 26 matching lines...) Expand all
488 // responsible for deleting it. In these unit tests, however, we 496 // responsible for deleting it. In these unit tests, however, we
489 // don't call the function that deletes it, so we do so ourselves. 497 // don't call the function that deletes it, so we do so ourselves.
490 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 498 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
491 info->download_id = DownloadId(kValidIdDomain, 0); 499 info->download_id = DownloadId(kValidIdDomain, 0);
492 info->prompt_user_for_save_location = false; 500 info->prompt_user_for_save_location = false;
493 info->url_chain.push_back(GURL()); 501 info->url_chain.push_back(GURL());
494 info->total_bytes = static_cast<int64>(kTestDataLen); 502 info->total_bytes = static_cast<int64>(kTestDataLen);
495 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 503 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
496 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); 504 const FilePath cr_path(download_util::GetCrDownloadPath(new_path));
497 505
498 MockDownloadFile* download_file( 506 GMockDownloadFile* download_file(
499 new MockDownloadFile(info.get(), download_manager_)); 507 new GMockDownloadFile(info.get(), download_manager_));
500 AddDownloadToFileManager(info->download_id.local(), download_file); 508 AddDownloadToFileManager(info->download_id.local(), download_file);
501 509
502 // |download_file| is owned by DownloadFileManager. 510 // |download_file| is owned by DownloadFileManager.
503 ::testing::Mock::AllowLeak(download_file); 511 ::testing::Mock::AllowLeak(download_file);
504 EXPECT_CALL(*download_file, Destructed()).Times(1); 512 EXPECT_CALL(*download_file, Destructed()).Times(1);
505 513
506 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(net::OK)); 514 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(net::OK));
507 515
508 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 516 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
509 517
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 } 570 }
563 571
564 // Test the behavior of DownloadFileManager and DownloadManager in the event 572 // Test the behavior of DownloadFileManager and DownloadManager in the event
565 // of a file error while writing the download to disk. 573 // of a file error while writing the download to disk.
566 TEST_F(DownloadManagerTest, DownloadFileErrorTest) { 574 TEST_F(DownloadManagerTest, DownloadFileErrorTest) {
567 // Create a temporary file and a mock stream. 575 // Create a temporary file and a mock stream.
568 FilePath path; 576 FilePath path;
569 ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); 577 ASSERT_TRUE(file_util::CreateTemporaryFile(&path));
570 578
571 // This file stream will be used, until the first rename occurs. 579 // This file stream will be used, until the first rename occurs.
572 net::testing::MockFileStream* mock_stream = new net::testing::MockFileStream; 580 net::FileStream* stream = new net::FileStream;
573 ASSERT_EQ(0, mock_stream->Open( 581 ASSERT_EQ(0, stream->Open(
574 path, 582 path,
575 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); 583 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE));
576 584
577 // Normally, the download system takes ownership of info, and is 585 // Normally, the download system takes ownership of info, and is
578 // responsible for deleting it. In these unit tests, however, we 586 // responsible for deleting it. In these unit tests, however, we
579 // don't call the function that deletes it, so we do so ourselves. 587 // don't call the function that deletes it, so we do so ourselves.
580 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 588 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
581 static const int32 local_id = 0; 589 static const int32 local_id = 0;
582 info->download_id = DownloadId(kValidIdDomain, local_id); 590 info->download_id = DownloadId(kValidIdDomain, local_id);
583 info->prompt_user_for_save_location = false; 591 info->prompt_user_for_save_location = false;
584 info->url_chain.push_back(GURL()); 592 info->url_chain.push_back(GURL());
585 info->total_bytes = static_cast<int64>(kTestDataLen * 3); 593 info->total_bytes = static_cast<int64>(kTestDataLen * 3);
586 info->save_info.file_path = path; 594 info->save_info.file_path = path;
595 info->save_info.file_stream.reset(stream);
587 596
588 // Create a download file that we can insert errors into. 597 // Create a download file that we can insert errors into.
589 DownloadFileWithMockStream* download_file(new DownloadFileWithMockStream( 598 DownloadFileWithErrors* download_file(new DownloadFileWithErrors(
590 info.get(), download_manager_, mock_stream)); 599 info.get(), download_manager_));
591 AddDownloadToFileManager(local_id, download_file); 600 AddDownloadToFileManager(local_id, download_file);
592 601
593 // |download_file| is owned by DownloadFileManager. 602 // |download_file| is owned by DownloadFileManager.
594 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 603 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
595 604
596 DownloadItem* download = GetActiveDownloadItem(0); 605 DownloadItem* download = GetActiveDownloadItem(0);
597 ASSERT_TRUE(download != NULL); 606 ASSERT_TRUE(download != NULL);
598 // This will keep track of what should be displayed on the shelf. 607 // This will keep track of what should be displayed on the shelf.
599 scoped_ptr<DownloadItemModel> download_item_model( 608 scoped_ptr<DownloadItemModel> download_item_model(
600 new DownloadItemModel(download)); 609 new DownloadItemModel(download));
601 610
602 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 611 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
603 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 612 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
604 613
605 // Add some data before finalizing the file name. 614 // Add some data before finalizing the file name.
606 UpdateData(local_id, kTestData, kTestDataLen); 615 UpdateData(local_id, kTestData, kTestDataLen);
607 616
608 // Finalize the file name. 617 // Finalize the file name.
609 ContinueDownloadWithPath(download, path); 618 ContinueDownloadWithPath(download, path);
610 message_loop_.RunAllPending(); 619 message_loop_.RunAllPending();
611 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 620 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
612 621
613 // Add more data. 622 // Add more data.
614 UpdateData(local_id, kTestData, kTestDataLen); 623 UpdateData(local_id, kTestData, kTestDataLen);
615 624
616 // Add more data, but an error occurs. 625 // Add more data, but an error occurs.
617 download_file->SetForcedError(net::ERR_FAILED); 626 download_file->set_forced_error(net::ERR_FAILED);
618 UpdateData(local_id, kTestData, kTestDataLen); 627 UpdateData(local_id, kTestData, kTestDataLen);
619 628
620 // Check the state. The download should have been interrupted. 629 // Check the state. The download should have been interrupted.
621 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 630 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
622 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 631 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
623 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); 632 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED));
624 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); 633 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE));
625 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 634 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
626 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 635 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
627 EXPECT_TRUE(observer->was_updated()); 636 EXPECT_TRUE(observer->was_updated());
628 EXPECT_FALSE(observer->was_opened()); 637 EXPECT_FALSE(observer->was_opened());
629 EXPECT_FALSE(download->file_externally_removed()); 638 EXPECT_FALSE(download->file_externally_removed());
630 EXPECT_EQ(DownloadItem::INTERRUPTED, download->state()); 639 EXPECT_EQ(DownloadItem::INTERRUPTED, download->state());
631 640
632 // Check the download shelf's information. 641 // Check the download shelf's information.
633 size_t error_size = kTestDataLen * 2; 642 size_t error_size = kTestDataLen * 3;
634 size_t total_size = kTestDataLen * 3; 643 size_t total_size = kTestDataLen * 3;
635 ui::DataUnits amount_units = ui::GetByteDisplayUnits(kTestDataLen); 644 ui::DataUnits amount_units = ui::GetByteDisplayUnits(kTestDataLen);
636 string16 simple_size = 645 string16 simple_size =
637 ui::FormatBytesWithUnits(error_size, amount_units, false); 646 ui::FormatBytesWithUnits(error_size, amount_units, false);
638 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( 647 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality(
639 ui::FormatBytesWithUnits(total_size, amount_units, true)); 648 ui::FormatBytesWithUnits(total_size, amount_units, true));
640 EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED, 649 EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED,
641 simple_size, 650 simple_size,
642 simple_total), 651 simple_total),
643 download_item_model->GetStatusText()); 652 download_item_model->GetStatusText());
(...skipping 12 matching lines...) Expand all
656 // Normally, the download system takes ownership of info, and is 665 // Normally, the download system takes ownership of info, and is
657 // responsible for deleting it. In these unit tests, however, we 666 // responsible for deleting it. In these unit tests, however, we
658 // don't call the function that deletes it, so we do so ourselves. 667 // don't call the function that deletes it, so we do so ourselves.
659 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 668 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
660 info->download_id = DownloadId(kValidIdDomain, 0); 669 info->download_id = DownloadId(kValidIdDomain, 0);
661 info->prompt_user_for_save_location = false; 670 info->prompt_user_for_save_location = false;
662 info->url_chain.push_back(GURL()); 671 info->url_chain.push_back(GURL());
663 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 672 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
664 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); 673 const FilePath cr_path(download_util::GetCrDownloadPath(new_path));
665 674
666 MockDownloadFile* download_file( 675 GMockDownloadFile* download_file(
667 new MockDownloadFile(info.get(), download_manager_)); 676 new GMockDownloadFile(info.get(), download_manager_));
668 AddDownloadToFileManager(info->download_id.local(), download_file); 677 AddDownloadToFileManager(info->download_id.local(), download_file);
669 678
670 // |download_file| is owned by DownloadFileManager. 679 // |download_file| is owned by DownloadFileManager.
671 ::testing::Mock::AllowLeak(download_file); 680 ::testing::Mock::AllowLeak(download_file);
672 EXPECT_CALL(*download_file, Destructed()).Times(1); 681 EXPECT_CALL(*download_file, Destructed()).Times(1);
673 682
674 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(net::OK)); 683 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(net::OK));
675 684
676 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 685 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
677 686
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 new DownloadItemModel(download)); 762 new DownloadItemModel(download));
754 763
755 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 764 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
756 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 765 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
757 766
758 // Create and initialize the download file. We're bypassing the first part 767 // 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 768 // 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 769 // name has been chosen, so we need to initialize the download file
761 // properly. 770 // properly.
762 DownloadFile* download_file( 771 DownloadFile* download_file(
763 new DownloadFile(info.get(), new DownloadRequestHandle(), 772 new DownloadFileImpl(info.get(), new DownloadRequestHandle(),
764 download_manager_)); 773 download_manager_));
765 download_file->Rename(cr_path); 774 download_file->Rename(cr_path);
766 // This creates the .crdownload version of the file. 775 // This creates the .crdownload version of the file.
767 download_file->Initialize(false); 776 download_file->Initialize(false);
768 // |download_file| is owned by DownloadFileManager. 777 // |download_file| is owned by DownloadFileManager.
769 AddDownloadToFileManager(info->download_id.local(), download_file); 778 AddDownloadToFileManager(info->download_id.local(), download_file);
770 779
771 ContinueDownloadWithPath(download, new_path); 780 ContinueDownloadWithPath(download, new_path);
772 message_loop_.RunAllPending(); 781 message_loop_.RunAllPending();
773 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 782 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
774 783
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 new DownloadItemModel(download)); 839 new DownloadItemModel(download));
831 840
832 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 841 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
833 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 842 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
834 843
835 // Create and initialize the download file. We're bypassing the first part 844 // 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 845 // 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 846 // name has been chosen, so we need to initialize the download file
838 // properly. 847 // properly.
839 DownloadFile* download_file( 848 DownloadFile* download_file(
840 new DownloadFile(info.get(), new DownloadRequestHandle(), 849 new DownloadFileImpl(info.get(), new DownloadRequestHandle(),
841 download_manager_)); 850 download_manager_));
842 download_file->Rename(cr_path); 851 download_file->Rename(cr_path);
843 // This creates the .crdownload version of the file. 852 // This creates the .crdownload version of the file.
844 download_file->Initialize(false); 853 download_file->Initialize(false);
845 // |download_file| is owned by DownloadFileManager. 854 // |download_file| is owned by DownloadFileManager.
846 AddDownloadToFileManager(info->download_id.local(), download_file); 855 AddDownloadToFileManager(info->download_id.local(), download_file);
847 856
848 ContinueDownloadWithPath(download, new_path); 857 ContinueDownloadWithPath(download, new_path);
849 message_loop_.RunAllPending(); 858 message_loop_.RunAllPending();
850 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 859 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
851 860
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 893 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
885 EXPECT_TRUE(observer->was_updated()); 894 EXPECT_TRUE(observer->was_updated());
886 EXPECT_FALSE(observer->was_opened()); 895 EXPECT_FALSE(observer->was_opened());
887 EXPECT_TRUE(download->file_externally_removed()); 896 EXPECT_TRUE(download->file_externally_removed());
888 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); 897 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
889 EXPECT_EQ(download_item_model->GetStatusText(), 898 EXPECT_EQ(download_item_model->GetStatusText(),
890 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); 899 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED));
891 900
892 EXPECT_FALSE(file_util::PathExists(new_path)); 901 EXPECT_FALSE(file_util::PathExists(new_path));
893 } 902 }
OLDNEW
« no previous file with comments | « no previous file | content/browser/download/base_file.h » ('j') | content/browser/download/download_file.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698