OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
6 #include <set> | 6 #include <set> |
7 | 7 |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/download/download_create_info.h" |
13 #include "chrome/browser/download/download_file.h" | 14 #include "chrome/browser/download/download_file.h" |
14 #include "chrome/browser/download/download_file_manager.h" | 15 #include "chrome/browser/download/download_file_manager.h" |
15 #include "chrome/browser/download/download_item.h" | 16 #include "chrome/browser/download/download_item.h" |
16 #include "chrome/browser/download/download_manager.h" | 17 #include "chrome/browser/download/download_manager.h" |
17 #include "chrome/browser/download/download_prefs.h" | 18 #include "chrome/browser/download/download_prefs.h" |
18 #include "chrome/browser/download/download_status_updater.h" | 19 #include "chrome/browser/download/download_status_updater.h" |
19 #include "chrome/browser/download/download_util.h" | 20 #include "chrome/browser/download/download_util.h" |
20 #include "chrome/browser/download/mock_download_manager.h" | 21 #include "chrome/browser/download/mock_download_manager.h" |
21 #include "chrome/browser/history/download_create_info.h" | |
22 #include "chrome/browser/prefs/pref_service.h" | 22 #include "chrome/browser/prefs/pref_service.h" |
23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
24 #include "chrome/test/testing_profile.h" | 24 #include "chrome/test/testing_profile.h" |
25 #include "content/browser/browser_thread.h" | 25 #include "content/browser/browser_thread.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gmock_mutant.h" | 27 #include "testing/gmock_mutant.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 | 29 |
30 class DownloadManagerTest : public testing::Test { | 30 class DownloadManagerTest : public testing::Test { |
31 public: | 31 public: |
(...skipping 22 matching lines...) Expand all Loading... |
54 } | 54 } |
55 | 55 |
56 void OnAllDataSaved(int32 download_id, int64 size, const std::string& hash) { | 56 void OnAllDataSaved(int32 download_id, int64 size, const std::string& hash) { |
57 download_manager_->OnAllDataSaved(download_id, size, hash); | 57 download_manager_->OnAllDataSaved(download_id, size, hash); |
58 } | 58 } |
59 | 59 |
60 void FileSelected(const FilePath& path, int index, void* params) { | 60 void FileSelected(const FilePath& path, int index, void* params) { |
61 download_manager_->FileSelected(path, index, params); | 61 download_manager_->FileSelected(path, index, params); |
62 } | 62 } |
63 | 63 |
64 void AttachDownloadItem(DownloadCreateInfo* info) { | 64 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { |
65 download_manager_->AttachDownloadItem(info); | 65 download_manager_->ContinueDownloadWithPath(download, path); |
66 } | 66 } |
67 | 67 |
68 void OnDownloadError(int32 download_id, int64 size, int os_error) { | 68 void OnDownloadError(int32 download_id, int64 size, int os_error) { |
69 download_manager_->OnDownloadError(download_id, size, os_error); | 69 download_manager_->OnDownloadError(download_id, size, os_error); |
70 } | 70 } |
71 | 71 |
72 // Get the download item with ID |id|. | 72 // Get the download item with ID |id|. |
73 DownloadItem* GetActiveDownloadItem(int32 id) { | 73 DownloadItem* GetActiveDownloadItem(int32 id) { |
74 if (ContainsKey(download_manager_->active_downloads_, id)) | 74 if (ContainsKey(download_manager_->active_downloads_, id)) |
75 return download_manager_->active_downloads_[id]; | 75 return download_manager_->active_downloads_[id]; |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 DownloadCreateInfo* info = new DownloadCreateInfo; | 305 DownloadCreateInfo* info = new DownloadCreateInfo; |
306 info->download_id = static_cast<int>(i); | 306 info->download_id = static_cast<int>(i); |
307 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; | 307 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; |
308 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); | 308 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); |
309 info->mime_type = kStartDownloadCases[i].mime_type; | 309 info->mime_type = kStartDownloadCases[i].mime_type; |
310 download_manager_->CreateDownloadItem(info); | 310 download_manager_->CreateDownloadItem(info); |
311 | 311 |
312 DownloadFile* download_file(new DownloadFile(info, download_manager_)); | 312 DownloadFile* download_file(new DownloadFile(info, download_manager_)); |
313 AddDownloadToFileManager(info->download_id, download_file); | 313 AddDownloadToFileManager(info->download_id, download_file); |
314 download_file->Initialize(false); | 314 download_file->Initialize(false); |
315 download_manager_->StartDownload(info); | 315 download_manager_->StartDownload(info->download_id); |
316 message_loop_.RunAllPending(); | 316 message_loop_.RunAllPending(); |
317 | 317 |
318 // NOTE: At this point, |AttachDownloadItem| will have been run if we don't | 318 // NOTE: At this point, |ContinueDownloadWithPath| will have been run if |
319 // need to prompt the user, so |info| could have been destructed. | 319 // we don't need to prompt the user, so |info| could have been destructed. |
320 // This means that we can't check any of its values. | 320 // This means that we can't check any of its values. |
321 // However, SelectFileObserver will have recorded any attempt to open the | 321 // However, SelectFileObserver will have recorded any attempt to open the |
322 // select file dialog. | 322 // select file dialog. |
323 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, | 323 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, |
324 observer.ShowedFileDialogForId(i)); | 324 observer.ShowedFileDialogForId(i)); |
325 | 325 |
326 // If the Save As dialog pops up, it never reached | 326 // If the Save As dialog pops up, it never reached |
327 // DownloadManager::AttachDownloadItem(), and never deleted info or | 327 // DownloadManager::ContinueDownloadWithPath(), and never deleted info or |
328 // completed. This cleans up info. | 328 // completed. This cleans up info. |
329 // Note that DownloadManager::FileSelectionCanceled() is never called. | 329 // Note that DownloadManager::FileSelectionCanceled() is never called. |
330 if (observer.ShowedFileDialogForId(i)) { | 330 if (observer.ShowedFileDialogForId(i)) { |
331 delete info; | 331 delete info; |
332 } | 332 } |
333 } | 333 } |
334 } | 334 } |
335 | 335 |
336 TEST_F(DownloadManagerTest, DownloadRenameTest) { | 336 TEST_F(DownloadManagerTest, DownloadRenameTest) { |
337 using ::testing::_; | 337 using ::testing::_; |
338 using ::testing::CreateFunctor; | 338 using ::testing::CreateFunctor; |
339 using ::testing::Invoke; | 339 using ::testing::Invoke; |
340 using ::testing::Return; | 340 using ::testing::Return; |
341 | 341 |
342 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { | 342 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { |
343 // |info| will be destroyed in download_manager_. | 343 // |info| will be destroyed in download_manager_. |
344 DownloadCreateInfo* info(new DownloadCreateInfo); | 344 DownloadCreateInfo* info(new DownloadCreateInfo); |
345 info->download_id = static_cast<int>(i); | 345 info->download_id = static_cast<int>(i); |
346 info->prompt_user_for_save_location = false; | 346 info->prompt_user_for_save_location = false; |
347 info->url_chain.push_back(GURL()); | 347 info->url_chain.push_back(GURL()); |
348 info->is_dangerous_file = kDownloadRenameCases[i].is_dangerous_file; | 348 info->is_dangerous_file = kDownloadRenameCases[i].is_dangerous_file; |
349 info->is_dangerous_url = kDownloadRenameCases[i].is_dangerous_url; | 349 info->is_dangerous_url = kDownloadRenameCases[i].is_dangerous_url; |
350 FilePath new_path(kDownloadRenameCases[i].suggested_path); | 350 const FilePath new_path(kDownloadRenameCases[i].suggested_path); |
351 | 351 |
352 MockDownloadFile* download_file( | 352 MockDownloadFile* download_file( |
353 new MockDownloadFile(info, download_manager_)); | 353 new MockDownloadFile(info, download_manager_)); |
354 AddDownloadToFileManager(info->download_id, download_file); | 354 AddDownloadToFileManager(info->download_id, download_file); |
355 | 355 |
356 // |download_file| is owned by DownloadFileManager. | 356 // |download_file| is owned by DownloadFileManager. |
357 ::testing::Mock::AllowLeak(download_file); | 357 ::testing::Mock::AllowLeak(download_file); |
358 EXPECT_CALL(*download_file, Destructed()).Times(1); | 358 EXPECT_CALL(*download_file, Destructed()).Times(1); |
359 | 359 |
360 if (kDownloadRenameCases[i].expected_rename_count == 1) { | 360 if (kDownloadRenameCases[i].expected_rename_count == 1) { |
361 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(true)); | 361 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(true)); |
362 } else { | 362 } else { |
363 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); | 363 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); |
364 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); | 364 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); |
365 EXPECT_CALL(*download_file, Rename(_)) | 365 EXPECT_CALL(*download_file, Rename(_)) |
366 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( | 366 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( |
367 download_file, &MockDownloadFile::TestMultipleRename, | 367 download_file, &MockDownloadFile::TestMultipleRename, |
368 1, crdownload)))) | 368 1, crdownload)))) |
369 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( | 369 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( |
370 download_file, &MockDownloadFile::TestMultipleRename, | 370 download_file, &MockDownloadFile::TestMultipleRename, |
371 2, new_path)))); | 371 2, new_path)))); |
372 } | 372 } |
373 download_manager_->CreateDownloadItem(info); | 373 download_manager_->CreateDownloadItem(info); |
374 | 374 |
| 375 int32* id_ptr = new int32; |
| 376 *id_ptr = i; // Deleted in FileSelected(). |
375 if (kDownloadRenameCases[i].finish_before_rename) { | 377 if (kDownloadRenameCases[i].finish_before_rename) { |
376 OnAllDataSaved(i, 1024, std::string("fake_hash")); | 378 OnAllDataSaved(i, 1024, std::string("fake_hash")); |
377 message_loop_.RunAllPending(); | 379 message_loop_.RunAllPending(); |
378 FileSelected(new_path, i, info); | 380 FileSelected(new_path, i, id_ptr); |
379 } else { | 381 } else { |
380 FileSelected(new_path, i, info); | 382 FileSelected(new_path, i, id_ptr); |
381 message_loop_.RunAllPending(); | 383 message_loop_.RunAllPending(); |
382 OnAllDataSaved(i, 1024, std::string("fake_hash")); | 384 OnAllDataSaved(i, 1024, std::string("fake_hash")); |
383 } | 385 } |
384 | 386 |
385 message_loop_.RunAllPending(); | 387 message_loop_.RunAllPending(); |
386 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file, | 388 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file, |
387 kDownloadRenameCases[i].is_dangerous_url, | 389 kDownloadRenameCases[i].is_dangerous_url, |
388 i)); | 390 i)); |
389 } | 391 } |
390 } | 392 } |
(...skipping 27 matching lines...) Expand all Loading... |
418 download_manager_->CreateDownloadItem(info); | 420 download_manager_->CreateDownloadItem(info); |
419 | 421 |
420 DownloadItem* download = GetActiveDownloadItem(0); | 422 DownloadItem* download = GetActiveDownloadItem(0); |
421 ASSERT_TRUE(download != NULL); | 423 ASSERT_TRUE(download != NULL); |
422 | 424 |
423 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); | 425 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
424 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 426 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
425 | 427 |
426 download_file->AppendDataToFile(kTestData, kTestDataLen); | 428 download_file->AppendDataToFile(kTestData, kTestDataLen); |
427 | 429 |
428 info->path = new_path; | 430 ContinueDownloadWithPath(download, new_path); |
429 AttachDownloadItem(info); | |
430 message_loop_.RunAllPending(); | 431 message_loop_.RunAllPending(); |
431 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 432 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
432 | 433 |
433 OnDownloadError(0, 1024, -6); | 434 OnDownloadError(0, 1024, -6); |
434 message_loop_.RunAllPending(); | 435 message_loop_.RunAllPending(); |
435 | 436 |
436 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 437 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
437 EXPECT_EQ(DownloadItem::INTERRUPTED, download->state()); | 438 EXPECT_EQ(DownloadItem::INTERRUPTED, download->state()); |
438 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 439 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
439 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); | 440 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true)); | 483 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true)); |
483 | 484 |
484 download_manager_->CreateDownloadItem(info); | 485 download_manager_->CreateDownloadItem(info); |
485 | 486 |
486 DownloadItem* download = GetActiveDownloadItem(0); | 487 DownloadItem* download = GetActiveDownloadItem(0); |
487 ASSERT_TRUE(download != NULL); | 488 ASSERT_TRUE(download != NULL); |
488 | 489 |
489 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); | 490 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
490 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 491 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
491 | 492 |
492 info->path = new_path; | 493 ContinueDownloadWithPath(download, new_path); |
493 AttachDownloadItem(info); | |
494 message_loop_.RunAllPending(); | 494 message_loop_.RunAllPending(); |
495 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 495 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
496 | 496 |
497 download_file->AppendDataToFile(kTestData, kTestDataLen); | 497 download_file->AppendDataToFile(kTestData, kTestDataLen); |
498 | 498 |
499 download->Cancel(false); | 499 download->Cancel(false); |
500 message_loop_.RunAllPending(); | 500 message_loop_.RunAllPending(); |
501 | 501 |
502 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 502 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
503 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 503 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 // name has been chosen, so we need to initialize the download file | 561 // name has been chosen, so we need to initialize the download file |
562 // properly. | 562 // properly. |
563 DownloadFile* download_file( | 563 DownloadFile* download_file( |
564 new DownloadFile(info, download_manager_)); | 564 new DownloadFile(info, download_manager_)); |
565 download_file->Rename(cr_path); | 565 download_file->Rename(cr_path); |
566 // This creates the .crdownload version of the file. | 566 // This creates the .crdownload version of the file. |
567 download_file->Initialize(false); | 567 download_file->Initialize(false); |
568 // |download_file| is owned by DownloadFileManager. | 568 // |download_file| is owned by DownloadFileManager. |
569 AddDownloadToFileManager(info->download_id, download_file); | 569 AddDownloadToFileManager(info->download_id, download_file); |
570 | 570 |
571 info->path = new_path; | 571 ContinueDownloadWithPath(download, new_path); |
572 AttachDownloadItem(info); | |
573 message_loop_.RunAllPending(); | 572 message_loop_.RunAllPending(); |
574 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 573 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
575 | 574 |
576 download_file->AppendDataToFile(kTestData, kTestDataLen); | 575 download_file->AppendDataToFile(kTestData, kTestDataLen); |
577 | 576 |
578 // Finish the download. | 577 // Finish the download. |
579 OnAllDataSaved(0, kTestDataLen, ""); | 578 OnAllDataSaved(0, kTestDataLen, ""); |
580 message_loop_.RunAllPending(); | 579 message_loop_.RunAllPending(); |
581 | 580 |
582 // Download is complete. | 581 // Download is complete. |
583 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 582 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
584 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 583 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
585 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 584 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
586 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); | 585 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
587 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); | 586 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); |
588 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 587 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
589 EXPECT_TRUE(observer->was_updated()); | 588 EXPECT_TRUE(observer->was_updated()); |
590 EXPECT_FALSE(observer->was_opened()); | 589 EXPECT_FALSE(observer->was_opened()); |
591 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 590 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
592 | 591 |
593 EXPECT_TRUE(file_util::PathExists(new_path)); | 592 EXPECT_TRUE(file_util::PathExists(new_path)); |
594 EXPECT_FALSE(file_util::PathExists(cr_path)); | 593 EXPECT_FALSE(file_util::PathExists(cr_path)); |
595 EXPECT_FALSE(file_util::PathExists(unique_new_path)); | 594 EXPECT_FALSE(file_util::PathExists(unique_new_path)); |
596 std::string file_contents; | 595 std::string file_contents; |
597 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); | 596 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); |
598 EXPECT_EQ(std::string(kTestData), file_contents); | 597 EXPECT_EQ(std::string(kTestData), file_contents); |
599 } | 598 } |
OLD | NEW |