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 "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
10 #include "chrome/browser/download/download_history.h" | 10 #include "chrome/browser/download/download_history.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
37 #endif | 37 #endif |
38 | 38 |
39 class SavePageBrowserTest : public InProcessBrowserTest { | 39 class SavePageBrowserTest : public InProcessBrowserTest { |
40 protected: | 40 protected: |
41 void SetUp() { | 41 void SetUp() { |
42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); | 42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); |
43 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 43 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
44 InProcessBrowserTest::SetUp(); | 44 InProcessBrowserTest::SetUp(); |
45 } | 45 } |
46 | 46 |
47 GURL Navigate(const std::string& prefix) { | |
Paweł Hajdan Jr.
2011/08/15 17:16:00
nit: Let's be more precise: NavigateToMockURL.
achuithb
2011/08/15 22:39:05
Done.
| |
48 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
49 FilePath(kTestDir).AppendASCII(prefix + ".htm")); | |
50 ui_test_utils::NavigateToURL(browser(), url); | |
51 return url; | |
52 } | |
53 | |
54 void GetPaths(const std::string& prefix, | |
Paweł Hajdan Jr.
2011/08/15 17:16:00
nit: Make the method name more precise: _what_ pat
achuithb
2011/08/15 22:39:05
I've renamed this to GetDestinationPaths and added
| |
55 FilePath* full_file_name, | |
56 FilePath* dir) { | |
57 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); | |
58 *dir = save_dir_.path().AppendASCII(prefix + "_files"); | |
59 } | |
60 | |
61 TabContents* GetCurrentTab() const { | |
62 TabContents* current_tab = browser()->GetSelectedTabContents(); | |
63 EXPECT_TRUE(current_tab); | |
64 return current_tab; | |
65 } | |
66 | |
67 | |
47 GURL WaitForSavePackageToFinish() const { | 68 GURL WaitForSavePackageToFinish() const { |
48 ui_test_utils::TestNotificationObserver observer; | 69 ui_test_utils::TestNotificationObserver observer; |
49 ui_test_utils::RegisterAndWait(&observer, | 70 ui_test_utils::RegisterAndWait(&observer, |
50 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 71 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
51 NotificationService::AllSources()); | 72 NotificationService::AllSources()); |
52 return Details<DownloadItem>(observer.details()).ptr()->original_url(); | 73 return Details<DownloadItem>(observer.details()).ptr()->original_url(); |
53 } | 74 } |
54 | 75 |
55 #if defined(OS_CHROMEOS) | 76 #if defined(OS_CHROMEOS) |
56 const ActiveDownloadsUI::DownloadList& GetDownloads() const { | 77 const ActiveDownloadsUI::DownloadList& GetDownloads() const { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 std::vector<DownloadHistoryInfo> history_entries_; | 166 std::vector<DownloadHistoryInfo> history_entries_; |
146 | 167 |
147 // Path to directory containing test data. | 168 // Path to directory containing test data. |
148 FilePath test_dir_; | 169 FilePath test_dir_; |
149 | 170 |
150 // Temporary directory we will save pages to. | 171 // Temporary directory we will save pages to. |
151 ScopedTempDir save_dir_; | 172 ScopedTempDir save_dir_; |
152 }; | 173 }; |
153 | 174 |
154 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 175 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
155 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 176 GURL url = Navigate("a"); |
156 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
157 FilePath(kTestDir).Append(file_name)); | |
158 ui_test_utils::NavigateToURL(browser(), url); | |
159 | 177 |
160 TabContents* current_tab = browser()->GetSelectedTabContents(); | 178 FilePath full_file_name, dir; |
161 ASSERT_TRUE(current_tab); | 179 GetPaths("a", &full_file_name, &dir); |
162 | 180 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
163 FilePath full_file_name = save_dir_.path().Append(file_name); | 181 SavePackage::SAVE_AS_ONLY_HTML)); |
164 FilePath dir = save_dir_.path().AppendASCII("a_files"); | |
165 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | |
166 SavePackage::SAVE_AS_ONLY_HTML)); | |
167 | 182 |
168 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 183 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
169 | 184 |
170 CheckDownloadUI(full_file_name); | 185 CheckDownloadUI(full_file_name); |
171 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. | 186 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. |
172 | 187 |
173 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 188 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
174 EXPECT_FALSE(file_util::PathExists(dir)); | 189 EXPECT_FALSE(file_util::PathExists(dir)); |
175 EXPECT_TRUE(file_util::ContentsEqual( | 190 EXPECT_TRUE(file_util::ContentsEqual( |
176 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 191 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
177 full_file_name)); | 192 full_file_name)); |
178 } | 193 } |
179 | 194 |
180 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { | 195 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { |
181 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 196 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
182 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( | 197 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( |
183 FilePath(kTestDir).Append(file_name)); | 198 FilePath(kTestDir).Append(file_name)); |
184 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( | 199 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( |
185 FilePath(kTestDir).Append(file_name)); | 200 FilePath(kTestDir).Append(file_name)); |
186 ui_test_utils::NavigateToURL(browser(), view_source_url); | 201 ui_test_utils::NavigateToURL(browser(), view_source_url); |
187 | 202 |
188 TabContents* current_tab = browser()->GetSelectedTabContents(); | 203 FilePath full_file_name, dir; |
189 ASSERT_TRUE(current_tab); | 204 GetPaths("a", &full_file_name, &dir); |
190 | 205 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
191 FilePath full_file_name = save_dir_.path().Append(file_name); | 206 SavePackage::SAVE_AS_ONLY_HTML)); |
192 FilePath dir = save_dir_.path().AppendASCII("a_files"); | |
193 | |
194 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | |
195 SavePackage::SAVE_AS_ONLY_HTML)); | |
196 | 207 |
197 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); | 208 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); |
198 | 209 |
199 CheckDownloadUI(full_file_name); | 210 CheckDownloadUI(full_file_name); |
200 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file. | 211 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file. |
201 | 212 |
202 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 213 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
203 EXPECT_FALSE(file_util::PathExists(dir)); | 214 EXPECT_FALSE(file_util::PathExists(dir)); |
204 EXPECT_TRUE(file_util::ContentsEqual( | 215 EXPECT_TRUE(file_util::ContentsEqual( |
205 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 216 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
206 full_file_name)); | 217 full_file_name)); |
207 } | 218 } |
208 | 219 |
209 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { | 220 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { |
210 FilePath file_name(FILE_PATH_LITERAL("b.htm")); | 221 GURL url = Navigate("b"); |
211 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
212 FilePath(kTestDir).Append(file_name)); | |
213 ui_test_utils::NavigateToURL(browser(), url); | |
214 | 222 |
215 TabContents* current_tab = browser()->GetSelectedTabContents(); | 223 FilePath full_file_name, dir; |
216 ASSERT_TRUE(current_tab); | 224 GetPaths("b", &full_file_name, &dir); |
217 | 225 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
218 FilePath full_file_name = save_dir_.path().Append(file_name); | 226 SavePackage::SAVE_AS_COMPLETE_HTML)); |
219 FilePath dir = save_dir_.path().AppendASCII("b_files"); | |
220 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | |
221 SavePackage::SAVE_AS_COMPLETE_HTML)); | |
222 | 227 |
223 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 228 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
224 | 229 |
225 CheckDownloadUI(full_file_name); | 230 CheckDownloadUI(full_file_name); |
226 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. | 231 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. |
227 | 232 |
228 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 233 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
229 EXPECT_TRUE(file_util::PathExists(dir)); | 234 EXPECT_TRUE(file_util::PathExists(dir)); |
230 EXPECT_TRUE(file_util::TextContentsEqual( | 235 EXPECT_TRUE(file_util::TextContentsEqual( |
231 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), | 236 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), |
232 full_file_name)); | 237 full_file_name)); |
233 EXPECT_TRUE(file_util::ContentsEqual( | 238 EXPECT_TRUE(file_util::ContentsEqual( |
234 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 239 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
235 dir.AppendASCII("1.png"))); | 240 dir.AppendASCII("1.png"))); |
236 EXPECT_TRUE(file_util::ContentsEqual( | 241 EXPECT_TRUE(file_util::ContentsEqual( |
237 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 242 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
238 dir.AppendASCII("1.css"))); | 243 dir.AppendASCII("1.css"))); |
239 } | 244 } |
240 | 245 |
241 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { | 246 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { |
242 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); | 247 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); |
243 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); | 248 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); |
244 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); | 249 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); |
245 } | 250 } |
246 | 251 |
247 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { | 252 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { |
248 FilePath file_name(FILE_PATH_LITERAL("b.htm")); | 253 GURL url = Navigate("b"); |
249 | |
250 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
251 FilePath(kTestDir).Append(file_name)); | |
252 ui_test_utils::NavigateToURL(browser(), url); | |
253 | 254 |
254 FilePath full_file_name = save_dir_.path().AppendASCII( | 255 FilePath full_file_name = save_dir_.path().AppendASCII( |
255 std::string("Test page for saving page feature") + kAppendedExtension); | 256 std::string("Test page for saving page feature") + kAppendedExtension); |
256 FilePath dir = save_dir_.path().AppendASCII( | 257 FilePath dir = save_dir_.path().AppendASCII( |
257 "Test page for saving page feature_files"); | 258 "Test page for saving page feature_files"); |
258 | 259 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
259 TabContents* current_tab = browser()->GetSelectedTabContents(); | 260 SavePackage::SAVE_AS_COMPLETE_HTML)); |
260 ASSERT_TRUE(current_tab); | |
261 | |
262 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | |
263 SavePackage::SAVE_AS_COMPLETE_HTML)); | |
264 | 261 |
265 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 262 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
266 | 263 |
267 CheckDownloadUI(full_file_name); | 264 CheckDownloadUI(full_file_name); |
268 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. | 265 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. |
269 | 266 |
270 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 267 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
271 EXPECT_TRUE(file_util::PathExists(dir)); | 268 EXPECT_TRUE(file_util::PathExists(dir)); |
272 EXPECT_TRUE(file_util::TextContentsEqual( | 269 EXPECT_TRUE(file_util::TextContentsEqual( |
273 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), | 270 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), |
274 full_file_name)); | 271 full_file_name)); |
275 EXPECT_TRUE(file_util::ContentsEqual( | 272 EXPECT_TRUE(file_util::ContentsEqual( |
276 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 273 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
277 dir.AppendASCII("1.png"))); | 274 dir.AppendASCII("1.png"))); |
278 EXPECT_TRUE(file_util::ContentsEqual( | 275 EXPECT_TRUE(file_util::ContentsEqual( |
279 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 276 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
280 dir.AppendASCII("1.css"))); | 277 dir.AppendASCII("1.css"))); |
281 } | 278 } |
282 | 279 |
283 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { | 280 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { |
284 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 281 GURL url = Navigate("a"); |
285 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
286 FilePath(kTestDir).Append(file_name)); | |
287 ui_test_utils::NavigateToURL(browser(), url); | |
288 | 282 |
289 TabContents* current_tab = browser()->GetSelectedTabContents(); | 283 FilePath full_file_name, dir; |
290 ASSERT_TRUE(current_tab); | 284 GetPaths("a", &full_file_name, &dir); |
291 | 285 ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
292 FilePath full_file_name = save_dir_.path().Append(file_name); | 286 SavePackage::SAVE_AS_ONLY_HTML)); |
293 FilePath dir = save_dir_.path().AppendASCII("a_files"); | |
294 ASSERT_TRUE(current_tab->SavePage(full_file_name, dir, | |
295 SavePackage::SAVE_AS_ONLY_HTML)); | |
296 | 287 |
297 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 288 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
298 | 289 |
299 CheckDownloadUI(full_file_name); | 290 CheckDownloadUI(full_file_name); |
300 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. | 291 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. |
301 | 292 |
302 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); | 293 EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1); |
303 | 294 |
304 #if defined(OS_CHROMEOS) | 295 #if defined(OS_CHROMEOS) |
305 EXPECT_EQ(GetDownloads().size(), 0U); | 296 EXPECT_EQ(GetDownloads().size(), 0U); |
306 #endif | 297 #endif |
307 | 298 |
308 // Should not be in history. | 299 // Should not be in history. |
309 QueryDownloadHistory(); | 300 QueryDownloadHistory(); |
310 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), | 301 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), |
311 DownloadHistoryInfoMatch(url, full_file_name, 1)), | 302 DownloadHistoryInfoMatch(url, full_file_name, 1)), |
312 history_entries_.end()); | 303 history_entries_.end()); |
313 | 304 |
314 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 305 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
315 EXPECT_FALSE(file_util::PathExists(dir)); | 306 EXPECT_FALSE(file_util::PathExists(dir)); |
316 EXPECT_TRUE(file_util::ContentsEqual( | 307 EXPECT_TRUE(file_util::ContentsEqual( |
317 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 308 test_dir_.Append(FilePath(kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), |
318 full_file_name)); | 309 full_file_name)); |
319 } | 310 } |
320 | 311 |
312 // Create a SavePackage and delete it without calling Init. | |
313 // SavePackage dtor has various asserts/checks that should not fire. | |
314 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, ImplicitCancel) { | |
315 GURL url = Navigate("a"); | |
316 FilePath full_file_name, dir; | |
317 GetPaths("a", &full_file_name, &dir); | |
318 scoped_refptr<SavePackage> save_package(new SavePackage(GetCurrentTab(), | |
319 SavePackage::SAVE_AS_ONLY_HTML, full_file_name, dir)); | |
321 } | 320 } |
321 | |
322 // Create a SavePackage, call Cancel, then delete it. | |
323 // SavePackage dtor has various asserts/checks that should not fire. | |
324 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, ExplicitCancel) { | |
325 GURL url = Navigate("a"); | |
326 FilePath full_file_name, dir; | |
327 GetPaths("a", &full_file_name, &dir); | |
328 scoped_refptr<SavePackage> save_package(new SavePackage(GetCurrentTab(), | |
329 SavePackage::SAVE_AS_ONLY_HTML, full_file_name, dir)); | |
330 save_package->Cancel(true); | |
331 } | |
332 | |
333 } // namespace | |
OLD | NEW |