OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <shlwapi.h> | |
6 #include <sstream> | |
7 #include <string> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/file_util.h" | |
11 #include "base/path_service.h" | |
12 #include "base/string_util.h" | |
13 #include "chrome/browser/automation/url_request_mock_http_job.h" | |
14 #include "chrome/browser/automation/url_request_slow_download_job.h" | |
15 #include "chrome/common/chrome_constants.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "chrome/common/chrome_switches.h" | |
18 #include "chrome/test/ui/ui_test.h" | |
19 #include "chrome/test/automation/tab_proxy.h" | |
20 #include "chrome/test/automation/browser_proxy.h" | |
21 #include "net/url_request/url_request_unittest.h" | |
22 | |
23 using std::wstring; | |
24 | |
25 namespace { | |
26 | |
27 const wchar_t kDocRoot[] = L"chrome/test/data"; | |
28 | |
29 // Checks if the volume supports Alternate Data Streams. This is required for | |
30 // the Zone Identifier implementation. | |
31 bool VolumeSupportsADS(const std::wstring path) { | |
32 wchar_t drive[MAX_PATH] = {0}; | |
33 wcscpy_s(drive, MAX_PATH, path.c_str()); | |
34 | |
35 EXPECT_TRUE(PathStripToRootW(drive)); | |
36 | |
37 DWORD fs_flags = 0; | |
38 EXPECT_TRUE(GetVolumeInformationW(drive, NULL, 0, 0, NULL, &fs_flags, NULL, | |
39 0)); | |
40 | |
41 if (fs_flags & FILE_NAMED_STREAMS) | |
42 return true; | |
43 | |
44 return false; | |
45 } | |
46 | |
47 // Checks if the ZoneIdentifier is correctly set to "Internet" (3) | |
48 void CheckZoneIdentifier(const std::wstring full_path) { | |
49 const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | |
50 | |
51 std::wstring path = full_path + L":Zone.Identifier"; | |
52 HANDLE file = CreateFile(path.c_str(), GENERIC_READ, kShare, NULL, | |
53 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
54 ASSERT_TRUE(INVALID_HANDLE_VALUE != file); | |
55 | |
56 char buffer[100] = {0}; | |
57 DWORD read = 0; | |
58 ASSERT_TRUE(ReadFile(file, buffer, 100, &read, NULL)); | |
59 CloseHandle(file); | |
60 | |
61 const char kIdentifier[] = "[ZoneTransfer]\nZoneId=3"; | |
62 ASSERT_EQ(arraysize(kIdentifier), read); | |
63 | |
64 ASSERT_EQ(0, strcmp(kIdentifier, buffer)); | |
65 } | |
66 | |
67 class DownloadTest : public UITest { | |
68 protected: | |
69 DownloadTest() : UITest() {} | |
70 | |
71 void CleanUpDownload(const std::wstring& client_filename, | |
72 const std::wstring& server_filename) { | |
73 // Find the path on the client. | |
74 std::wstring file_on_client(download_prefix_); | |
75 file_on_client.append(client_filename); | |
76 EXPECT_PRED1(file_util::PathExists, file_on_client); | |
77 | |
78 // Find the path on the server. | |
79 std::wstring file_on_server; | |
80 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, | |
81 &file_on_server)); | |
82 file_on_server.append(L"\\"); | |
83 file_on_server.append(server_filename); | |
84 ASSERT_TRUE(file_util::PathExists(file_on_server)); | |
85 | |
86 // Check that we downloaded the file correctly. | |
87 EXPECT_PRED2(file_util::ContentsEqual, file_on_server, file_on_client); | |
88 | |
89 // Check if the Zone Identifier is correclty set. | |
90 if (VolumeSupportsADS(file_on_client)) | |
91 CheckZoneIdentifier(file_on_client); | |
92 | |
93 // Delete the client copy of the file. | |
94 EXPECT_PRED2(file_util::Delete, file_on_client, false); | |
95 } | |
96 | |
97 void CleanUpDownload(const std::wstring& file) { | |
98 CleanUpDownload(file, file); | |
99 } | |
100 | |
101 virtual void SetUp() { | |
102 UITest::SetUp(); | |
103 download_prefix_ = GetDownloadDirectory(); | |
104 download_prefix_ += file_util::kPathSeparator; | |
105 } | |
106 | |
107 protected: | |
108 void RunSizeTest(const wstring& url, | |
109 const wstring& expected_title_in_progress, | |
110 const wstring& expected_title_finished) { | |
111 { | |
112 EXPECT_EQ(1, GetTabCount()); | |
113 | |
114 NavigateToURL(GURL(url)); | |
115 // Downloads appear in the shelf | |
116 WaitUntilTabCount(1); | |
117 // TODO(tc): check download status text | |
118 | |
119 // Complete sending the request. We do this by loading a second URL in a | |
120 // separate tab. | |
121 scoped_ptr<BrowserProxy> window(automation()->GetBrowserWindow(0)); | |
122 EXPECT_TRUE(window->AppendTab(GURL( | |
123 URLRequestSlowDownloadJob::kFinishDownloadUrl))); | |
124 EXPECT_EQ(2, GetTabCount()); | |
125 // TODO(tc): check download status text | |
126 | |
127 // Make sure the download shelf is showing. | |
128 scoped_ptr<TabProxy> dl_tab(window->GetTab(0)); | |
129 ASSERT_TRUE(dl_tab.get()); | |
130 EXPECT_TRUE(WaitForDownloadShelfVisible(dl_tab.get())); | |
131 } | |
132 | |
133 std::wstring filename = file_util::GetFilenameFromPath(url); | |
134 EXPECT_PRED1(file_util::PathExists, download_prefix_ + filename); | |
135 | |
136 // Delete the file we just downloaded. | |
137 for (int i = 0; i < 10; ++i) { | |
138 if (file_util::Delete(download_prefix_ + filename, false)) | |
139 break; | |
140 Sleep(kWaitForActionMaxMsec / 10); | |
141 } | |
142 EXPECT_FALSE(file_util::PathExists(download_prefix_ + filename)); | |
143 } | |
144 | |
145 wstring download_prefix_; | |
146 }; | |
147 | |
148 } // namespace | |
149 | |
150 // Download a file with non-viewable content, verify that the | |
151 // download tab opened and the file exists. | |
152 TEST_F(DownloadTest, DownloadMimeType) { | |
153 wstring file = L"download-test1.lib"; | |
154 wstring expected_title = L"100% - " + file; | |
155 | |
156 EXPECT_EQ(1, GetTabCount()); | |
157 | |
158 NavigateToURL(URLRequestMockHTTPJob::GetMockUrl(file)); | |
159 // No new tabs created, downloads appear in the current tab's download shelf. | |
160 WaitUntilTabCount(1); | |
161 | |
162 // Wait until the file is downloaded. | |
163 Sleep(1000); | |
164 | |
165 CleanUpDownload(file); | |
166 | |
167 scoped_ptr<TabProxy> tab_proxy(GetActiveTab()); | |
168 ASSERT_TRUE(tab_proxy.get()); | |
169 EXPECT_TRUE(WaitForDownloadShelfVisible(tab_proxy.get())); | |
170 } | |
171 | |
172 // Access a file with a viewable mime-type, verify that a download | |
173 // did not initiate. | |
174 TEST_F(DownloadTest, NoDownload) { | |
175 wstring file = L"download-test2.html"; | |
176 wstring file_path = download_prefix_; | |
177 file_util::AppendToPath(&file_path, file); | |
178 | |
179 if (file_util::PathExists(file_path)) | |
180 ASSERT_TRUE(file_util::Delete(file_path, false)); | |
181 | |
182 EXPECT_EQ(1, GetTabCount()); | |
183 | |
184 NavigateToURL(URLRequestMockHTTPJob::GetMockUrl(file)); | |
185 WaitUntilTabCount(1); | |
186 | |
187 // Wait to see if the file will be downloaded. | |
188 Sleep(1000); | |
189 | |
190 EXPECT_FALSE(file_util::PathExists(file_path)); | |
191 if (file_util::PathExists(file_path)) | |
192 ASSERT_TRUE(file_util::Delete(file_path, false)); | |
193 | |
194 scoped_ptr<TabProxy> tab_proxy(GetActiveTab()); | |
195 ASSERT_TRUE(tab_proxy.get()); | |
196 EXPECT_FALSE(WaitForDownloadShelfVisible(tab_proxy.get())); | |
197 } | |
198 | |
199 // Download a 0-size file with a content-disposition header, verify that the | |
200 // download tab opened and the file exists as the filename specified in the | |
201 // header. This also ensures we properly handle empty file downloads. | |
202 TEST_F(DownloadTest, ContentDisposition) { | |
203 wstring file = L"download-test3.html"; | |
204 wstring download_file = L"download-test3-attachment.html"; | |
205 wstring expected_title = L"100% - " + download_file; | |
206 | |
207 EXPECT_EQ(1, GetTabCount()); | |
208 | |
209 NavigateToURL(URLRequestMockHTTPJob::GetMockUrl(file)); | |
210 WaitUntilTabCount(1); | |
211 | |
212 // Wait until the file is downloaded. | |
213 Sleep(1000); | |
214 | |
215 CleanUpDownload(download_file, file); | |
216 | |
217 // Ensure the download shelf is visible on the current tab. | |
218 scoped_ptr<TabProxy> tab_proxy(GetActiveTab()); | |
219 ASSERT_TRUE(tab_proxy.get()); | |
220 EXPECT_TRUE(WaitForDownloadShelfVisible(tab_proxy.get())); | |
221 } | |
222 | |
223 // UnknownSize and KnownSize are tests which depend on | |
224 // URLRequestSlowDownloadJob to serve content in a certain way. Data will be | |
225 // sent in two chunks where the first chunk is 35K and the second chunk is 10K. | |
226 // The test will first attempt to download a file; but the server will "pause" | |
227 // in the middle until the server receives a second request for | |
228 // "download-finish. At that time, the download will finish. | |
229 TEST_F(DownloadTest, UnknownSize) { | |
230 std::wstring url(URLRequestSlowDownloadJob::kUnknownSizeUrl); | |
231 std::wstring filename = file_util::GetFilenameFromPath(url); | |
232 RunSizeTest(url, L"32.0 KB - " + filename, L"100% - " + filename); | |
233 } | |
234 | |
235 // http://b/1158253 | |
236 TEST_F(DownloadTest, DISABLED_KnownSize) { | |
237 std::wstring url(URLRequestSlowDownloadJob::kKnownSizeUrl); | |
238 std::wstring filename = file_util::GetFilenameFromPath(url); | |
239 RunSizeTest(url, L"71% - " + filename, L"100% - " + filename); | |
240 } | |
241 | |
OLD | NEW |