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

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 and fixed tests. Created 8 years, 7 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 ByteStreamOutput;
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::ByteStreamOutput> pipe,
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::ByteStreamOutput> pipe,
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::ByteStreamOutput> pipe,
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::ByteStreamOutput> pipe,
344 DownloadManager* manager,
345 bool calculate_hash)
357 : DownloadFileImpl(info, 346 : DownloadFileImpl(info,
347 pipe.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::ByteStreamOutput>(),
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 WriteCopyToPipe(content::ByteStreamInput* pipe,
benjhayden 2012/05/26 20:42:02 inputs before outputs
Randy Smith (Not in Mondays) 2012/05/28 02:51:37 Done, but I don't consider pipe an output in that
935 const char *source,
936 size_t len) {
937 scoped_refptr<net::IOBuffer> copy(new net::IOBuffer(len));
938 memcpy(copy.get()->data(), source, len);
939 pipe->Write(copy, len);
940 }
941
942 TEST_F(DownloadManagerTest, DownloadInterruptTest) { 942 TEST_F(DownloadManagerTest, DownloadInterruptTest) {
943 using ::testing::_; 943 using ::testing::_;
944 using ::testing::CreateFunctor; 944 using ::testing::CreateFunctor;
945 using ::testing::Invoke; 945 using ::testing::Invoke;
946 using ::testing::Return; 946 using ::testing::Return;
947 947
948 // Normally, the download system takes ownership of info, and is 948 // Normally, the download system takes ownership of info, and is
949 // responsible for deleting it. In these unit tests, however, we 949 // responsible for deleting it. In these unit tests, however, we
950 // don't call the function that deletes it, so we do so ourselves. 950 // don't call the function that deletes it, so we do so ourselves.
951 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 951 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
952 info->download_id = DownloadId(kValidIdDomain, 0); 952 info->download_id = DownloadId(kValidIdDomain, 0);
953 info->prompt_user_for_save_location = false; 953 info->prompt_user_for_save_location = false;
954 info->url_chain.push_back(GURL()); 954 info->url_chain.push_back(GURL());
955 info->total_bytes = static_cast<int64>(kTestDataLen); 955 info->total_bytes = static_cast<int64>(kTestDataLen);
956 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 956 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
957 const FilePath cr_path(GetTempDownloadPath(new_path)); 957 const FilePath cr_path(GetTempDownloadPath(new_path));
958 958
959 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); 959 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>());
960 ON_CALL(*download_file, AppendDataToFile(_, _))
961 .WillByDefault(Return(net::OK));
962 960
963 // |download_file| is owned by DownloadFileManager. 961 // |download_file| is owned by DownloadFileManager.
964 AddMockDownloadToFileManager(info->download_id.local(), download_file); 962 AddMockDownloadToFileManager(info->download_id.local(), download_file);
965 963
966 EXPECT_CALL(*download_file, Rename(cr_path)) 964 EXPECT_CALL(*download_file, Rename(cr_path))
967 .Times(1) 965 .Times(1)
968 .WillOnce(Return(net::OK)); 966 .WillOnce(Return(net::OK));
969 967
970 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 968 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
971 969
972 DownloadItem* download = GetActiveDownloadItem(0); 970 DownloadItem* download = GetActiveDownloadItem(0);
973 ASSERT_TRUE(download != NULL); 971 ASSERT_TRUE(download != NULL);
974 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 972 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
975 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 973 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
976 974
977 download_file->AppendDataToFile(kTestData, kTestDataLen);
978
979 ContinueDownloadWithPath(download, new_path); 975 ContinueDownloadWithPath(download, new_path);
980 message_loop_.RunAllPending(); 976 message_loop_.RunAllPending();
981 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 977 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
982 978
983 int64 error_size = 3; 979 int64 error_size = 3;
984 OnDownloadInterrupted(0, error_size, "", 980 OnDownloadInterrupted(0, error_size, "",
985 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 981 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
986 message_loop_.RunAllPending(); 982 message_loop_.RunAllPending();
987 983
988 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 984 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 1024 // responsible for deleting it. In these unit tests, however, we
1029 // don't call the function that deletes it, so we do so ourselves. 1025 // don't call the function that deletes it, so we do so ourselves.
1030 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1026 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1031 static const int32 local_id = 0; 1027 static const int32 local_id = 0;
1032 info->download_id = DownloadId(kValidIdDomain, local_id); 1028 info->download_id = DownloadId(kValidIdDomain, local_id);
1033 info->prompt_user_for_save_location = false; 1029 info->prompt_user_for_save_location = false;
1034 info->url_chain.push_back(GURL()); 1030 info->url_chain.push_back(GURL());
1035 info->total_bytes = static_cast<int64>(kTestDataLen * 3); 1031 info->total_bytes = static_cast<int64>(kTestDataLen * 3);
1036 info->save_info.file_path = path; 1032 info->save_info.file_path = path;
1037 info->save_info.file_stream.reset(stream); 1033 info->save_info.file_stream.reset(stream);
1034 scoped_ptr<content::ByteStreamInput> pipe_input;
1035 scoped_ptr<content::ByteStreamOutput> pipe_output;
1036 content::CreateByteStream(message_loop_.message_loop_proxy(),
1037 message_loop_.message_loop_proxy(),
1038 kTestDataLen, &pipe_input, &pipe_output);
1038 1039
1039 // Create a download file that we can insert errors into. 1040 // Create a download file that we can insert errors into.
1040 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( 1041 scoped_ptr<DownloadFileWithErrors> download_file(new DownloadFileWithErrors(
1041 info.get(), download_manager_, false)); 1042 info.get(), pipe_output.Pass(), download_manager_, false));
1042 download_file->Initialize(); 1043 download_file->Initialize();
1043 AddDownloadToFileManager(local_id, download_file);
1044 1044
1045 // |download_file| is owned by DownloadFileManager.
1046 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1045 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1047 1046
1048 DownloadItem* download = GetActiveDownloadItem(0); 1047 DownloadItem* download = GetActiveDownloadItem(0);
1049 ASSERT_TRUE(download != NULL); 1048 ASSERT_TRUE(download != NULL);
1050 1049
1051 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1050 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1052 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1051 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1053 1052
1054 // Add some data before finalizing the file name. 1053 // Add some data before finalizing the file name.
1055 UpdateData(local_id, kTestData, kTestDataLen); 1054 WriteCopyToPipe(pipe_input.get(), kTestData, kTestDataLen);
1056 1055
1057 // Finalize the file name. 1056 // Finalize the file name.
1058 ContinueDownloadWithPath(download, path); 1057 ContinueDownloadWithPath(download, path);
1059 message_loop_.RunAllPending(); 1058 message_loop_.RunAllPending();
1060 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1059 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1061 1060
1062 // Add more data. 1061 // Add more data.
1063 UpdateData(local_id, kTestData, kTestDataLen); 1062 WriteCopyToPipe(pipe_input.get(), kTestData, kTestDataLen);
1064 1063
1065 // Add more data, but an error occurs. 1064 // Add more data, but an error occurs.
1066 download_file->set_forced_error(net::ERR_FAILED); 1065 download_file->set_forced_error(net::ERR_FAILED);
1067 UpdateData(local_id, kTestData, kTestDataLen); 1066 WriteCopyToPipe(pipe_input.get(), kTestData, kTestDataLen);
1068 1067
1069 // Check the state. The download should have been interrupted. 1068 // Check the state. The download should have been interrupted.
1070 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1069 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1071 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1070 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1072 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); 1071 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED));
1073 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); 1072 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE));
1074 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1073 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1075 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1074 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1076 EXPECT_TRUE(observer->was_updated()); 1075 EXPECT_TRUE(observer->was_updated());
1077 EXPECT_FALSE(observer->was_opened()); 1076 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. 1093 // don't call the function that deletes it, so we do so ourselves.
1095 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1094 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1096 DownloadId id = DownloadId(kValidIdDomain, 0); 1095 DownloadId id = DownloadId(kValidIdDomain, 0);
1097 info->download_id = id; 1096 info->download_id = id;
1098 info->prompt_user_for_save_location = false; 1097 info->prompt_user_for_save_location = false;
1099 info->url_chain.push_back(GURL()); 1098 info->url_chain.push_back(GURL());
1100 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 1099 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
1101 const FilePath cr_path(GetTempDownloadPath(new_path)); 1100 const FilePath cr_path(GetTempDownloadPath(new_path));
1102 1101
1103 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); 1102 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); 1103 AddMockDownloadToFileManager(info->download_id.local(), download_file);
1107 1104
1108 // |download_file| is owned by DownloadFileManager. 1105 // |download_file| is owned by DownloadFileManager.
1109 EXPECT_CALL(*download_file, Rename(cr_path)) 1106 EXPECT_CALL(*download_file, Rename(cr_path))
1110 .Times(1) 1107 .Times(1)
1111 .WillOnce(Return(net::OK)); 1108 .WillOnce(Return(net::OK));
1112 1109
1113 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1110 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1114 1111
1115 DownloadItem* download = GetActiveDownloadItem(0); 1112 DownloadItem* download = GetActiveDownloadItem(0);
1116 ASSERT_TRUE(download != NULL); 1113 ASSERT_TRUE(download != NULL);
1117 1114
1118 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1115 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1119 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1116 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1120 1117
1121 ContinueDownloadWithPath(download, new_path); 1118 ContinueDownloadWithPath(download, new_path);
1122 message_loop_.RunAllPending(); 1119 message_loop_.RunAllPending();
1123 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1120 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1124 1121
1125 download_file->AppendDataToFile(kTestData, kTestDataLen);
1126
1127 download->Cancel(false); 1122 download->Cancel(false);
1128 message_loop_.RunAllPending(); 1123 message_loop_.RunAllPending();
1129 1124
1130 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1125 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1131 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1126 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1132 EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED)); 1127 EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED));
1133 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1128 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
1134 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); 1129 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE));
1135 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1130 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1136 EXPECT_TRUE(observer->was_updated()); 1131 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( 1169 unique_new_path = unique_new_path.InsertBeforeExtensionASCII(
1175 StringPrintf(" (%d)", uniquifier)); 1170 StringPrintf(" (%d)", uniquifier));
1176 1171
1177 // Normally, the download system takes ownership of info, and is 1172 // Normally, the download system takes ownership of info, and is
1178 // responsible for deleting it. In these unit tests, however, we 1173 // responsible for deleting it. In these unit tests, however, we
1179 // don't call the function that deletes it, so we do so ourselves. 1174 // don't call the function that deletes it, so we do so ourselves.
1180 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1175 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1181 info->download_id = DownloadId(kValidIdDomain, 0); 1176 info->download_id = DownloadId(kValidIdDomain, 0);
1182 info->prompt_user_for_save_location = true; 1177 info->prompt_user_for_save_location = true;
1183 info->url_chain.push_back(GURL()); 1178 info->url_chain.push_back(GURL());
1179 scoped_ptr<content::ByteStreamInput> pipe_input;
1180 scoped_ptr<content::ByteStreamOutput> pipe_output;
1181 content::CreateByteStream(message_loop_.message_loop_proxy(),
1182 message_loop_.message_loop_proxy(),
1183 kTestDataLen, &pipe_input, &pipe_output);
1184 1184
1185 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1185 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1186 1186
1187 DownloadItem* download = GetActiveDownloadItem(0); 1187 DownloadItem* download = GetActiveDownloadItem(0);
1188 ASSERT_TRUE(download != NULL); 1188 ASSERT_TRUE(download != NULL);
1189 1189
1190 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1190 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1191 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1191 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1192 1192
1193 // Create and initialize the download file. We're bypassing the first part 1193 // 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 1194 // 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 1195 // name has been chosen, so we need to initialize the download file
1196 // properly. 1196 // properly.
1197 DownloadFile* download_file( 1197 DownloadFile* download_file(
1198 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), 1198 new DownloadFileImpl(info.get(), pipe_output.Pass(),
1199 new DownloadRequestHandle(),
1199 download_manager_, false, 1200 download_manager_, false,
1200 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 1201 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
1201 net::BoundNetLog())); 1202 net::BoundNetLog()));
1202 download_file->Rename(cr_path); 1203 download_file->Rename(cr_path);
1203 // This creates the .temp version of the file. 1204 // This creates the .temp version of the file.
1204 download_file->Initialize(); 1205 download_file->Initialize();
1205 // |download_file| is owned by DownloadFileManager. 1206 // |download_file| is owned by DownloadFileManager.
1206 AddDownloadToFileManager(info->download_id.local(), download_file); 1207 AddDownloadToFileManager(info->download_id.local(), download_file);
1207 1208
1208 ContinueDownloadWithPath(download, new_path); 1209 ContinueDownloadWithPath(download, new_path);
1209 message_loop_.RunAllPending(); 1210 message_loop_.RunAllPending();
1210 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1211 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1211 1212
1212 download_file->AppendDataToFile(kTestData, kTestDataLen); 1213 WriteCopyToPipe(pipe_input.get(), kTestData, kTestDataLen);
1213 1214
1214 // Finish the download. 1215 // Finish the download.
1215 OnResponseCompleted(0, kTestDataLen, ""); 1216 OnResponseCompleted(0, kTestDataLen, "");
1216 message_loop_.RunAllPending(); 1217 message_loop_.RunAllPending();
1217 1218
1218 // Download is complete. 1219 // Download is complete.
1219 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1220 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1220 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1221 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1221 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1222 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1222 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1223 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
(...skipping 27 matching lines...) Expand all
1250 const FilePath cr_path(GetTempDownloadPath(new_path)); 1251 const FilePath cr_path(GetTempDownloadPath(new_path));
1251 EXPECT_FALSE(file_util::PathExists(new_path)); 1252 EXPECT_FALSE(file_util::PathExists(new_path));
1252 1253
1253 // Normally, the download system takes ownership of info, and is 1254 // Normally, the download system takes ownership of info, and is
1254 // responsible for deleting it. In these unit tests, however, we 1255 // responsible for deleting it. In these unit tests, however, we
1255 // don't call the function that deletes it, so we do so ourselves. 1256 // don't call the function that deletes it, so we do so ourselves.
1256 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); 1257 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
1257 info->download_id = DownloadId(kValidIdDomain, 0); 1258 info->download_id = DownloadId(kValidIdDomain, 0);
1258 info->prompt_user_for_save_location = true; 1259 info->prompt_user_for_save_location = true;
1259 info->url_chain.push_back(GURL()); 1260 info->url_chain.push_back(GURL());
1261 scoped_ptr<content::ByteStreamInput> pipe_input;
1262 scoped_ptr<content::ByteStreamOutput> pipe_output;
1263 content::CreateByteStream(message_loop_.message_loop_proxy(),
1264 message_loop_.message_loop_proxy(),
1265 kTestDataLen, &pipe_input, &pipe_output);
1260 1266
1261 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); 1267 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
1262 1268
1263 DownloadItem* download = GetActiveDownloadItem(0); 1269 DownloadItem* download = GetActiveDownloadItem(0);
1264 ASSERT_TRUE(download != NULL); 1270 ASSERT_TRUE(download != NULL);
1265 1271
1266 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); 1272 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState());
1267 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 1273 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
1268 1274
1269 // Create and initialize the download file. We're bypassing the first part 1275 // 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 1276 // 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 1277 // name has been chosen, so we need to initialize the download file
1272 // properly. 1278 // properly.
1273 DownloadFile* download_file( 1279 DownloadFile* download_file(
1274 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), 1280 new DownloadFileImpl(info.get(), pipe_output.Pass(),
1281 new DownloadRequestHandle(),
1275 download_manager_, false, 1282 download_manager_, false,
1276 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), 1283 scoped_ptr<PowerSaveBlocker>(NULL).Pass(),
1277 net::BoundNetLog())); 1284 net::BoundNetLog()));
1278 download_file->Rename(cr_path); 1285 download_file->Rename(cr_path);
1279 // This creates the .temp version of the file. 1286 // This creates the .temp version of the file.
1280 download_file->Initialize(); 1287 download_file->Initialize();
1281 // |download_file| is owned by DownloadFileManager. 1288 // |download_file| is owned by DownloadFileManager.
1282 AddDownloadToFileManager(info->download_id.local(), download_file); 1289 AddDownloadToFileManager(info->download_id.local(), download_file);
1283 1290
1284 ContinueDownloadWithPath(download, new_path); 1291 ContinueDownloadWithPath(download, new_path);
1285 message_loop_.RunAllPending(); 1292 message_loop_.RunAllPending();
1286 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 1293 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
1287 1294
1288 download_file->AppendDataToFile(kTestData, kTestDataLen); 1295 WriteCopyToPipe(pipe_input.get(), kTestData, kTestDataLen);
1289 1296
1290 // Finish the download. 1297 // Finish the download.
1291 OnResponseCompleted(0, kTestDataLen, ""); 1298 OnResponseCompleted(0, kTestDataLen, "");
1292 message_loop_.RunAllPending(); 1299 message_loop_.RunAllPending();
1293 1300
1294 // Download is complete. 1301 // Download is complete.
1295 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 1302 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
1296 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 1303 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
1297 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 1304 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
1298 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1305 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
(...skipping 18 matching lines...) Expand all
1317 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 1324 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
1318 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); 1325 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE));
1319 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 1326 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
1320 EXPECT_TRUE(observer->was_updated()); 1327 EXPECT_TRUE(observer->was_updated());
1321 EXPECT_FALSE(observer->was_opened()); 1328 EXPECT_FALSE(observer->was_opened());
1322 EXPECT_TRUE(download->GetFileExternallyRemoved()); 1329 EXPECT_TRUE(download->GetFileExternallyRemoved());
1323 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); 1330 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState());
1324 1331
1325 EXPECT_FALSE(file_util::PathExists(new_path)); 1332 EXPECT_FALSE(file_util::PathExists(new_path));
1326 } 1333 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698