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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 DOWNLOAD_DIRECT | 243 DOWNLOAD_DIRECT |
244 }; | 244 }; |
245 | 245 |
246 // Information passed in to |DownloadFileCheckErrors()|. | 246 // Information passed in to |DownloadFileCheckErrors()|. |
247 struct DownloadInfo { | 247 struct DownloadInfo { |
248 const char* url_name; // URL for the download. | 248 const char* url_name; // URL for the download. |
249 DownloadMethod download_method; // Navigation or Direct. | 249 DownloadMethod download_method; // Navigation or Direct. |
250 // Download interrupt reason (NONE is OK). | 250 // Download interrupt reason (NONE is OK). |
251 content::DownloadInterruptReason reason; | 251 content::DownloadInterruptReason reason; |
252 bool show_download_item; // True if the download item appears on the shelf. | 252 bool show_download_item; // True if the download item appears on the shelf. |
| 253 bool should_redirect_to_documents; // True if we save it in "My Documents". |
253 }; | 254 }; |
254 | 255 |
255 DownloadTest() { | 256 DownloadTest() { |
256 EnableDOMAutomation(); | 257 EnableDOMAutomation(); |
257 } | 258 } |
258 | 259 |
259 void SetUpOnMainThread() OVERRIDE { | 260 void SetUpOnMainThread() OVERRIDE { |
260 BrowserThread::PostTask( | 261 BrowserThread::PostTask( |
261 BrowserThread::IO, FROM_HERE, | 262 BrowserThread::IO, FROM_HERE, |
262 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 263 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 | 635 |
635 // Check the contents. | 636 // Check the contents. |
636 EXPECT_EQ(value, file_contents); | 637 EXPECT_EQ(value, file_contents); |
637 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) | 638 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) |
638 return false; | 639 return false; |
639 | 640 |
640 return true; | 641 return true; |
641 } | 642 } |
642 | 643 |
643 // Attempts to download a file, based on information in |download_info|. | 644 // Attempts to download a file, based on information in |download_info|. |
| 645 // If a Select File dialog opens, will automatically choose the default. |
644 void DownloadFileCheckErrors(const DownloadInfo& download_info) { | 646 void DownloadFileCheckErrors(const DownloadInfo& download_info) { |
645 ASSERT_TRUE(test_server()->Start()); | 647 ASSERT_TRUE(test_server()->Start()); |
646 std::vector<DownloadItem*> download_items; | 648 std::vector<DownloadItem*> download_items; |
647 GetDownloads(browser(), &download_items); | 649 GetDownloads(browser(), &download_items); |
648 ASSERT_TRUE(download_items.empty()); | 650 ASSERT_TRUE(download_items.empty()); |
649 | 651 |
650 NullSelectFile(browser()); | 652 NullSelectFile(browser()); |
651 | 653 |
652 std::string server_path = "files/downloads/"; | 654 std::string server_path = "files/downloads/"; |
653 server_path += download_info.url_name; | 655 server_path += download_info.url_name; |
654 GURL url = test_server()->GetURL(server_path); | 656 GURL url = test_server()->GetURL(server_path); |
655 ASSERT_TRUE(url.is_valid()); | 657 ASSERT_TRUE(url.is_valid()); |
656 | 658 |
| 659 NullSelectFile(browser()); // Needed for read-only tests. |
| 660 |
657 DownloadManager* download_manager = DownloadManagerForBrowser(browser()); | 661 DownloadManager* download_manager = DownloadManagerForBrowser(browser()); |
658 scoped_ptr<DownloadTestObserver> observer( | 662 scoped_ptr<DownloadTestObserver> observer( |
659 new DownloadTestObserverTerminal( | 663 new DownloadTestObserverTerminal( |
660 download_manager, | 664 download_manager, |
661 1, | 665 1, |
662 false, // Don't bail on select file. | 666 false, // Don't bail on select file. |
663 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); | 667 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL)); |
664 | 668 |
665 if (download_info.download_method == DOWNLOAD_DIRECT) { | 669 if (download_info.download_method == DOWNLOAD_DIRECT) { |
666 // Go directly to download. | 670 // Go directly to download. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 download_items.clear(); | 714 download_items.clear(); |
711 GetDownloads(browser(), &download_items); | 715 GetDownloads(browser(), &download_items); |
712 size_t item_count = download_info.show_download_item ? 1 : 0; | 716 size_t item_count = download_info.show_download_item ? 1 : 0; |
713 ASSERT_EQ(item_count, download_items.size()); | 717 ASSERT_EQ(item_count, download_items.size()); |
714 | 718 |
715 if (download_info.show_download_item) { | 719 if (download_info.show_download_item) { |
716 DownloadItem* item = download_items[0]; | 720 DownloadItem* item = download_items[0]; |
717 ASSERT_EQ(url, item->GetOriginalUrl()); | 721 ASSERT_EQ(url, item->GetOriginalUrl()); |
718 | 722 |
719 ASSERT_EQ(download_info.reason, item->GetLastReason()); | 723 ASSERT_EQ(download_info.reason, item->GetLastReason()); |
| 724 |
| 725 if (item->GetState() == content::DownloadItem::COMPLETE) { |
| 726 // Clean up the file, in case it ended up in the My Documents folder. |
| 727 FilePath destination_folder = GetDownloadDirectory(browser()); |
| 728 FilePath my_downloaded_file = item->GetTargetFilePath(); |
| 729 EXPECT_TRUE(file_util::PathExists(my_downloaded_file)); |
| 730 EXPECT_TRUE(file_util::Delete(my_downloaded_file, false)); |
| 731 |
| 732 EXPECT_EQ(download_info.should_redirect_to_documents ? |
| 733 std::string::npos : |
| 734 0u, |
| 735 my_downloaded_file.value().find(destination_folder.value())); |
| 736 if (download_info.should_redirect_to_documents) { |
| 737 // If it's not where we asked it to be, it should be in the |
| 738 // My Documents folder. |
| 739 FilePath my_docs_folder; |
| 740 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DOCUMENTS, |
| 741 &my_docs_folder)); |
| 742 EXPECT_EQ(0u, |
| 743 my_downloaded_file.value().find(my_docs_folder.value())); |
| 744 } |
| 745 } |
720 } | 746 } |
721 } | 747 } |
722 | 748 |
| 749 // Attempts to download a file to a read-only folder, based on information |
| 750 // in |download_info|. |
| 751 void DownloadFileToReadonlyFolder(const DownloadInfo& download_info) { |
| 752 ASSERT_TRUE(InitialSetup(false)); // Creates temporary download folder. |
| 753 |
| 754 // Make the test folder unwritable. |
| 755 FilePath destination_folder = GetDownloadDirectory(browser()); |
| 756 DVLOG(1) << " " << __FUNCTION__ << "()" |
| 757 << " folder = '" << destination_folder.value() << "'"; |
| 758 file_util::PermissionRestorer permission_restorer(destination_folder); |
| 759 EXPECT_TRUE(file_util::MakeFileUnwritable(destination_folder)); |
| 760 |
| 761 DownloadFileCheckErrors(download_info); |
| 762 } |
| 763 |
723 bool EnsureNoPendingDownloads() { | 764 bool EnsureNoPendingDownloads() { |
724 bool result = true; | 765 bool result = true; |
725 BrowserThread::PostTask( | 766 BrowserThread::PostTask( |
726 BrowserThread::IO, FROM_HERE, | 767 BrowserThread::IO, FROM_HERE, |
727 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 768 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
728 MessageLoop::current()->Run(); | 769 MessageLoop::current()->Run(); |
729 return result && DownloadManager::EnsureNoPendingDownloadsForTesting(); | 770 return result && DownloadManager::EnsureNoPendingDownloadsForTesting(); |
730 } | 771 } |
731 | 772 |
732 private: | 773 private: |
(...skipping 1288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2021 ASSERT_EQ(2u, download_items.size()); | 2062 ASSERT_EQ(2u, download_items.size()); |
2022 ASSERT_EQ(jpeg_url, download_items[0]->GetOriginalUrl()); | 2063 ASSERT_EQ(jpeg_url, download_items[0]->GetOriginalUrl()); |
2023 ASSERT_EQ(jpeg_url, download_items[1]->GetOriginalUrl()); | 2064 ASSERT_EQ(jpeg_url, download_items[1]->GetOriginalUrl()); |
2024 } | 2065 } |
2025 | 2066 |
2026 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadNavigate) { | 2067 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadNavigate) { |
2027 DownloadInfo download_info = { | 2068 DownloadInfo download_info = { |
2028 "a_zip_file.zip", | 2069 "a_zip_file.zip", |
2029 DOWNLOAD_NAVIGATE, | 2070 DOWNLOAD_NAVIGATE, |
2030 content::DOWNLOAD_INTERRUPT_REASON_NONE, | 2071 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
2031 true | 2072 true, |
| 2073 false |
2032 }; | 2074 }; |
2033 | 2075 |
2034 // Do initial setup. | 2076 // Do initial setup. |
2035 ASSERT_TRUE(InitialSetup(false)); | 2077 ASSERT_TRUE(InitialSetup(false)); |
2036 DownloadFileCheckErrors(download_info); | 2078 DownloadFileCheckErrors(download_info); |
2037 } | 2079 } |
2038 | 2080 |
2039 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDirect) { | 2081 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDirect) { |
2040 DownloadInfo download_info = { | 2082 DownloadInfo download_info = { |
2041 "a_zip_file.zip", | 2083 "a_zip_file.zip", |
2042 DOWNLOAD_DIRECT, | 2084 DOWNLOAD_DIRECT, |
2043 content::DOWNLOAD_INTERRUPT_REASON_NONE, | 2085 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
2044 true | 2086 true, |
| 2087 false |
2045 }; | 2088 }; |
2046 | 2089 |
2047 // Do initial setup. | 2090 // Do initial setup. |
2048 ASSERT_TRUE(InitialSetup(false)); | 2091 ASSERT_TRUE(InitialSetup(false)); |
2049 DownloadFileCheckErrors(download_info); | 2092 DownloadFileCheckErrors(download_info); |
2050 } | 2093 } |
2051 | 2094 |
2052 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadError404Direct) { | 2095 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadError404Direct) { |
2053 DownloadInfo download_info = { | 2096 DownloadInfo download_info = { |
2054 "there_IS_no_spoon.zip", | 2097 "there_IS_no_spoon.zip", |
2055 DOWNLOAD_DIRECT, | 2098 DOWNLOAD_DIRECT, |
2056 content::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT, | 2099 content::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT, |
2057 true | 2100 true, |
| 2101 false |
2058 }; | 2102 }; |
2059 | 2103 |
2060 // Do initial setup. | 2104 // Do initial setup. |
2061 ASSERT_TRUE(InitialSetup(false)); | 2105 ASSERT_TRUE(InitialSetup(false)); |
2062 DownloadFileCheckErrors(download_info); | 2106 DownloadFileCheckErrors(download_info); |
2063 } | 2107 } |
2064 | 2108 |
2065 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadError404Navigate) { | 2109 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadError404Navigate) { |
2066 DownloadInfo download_info = { | 2110 DownloadInfo download_info = { |
2067 "there_IS_no_spoon.zip", | 2111 "there_IS_no_spoon.zip", |
2068 DOWNLOAD_NAVIGATE, | 2112 DOWNLOAD_NAVIGATE, |
2069 content::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT, | 2113 content::DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT, |
| 2114 false, |
2070 false | 2115 false |
2071 }; | 2116 }; |
2072 | 2117 |
2073 // Do initial setup. | 2118 // Do initial setup. |
2074 ASSERT_TRUE(InitialSetup(false)); | 2119 ASSERT_TRUE(InitialSetup(false)); |
2075 DownloadFileCheckErrors(download_info); | 2120 DownloadFileCheckErrors(download_info); |
2076 } | 2121 } |
2077 | 2122 |
2078 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadError400Direct) { | 2123 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadError400Direct) { |
2079 DownloadInfo download_info = { | 2124 DownloadInfo download_info = { |
2080 "zip_file_not_found.zip", | 2125 "zip_file_not_found.zip", |
2081 DOWNLOAD_DIRECT, | 2126 DOWNLOAD_DIRECT, |
2082 content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, | 2127 content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, |
2083 true | 2128 true, |
| 2129 false |
2084 }; | 2130 }; |
2085 | 2131 |
2086 // Do initial setup. | 2132 // Do initial setup. |
2087 ASSERT_TRUE(InitialSetup(false)); | 2133 ASSERT_TRUE(InitialSetup(false)); |
2088 DownloadFileCheckErrors(download_info); | 2134 DownloadFileCheckErrors(download_info); |
2089 } | 2135 } |
2090 | 2136 |
2091 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadError400Navigate) { | 2137 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadError400Navigate) { |
2092 DownloadInfo download_info = { | 2138 DownloadInfo download_info = { |
2093 "zip_file_not_found.zip", | 2139 "zip_file_not_found.zip", |
2094 DOWNLOAD_NAVIGATE, | 2140 DOWNLOAD_NAVIGATE, |
2095 content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, | 2141 content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED, |
| 2142 false, |
2096 false | 2143 false |
2097 }; | 2144 }; |
2098 | 2145 |
2099 // Do initial setup. | 2146 // Do initial setup. |
2100 ASSERT_TRUE(InitialSetup(false)); | 2147 ASSERT_TRUE(InitialSetup(false)); |
2101 DownloadFileCheckErrors(download_info); | 2148 DownloadFileCheckErrors(download_info); |
2102 } | 2149 } |
| 2150 |
| 2151 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadErrorReadonlyFolderDirect) { |
| 2152 DownloadInfo download_info = { |
| 2153 "a_zip_file.zip", |
| 2154 DOWNLOAD_DIRECT, |
| 2155 // This passes because we switch to the My Documents folder. |
| 2156 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 2157 true, |
| 2158 true |
| 2159 }; |
| 2160 |
| 2161 DownloadFileToReadonlyFolder(download_info); |
| 2162 } |
| 2163 |
| 2164 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadErrorReadonlyFolderNavigate) { |
| 2165 DownloadInfo download_info = { |
| 2166 "a_zip_file.zip", |
| 2167 DOWNLOAD_NAVIGATE, |
| 2168 // This passes because we switch to the My Documents folder. |
| 2169 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 2170 true, |
| 2171 true |
| 2172 }; |
| 2173 |
| 2174 DownloadFileToReadonlyFolder(download_info); |
| 2175 } |
OLD | NEW |