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

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: Incorporated comments. 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 public: 224 public:
220 static const char* kTestData; 225 static const char* kTestData;
221 static const size_t kTestDataLen; 226 static const size_t kTestDataLen;
222 227
223 DownloadManagerTest() 228 DownloadManagerTest()
224 : browser_context(new TestBrowserContext()), 229 : browser_context(new TestBrowserContext()),
225 download_manager_delegate_(new TestDownloadManagerDelegate()), 230 download_manager_delegate_(new TestDownloadManagerDelegate()),
226 download_manager_(new DownloadManagerImpl( 231 download_manager_(new DownloadManagerImpl(
227 download_manager_delegate_.get(), NULL)), 232 download_manager_delegate_.get(), NULL)),
228 ui_thread_(BrowserThread::UI, &message_loop_), 233 ui_thread_(BrowserThread::UI, &message_loop_),
229 file_thread_(BrowserThread::FILE, &message_loop_), 234 file_thread_(BrowserThread::FILE, &message_loop_) {
230 download_buffer_(new content::DownloadBuffer) {
231 download_manager_->Init(browser_context.get()); 235 download_manager_->Init(browser_context.get());
232 download_manager_delegate_->set_download_manager(download_manager_); 236 download_manager_delegate_->set_download_manager(download_manager_);
233 } 237 }
234 238
235 ~DownloadManagerTest() { 239 ~DownloadManagerTest() {
236 download_manager_->Shutdown(); 240 download_manager_->Shutdown();
237 // browser_context must outlive download_manager_, so we explicitly delete 241 // browser_context must outlive download_manager_, so we explicitly delete
238 // download_manager_ first. 242 // download_manager_ first.
239 download_manager_ = NULL; 243 download_manager_ = NULL;
240 download_manager_delegate_.reset(); 244 download_manager_delegate_.reset();
(...skipping 18 matching lines...) Expand all
259 } 263 }
260 264
261 void FileSelected(const FilePath& path, int32 download_id) { 265 void FileSelected(const FilePath& path, int32 download_id) {
262 download_manager_->FileSelected(path, download_id); 266 download_manager_->FileSelected(path, download_id);
263 } 267 }
264 268
265 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { 269 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) {
266 download_manager_->ContinueDownloadWithPath(download, path); 270 download_manager_->ContinueDownloadWithPath(download, path);
267 } 271 }
268 272
269 void UpdateData(int32 id, const char* data, size_t length) {
270 // We are passing ownership of this buffer to the download file manager.
271 net::IOBuffer* io_buffer = new net::IOBuffer(length);
272 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|.
273 io_buffer->AddRef();
274 memcpy(io_buffer->data(), data, length);
275
276 download_buffer_->AddData(io_buffer, length);
277
278 BrowserThread::PostTask(
279 BrowserThread::FILE, FROM_HERE,
280 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(),
281 DownloadId(kValidIdDomain, id), download_buffer_));
282
283 message_loop_.RunAllPending();
284 }
285
286 void OnDownloadInterrupted(int32 download_id, int64 size, 273 void OnDownloadInterrupted(int32 download_id, int64 size,
287 const std::string& hash_state, 274 const std::string& hash_state,
288 content::DownloadInterruptReason reason) { 275 content::DownloadInterruptReason reason) {
289 download_manager_->OnDownloadInterrupted(download_id, size, 276 download_manager_->OnDownloadInterrupted(download_id, size,
290 hash_state, reason); 277 hash_state, reason);
291 } 278 }
292 279
293 // Get the download item with ID |id|. 280 // Get the download item with ID |id|.
294 DownloadItem* GetActiveDownloadItem(int32 id) { 281 DownloadItem* GetActiveDownloadItem(int32 id) {
295 return download_manager_->GetActiveDownload(id); 282 return download_manager_->GetActiveDownload(id);
296 } 283 }
297 284
298 protected: 285 protected:
299 scoped_ptr<TestBrowserContext> browser_context; 286 scoped_ptr<TestBrowserContext> browser_context;
300 scoped_ptr<TestDownloadManagerDelegate> download_manager_delegate_; 287 scoped_ptr<TestDownloadManagerDelegate> download_manager_delegate_;
301 scoped_refptr<DownloadManagerImpl> download_manager_; 288 scoped_refptr<DownloadManagerImpl> download_manager_;
302 scoped_refptr<DownloadFileManager> file_manager_; 289 scoped_refptr<DownloadFileManager> file_manager_;
303 MessageLoopForUI message_loop_; 290 MessageLoopForUI message_loop_;
304 content::TestBrowserThread ui_thread_; 291 content::TestBrowserThread ui_thread_;
305 content::TestBrowserThread file_thread_; 292 content::TestBrowserThread file_thread_;
306 scoped_refptr<content::DownloadBuffer> download_buffer_;
307 293
308 DownloadFileManager* file_manager() { 294 DownloadFileManager* file_manager() {
309 if (!file_manager_) { 295 if (!file_manager_) {
310 file_manager_ = new DownloadFileManager(new MockDownloadFileFactory); 296 file_manager_ = new DownloadFileManager(new MockDownloadFileFactory);
311 download_manager_->SetFileManagerForTesting(file_manager_); 297 download_manager_->SetFileManagerForTesting(file_manager_);
312 } 298 }
313 return file_manager_; 299 return file_manager_;
314 } 300 }
315 301
316 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); 302 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
317 }; 303 };
318 304
319 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; 305 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad";
320 const size_t DownloadManagerTest::kTestDataLen = 306 const size_t DownloadManagerTest::kTestDataLen =
321 strlen(DownloadManagerTest::kTestData); 307 strlen(DownloadManagerTest::kTestData);
322 308
323 // A DownloadFile that we can inject errors into. 309 // A DownloadFile that we can inject errors into.
324 class DownloadFileWithErrors : public DownloadFileImpl { 310 class DownloadFileWithErrors : public DownloadFileImpl {
325 public: 311 public:
326 DownloadFileWithErrors(DownloadCreateInfo* info, 312 DownloadFileWithErrors(DownloadCreateInfo* info,
313 scoped_ptr<content::ByteStreamReader> stream,
327 DownloadManager* manager, 314 DownloadManager* manager,
328 bool calculate_hash); 315 bool calculate_hash);
329 virtual ~DownloadFileWithErrors() {} 316 virtual ~DownloadFileWithErrors() {}
330 317
331 // BaseFile delegated functions. 318 // BaseFile delegated functions.
332 virtual net::Error Initialize(); 319 virtual net::Error Initialize();
333 virtual net::Error AppendDataToFile(const char* data, size_t data_len); 320 virtual net::Error AppendDataToFile(const char* data, size_t data_len);
334 virtual net::Error Rename(const FilePath& full_path); 321 virtual net::Error Rename(const FilePath& full_path);
335 322
336 void set_forced_error(net::Error error) { forced_error_ = error; } 323 void set_forced_error(net::Error error) { forced_error_ = error; }
337 void clear_forced_error() { forced_error_ = net::OK; } 324 void clear_forced_error() { forced_error_ = net::OK; }
338 net::Error forced_error() const { return forced_error_; } 325 net::Error forced_error() const { return forced_error_; }
339 326
340 private: 327 private:
341 net::Error ReturnError(net::Error function_error) { 328 net::Error ReturnError(net::Error function_error) {
342 if (forced_error_ != net::OK) { 329 if (forced_error_ != net::OK) {
343 net::Error ret = forced_error_; 330 net::Error ret = forced_error_;
344 clear_forced_error(); 331 clear_forced_error();
345 return ret; 332 return ret;
346 } 333 }
347 334
348 return function_error; 335 return function_error;
349 } 336 }
350 337
351 net::Error forced_error_; 338 net::Error forced_error_;
352 }; 339 };
353 340
354 DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info, 341 DownloadFileWithErrors::DownloadFileWithErrors(
355 DownloadManager* manager, 342 DownloadCreateInfo* info,
356 bool calculate_hash) 343 scoped_ptr<content::ByteStreamReader> stream,
344 DownloadManager* manager,
345 bool calculate_hash)
357 : DownloadFileImpl(info, 346 : DownloadFileImpl(info,
347 stream.Pass(),
358 new DownloadRequestHandle(), 348 new DownloadRequestHandle(),
359 manager, 349 manager,
360 calculate_hash, 350 calculate_hash,
361 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 351 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
362 net::BoundNetLog()), 352 net::BoundNetLog()),
363 forced_error_(net::OK) { 353 forced_error_(net::OK) {
364 } 354 }
365 355
366 net::Error DownloadFileWithErrors::Initialize() { 356 net::Error DownloadFileWithErrors::Initialize() {
367 return ReturnError(DownloadFileImpl::Initialize()); 357 return ReturnError(DownloadFileImpl::Initialize());
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 // don't call the function that deletes it, so we do so ourselves. 531 // don't call the function that deletes it, so we do so ourselves.
542 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 532 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
543 const DownloadId id = DownloadId(kValidIdDomain, static_cast<int>(i)); 533 const DownloadId id = DownloadId(kValidIdDomain, static_cast<int>(i));
544 info->download_id = id; 534 info->download_id = id;
545 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; 535 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
546 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); 536 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
547 info->mime_type = kStartDownloadCases[i].mime_type; 537 info->mime_type = kStartDownloadCases[i].mime_type;
548 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 538 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
549 539
550 DownloadFile* download_file( 540 DownloadFile* download_file(
551 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), 541 new DownloadFileImpl(info.get(),
542 scoped_ptr<content::ByteStreamReader>(),
543 new DownloadRequestHandle(),
552 download_manager_, false, 544 download_manager_, false,
553 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 545 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
554 net::BoundNetLog())); 546 net::BoundNetLog()));
555 AddDownloadToFileManager(info->download_id.local(), download_file); 547 AddDownloadToFileManager(info->download_id.local(), download_file);
556 download_file->Initialize(); 548 download_file->Initialize();
557 download_manager_->StartDownload(info->download_id.local()); 549 download_manager_->StartDownload(info->download_id.local());
558 message_loop_.RunAllPending(); 550 message_loop_.RunAllPending();
559 551
560 // SelectFileObserver will have recorded any attempt to open the 552 // SelectFileObserver will have recorded any attempt to open the
561 // select file dialog. 553 // select file dialog.
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 message_loop_.RunAllPending(); 924 message_loop_.RunAllPending();
933 OnResponseCompleted(i, 1024, std::string("fake_hash")); 925 OnResponseCompleted(i, 1024, std::string("fake_hash"));
934 } 926 }
935 // Validating the download item, so it will complete. 927 // Validating the download item, so it will complete.
936 if (state.danger == content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE) 928 if (state.danger == content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)
937 download->DangerousDownloadValidated(); 929 download->DangerousDownloadValidated();
938 message_loop_.RunAllPending(); 930 message_loop_.RunAllPending();
939 } 931 }
940 } 932 }
941 933
934 void WriteCopyToStream(const char *source,
935 size_t len, content::ByteStreamWriter* stream) {
936 scoped_refptr<net::IOBuffer> copy(new net::IOBuffer(len));
937 memcpy(copy.get()->data(), source, len);
938 stream->Write(copy, len);
939 }
940
942 TEST_F(DownloadManagerTest, DownloadInterruptTest) { 941 TEST_F(DownloadManagerTest, DownloadInterruptTest) {
943 using ::testing::_; 942 using ::testing::_;
944 using ::testing::CreateFunctor; 943 using ::testing::CreateFunctor;
945 using ::testing::Invoke; 944 using ::testing::Invoke;
946 using ::testing::Return; 945 using ::testing::Return;
947 946
948 // Normally, the download system takes ownership of info, and is 947 // Normally, the download system takes ownership of info, and is
949 // responsible for deleting it. In these unit tests, however, we 948 // responsible for deleting it. In these unit tests, however, we
950 // don't call the function that deletes it, so we do so ourselves. 949 // don't call the function that deletes it, so we do so ourselves.
951 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 950 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
952 info->download_id = DownloadId(kValidIdDomain, 0); 951 info->download_id = DownloadId(kValidIdDomain, 0);
953 info->prompt_user_for_save_location = false; 952 info->prompt_user_for_save_location = false;
954 info->url_chain.push_back(GURL()); 953 info->url_chain.push_back(GURL());
955 info->total_bytes = static_cast<int64>(kTestDataLen); 954 info->total_bytes = static_cast<int64>(kTestDataLen);
956 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 955 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
957 const FilePath cr_path(GetTempDownloadPath(new_path)); 956 const FilePath cr_path(GetTempDownloadPath(new_path));
958 957
959 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); 958 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>());
960 ON_CALL(*download_file, AppendDataToFile(_, _))
961 .WillByDefault(Return(net::OK));
962 959
963 // |download_file| is owned by DownloadFileManager. 960 // |download_file| is owned by DownloadFileManager.
964 AddMockDownloadToFileManager(info->download_id.local(), download_file); 961 AddMockDownloadToFileManager(info->download_id.local(), download_file);
965 962
966 EXPECT_CALL(*download_file, Rename(cr_path)) 963 EXPECT_CALL(*download_file, Rename(cr_path))
967 .Times(1) 964 .Times(1)
968 .WillOnce(Return(net::OK)); 965 .WillOnce(Return(net::OK));
969 966
970 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 967 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
971 968
972 DownloadItem* download = GetActiveDownloadItem(0); 969 DownloadItem* download = GetActiveDownloadItem(0);
973 ASSERT_TRUE(download != NULL); 970 ASSERT_TRUE(download != NULL);
974 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 971 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
975 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 972 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
976 973
977 download_file->AppendDataToFile(kTestData, kTestDataLen);
978
979 ContinueDownloadWithPath(download, new_path); 974 ContinueDownloadWithPath(download, new_path);
980 message_loop_.RunAllPending(); 975 message_loop_.RunAllPending();
981 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 976 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
982 977
983 int64 error_size = 3; 978 int64 error_size = 3;
984 OnDownloadInterrupted(0, error_size, "", 979 OnDownloadInterrupted(0, error_size, "",
985 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 980 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
986 message_loop_.RunAllPending(); 981 message_loop_.RunAllPending();
987 982
988 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 983 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1028 // responsible for deleting it. In these unit tests, however, we 1023 // responsible for deleting it. In these unit tests, however, we
1029 // don't call the function that deletes it, so we do so ourselves. 1024 // don't call the function that deletes it, so we do so ourselves.
1030 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1025 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1031 static const int32 local_id = 0; 1026 static const int32 local_id = 0;
1032 info->download_id = DownloadId(kValidIdDomain, local_id); 1027 info->download_id = DownloadId(kValidIdDomain, local_id);
1033 info->prompt_user_for_save_location = false; 1028 info->prompt_user_for_save_location = false;
1034 info->url_chain.push_back(GURL()); 1029 info->url_chain.push_back(GURL());
1035 info->total_bytes = static_cast<int64>(kTestDataLen * 3); 1030 info->total_bytes = static_cast<int64>(kTestDataLen * 3);
1036 info->save_info.file_path = path; 1031 info->save_info.file_path = path;
1037 info->save_info.file_stream.reset(stream); 1032 info->save_info.file_stream.reset(stream);
1033 scoped_ptr<content::ByteStreamWriter> stream_input;
1034 scoped_ptr<content::ByteStreamReader> stream_output;
1035 content::CreateByteStream(message_loop_.message_loop_proxy(),
1036 message_loop_.message_loop_proxy(),
1037 kTestDataLen, &stream_input, &stream_output);
1038 1038
1039 // Create a download file that we can insert errors into. 1039 // Create a download file that we can insert errors into.
1040 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( 1040 scoped_ptr<DownloadFileWithErrors> download_file(new DownloadFileWithErrors(
1041 info.get(), download_manager_, false)); 1041 info.get(), stream_output.Pass(), download_manager_, false));
1042 download_file->Initialize(); 1042 download_file->Initialize();
1043 AddDownloadToFileManager(local_id, download_file);
1044 1043
1045 // |download_file| is owned by DownloadFileManager.
1046 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1044 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1047 1045
1048 DownloadItem* download = GetActiveDownloadItem(0); 1046 DownloadItem* download = GetActiveDownloadItem(0);
1049 ASSERT_TRUE(download != NULL); 1047 ASSERT_TRUE(download != NULL);
1050 1048
1051 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1049 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1052 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1050 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1053 1051
1054 // Add some data before finalizing the file name. 1052 // Add some data before finalizing the file name.
1055 UpdateData(local_id, kTestData, kTestDataLen); 1053 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1056 1054
1057 // Finalize the file name. 1055 // Finalize the file name.
1058 ContinueDownloadWithPath(download, path); 1056 ContinueDownloadWithPath(download, path);
1059 message_loop_.RunAllPending(); 1057 message_loop_.RunAllPending();
1060 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1058 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1061 1059
1062 // Add more data. 1060 // Add more data.
1063 UpdateData(local_id, kTestData, kTestDataLen); 1061 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1064 1062
1065 // Add more data, but an error occurs. 1063 // Add more data, but an error occurs.
1066 download_file->set_forced_error(net::ERR_FAILED); 1064 download_file->set_forced_error(net::ERR_FAILED);
1067 UpdateData(local_id, kTestData, kTestDataLen); 1065 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1068 1066
1069 // Check the state. The download should have been interrupted. 1067 // Check the state. The download should have been interrupted.
1070 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1068 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1071 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1069 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1072 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); 1070 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED));
1073 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); 1071 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE));
1074 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1072 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1075 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1073 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1076 EXPECT_TRUE(observer->was_updated()); 1074 EXPECT_TRUE(observer->was_updated());
1077 EXPECT_FALSE(observer->was_opened()); 1075 EXPECT_FALSE(observer->was_opened());
(...skipping 16 matching lines...) Expand all
1094 // don't call the function that deletes it, so we do so ourselves. 1092 // don't call the function that deletes it, so we do so ourselves.
1095 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1093 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1096 DownloadId id = DownloadId(kValidIdDomain, 0); 1094 DownloadId id = DownloadId(kValidIdDomain, 0);
1097 info->download_id = id; 1095 info->download_id = id;
1098 info->prompt_user_for_save_location = false; 1096 info->prompt_user_for_save_location = false;
1099 info->url_chain.push_back(GURL()); 1097 info->url_chain.push_back(GURL());
1100 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 1098 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
1101 const FilePath cr_path(GetTempDownloadPath(new_path)); 1099 const FilePath cr_path(GetTempDownloadPath(new_path));
1102 1100
1103 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); 1101 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>());
1104 ON_CALL(*download_file, AppendDataToFile(_, _))
1105 .WillByDefault(Return(net::OK));
1106 AddMockDownloadToFileManager(info->download_id.local(), download_file); 1102 AddMockDownloadToFileManager(info->download_id.local(), download_file);
1107 1103
1108 // |download_file| is owned by DownloadFileManager. 1104 // |download_file| is owned by DownloadFileManager.
1109 EXPECT_CALL(*download_file, Rename(cr_path)) 1105 EXPECT_CALL(*download_file, Rename(cr_path))
1110 .Times(1) 1106 .Times(1)
1111 .WillOnce(Return(net::OK)); 1107 .WillOnce(Return(net::OK));
1112 1108
1113 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1109 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1114 1110
1115 DownloadItem* download = GetActiveDownloadItem(0); 1111 DownloadItem* download = GetActiveDownloadItem(0);
1116 ASSERT_TRUE(download != NULL); 1112 ASSERT_TRUE(download != NULL);
1117 1113
1118 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1114 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1119 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1115 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1120 1116
1121 ContinueDownloadWithPath(download, new_path); 1117 ContinueDownloadWithPath(download, new_path);
1122 message_loop_.RunAllPending(); 1118 message_loop_.RunAllPending();
1123 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1119 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1124 1120
1125 download_file->AppendDataToFile(kTestData, kTestDataLen);
1126
1127 download->Cancel(false); 1121 download->Cancel(false);
1128 message_loop_.RunAllPending(); 1122 message_loop_.RunAllPending();
1129 1123
1130 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1124 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1131 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1125 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1132 EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED)); 1126 EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED));
1133 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1127 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
1134 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); 1128 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE));
1135 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1129 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1136 EXPECT_TRUE(observer->was_updated()); 1130 EXPECT_TRUE(observer->was_updated());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1174 unique_new_path = unique_new_path.InsertBeforeExtensionASCII( 1168 unique_new_path = unique_new_path.InsertBeforeExtensionASCII(
1175 StringPrintf(" (%d)", uniquifier)); 1169 StringPrintf(" (%d)", uniquifier));
1176 1170
1177 // Normally, the download system takes ownership of info, and is 1171 // Normally, the download system takes ownership of info, and is
1178 // responsible for deleting it. In these unit tests, however, we 1172 // responsible for deleting it. In these unit tests, however, we
1179 // don't call the function that deletes it, so we do so ourselves. 1173 // don't call the function that deletes it, so we do so ourselves.
1180 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1174 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1181 info->download_id = DownloadId(kValidIdDomain, 0); 1175 info->download_id = DownloadId(kValidIdDomain, 0);
1182 info->prompt_user_for_save_location = true; 1176 info->prompt_user_for_save_location = true;
1183 info->url_chain.push_back(GURL()); 1177 info->url_chain.push_back(GURL());
1178 scoped_ptr<content::ByteStreamWriter> stream_input;
1179 scoped_ptr<content::ByteStreamReader> stream_output;
1180 content::CreateByteStream(message_loop_.message_loop_proxy(),
1181 message_loop_.message_loop_proxy(),
1182 kTestDataLen, &stream_input, &stream_output);
1184 1183
1185 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1184 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1186 1185
1187 DownloadItem* download = GetActiveDownloadItem(0); 1186 DownloadItem* download = GetActiveDownloadItem(0);
1188 ASSERT_TRUE(download != NULL); 1187 ASSERT_TRUE(download != NULL);
1189 1188
1190 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1189 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1191 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1190 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1192 1191
1193 // Create and initialize the download file. We're bypassing the first part 1192 // Create and initialize the download file. We're bypassing the first part
1194 // of the download process and skipping to the part after the final file 1193 // of the download process and skipping to the part after the final file
1195 // name has been chosen, so we need to initialize the download file 1194 // name has been chosen, so we need to initialize the download file
1196 // properly. 1195 // properly.
1197 DownloadFile* download_file( 1196 DownloadFile* download_file(
1198 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), 1197 new DownloadFileImpl(info.get(), stream_output.Pass(),
1198 new DownloadRequestHandle(),
1199 download_manager_, false, 1199 download_manager_, false,
1200 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 1200 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
1201 net::BoundNetLog())); 1201 net::BoundNetLog()));
1202 download_file->Rename(cr_path); 1202 download_file->Rename(cr_path);
1203 // This creates the .temp version of the file. 1203 // This creates the .temp version of the file.
1204 download_file->Initialize(); 1204 download_file->Initialize();
1205 // |download_file| is owned by DownloadFileManager. 1205 // |download_file| is owned by DownloadFileManager.
1206 AddDownloadToFileManager(info->download_id.local(), download_file); 1206 AddDownloadToFileManager(info->download_id.local(), download_file);
1207 1207
1208 ContinueDownloadWithPath(download, new_path); 1208 ContinueDownloadWithPath(download, new_path);
1209 message_loop_.RunAllPending(); 1209 message_loop_.RunAllPending();
1210 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1210 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1211 1211
1212 download_file->AppendDataToFile(kTestData, kTestDataLen); 1212 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1213 1213
1214 // Finish the download. 1214 // Finish the download.
1215 OnResponseCompleted(0, kTestDataLen, ""); 1215 OnResponseCompleted(0, kTestDataLen, "");
1216 message_loop_.RunAllPending(); 1216 message_loop_.RunAllPending();
1217 1217
1218 // Download is complete. 1218 // Download is complete.
1219 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1219 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1220 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1220 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1221 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1221 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1222 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1222 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
(...skipping 27 matching lines...) Expand all
1250 const FilePath cr_path(GetTempDownloadPath(new_path)); 1250 const FilePath cr_path(GetTempDownloadPath(new_path));
1251 EXPECT_FALSE(file_util::PathExists(new_path)); 1251 EXPECT_FALSE(file_util::PathExists(new_path));
1252 1252
1253 // Normally, the download system takes ownership of info, and is 1253 // Normally, the download system takes ownership of info, and is
1254 // responsible for deleting it. In these unit tests, however, we 1254 // responsible for deleting it. In these unit tests, however, we
1255 // don't call the function that deletes it, so we do so ourselves. 1255 // don't call the function that deletes it, so we do so ourselves.
1256 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1256 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1257 info->download_id = DownloadId(kValidIdDomain, 0); 1257 info->download_id = DownloadId(kValidIdDomain, 0);
1258 info->prompt_user_for_save_location = true; 1258 info->prompt_user_for_save_location = true;
1259 info->url_chain.push_back(GURL()); 1259 info->url_chain.push_back(GURL());
1260 scoped_ptr<content::ByteStreamWriter> stream_input;
1261 scoped_ptr<content::ByteStreamReader> stream_output;
1262 content::CreateByteStream(message_loop_.message_loop_proxy(),
1263 message_loop_.message_loop_proxy(),
1264 kTestDataLen, &stream_input, &stream_output);
1260 1265
1261 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1266 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1262 1267
1263 DownloadItem* download = GetActiveDownloadItem(0); 1268 DownloadItem* download = GetActiveDownloadItem(0);
1264 ASSERT_TRUE(download != NULL); 1269 ASSERT_TRUE(download != NULL);
1265 1270
1266 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1271 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1267 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1272 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1268 1273
1269 // Create and initialize the download file. We're bypassing the first part 1274 // Create and initialize the download file. We're bypassing the first part
1270 // of the download process and skipping to the part after the final file 1275 // of the download process and skipping to the part after the final file
1271 // name has been chosen, so we need to initialize the download file 1276 // name has been chosen, so we need to initialize the download file
1272 // properly. 1277 // properly.
1273 DownloadFile* download_file( 1278 DownloadFile* download_file(
1274 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), 1279 new DownloadFileImpl(info.get(), stream_output.Pass(),
1280 new DownloadRequestHandle(),
1275 download_manager_, false, 1281 download_manager_, false,
1276 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 1282 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
1277 net::BoundNetLog())); 1283 net::BoundNetLog()));
1278 download_file->Rename(cr_path); 1284 download_file->Rename(cr_path);
1279 // This creates the .temp version of the file. 1285 // This creates the .temp version of the file.
1280 download_file->Initialize(); 1286 download_file->Initialize();
1281 // |download_file| is owned by DownloadFileManager. 1287 // |download_file| is owned by DownloadFileManager.
1282 AddDownloadToFileManager(info->download_id.local(), download_file); 1288 AddDownloadToFileManager(info->download_id.local(), download_file);
1283 1289
1284 ContinueDownloadWithPath(download, new_path); 1290 ContinueDownloadWithPath(download, new_path);
1285 message_loop_.RunAllPending(); 1291 message_loop_.RunAllPending();
1286 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1292 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1287 1293
1288 download_file->AppendDataToFile(kTestData, kTestDataLen); 1294 WriteCopyToStream(kTestData, kTestDataLen, stream_input.get());
1289 1295
1290 // Finish the download. 1296 // Finish the download.
1291 OnResponseCompleted(0, kTestDataLen, ""); 1297 OnResponseCompleted(0, kTestDataLen, "");
1292 message_loop_.RunAllPending(); 1298 message_loop_.RunAllPending();
1293 1299
1294 // Download is complete. 1300 // Download is complete.
1295 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1301 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1296 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1302 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1297 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1303 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1298 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1304 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
(...skipping 18 matching lines...) Expand all
1317 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1323 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
1318 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); 1324 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE));
1319 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1325 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1320 EXPECT_TRUE(observer->was_updated()); 1326 EXPECT_TRUE(observer->was_updated());
1321 EXPECT_FALSE(observer->was_opened()); 1327 EXPECT_FALSE(observer->was_opened());
1322 EXPECT_TRUE(download->GetFileExternallyRemoved()); 1328 EXPECT_TRUE(download->GetFileExternallyRemoved());
1323 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); 1329 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState());
1324 1330
1325 EXPECT_FALSE(file_util::PathExists(new_path)); 1331 EXPECT_FALSE(file_util::PathExists(new_path));
1326 } 1332 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698