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 "content/browser/download/download_file_manager.h" | 5 #include "content/browser/download/download_file_manager.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "content/browser/browser_thread_impl.h" | 12 #include "content/browser/browser_thread_impl.h" |
13 #include "content/browser/download/download_buffer.h" | 13 #include "content/browser/download/byte_stream.h" |
14 #include "content/browser/download/download_create_info.h" | 14 #include "content/browser/download/download_create_info.h" |
15 #include "content/browser/download/download_interrupt_reasons_impl.h" | 15 #include "content/browser/download/download_interrupt_reasons_impl.h" |
16 #include "content/browser/download/download_request_handle.h" | 16 #include "content/browser/download/download_request_handle.h" |
17 #include "content/browser/download/mock_download_file.h" | 17 #include "content/browser/download/mock_download_file.h" |
18 #include "content/public/browser/download_id.h" | 18 #include "content/public/browser/download_id.h" |
19 #include "content/test/mock_download_manager.h" | 19 #include "content/test/mock_download_manager.h" |
20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 13 matching lines...) Expand all Loading... |
37 | 37 |
38 class MockDownloadFileFactory : | 38 class MockDownloadFileFactory : |
39 public DownloadFileManager::DownloadFileFactory { | 39 public DownloadFileManager::DownloadFileFactory { |
40 | 40 |
41 public: | 41 public: |
42 MockDownloadFileFactory() {} | 42 MockDownloadFileFactory() {} |
43 virtual ~MockDownloadFileFactory() {} | 43 virtual ~MockDownloadFileFactory() {} |
44 | 44 |
45 virtual content::DownloadFile* CreateFile( | 45 virtual content::DownloadFile* CreateFile( |
46 DownloadCreateInfo* info, | 46 DownloadCreateInfo* info, |
| 47 scoped_ptr<content::ByteStreamOutput> pipe, |
47 const DownloadRequestHandle& request_handle, | 48 const DownloadRequestHandle& request_handle, |
48 content::DownloadManager* download_manager, | 49 content::DownloadManager* download_manager, |
49 bool calculate_hash, | 50 bool calculate_hash, |
50 const net::BoundNetLog& bound_net_log) OVERRIDE; | 51 const net::BoundNetLog& bound_net_log) OVERRIDE; |
51 | 52 |
52 MockDownloadFile* GetExistingFile(const DownloadId& id); | 53 MockDownloadFile* GetExistingFile(const DownloadId& id); |
53 | 54 |
54 private: | 55 private: |
55 std::map<DownloadId, MockDownloadFile*> files_; | 56 std::map<DownloadId, MockDownloadFile*> files_; |
56 }; | 57 }; |
57 | 58 |
58 content::DownloadFile* MockDownloadFileFactory::CreateFile( | 59 content::DownloadFile* MockDownloadFileFactory::CreateFile( |
59 DownloadCreateInfo* info, | 60 DownloadCreateInfo* info, |
| 61 scoped_ptr<content::ByteStreamOutput> pipe, |
60 const DownloadRequestHandle& request_handle, | 62 const DownloadRequestHandle& request_handle, |
61 content::DownloadManager* download_manager, | 63 content::DownloadManager* download_manager, |
62 bool calculate_hash, | 64 bool calculate_hash, |
63 const net::BoundNetLog& bound_net_log) { | 65 const net::BoundNetLog& bound_net_log) { |
64 DCHECK(files_.end() == files_.find(info->download_id)); | 66 DCHECK(files_.end() == files_.find(info->download_id)); |
65 MockDownloadFile* created_file = new MockDownloadFile(); | 67 MockDownloadFile* created_file = new MockDownloadFile(); |
66 files_[info->download_id] = created_file; | 68 files_[info->download_id] = created_file; |
67 | 69 |
68 ON_CALL(*created_file, GetDownloadManager()) | 70 ON_CALL(*created_file, GetDownloadManager()) |
69 .WillByDefault(Return(download_manager)); | 71 .WillByDefault(Return(download_manager)); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 static const int32 kDummyDownloadId; | 122 static const int32 kDummyDownloadId; |
121 static const int32 kDummyDownloadId2; | 123 static const int32 kDummyDownloadId2; |
122 static const int kDummyChildId; | 124 static const int kDummyChildId; |
123 static const int kDummyRequestId; | 125 static const int kDummyRequestId; |
124 | 126 |
125 // We need a UI |BrowserThread| in order to destruct |download_manager_|, | 127 // We need a UI |BrowserThread| in order to destruct |download_manager_|, |
126 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, | 128 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, |
127 // calling Release() on |download_manager_| won't ever result in its | 129 // calling Release() on |download_manager_| won't ever result in its |
128 // destructor being called and we get a leak. | 130 // destructor being called and we get a leak. |
129 DownloadFileManagerTest() | 131 DownloadFileManagerTest() |
130 : download_buffer_(new content::DownloadBuffer()), | 132 : ui_thread_(BrowserThread::UI, &loop_), |
131 ui_thread_(BrowserThread::UI, &loop_), | |
132 file_thread_(BrowserThread::FILE, &loop_) { | 133 file_thread_(BrowserThread::FILE, &loop_) { |
133 } | 134 } |
134 | 135 |
135 ~DownloadFileManagerTest() { | 136 ~DownloadFileManagerTest() { |
136 } | 137 } |
137 | 138 |
138 virtual void SetUp() { | 139 virtual void SetUp() { |
139 download_manager_ = new MockDownloadManager(); | 140 download_manager_ = new MockDownloadManager(); |
140 request_handle_.reset(new MockDownloadRequestHandle(download_manager_)); | 141 request_handle_.reset(new MockDownloadRequestHandle(download_manager_)); |
141 download_file_factory_ = new MockDownloadFileFactory; | 142 download_file_factory_ = new MockDownloadFileFactory; |
(...skipping 17 matching lines...) Expand all Loading... |
159 // Clears all gmock expectations for the download file |id| and the manager. | 160 // Clears all gmock expectations for the download file |id| and the manager. |
160 void ClearExpectations(DownloadId id) { | 161 void ClearExpectations(DownloadId id) { |
161 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | 162 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); |
162 Mock::VerifyAndClearExpectations(file); | 163 Mock::VerifyAndClearExpectations(file); |
163 Mock::VerifyAndClearExpectations(download_manager_); | 164 Mock::VerifyAndClearExpectations(download_manager_); |
164 } | 165 } |
165 | 166 |
166 // Start a download. | 167 // Start a download. |
167 // |info| is the information needed to create a new download file. | 168 // |info| is the information needed to create a new download file. |
168 // |id| is the download ID of the new download file. | 169 // |id| is the download ID of the new download file. |
169 void StartDownload(DownloadCreateInfo* info, const DownloadId& id) { | 170 void StartDownload(scoped_ptr<DownloadCreateInfo> info, |
| 171 const DownloadId& id) { |
170 // Expected call sequence: | 172 // Expected call sequence: |
171 // StartDownload | 173 // StartDownload |
172 // DownloadManager::CreateDownloadItem | 174 // DownloadManager::CreateDownloadItem |
173 // DownloadManagerDelegate::GenerateFileHash | 175 // DownloadManagerDelegate::GenerateFileHash |
174 // Process one message in the message loop | 176 // Process one message in the message loop |
175 // CreateDownloadFile | 177 // CreateDownloadFile |
176 // new MockDownloadFile, add to downloads_[id] | 178 // new MockDownloadFile, add to downloads_[id] |
177 // DownloadRequestHandle::ResumeRequest | 179 // DownloadRequestHandle::ResumeRequest |
178 // StartUpdateTimer | 180 // StartUpdateTimer |
179 // DownloadCreateInfo is destroyed | 181 // DownloadCreateInfo is destroyed |
180 // Process one message in the message loop | 182 // Process one message in the message loop |
181 // DownloadManager::StartDownload | 183 // DownloadManager::StartDownload |
182 info->download_id = id; | 184 info->download_id = id; |
183 | 185 |
184 // Set expectations and return values. | 186 // Set expectations and return values. |
185 EXPECT_CALL(*download_manager_, CreateDownloadItem(info, _)) | 187 EXPECT_CALL(*download_manager_, CreateDownloadItem(info.get(), _)) |
186 .Times(1) | 188 .Times(1) |
187 .WillOnce(Return(net::BoundNetLog())); | 189 .WillOnce(Return(net::BoundNetLog())); |
188 EXPECT_CALL(*download_manager_, GenerateFileHash()) | 190 EXPECT_CALL(*download_manager_, GenerateFileHash()) |
189 .Times(AtLeast(1)) | 191 .Times(AtLeast(1)) |
190 .WillRepeatedly(Return(false)); | 192 .WillRepeatedly(Return(false)); |
191 EXPECT_CALL(*download_manager_, StartDownload(id.local())); | 193 EXPECT_CALL(*download_manager_, StartDownload(id.local())); |
192 | 194 |
193 download_file_manager_->StartDownload(info, *request_handle_); | 195 download_file_manager_->StartDownload( |
| 196 info.Pass(), scoped_ptr<content::ByteStreamOutput>(), *request_handle_); |
194 ProcessAllPendingMessages(); | 197 ProcessAllPendingMessages(); |
195 | 198 |
196 ClearExpectations(id); | 199 ClearExpectations(id); |
197 } | 200 } |
198 | 201 |
199 // Creates a new |net::IOBuffer| of size |length| with |data|, and attaches | |
200 // it to the download buffer. | |
201 bool UpdateBuffer(const char* data, size_t length) { | |
202 // We are passing ownership of this buffer to the download file manager. | |
203 // NOTE: we are padding io_buffer with one extra character so that the | |
204 // mock testing framework can treat it as a NULL-delimited string. | |
205 net::IOBuffer* io_buffer = new net::IOBuffer(length + 1); | |
206 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. | |
207 io_buffer->AddRef(); | |
208 memcpy(io_buffer->data(), data, length); | |
209 io_buffer->data()[length] = 0; | |
210 | |
211 download_buffer_->AddData(io_buffer, length); | |
212 | |
213 return true; | |
214 } | |
215 | |
216 // Updates the download file. | |
217 // |id| is the download ID of the download file. | |
218 // |data| is the buffer containing the data. | |
219 // |length| is the length of the data buffer. | |
220 // |error_to_insert| is the error to simulate. For no error, use net::OK. | |
221 void UpdateDownload(const DownloadId& id, | |
222 const char* data, | |
223 size_t length, | |
224 net::Error error_to_insert) { | |
225 // Expected call sequence: | |
226 // Create DownloadBuffer | |
227 // UpdateDownload | |
228 // GetDownloadFile | |
229 // DownloadFile::AppendDataToFile | |
230 // | |
231 // On error: | |
232 // DownloadFile::GetDownloadManager | |
233 // DownloadFile::BytesSoFar | |
234 // CancelDownload | |
235 // Process one message in the message loop | |
236 // DownloadManager::OnDownloadInterrupted | |
237 EXPECT_TRUE(UpdateBuffer(data, length)); | |
238 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | |
239 ASSERT_TRUE(file != NULL); | |
240 byte_count_[id] += length; | |
241 | |
242 // Make sure our comparison string is NULL-terminated. | |
243 char* expected_data = new char[length + 1]; | |
244 memcpy(expected_data, data, length); | |
245 expected_data[length] = 0; | |
246 | |
247 EXPECT_CALL(*file, AppendDataToFile(StrEq(expected_data), length)) | |
248 .Times(1) | |
249 .WillOnce(Return(error_to_insert)); | |
250 | |
251 if (error_to_insert != net::OK) { | |
252 EXPECT_CALL(*file, GetDownloadManager()) | |
253 .Times(AtLeast(1)); | |
254 EXPECT_CALL(*file, BytesSoFar()) | |
255 .Times(AtLeast(1)) | |
256 .WillRepeatedly(Return(byte_count_[id])); | |
257 EXPECT_CALL(*file, GetHashState()) | |
258 .Times(AtLeast(1)); | |
259 EXPECT_CALL(*file, Cancel()); | |
260 } | |
261 | |
262 download_file_manager_->UpdateDownload(id, download_buffer_.get()); | |
263 | |
264 if (error_to_insert != net::OK) { | |
265 EXPECT_CALL(*download_manager_, | |
266 OnDownloadInterrupted( | |
267 id.local(), | |
268 byte_count_[id], | |
269 "", | |
270 content::ConvertNetErrorToInterruptReason( | |
271 error_to_insert, content::DOWNLOAD_INTERRUPT_FROM_DISK))); | |
272 | |
273 ProcessAllPendingMessages(); | |
274 ++error_count_[id]; | |
275 } | |
276 | |
277 ClearExpectations(id); | |
278 delete [] expected_data; | |
279 } | |
280 | |
281 // Renames the download file. | 202 // Renames the download file. |
282 // |id| is the download ID of the download file. | 203 // |id| is the download ID of the download file. |
283 // |new_path| is the new file path. | 204 // |new_path| is the new file path. |
284 // |unique_path| is the actual path that the download file will be | 205 // |unique_path| is the actual path that the download file will be |
285 // renamed to. If there is an existing file at | 206 // renamed to. If there is an existing file at |
286 // |new_path| and |replace| is false, then |new_path| | 207 // |new_path| and |replace| is false, then |new_path| |
287 // will be uniquified. | 208 // will be uniquified. |
288 // |rename_error| is the error to inject. For no error, use net::OK. | 209 // |rename_error| is the error to inject. For no error, use net::OK. |
289 // |state| whether we are renaming an in-progress download or a | 210 // |state| whether we are renaming an in-progress download or a |
290 // completed download. | 211 // completed download. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { | 309 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
389 EXPECT_CALL(*file, GetHash(_)) | 310 EXPECT_CALL(*file, GetHash(_)) |
390 .WillOnce(Return(false)); | 311 .WillOnce(Return(false)); |
391 } else { | 312 } else { |
392 EXPECT_CALL(*file, GetHashState()); | 313 EXPECT_CALL(*file, GetHashState()); |
393 } | 314 } |
394 EXPECT_CALL(*file, BytesSoFar()) | 315 EXPECT_CALL(*file, BytesSoFar()) |
395 .Times(AtLeast(1)) | 316 .Times(AtLeast(1)) |
396 .WillRepeatedly(Return(byte_count_[id])); | 317 .WillRepeatedly(Return(byte_count_[id])); |
397 | 318 |
398 download_file_manager_->OnResponseCompleted(id, reason, security_string); | |
399 | |
400 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { | 319 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
401 EXPECT_CALL(*download_manager_, | 320 EXPECT_CALL(*download_manager_, |
402 OnResponseCompleted(id.local(), byte_count_[id], "")) | 321 OnResponseCompleted(id.local(), byte_count_[id], "")) |
403 .Times(1); | 322 .Times(1); |
404 } else { | 323 } else { |
405 EXPECT_EQ(0, error_count_[id]); | 324 EXPECT_EQ(0, error_count_[id]); |
406 | 325 |
407 EXPECT_CALL(*download_manager_, | 326 EXPECT_CALL(*download_manager_, |
408 OnDownloadInterrupted(id.local(), | 327 OnDownloadInterrupted(id.local(), |
409 byte_count_[id], | 328 byte_count_[id], |
(...skipping 25 matching lines...) Expand all Loading... |
435 download_file_manager_->CancelDownload(id); | 354 download_file_manager_->CancelDownload(id); |
436 | 355 |
437 EXPECT_TRUE(NULL == download_file_manager_->GetDownloadFile(id)); | 356 EXPECT_TRUE(NULL == download_file_manager_->GetDownloadFile(id)); |
438 } | 357 } |
439 | 358 |
440 protected: | 359 protected: |
441 scoped_refptr<MockDownloadManager> download_manager_; | 360 scoped_refptr<MockDownloadManager> download_manager_; |
442 scoped_ptr<MockDownloadRequestHandle> request_handle_; | 361 scoped_ptr<MockDownloadRequestHandle> request_handle_; |
443 MockDownloadFileFactory* download_file_factory_; | 362 MockDownloadFileFactory* download_file_factory_; |
444 scoped_refptr<DownloadFileManager> download_file_manager_; | 363 scoped_refptr<DownloadFileManager> download_file_manager_; |
445 scoped_refptr<content::DownloadBuffer> download_buffer_; | |
446 | 364 |
447 // Per-download statistics. | 365 // Per-download statistics. |
448 std::map<DownloadId, int64> byte_count_; | 366 std::map<DownloadId, int64> byte_count_; |
449 std::map<DownloadId, int> error_count_; | 367 std::map<DownloadId, int> error_count_; |
450 | 368 |
451 private: | 369 private: |
452 MessageLoop loop_; | 370 MessageLoop loop_; |
453 | 371 |
454 // UI thread. | 372 // UI thread. |
455 BrowserThreadImpl ui_thread_; | 373 BrowserThreadImpl ui_thread_; |
456 | 374 |
457 // File thread to satisfy debug checks in DownloadFile. | 375 // File thread to satisfy debug checks in DownloadFile. |
458 BrowserThreadImpl file_thread_; | 376 BrowserThreadImpl file_thread_; |
459 }; | 377 }; |
460 | 378 |
461 const char* DownloadFileManagerTest::kTestData1 = | 379 const char* DownloadFileManagerTest::kTestData1 = |
462 "Let's write some data to the file!\n"; | 380 "Let's write some data to the file!\n"; |
463 const char* DownloadFileManagerTest::kTestData2 = "Writing more data.\n"; | 381 const char* DownloadFileManagerTest::kTestData2 = "Writing more data.\n"; |
464 const char* DownloadFileManagerTest::kTestData3 = "Final line."; | 382 const char* DownloadFileManagerTest::kTestData3 = "Final line."; |
465 const char* DownloadFileManagerTest::kTestData4 = "Writing, writing, writing\n"; | 383 const char* DownloadFileManagerTest::kTestData4 = "Writing, writing, writing\n"; |
466 const char* DownloadFileManagerTest::kTestData5 = "All we do is writing,\n"; | 384 const char* DownloadFileManagerTest::kTestData5 = "All we do is writing,\n"; |
467 const char* DownloadFileManagerTest::kTestData6 = "Rawhide!"; | 385 const char* DownloadFileManagerTest::kTestData6 = "Rawhide!"; |
468 | 386 |
469 const int32 DownloadFileManagerTest::kDummyDownloadId = 23; | 387 const int32 DownloadFileManagerTest::kDummyDownloadId = 23; |
470 const int32 DownloadFileManagerTest::kDummyDownloadId2 = 77; | 388 const int32 DownloadFileManagerTest::kDummyDownloadId2 = 77; |
471 const int DownloadFileManagerTest::kDummyChildId = 3; | 389 const int DownloadFileManagerTest::kDummyChildId = 3; |
472 const int DownloadFileManagerTest::kDummyRequestId = 67; | 390 const int DownloadFileManagerTest::kDummyRequestId = 67; |
473 | 391 |
474 TEST_F(DownloadFileManagerTest, CancelAtStart) { | 392 TEST_F(DownloadFileManagerTest, CancelAtStart) { |
475 DownloadCreateInfo* info = new DownloadCreateInfo; | 393 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
476 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 394 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
477 | 395 |
478 StartDownload(info, dummy_id); | 396 StartDownload(info.Pass(), dummy_id); |
479 | 397 |
480 CleanUp(dummy_id); | 398 CleanUp(dummy_id); |
481 } | 399 } |
482 | 400 |
483 TEST_F(DownloadFileManagerTest, CancelBeforeFinished) { | 401 TEST_F(DownloadFileManagerTest, CancelBeforeFinished) { |
484 // Same as StartDownload, at first. | 402 // Same as StartDownload, at first. |
485 DownloadCreateInfo* info = new DownloadCreateInfo; | 403 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
486 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 404 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
487 | 405 |
488 StartDownload(info, dummy_id); | 406 StartDownload(info.Pass(), dummy_id); |
489 | |
490 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
491 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
492 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
493 | 407 |
494 CleanUp(dummy_id); | 408 CleanUp(dummy_id); |
495 } | 409 } |
496 | 410 |
497 TEST_F(DownloadFileManagerTest, DownloadWithError) { | 411 TEST_F(DownloadFileManagerTest, DownloadWithError) { |
498 // Same as StartDownload, at first. | 412 // Same as StartDownload, at first. |
499 DownloadCreateInfo* info = new DownloadCreateInfo; | 413 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
500 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 414 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
501 | 415 |
502 StartDownload(info, dummy_id); | 416 StartDownload(info.Pass(), dummy_id); |
503 | 417 |
504 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
505 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), | |
506 net::ERR_FILE_NO_SPACE); | |
507 } | 418 } |
508 | 419 |
509 TEST_F(DownloadFileManagerTest, CompleteDownload) { | 420 TEST_F(DownloadFileManagerTest, CompleteDownload) { |
510 // Same as StartDownload, at first. | 421 // Same as StartDownload, at first. |
511 DownloadCreateInfo* info = new DownloadCreateInfo; | 422 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
512 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 423 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
513 | 424 |
514 StartDownload(info, dummy_id); | 425 StartDownload(info.Pass(), dummy_id); |
515 | |
516 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
517 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
518 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
519 | 426 |
520 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 427 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
521 | 428 |
522 CleanUp(dummy_id); | 429 CleanUp(dummy_id); |
523 } | 430 } |
524 | 431 |
525 TEST_F(DownloadFileManagerTest, CompleteDownloadWithError) { | 432 TEST_F(DownloadFileManagerTest, CompleteDownloadWithError) { |
526 // Same as StartDownload, at first. | 433 // Same as StartDownload, at first. |
527 DownloadCreateInfo* info = new DownloadCreateInfo; | 434 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
528 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 435 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
529 | 436 |
530 StartDownload(info, dummy_id); | 437 StartDownload(info.Pass(), dummy_id); |
531 | |
532 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
533 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
534 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
535 | 438 |
536 OnResponseCompleted(dummy_id, | 439 OnResponseCompleted(dummy_id, |
537 content::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED, | 440 content::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED, |
538 ""); | 441 ""); |
539 | 442 |
540 CleanUp(dummy_id); | 443 CleanUp(dummy_id); |
541 } | 444 } |
542 | 445 |
543 TEST_F(DownloadFileManagerTest, RenameInProgress) { | 446 TEST_F(DownloadFileManagerTest, RenameInProgress) { |
544 // Same as StartDownload, at first. | 447 // Same as StartDownload, at first. |
545 DownloadCreateInfo* info = new DownloadCreateInfo; | 448 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
546 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 449 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
547 ScopedTempDir download_dir; | 450 ScopedTempDir download_dir; |
548 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 451 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
549 | 452 |
550 StartDownload(info, dummy_id); | 453 StartDownload(info.Pass(), dummy_id); |
551 | 454 |
552 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
553 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
554 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 455 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
555 RenameFile(dummy_id, foo, foo, net::OK, IN_PROGRESS, OVERWRITE); | 456 RenameFile(dummy_id, foo, foo, net::OK, IN_PROGRESS, OVERWRITE); |
556 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
557 | 457 |
558 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 458 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
559 | 459 |
560 CleanUp(dummy_id); | 460 CleanUp(dummy_id); |
561 } | 461 } |
562 | 462 |
563 TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { | 463 TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { |
564 // Same as StartDownload, at first. | 464 // Same as StartDownload, at first. |
565 DownloadCreateInfo* info = new DownloadCreateInfo; | 465 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
566 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 466 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
567 ScopedTempDir download_dir; | 467 ScopedTempDir download_dir; |
568 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 468 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
569 | 469 |
570 StartDownload(info, dummy_id); | 470 StartDownload(info.Pass(), dummy_id); |
571 | 471 |
572 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
573 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
574 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 472 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
575 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, | 473 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, |
576 IN_PROGRESS, OVERWRITE); | 474 IN_PROGRESS, OVERWRITE); |
577 | 475 |
578 CleanUp(dummy_id); | 476 CleanUp(dummy_id); |
579 } | 477 } |
580 | 478 |
581 TEST_F(DownloadFileManagerTest, RenameCompleting) { | 479 TEST_F(DownloadFileManagerTest, RenameCompleting) { |
582 // Same as StartDownload, at first. | 480 // Same as StartDownload, at first. |
583 DownloadCreateInfo* info = new DownloadCreateInfo; | 481 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
584 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 482 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
585 ScopedTempDir download_dir; | 483 ScopedTempDir download_dir; |
586 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 484 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
587 | 485 |
588 StartDownload(info, dummy_id); | 486 StartDownload(info.Pass(), dummy_id); |
589 | |
590 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
591 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
592 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
593 | 487 |
594 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 488 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
595 | 489 |
596 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 490 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
597 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | 491 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); |
598 | 492 |
599 CleanUp(dummy_id); | 493 CleanUp(dummy_id); |
600 } | 494 } |
601 | 495 |
602 TEST_F(DownloadFileManagerTest, RenameCompletingWithUniquification) { | 496 TEST_F(DownloadFileManagerTest, RenameCompletingWithUniquification) { |
603 // Same as StartDownload, at first. | 497 // Same as StartDownload, at first. |
604 DownloadCreateInfo* info = new DownloadCreateInfo; | 498 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
605 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 499 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
606 ScopedTempDir download_dir; | 500 ScopedTempDir download_dir; |
607 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 501 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
608 | 502 |
609 StartDownload(info, dummy_id); | 503 StartDownload(info.Pass(), dummy_id); |
610 | |
611 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
612 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
613 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
614 | 504 |
615 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 505 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
616 | 506 |
617 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 507 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
618 FilePath unique_foo(foo.InsertBeforeExtension(FILE_PATH_LITERAL(" (1)"))); | 508 FilePath unique_foo(foo.InsertBeforeExtension(FILE_PATH_LITERAL(" (1)"))); |
619 // Create a file at |foo|. Since we are specifying DONT_OVERWRITE, | 509 // Create a file at |foo|. Since we are specifying DONT_OVERWRITE, |
620 // RenameCompletingDownloadFile() should pick "foo (1).txt" instead of | 510 // RenameCompletingDownloadFile() should pick "foo (1).txt" instead of |
621 // overwriting this file. | 511 // overwriting this file. |
622 ASSERT_EQ(0, file_util::WriteFile(foo, "", 0)); | 512 ASSERT_EQ(0, file_util::WriteFile(foo, "", 0)); |
623 RenameFile(dummy_id, foo, unique_foo, net::OK, COMPLETE, DONT_OVERWRITE); | 513 RenameFile(dummy_id, foo, unique_foo, net::OK, COMPLETE, DONT_OVERWRITE); |
624 | 514 |
625 CleanUp(dummy_id); | 515 CleanUp(dummy_id); |
626 } | 516 } |
627 | 517 |
628 TEST_F(DownloadFileManagerTest, RenameCompletingWithError) { | 518 TEST_F(DownloadFileManagerTest, RenameCompletingWithError) { |
629 // Same as StartDownload, at first. | 519 // Same as StartDownload, at first. |
630 DownloadCreateInfo* info = new DownloadCreateInfo; | 520 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
631 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 521 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
632 ScopedTempDir download_dir; | 522 ScopedTempDir download_dir; |
633 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 523 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
634 | 524 |
635 StartDownload(info, dummy_id); | 525 StartDownload(info.Pass(), dummy_id); |
636 | |
637 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
638 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
639 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
640 | 526 |
641 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 527 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
642 | 528 |
643 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 529 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
644 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, | 530 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, |
645 COMPLETE, OVERWRITE); | 531 COMPLETE, OVERWRITE); |
646 | 532 |
647 CleanUp(dummy_id); | 533 CleanUp(dummy_id); |
648 } | 534 } |
649 | 535 |
650 TEST_F(DownloadFileManagerTest, RenameTwice) { | 536 TEST_F(DownloadFileManagerTest, RenameTwice) { |
651 // Same as StartDownload, at first. | 537 // Same as StartDownload, at first. |
652 DownloadCreateInfo* info = new DownloadCreateInfo; | 538 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
653 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 539 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
654 ScopedTempDir download_dir; | 540 ScopedTempDir download_dir; |
655 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 541 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
656 | 542 |
657 StartDownload(info, dummy_id); | 543 StartDownload(info.Pass(), dummy_id); |
658 | 544 |
659 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
660 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
661 FilePath crfoo(download_dir.path().Append( | 545 FilePath crfoo(download_dir.path().Append( |
662 FILE_PATH_LITERAL("foo.txt.crdownload"))); | 546 FILE_PATH_LITERAL("foo.txt.crdownload"))); |
663 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); | 547 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); |
664 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
665 | 548 |
666 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 549 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
667 | 550 |
668 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 551 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
669 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | 552 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); |
670 | 553 |
671 CleanUp(dummy_id); | 554 CleanUp(dummy_id); |
672 } | 555 } |
673 | 556 |
674 TEST_F(DownloadFileManagerTest, TwoDownloads) { | 557 TEST_F(DownloadFileManagerTest, TwoDownloads) { |
675 // Same as StartDownload, at first. | 558 // Same as StartDownload, at first. |
676 DownloadCreateInfo* info = new DownloadCreateInfo; | 559 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
677 DownloadCreateInfo* info2 = new DownloadCreateInfo; | 560 scoped_ptr<DownloadCreateInfo> info2(new DownloadCreateInfo); |
678 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 561 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
679 DownloadId dummy_id2(download_manager_.get(), kDummyDownloadId2); | 562 DownloadId dummy_id2(download_manager_.get(), kDummyDownloadId2); |
680 ScopedTempDir download_dir; | 563 ScopedTempDir download_dir; |
681 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 564 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
682 | 565 |
683 StartDownload(info, dummy_id); | 566 StartDownload(info.Pass(), dummy_id); |
684 | 567 |
685 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | 568 StartDownload(info2.Pass(), dummy_id2); |
686 | 569 |
687 StartDownload(info2, dummy_id2); | |
688 | |
689 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
690 | |
691 UpdateDownload(dummy_id2, kTestData4, strlen(kTestData4), net::OK); | |
692 FilePath crbar(download_dir.path().Append( | 570 FilePath crbar(download_dir.path().Append( |
693 FILE_PATH_LITERAL("bar.txt.crdownload"))); | 571 FILE_PATH_LITERAL("bar.txt.crdownload"))); |
694 RenameFile(dummy_id2, crbar, crbar, net::OK, IN_PROGRESS, OVERWRITE); | 572 RenameFile(dummy_id2, crbar, crbar, net::OK, IN_PROGRESS, OVERWRITE); |
695 | 573 |
696 FilePath crfoo(download_dir.path().Append( | 574 FilePath crfoo(download_dir.path().Append( |
697 FILE_PATH_LITERAL("foo.txt.crdownload"))); | 575 FILE_PATH_LITERAL("foo.txt.crdownload"))); |
698 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); | 576 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); |
699 | 577 |
700 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
701 | |
702 UpdateDownload(dummy_id2, kTestData5, strlen(kTestData5), net::OK); | |
703 UpdateDownload(dummy_id2, kTestData6, strlen(kTestData6), net::OK); | |
704 | |
705 OnResponseCompleted(dummy_id2, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 578 OnResponseCompleted(dummy_id2, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
706 | 579 |
707 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | 580 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); |
708 | 581 |
709 FilePath bar(download_dir.path().Append(FILE_PATH_LITERAL("bar.txt"))); | 582 FilePath bar(download_dir.path().Append(FILE_PATH_LITERAL("bar.txt"))); |
710 RenameFile(dummy_id2, bar, bar, net::OK, COMPLETE, OVERWRITE); | 583 RenameFile(dummy_id2, bar, bar, net::OK, COMPLETE, OVERWRITE); |
711 | 584 |
712 CleanUp(dummy_id2); | 585 CleanUp(dummy_id2); |
713 | 586 |
714 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 587 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
715 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | 588 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); |
716 | 589 |
717 CleanUp(dummy_id); | 590 CleanUp(dummy_id); |
718 } | 591 } |
719 | 592 |
720 | 593 |
721 // TODO(ahendrickson) -- A test for updating progress. | 594 // TODO(ahendrickson) -- A test for updating progress. |
722 // Expected call sequence: | 595 // Expected call sequence: |
723 // UpdateInProgressDownloads | 596 // UpdateInProgressDownloads |
724 // DownloadFile::GetDownloadFile | 597 // DownloadFile::GetDownloadFile |
725 // Process one message in the message loop | 598 // Process one message in the message loop |
726 // DownloadManager::UpdateDownload | 599 // DownloadManager::UpdateDownload |
727 | 600 |
728 // TODO(ahendrickson) -- A test for download manager shutdown. | 601 // TODO(ahendrickson) -- A test for download manager shutdown. |
729 // Expected call sequence: | 602 // Expected call sequence: |
730 // OnDownloadManagerShutdown | 603 // OnDownloadManagerShutdown |
731 // DownloadFile::GetDownloadManager | 604 // DownloadFile::GetDownloadManager |
732 // DownloadFile::CancelDownloadRequest | 605 // DownloadFile::CancelDownloadRequest |
733 // DownloadFile::~DownloadFile | 606 // DownloadFile::~DownloadFile |
OLD | NEW |