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