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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 | 674 |
675 // Check the contents. | 675 // Check the contents. |
676 EXPECT_EQ(value, file_contents); | 676 EXPECT_EQ(value, file_contents); |
677 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) | 677 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) |
678 return false; | 678 return false; |
679 | 679 |
680 return true; | 680 return true; |
681 } | 681 } |
682 | 682 |
683 // Attempts to download a file, based on information in |download_info|. | 683 // Attempts to download a file, based on information in |download_info|. |
| 684 // If a Select File dialog opens, will automatically choose the default. |
684 void DownloadFileCheckErrors(const DownloadInfo& download_info) { | 685 void DownloadFileCheckErrors(const DownloadInfo& download_info) { |
685 ASSERT_TRUE(test_server()->Start()); | 686 ASSERT_TRUE(test_server()->Start()); |
686 std::vector<DownloadItem*> download_items; | 687 std::vector<DownloadItem*> download_items; |
687 GetDownloads(browser(), &download_items); | 688 GetDownloads(browser(), &download_items); |
688 ASSERT_TRUE(download_items.empty()); | 689 ASSERT_TRUE(download_items.empty()); |
689 | 690 |
690 std::string server_path = "files/downloads/"; | 691 std::string server_path = "files/downloads/"; |
691 server_path += download_info.url_name; | 692 server_path += download_info.url_name; |
692 GURL url = test_server()->GetURL(server_path); | 693 GURL url = test_server()->GetURL(server_path); |
693 ASSERT_TRUE(url.is_valid()); | 694 ASSERT_TRUE(url.is_valid()); |
694 | 695 |
| 696 NullSelectFile(browser()); // Needed for read-only tests. |
| 697 |
695 DownloadManager* download_manager = DownloadManagerForBrowser(browser()); | 698 DownloadManager* download_manager = DownloadManagerForBrowser(browser()); |
696 scoped_ptr<DownloadTestObserver> observer( | 699 scoped_ptr<DownloadTestObserver> observer( |
697 new DownloadTestObserver( | 700 new DownloadTestObserver( |
698 download_manager, | 701 download_manager, |
699 1, | 702 1, |
700 download_info.reason == DOWNLOAD_INTERRUPT_REASON_NONE ? | 703 download_info.reason == DOWNLOAD_INTERRUPT_REASON_NONE ? |
701 DownloadItem::COMPLETE : // Really done | 704 DownloadItem::COMPLETE : // Really done |
702 DownloadItem::INTERRUPTED, | 705 DownloadItem::INTERRUPTED, |
703 true, // Bail on select file | 706 false, // Don't finish on select file |
704 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | 707 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
705 | 708 |
706 if (download_info.download_method == DOWNLOAD_DIRECT) { | 709 if (download_info.download_method == DOWNLOAD_DIRECT) { |
707 // Go directly to download. | 710 // Go directly to download. |
708 WebContents* web_contents = browser()->GetSelectedWebContents(); | 711 WebContents* web_contents = browser()->GetSelectedWebContents(); |
709 ASSERT_TRUE(web_contents); | 712 ASSERT_TRUE(web_contents); |
710 DownloadSaveInfo save_info; | 713 DownloadSaveInfo save_info; |
711 save_info.prompt_for_save_location = false; | 714 save_info.prompt_for_save_location = false; |
712 | 715 |
713 DownloadManagerForBrowser(browser())->DownloadUrl( | 716 DownloadManagerForBrowser(browser())->DownloadUrl( |
(...skipping 12 matching lines...) Expand all Loading... |
726 download_items.clear(); | 729 download_items.clear(); |
727 GetDownloads(browser(), &download_items); | 730 GetDownloads(browser(), &download_items); |
728 size_t item_count = download_info.show_download_item ? 1 : 0; | 731 size_t item_count = download_info.show_download_item ? 1 : 0; |
729 ASSERT_EQ(item_count, download_items.size()); | 732 ASSERT_EQ(item_count, download_items.size()); |
730 | 733 |
731 if (download_info.show_download_item) { | 734 if (download_info.show_download_item) { |
732 DownloadItem* item = download_items[0]; | 735 DownloadItem* item = download_items[0]; |
733 ASSERT_EQ(url, item->GetOriginalUrl()); | 736 ASSERT_EQ(url, item->GetOriginalUrl()); |
734 | 737 |
735 ASSERT_EQ(download_info.reason, item->GetLastReason()); | 738 ASSERT_EQ(download_info.reason, item->GetLastReason()); |
| 739 |
| 740 if (item->GetLastReason() == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 741 // Clean up the file, in case it ended up in the My Documents folder. |
| 742 FilePath destination_folder = GetDownloadDirectory(browser()); |
| 743 FilePath my_downloaded_file = item->GetTargetFilePath(); |
| 744 EXPECT_TRUE(file_util::PathExists(my_downloaded_file)); |
| 745 EXPECT_TRUE(file_util::Delete(my_downloaded_file, false)); |
| 746 |
| 747 // If it's not where we asked it to be, it should be in the |
| 748 // My Documents folder. |
| 749 if (my_downloaded_file.value().find(destination_folder.value()) == |
| 750 std::string::npos) { |
| 751 FilePath my_docs_folder; |
| 752 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DOCUMENTS, |
| 753 &my_docs_folder)); |
| 754 EXPECT_EQ(0u, |
| 755 my_downloaded_file.value().find(my_docs_folder.value())); |
| 756 } |
| 757 } |
736 } | 758 } |
737 } | 759 } |
738 | 760 |
| 761 // Attempts to download a file to a read-only folder, based on information |
| 762 // in |download_info|. |
| 763 void DownloadFileToReadonlyFolder(const DownloadInfo& download_info) { |
| 764 ASSERT_TRUE(InitialSetup(false)); // Creates temporary download folder. |
| 765 |
| 766 // Make the test folder unwritable. |
| 767 FilePath destination_folder = GetDownloadDirectory(browser()); |
| 768 DVLOG(1) << " " << __FUNCTION__ << "()" |
| 769 << " folder = '" << destination_folder.value() << "'"; |
| 770 size_t perm_length = 0; |
| 771 void* original_permissions = |
| 772 file_util::GetPermissionInfo(destination_folder, &perm_length); |
| 773 EXPECT_TRUE(original_permissions != NULL); |
| 774 EXPECT_TRUE(file_util::MakeFileUnwritable(destination_folder)); |
| 775 |
| 776 DownloadFileCheckErrors(download_info); |
| 777 |
| 778 EXPECT_TRUE(file_util::RestorePermissionInfo(destination_folder, |
| 779 original_permissions, |
| 780 perm_length)); |
| 781 } |
739 private: | 782 private: |
740 // Location of the test data. | 783 // Location of the test data. |
741 FilePath test_dir_; | 784 FilePath test_dir_; |
742 | 785 |
743 // Location of the downloads directory for these tests | 786 // Location of the downloads directory for these tests |
744 ScopedTempDir downloads_directory_; | 787 ScopedTempDir downloads_directory_; |
745 }; | 788 }; |
746 | 789 |
747 // NOTES: | 790 // NOTES: |
748 // | 791 // |
(...skipping 1325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2074 "zip_file_not_found.zip", | 2117 "zip_file_not_found.zip", |
2075 DOWNLOAD_NAVIGATE, | 2118 DOWNLOAD_NAVIGATE, |
2076 DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, | 2119 DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, |
2077 false | 2120 false |
2078 }; | 2121 }; |
2079 | 2122 |
2080 // Do initial setup. | 2123 // Do initial setup. |
2081 ASSERT_TRUE(InitialSetup(false)); | 2124 ASSERT_TRUE(InitialSetup(false)); |
2082 DownloadFileCheckErrors(download_info); | 2125 DownloadFileCheckErrors(download_info); |
2083 } | 2126 } |
| 2127 |
| 2128 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadErrorReadonlyFolderDirect) { |
| 2129 DownloadInfo download_info = { |
| 2130 "a_zip_file.zip", |
| 2131 DOWNLOAD_DIRECT, |
| 2132 // This passes because we switch to the My Documents folder. |
| 2133 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 2134 true |
| 2135 }; |
| 2136 |
| 2137 DownloadFileToReadonlyFolder(download_info); |
| 2138 } |
| 2139 |
| 2140 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadErrorReadonlyFolderNavigate) { |
| 2141 DownloadInfo download_info = { |
| 2142 "a_zip_file.zip", |
| 2143 DOWNLOAD_NAVIGATE, |
| 2144 // This passes because we switch to the My Documents folder. |
| 2145 DOWNLOAD_INTERRUPT_REASON_NONE, |
| 2146 true |
| 2147 }; |
| 2148 |
| 2149 DownloadFileToReadonlyFolder(download_info); |
| 2150 } |
OLD | NEW |