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

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

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

Powered by Google App Engine
This is Rietveld 408576698