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

Side by Side Diff: content/browser/download/download_manager_impl_unittest.cc

Issue 10392111: Use ByteStream in downloads system to decouple source and sink. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync'd to LKGR. Created 8 years, 6 months 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <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/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/string16.h" 15 #include "base/string16.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "content/browser/download/download_buffer.h"
20 #include "content/browser/download/download_create_info.h" 19 #include "content/browser/download/download_create_info.h"
21 #include "content/browser/download/download_file_impl.h" 20 #include "content/browser/download/download_file_impl.h"
22 #include "content/browser/download/download_file_manager.h" 21 #include "content/browser/download/download_file_manager.h"
23 #include "content/browser/download/download_manager_impl.h" 22 #include "content/browser/download/download_manager_impl.h"
24 #include "content/browser/download/download_request_handle.h" 23 #include "content/browser/download/download_request_handle.h"
25 #include "content/browser/download/mock_download_file.h" 24 #include "content/browser/download/mock_download_file.h"
26 #include "content/browser/power_save_blocker.h" 25 #include "content/browser/power_save_blocker.h"
27 #include "content/public/browser/download_interrupt_reasons.h" 26 #include "content/public/browser/download_interrupt_reasons.h"
28 #include "content/public/browser/download_item.h" 27 #include "content/public/browser/download_item.h"
29 #include "content/public/browser/download_manager_delegate.h" 28 #include "content/public/browser/download_manager_delegate.h"
(...skipping 29 matching lines...) Expand all
59 using content::BrowserThread; 58 using content::BrowserThread;
60 using content::DownloadFile; 59 using content::DownloadFile;
61 using content::DownloadId; 60 using content::DownloadId;
62 using content::DownloadItem; 61 using content::DownloadItem;
63 using content::DownloadManager; 62 using content::DownloadManager;
64 using content::WebContents; 63 using content::WebContents;
65 using ::testing::NiceMock; 64 using ::testing::NiceMock;
66 using ::testing::ReturnRef; 65 using ::testing::ReturnRef;
67 using ::testing::Return; 66 using ::testing::Return;
68 67
68 namespace content {
69 class ByteStreamReader;
70 }
71
69 namespace { 72 namespace {
70 73
71 FilePath GetTempDownloadPath(const FilePath& suggested_path) { 74 FilePath GetTempDownloadPath(const FilePath& suggested_path) {
72 return FilePath(suggested_path.value() + FILE_PATH_LITERAL(".temp")); 75 return FilePath(suggested_path.value() + FILE_PATH_LITERAL(".temp"));
73 } 76 }
74 77
75 class MockDownloadFileFactory 78 class MockDownloadFileFactory
76 : public DownloadFileManager::DownloadFileFactory { 79 : public DownloadFileManager::DownloadFileFactory {
77 public: 80 public:
78 MockDownloadFileFactory() {} 81 MockDownloadFileFactory() {}
79 82
80 virtual DownloadFile* CreateFile( 83 virtual DownloadFile* CreateFile(
81 DownloadCreateInfo* info, 84 DownloadCreateInfo* info,
85 scoped_ptr<content::ByteStreamReader> stream,
82 const DownloadRequestHandle& request_handle, 86 const DownloadRequestHandle& request_handle,
83 DownloadManager* download_manager, 87 DownloadManager* download_manager,
84 bool calculate_hash, 88 bool calculate_hash,
85 const net::BoundNetLog& bound_net_log) OVERRIDE; 89 const net::BoundNetLog& bound_net_log) OVERRIDE;
86 }; 90 };
87 91
88 DownloadFile* MockDownloadFileFactory::CreateFile( 92 DownloadFile* MockDownloadFileFactory::CreateFile(
89 DownloadCreateInfo* info, 93 DownloadCreateInfo* info,
94 scoped_ptr<content::ByteStreamReader> stream,
90 const DownloadRequestHandle& request_handle, 95 const DownloadRequestHandle& request_handle,
91 DownloadManager* download_manager, 96 DownloadManager* download_manager,
92 bool calculate_hash, 97 bool calculate_hash,
93 const net::BoundNetLog& bound_net_log) { 98 const net::BoundNetLog& bound_net_log) {
94 NOTREACHED(); 99 NOTREACHED();
95 return NULL; 100 return NULL;
96 } 101 }
97 102
98 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; 103 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain";
99 104
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 public: 259 public:
255 static const char* kTestData; 260 static const char* kTestData;
256 static const size_t kTestDataLen; 261 static const size_t kTestDataLen;
257 262
258 DownloadManagerTest() 263 DownloadManagerTest()
259 : browser_context(new content::TestBrowserContext()), 264 : browser_context(new content::TestBrowserContext()),
260 download_manager_delegate_(new TestDownloadManagerDelegate()), 265 download_manager_delegate_(new TestDownloadManagerDelegate()),
261 download_manager_(new DownloadManagerImpl( 266 download_manager_(new DownloadManagerImpl(
262 download_manager_delegate_.get(), NULL)), 267 download_manager_delegate_.get(), NULL)),
263 ui_thread_(BrowserThread::UI, &message_loop_), 268 ui_thread_(BrowserThread::UI, &message_loop_),
264 file_thread_(BrowserThread::FILE, &message_loop_), 269 file_thread_(BrowserThread::FILE, &message_loop_) {
265 download_buffer_(new content::DownloadBuffer) {
266 download_manager_->Init(browser_context.get()); 270 download_manager_->Init(browser_context.get());
267 download_manager_delegate_->set_download_manager(download_manager_); 271 download_manager_delegate_->set_download_manager(download_manager_);
268 } 272 }
269 273
270 ~DownloadManagerTest() { 274 ~DownloadManagerTest() {
271 download_manager_->Shutdown(); 275 download_manager_->Shutdown();
272 // browser_context must outlive download_manager_, so we explicitly delete 276 // browser_context must outlive download_manager_, so we explicitly delete
273 // download_manager_ first. 277 // download_manager_ first.
274 download_manager_ = NULL; 278 download_manager_ = NULL;
275 download_manager_delegate_.reset(); 279 download_manager_delegate_.reset();
(...skipping 30 matching lines...) Expand all
306 download_manager_->FileSelected(path, download_id); 310 download_manager_->FileSelected(path, download_id);
307 } 311 }
308 312
309 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { 313 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) {
310 download->OnTargetPathDetermined( 314 download->OnTargetPathDetermined(
311 path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 315 path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
312 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 316 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
313 download_manager_->OnTargetPathAvailable(download); 317 download_manager_->OnTargetPathAvailable(download);
314 } 318 }
315 319
316 void UpdateData(int32 id, const char* data, size_t length) {
317 // We are passing ownership of this buffer to the download file manager.
318 net::IOBuffer* io_buffer = new net::IOBuffer(length);
319 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|.
320 io_buffer->AddRef();
321 memcpy(io_buffer->data(), data, length);
322
323 download_buffer_->AddData(io_buffer, length);
324
325 BrowserThread::PostTask(
326 BrowserThread::FILE, FROM_HERE,
327 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(),
328 DownloadId(kValidIdDomain, id), download_buffer_));
329
330 message_loop_.RunAllPending();
331 }
332
333 void OnDownloadInterrupted(int32 download_id, int64 size, 320 void OnDownloadInterrupted(int32 download_id, int64 size,
334 const std::string& hash_state, 321 const std::string& hash_state,
335 content::DownloadInterruptReason reason) { 322 content::DownloadInterruptReason reason) {
336 download_manager_->OnDownloadInterrupted(download_id, size, 323 download_manager_->OnDownloadInterrupted(download_id, size,
337 hash_state, reason); 324 hash_state, reason);
338 } 325 }
339 326
340 // Get the download item with ID |id|. 327 // Get the download item with ID |id|.
341 DownloadItem* GetActiveDownloadItem(int32 id) { 328 DownloadItem* GetActiveDownloadItem(int32 id) {
342 return download_manager_->GetActiveDownload(id); 329 return download_manager_->GetActiveDownload(id);
343 } 330 }
344 331
345 FilePath GetPathInDownloadsDir(const FilePath::StringType& fragment) { 332 FilePath GetPathInDownloadsDir(const FilePath::StringType& fragment) {
346 DCHECK(scoped_download_dir_.IsValid()); 333 DCHECK(scoped_download_dir_.IsValid());
347 FilePath full_path(scoped_download_dir_.path().Append(fragment)); 334 FilePath full_path(scoped_download_dir_.path().Append(fragment));
348 return full_path.NormalizePathSeparators(); 335 return full_path.NormalizePathSeparators();
349 } 336 }
350 337
351 protected: 338 protected:
352 scoped_ptr<content::TestBrowserContext> browser_context; 339 scoped_ptr<content::TestBrowserContext> browser_context;
353 scoped_ptr<TestDownloadManagerDelegate> download_manager_delegate_; 340 scoped_ptr<TestDownloadManagerDelegate> download_manager_delegate_;
354 scoped_refptr<DownloadManagerImpl> download_manager_; 341 scoped_refptr<DownloadManagerImpl> download_manager_;
355 scoped_refptr<DownloadFileManager> file_manager_; 342 scoped_refptr<DownloadFileManager> file_manager_;
356 MessageLoopForUI message_loop_; 343 MessageLoopForUI message_loop_;
357 content::TestBrowserThread ui_thread_; 344 content::TestBrowserThread ui_thread_;
358 content::TestBrowserThread file_thread_; 345 content::TestBrowserThread file_thread_;
359 scoped_refptr<content::DownloadBuffer> download_buffer_;
360 ScopedTempDir scoped_download_dir_; 346 ScopedTempDir scoped_download_dir_;
361 347
362 DownloadFileManager* file_manager() { 348 DownloadFileManager* file_manager() {
363 if (!file_manager_) { 349 if (!file_manager_) {
364 file_manager_ = new DownloadFileManager(new MockDownloadFileFactory); 350 file_manager_ = new DownloadFileManager(new MockDownloadFileFactory);
365 download_manager_->SetFileManagerForTesting(file_manager_); 351 download_manager_->SetFileManagerForTesting(file_manager_);
366 } 352 }
367 return file_manager_; 353 return file_manager_;
368 } 354 }
369 355
370 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 356 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
371 }; 357 };
372 358
373 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; 359 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad";
374 const size_t DownloadManagerTest::kTestDataLen = 360 const size_t DownloadManagerTest::kTestDataLen =
375 strlen(DownloadManagerTest::kTestData); 361 strlen(DownloadManagerTest::kTestData);
376 362
377 // A DownloadFile that we can inject errors into. 363 // A DownloadFile that we can inject errors into.
378 class DownloadFileWithErrors : public DownloadFileImpl { 364 class DownloadFileWithErrors : public DownloadFileImpl {
379 public: 365 public:
380 DownloadFileWithErrors(DownloadCreateInfo* info, 366 DownloadFileWithErrors(DownloadCreateInfo* info,
367 scoped_ptr<content::ByteStreamReader> stream,
381 DownloadManager* manager, 368 DownloadManager* manager,
382 bool calculate_hash); 369 bool calculate_hash);
383 virtual ~DownloadFileWithErrors() {} 370 virtual ~DownloadFileWithErrors() {}
384 371
385 // BaseFile delegated functions. 372 // BaseFile delegated functions.
386 virtual net::Error Initialize() OVERRIDE; 373 virtual net::Error Initialize() OVERRIDE;
387 virtual net::Error AppendDataToFile(const char* data, 374 virtual net::Error AppendDataToFile(const char* data,
388 size_t data_len) OVERRIDE; 375 size_t data_len) OVERRIDE;
389 virtual net::Error Rename(const FilePath& full_path) OVERRIDE; 376 virtual net::Error Rename(const FilePath& full_path) OVERRIDE;
390 377
391 void set_forced_error(net::Error error) { forced_error_ = error; } 378 void set_forced_error(net::Error error) { forced_error_ = error; }
392 void clear_forced_error() { forced_error_ = net::OK; } 379 void clear_forced_error() { forced_error_ = net::OK; }
393 net::Error forced_error() const { return forced_error_; } 380 net::Error forced_error() const { return forced_error_; }
394 381
395 private: 382 private:
396 net::Error ReturnError(net::Error function_error) { 383 net::Error ReturnError(net::Error function_error) {
397 if (forced_error_ != net::OK) { 384 if (forced_error_ != net::OK) {
398 net::Error ret = forced_error_; 385 net::Error ret = forced_error_;
399 clear_forced_error(); 386 clear_forced_error();
400 return ret; 387 return ret;
401 } 388 }
402 389
403 return function_error; 390 return function_error;
404 } 391 }
405 392
406 net::Error forced_error_; 393 net::Error forced_error_;
407 }; 394 };
408 395
409 DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info, 396 DownloadFileWithErrors::DownloadFileWithErrors(
410 DownloadManager* manager, 397 DownloadCreateInfo* info,
411 bool calculate_hash) 398 scoped_ptr<content::ByteStreamReader> stream,
399 DownloadManager* manager,
400 bool calculate_hash)
412 : DownloadFileImpl(info, 401 : DownloadFileImpl(info,
402 stream.Pass(),
413 new DownloadRequestHandle(), 403 new DownloadRequestHandle(),
414 manager, 404 manager,
415 calculate_hash, 405 calculate_hash,
416 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 406 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
417 net::BoundNetLog()), 407 net::BoundNetLog()),
418 forced_error_(net::OK) { 408 forced_error_(net::OK) {
419 } 409 }
420 410
421 net::Error DownloadFileWithErrors::Initialize() { 411 net::Error DownloadFileWithErrors::Initialize() {
422 return ReturnError(DownloadFileImpl::Initialize()); 412 return ReturnError(DownloadFileImpl::Initialize());
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 // responsible for deleting it. In these unit tests, however, we 558 // responsible for deleting it. In these unit tests, however, we
569 // don't call the function that deletes it, so we do so ourselves. 559 // don't call the function that deletes it, so we do so ourselves.
570 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 560 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
571 const DownloadId id = DownloadId(kValidIdDomain, static_cast<int>(i)); 561 const DownloadId id = DownloadId(kValidIdDomain, static_cast<int>(i));
572 info->download_id = id; 562 info->download_id = id;
573 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; 563 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
574 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); 564 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
575 info->mime_type = kStartDownloadCases[i].mime_type; 565 info->mime_type = kStartDownloadCases[i].mime_type;
576 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 566 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
577 567
568 scoped_ptr<content::ByteStreamWriter> stream_writer;
569 scoped_ptr<content::ByteStreamReader> stream_reader;
570 content::CreateByteStream(message_loop_.message_loop_proxy(),
571 message_loop_.message_loop_proxy(),
572 kTestDataLen, &stream_writer, &stream_reader);
573
578 DownloadFile* download_file( 574 DownloadFile* download_file(
579 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), 575 new DownloadFileImpl(info.get(),
576 stream_reader.Pass(),
577 new DownloadRequestHandle(),
580 download_manager_, false, 578 download_manager_, false,
581 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 579 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
582 net::BoundNetLog())); 580 net::BoundNetLog()));
583 AddDownloadToFileManager(info->download_id.local(), download_file); 581 AddDownloadToFileManager(info->download_id.local(), download_file);
584 download_file->Initialize(); 582 download_file->Initialize();
585 download_manager_->StartDownload(info->download_id.local()); 583 download_manager_->StartDownload(info->download_id.local());
586 message_loop_.RunAllPending(); 584 message_loop_.RunAllPending();
587 585
588 // SelectFileObserver will have recorded any attempt to open the 586 // SelectFileObserver will have recorded any attempt to open the
589 // select file dialog. 587 // select file dialog.
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 EXPECT_EQ(expected_intermediate_path.value(), 873 EXPECT_EQ(expected_intermediate_path.value(),
876 download->GetFullPath().value()); 874 download->GetFullPath().value());
877 download_manager_delegate_->SetShouldCompleteDownload(true); 875 download_manager_delegate_->SetShouldCompleteDownload(true);
878 download_manager_delegate_->InvokeDownloadCompletionCallback(); 876 download_manager_delegate_->InvokeDownloadCompletionCallback();
879 message_loop_.RunAllPending(); 877 message_loop_.RunAllPending();
880 EXPECT_EQ(expected_final_path.value(), 878 EXPECT_EQ(expected_final_path.value(),
881 download->GetTargetFilePath().value()); 879 download->GetTargetFilePath().value());
882 } 880 }
883 } 881 }
884 882
883 void WriteCopyToStream(const char *source,
884 size_t len, content::ByteStreamWriter* stream) {
885 scoped_refptr<net::IOBuffer> copy(new net::IOBuffer(len));
886 memcpy(copy.get()->data(), source, len);
887 stream->Write(copy, len);
888 }
889
885 TEST_F(DownloadManagerTest, DownloadInterruptTest) { 890 TEST_F(DownloadManagerTest, DownloadInterruptTest) {
886 using ::testing::_; 891 using ::testing::_;
887 using ::testing::CreateFunctor; 892 using ::testing::CreateFunctor;
888 using ::testing::Invoke; 893 using ::testing::Invoke;
889 using ::testing::Return; 894 using ::testing::Return;
890 895
891 // Normally, the download system takes ownership of info, and is 896 // Normally, the download system takes ownership of info, and is
892 // responsible for deleting it. In these unit tests, however, we 897 // responsible for deleting it. In these unit tests, however, we
893 // don't call the function that deletes it, so we do so ourselves. 898 // don't call the function that deletes it, so we do so ourselves.
894 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 899 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
895 info->download_id = DownloadId(kValidIdDomain, 0); 900 info->download_id = DownloadId(kValidIdDomain, 0);
896 info->prompt_user_for_save_location = false; 901 info->prompt_user_for_save_location = false;
897 info->url_chain.push_back(GURL()); 902 info->url_chain.push_back(GURL());
898 info->total_bytes = static_cast<int64>(kTestDataLen); 903 info->total_bytes = static_cast<int64>(kTestDataLen);
899 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 904 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
900 const FilePath cr_path(GetTempDownloadPath(new_path)); 905 const FilePath cr_path(GetTempDownloadPath(new_path));
901 906
902 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); 907 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>());
903 ON_CALL(*download_file, AppendDataToFile(_, _))
904 .WillByDefault(Return(net::OK));
905 908
906 // |download_file| is owned by DownloadFileManager. 909 // |download_file| is owned by DownloadFileManager.
907 AddMockDownloadToFileManager(info->download_id.local(), download_file); 910 AddMockDownloadToFileManager(info->download_id.local(), download_file);
908 911
909 EXPECT_CALL(*download_file, Rename(cr_path)) 912 EXPECT_CALL(*download_file, Rename(cr_path))
910 .WillOnce(Return(net::OK)); 913 .WillOnce(Return(net::OK));
911 EXPECT_CALL(*download_file, AppendDataToFile(kTestData, kTestDataLen))
912 .WillOnce(Return(net::OK));
913 EXPECT_CALL(*download_file, Cancel()); 914 EXPECT_CALL(*download_file, Cancel());
914 915
915 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 916 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
916 917
917 DownloadItem* download = GetActiveDownloadItem(0); 918 DownloadItem* download = GetActiveDownloadItem(0);
918 ASSERT_TRUE(download != NULL); 919 ASSERT_TRUE(download != NULL);
919 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 920 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
920 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 921 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
921 922
922 download_file->AppendDataToFile(kTestData, kTestDataLen);
923 ContinueDownloadWithPath(download, new_path); 923 ContinueDownloadWithPath(download, new_path);
924 message_loop_.RunAllPending(); 924 message_loop_.RunAllPending();
925 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 925 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
926 926
927 int64 error_size = 3; 927 int64 error_size = 3;
928 OnDownloadInterrupted(0, error_size, "", 928 OnDownloadInterrupted(0, error_size, "",
929 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 929 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
930 message_loop_.RunAllPending(); 930 message_loop_.RunAllPending();
931 931
932 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 932 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
(...skipping 28 matching lines...) Expand all
961 // Create a temporary file and a mock stream. 961 // Create a temporary file and a mock stream.
962 FilePath path; 962 FilePath path;
963 ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); 963 ASSERT_TRUE(file_util::CreateTemporaryFile(&path));
964 964
965 // This file stream will be used, until the first rename occurs. 965 // This file stream will be used, until the first rename occurs.
966 net::FileStream* stream = new net::FileStream(NULL); 966 net::FileStream* stream = new net::FileStream(NULL);
967 ASSERT_EQ(0, stream->OpenSync( 967 ASSERT_EQ(0, stream->OpenSync(
968 path, 968 path,
969 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); 969 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE));
970 970
971 // Make sure the DFM exists; we'll need it later.
972 // TODO(rdsmith): This is ugly and should be rewritten, but a large
973 // rewrite of this test is in progress in a different CL, so doing it
974 // the hacky way for now.
975 (void) file_manager();
976
971 // Normally, the download system takes ownership of info, and is 977 // Normally, the download system takes ownership of info, and is
972 // responsible for deleting it. In these unit tests, however, we 978 // responsible for deleting it. In these unit tests, however, we
973 // don't call the function that deletes it, so we do so ourselves. 979 // don't call the function that deletes it, so we do so ourselves.
974 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 980 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
975 static const int32 local_id = 0; 981 static const int32 local_id = 0;
976 info->download_id = DownloadId(kValidIdDomain, local_id); 982 info->download_id = DownloadId(kValidIdDomain, local_id);
977 info->prompt_user_for_save_location = false; 983 info->prompt_user_for_save_location = false;
978 info->url_chain.push_back(GURL()); 984 info->url_chain.push_back(GURL());
979 info->total_bytes = static_cast<int64>(kTestDataLen * 3); 985 info->total_bytes = static_cast<int64>(kTestDataLen * 3);
980 info->save_info.file_path = path; 986 info->save_info.file_path = path;
981 info->save_info.file_stream.reset(stream); 987 info->save_info.file_stream.reset(stream);
988 scoped_ptr<content::ByteStreamWriter> stream_input;
989 scoped_ptr<content::ByteStreamReader> stream_output;
990 content::CreateByteStream(message_loop_.message_loop_proxy(),
991 message_loop_.message_loop_proxy(),
992 kTestDataLen, &stream_input, &stream_output);
982 993
983 // Create a download file that we can insert errors into. 994 // Create a download file that we can insert errors into.
984 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( 995 scoped_ptr<DownloadFileWithErrors> download_file(new DownloadFileWithErrors(
985 info.get(), download_manager_, false)); 996 info.get(), stream_output.Pass(), download_manager_, false));
986 download_file->Initialize(); 997 download_file->Initialize();
987 AddDownloadToFileManager(local_id, download_file);
988 998
989 // |download_file| is owned by DownloadFileManager.
990 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 999 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
991 1000
992 DownloadItem* download = GetActiveDownloadItem(0); 1001 DownloadItem* download = GetActiveDownloadItem(0);
993 ASSERT_TRUE(download != NULL); 1002 ASSERT_TRUE(download != NULL);
994 1003
995 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1004 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
996 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1005 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
997 1006
998 // Add some data before finalizing the file name. 1007 // Add some data before finalizing the file name.
999 UpdateData(local_id, kTestData, kTestDataLen); 1008 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1000 1009
1001 // Finalize the file name. 1010 // Finalize the file name.
1002 ContinueDownloadWithPath(download, path); 1011 ContinueDownloadWithPath(download, path);
1003 message_loop_.RunAllPending(); 1012 message_loop_.RunAllPending();
1004 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1013 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1005 1014
1006 // Add more data. 1015 // Add more data.
1007 UpdateData(local_id, kTestData, kTestDataLen); 1016 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1008 1017
1009 // Add more data, but an error occurs. 1018 // Add more data, but an error occurs.
1010 download_file->set_forced_error(net::ERR_FAILED); 1019 download_file->set_forced_error(net::ERR_FAILED);
1011 UpdateData(local_id, kTestData, kTestDataLen); 1020 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1021 message_loop_.RunAllPending();
1012 1022
1013 // Check the state. The download should have been interrupted. 1023 // Check the state. The download should have been interrupted.
1014 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1024 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1015 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1025 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1016 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); 1026 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED));
1017 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); 1027 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE));
1018 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1028 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1019 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1029 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1020 EXPECT_TRUE(observer->was_updated()); 1030 EXPECT_TRUE(observer->was_updated());
1021 EXPECT_FALSE(observer->was_opened()); 1031 EXPECT_FALSE(observer->was_opened());
(...skipping 16 matching lines...) Expand all
1038 // don't call the function that deletes it, so we do so ourselves. 1048 // don't call the function that deletes it, so we do so ourselves.
1039 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1049 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1040 DownloadId id = DownloadId(kValidIdDomain, 0); 1050 DownloadId id = DownloadId(kValidIdDomain, 0);
1041 info->download_id = id; 1051 info->download_id = id;
1042 info->prompt_user_for_save_location = false; 1052 info->prompt_user_for_save_location = false;
1043 info->url_chain.push_back(GURL()); 1053 info->url_chain.push_back(GURL());
1044 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 1054 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
1045 const FilePath cr_path(GetTempDownloadPath(new_path)); 1055 const FilePath cr_path(GetTempDownloadPath(new_path));
1046 1056
1047 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); 1057 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>());
1048 ON_CALL(*download_file, AppendDataToFile(_, _))
1049 .WillByDefault(Return(net::OK));
1050 AddMockDownloadToFileManager(info->download_id.local(), download_file); 1058 AddMockDownloadToFileManager(info->download_id.local(), download_file);
1051 1059
1052 // |download_file| is owned by DownloadFileManager. 1060 // |download_file| is owned by DownloadFileManager.
1053 EXPECT_CALL(*download_file, Rename(cr_path)) 1061 EXPECT_CALL(*download_file, Rename(cr_path))
1054 .WillOnce(Return(net::OK)); 1062 .WillOnce(Return(net::OK));
1055 EXPECT_CALL(*download_file, AppendDataToFile(kTestData, kTestDataLen))
1056 .WillOnce(Return(net::OK));
1057 EXPECT_CALL(*download_file, Cancel()); 1063 EXPECT_CALL(*download_file, Cancel());
1058 1064
1059 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1065 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1060 1066
1061 DownloadItem* download = GetActiveDownloadItem(0); 1067 DownloadItem* download = GetActiveDownloadItem(0);
1062 ASSERT_TRUE(download != NULL); 1068 ASSERT_TRUE(download != NULL);
1063 1069
1064 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1070 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1065 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1071 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1066 1072
1067 ContinueDownloadWithPath(download, new_path); 1073 ContinueDownloadWithPath(download, new_path);
1068 message_loop_.RunAllPending(); 1074 message_loop_.RunAllPending();
1069 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1075 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1070 1076
1071 download_file->AppendDataToFile(kTestData, kTestDataLen);
1072
1073 download->Cancel(false); 1077 download->Cancel(false);
1074 message_loop_.RunAllPending(); 1078 message_loop_.RunAllPending();
1075 1079
1076 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1080 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1077 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1081 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1078 EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED)); 1082 EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED));
1079 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1083 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
1080 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); 1084 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE));
1081 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1085 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1082 EXPECT_TRUE(observer->was_updated()); 1086 EXPECT_TRUE(observer->was_updated());
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1117 unique_new_path = unique_new_path.InsertBeforeExtensionASCII( 1121 unique_new_path = unique_new_path.InsertBeforeExtensionASCII(
1118 StringPrintf(" (%d)", uniquifier)); 1122 StringPrintf(" (%d)", uniquifier));
1119 1123
1120 // Normally, the download system takes ownership of info, and is 1124 // Normally, the download system takes ownership of info, and is
1121 // responsible for deleting it. In these unit tests, however, we 1125 // responsible for deleting it. In these unit tests, however, we
1122 // don't call the function that deletes it, so we do so ourselves. 1126 // don't call the function that deletes it, so we do so ourselves.
1123 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1127 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1124 info->download_id = DownloadId(kValidIdDomain, 0); 1128 info->download_id = DownloadId(kValidIdDomain, 0);
1125 info->prompt_user_for_save_location = true; 1129 info->prompt_user_for_save_location = true;
1126 info->url_chain.push_back(GURL()); 1130 info->url_chain.push_back(GURL());
1131 scoped_ptr<content::ByteStreamWriter> stream_input;
1132 scoped_ptr<content::ByteStreamReader> stream_output;
1133 content::CreateByteStream(message_loop_.message_loop_proxy(),
1134 message_loop_.message_loop_proxy(),
1135 kTestDataLen, &stream_input, &stream_output);
1127 1136
1128 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1137 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1129 1138
1130 DownloadItem* download = GetActiveDownloadItem(0); 1139 DownloadItem* download = GetActiveDownloadItem(0);
1131 ASSERT_TRUE(download != NULL); 1140 ASSERT_TRUE(download != NULL);
1132 1141
1133 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1142 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1134 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1143 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1135 1144
1136 // Create and initialize the download file. We're bypassing the first part 1145 // Create and initialize the download file. We're bypassing the first part
1137 // of the download process and skipping to the part after the final file 1146 // of the download process and skipping to the part after the final file
1138 // name has been chosen, so we need to initialize the download file 1147 // name has been chosen, so we need to initialize the download file
1139 // properly. 1148 // properly.
1140 DownloadFile* download_file( 1149 DownloadFile* download_file(
1141 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), 1150 new DownloadFileImpl(info.get(), stream_output.Pass(),
1151 new DownloadRequestHandle(),
1142 download_manager_, false, 1152 download_manager_, false,
1143 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 1153 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
1144 net::BoundNetLog())); 1154 net::BoundNetLog()));
1145 download_file->Rename(cr_path); 1155 download_file->Rename(cr_path);
1146 // This creates the .temp version of the file. 1156 // This creates the .temp version of the file.
1147 download_file->Initialize(); 1157 download_file->Initialize();
1148 // |download_file| is owned by DownloadFileManager. 1158 // |download_file| is owned by DownloadFileManager.
1149 AddDownloadToFileManager(info->download_id.local(), download_file); 1159 AddDownloadToFileManager(info->download_id.local(), download_file);
1150 1160
1151 ContinueDownloadWithPath(download, new_path); 1161 ContinueDownloadWithPath(download, new_path);
1152 message_loop_.RunAllPending(); 1162 message_loop_.RunAllPending();
1153 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1163 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1154 1164
1155 download_file->AppendDataToFile(kTestData, kTestDataLen); 1165 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1156 1166
1157 // Finish the download. 1167 // Finish the download.
1158 OnResponseCompleted(0, kTestDataLen, ""); 1168 OnResponseCompleted(0, kTestDataLen, "");
1159 message_loop_.RunAllPending(); 1169 message_loop_.RunAllPending();
1160 1170
1161 // Download is complete. 1171 // Download is complete.
1162 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1172 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1163 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1173 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1164 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1174 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1165 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1175 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
(...skipping 25 matching lines...) Expand all
1191 const FilePath cr_path(GetTempDownloadPath(new_path)); 1201 const FilePath cr_path(GetTempDownloadPath(new_path));
1192 EXPECT_FALSE(file_util::PathExists(new_path)); 1202 EXPECT_FALSE(file_util::PathExists(new_path));
1193 1203
1194 // Normally, the download system takes ownership of info, and is 1204 // Normally, the download system takes ownership of info, and is
1195 // responsible for deleting it. In these unit tests, however, we 1205 // responsible for deleting it. In these unit tests, however, we
1196 // don't call the function that deletes it, so we do so ourselves. 1206 // don't call the function that deletes it, so we do so ourselves.
1197 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1207 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1198 info->download_id = DownloadId(kValidIdDomain, 0); 1208 info->download_id = DownloadId(kValidIdDomain, 0);
1199 info->prompt_user_for_save_location = true; 1209 info->prompt_user_for_save_location = true;
1200 info->url_chain.push_back(GURL()); 1210 info->url_chain.push_back(GURL());
1211 scoped_ptr<content::ByteStreamWriter> stream_input;
1212 scoped_ptr<content::ByteStreamReader> stream_output;
1213 content::CreateByteStream(message_loop_.message_loop_proxy(),
1214 message_loop_.message_loop_proxy(),
1215 kTestDataLen, &stream_input, &stream_output);
1201 1216
1202 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1217 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1203 1218
1204 DownloadItem* download = GetActiveDownloadItem(0); 1219 DownloadItem* download = GetActiveDownloadItem(0);
1205 ASSERT_TRUE(download != NULL); 1220 ASSERT_TRUE(download != NULL);
1206 1221
1207 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1222 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1208 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1223 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1209 1224
1210 // Create and initialize the download file. We're bypassing the first part 1225 // Create and initialize the download file. We're bypassing the first part
1211 // of the download process and skipping to the part after the final file 1226 // of the download process and skipping to the part after the final file
1212 // name has been chosen, so we need to initialize the download file 1227 // name has been chosen, so we need to initialize the download file
1213 // properly. 1228 // properly.
1214 DownloadFile* download_file( 1229 DownloadFile* download_file(
1215 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), 1230 new DownloadFileImpl(info.get(), stream_output.Pass(),
1231 new DownloadRequestHandle(),
1216 download_manager_, false, 1232 download_manager_, false,
1217 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 1233 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
1218 net::BoundNetLog())); 1234 net::BoundNetLog()));
1219 download_file->Rename(cr_path); 1235 download_file->Rename(cr_path);
1220 // This creates the .temp version of the file. 1236 // This creates the .temp version of the file.
1221 download_file->Initialize(); 1237 download_file->Initialize();
1222 // |download_file| is owned by DownloadFileManager. 1238 // |download_file| is owned by DownloadFileManager.
1223 AddDownloadToFileManager(info->download_id.local(), download_file); 1239 AddDownloadToFileManager(info->download_id.local(), download_file);
1224 1240
1225 ContinueDownloadWithPath(download, new_path); 1241 ContinueDownloadWithPath(download, new_path);
1226 message_loop_.RunAllPending(); 1242 message_loop_.RunAllPending();
1227 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1243 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1228 1244
1229 download_file->AppendDataToFile(kTestData, kTestDataLen); 1245 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1230 1246
1231 // Finish the download. 1247 // Finish the download.
1232 OnResponseCompleted(0, kTestDataLen, ""); 1248 OnResponseCompleted(0, kTestDataLen, "");
1233 message_loop_.RunAllPending(); 1249 message_loop_.RunAllPending();
1234 1250
1235 // Download is complete. 1251 // Download is complete.
1236 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1252 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1237 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1253 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1238 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1254 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1239 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1255 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
(...skipping 18 matching lines...) Expand all
1258 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1274 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
1259 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); 1275 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE));
1260 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1276 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1261 EXPECT_TRUE(observer->was_updated()); 1277 EXPECT_TRUE(observer->was_updated());
1262 EXPECT_FALSE(observer->was_opened()); 1278 EXPECT_FALSE(observer->was_opened());
1263 EXPECT_TRUE(download->GetFileExternallyRemoved()); 1279 EXPECT_TRUE(download->GetFileExternallyRemoved());
1264 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); 1280 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState());
1265 1281
1266 EXPECT_FALSE(file_util::PathExists(new_path)); 1282 EXPECT_FALSE(file_util::PathExists(new_path));
1267 } 1283 }
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl_unittest.cc ('k') | content/browser/download/download_net_log_parameters.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698