Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: chrome/browser/download/save_page_browsertest.cc

Issue 6973052: When the download folder does not exist, change the download folder to a user's "Downloads" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Overrides the user's "Downloads" folder in DownloadPrefs Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/i18n/file_util_icu.h"
7 #include "base/path_service.h" 8 #include "base/path_service.h"
8 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
10 #include "base/string_util.h"
9 #include "chrome/app/chrome_command_ids.h" 11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/download/download_manager.h"
13 #include "chrome/browser/download/download_prefs.h"
14 #include "chrome/browser/download/download_test_util.h"
10 #include "chrome/browser/net/url_request_mock_http_job.h" 15 #include "chrome/browser/net/url_request_mock_http_job.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h" 19 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/download/download_tab_helper.h" 20 #include "chrome/browser/ui/download/download_tab_helper.h"
14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 21 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
15 #include "chrome/common/chrome_paths.h" 22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h" 24 #include "chrome/common/url_constants.h"
17 #include "chrome/test/in_process_browser_test.h" 25 #include "chrome/test/in_process_browser_test.h"
18 #include "chrome/test/ui_test_utils.h" 26 #include "chrome/test/ui_test_utils.h"
19 #include "content/browser/tab_contents/tab_contents.h" 27 #include "content/browser/tab_contents/tab_contents.h"
20 #include "content/common/notification_service.h" 28 #include "content/common/notification_service.h"
21 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
22 30
23 namespace { 31 namespace {
24 32
25 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); 33 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page");
(...skipping 14 matching lines...) Expand all
40 } 48 }
41 49
42 GURL WaitForSavePackageToFinish() { 50 GURL WaitForSavePackageToFinish() {
43 ui_test_utils::TestNotificationObserver observer; 51 ui_test_utils::TestNotificationObserver observer;
44 ui_test_utils::RegisterAndWait(&observer, 52 ui_test_utils::RegisterAndWait(&observer,
45 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, 53 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
46 NotificationService::AllSources()); 54 NotificationService::AllSources());
47 return *Details<GURL>(observer.details()).ptr(); 55 return *Details<GURL>(observer.details()).ptr();
48 } 56 }
49 57
58 // Changes the default folder prefs. This method saves the current folder
59 // for saving HTML, the current folder for saving downloaded files,
60 // the current user's "Downloads" folder and a save type (HTML only or
61 // complete HTML files), and then changes them to |website_save_dir|,
62 // |download_save_dir| and |save_type|, respectively.
63 // If we call this method, we must call RestoreDirectoryPrefs()
64 // after the test to restore the default folder prefs.
65 void ChangeDirectoryPrefs(
66 Profile* profile,
67 const FilePath& website_save_dir,
68 const FilePath& download_save_dir,
69 const SavePackage::SavePackageType save_type) {
70 DCHECK(profile);
71 PrefService* prefs = profile->GetPrefs();
72
73 DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory));
74 prev_download_save_dir_ = prefs->GetFilePath(
75 prefs::kDownloadDefaultDirectory);
76
77 // Check whether the preference has the default folder for saving HTML.
78 // If not, initialize it with the default folder for downloaded files.
79 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) {
80 prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory,
81 prev_download_save_dir_,
82 PrefService::UNSYNCABLE_PREF);
83 }
84 prev_website_save_dir_ = prefs->GetFilePath(
85 prefs::kSaveFileDefaultDirectory);
86
87 DownloadPrefs* download_prefs =
88 profile->GetDownloadManager()->download_prefs();
89 prev_save_type_ =
90 static_cast<SavePackage::SavePackageType>
91 (download_prefs->save_file_type());
92
93 prefs->SetFilePath(
94 prefs::kSaveFileDefaultDirectory, website_save_dir);
95 prefs->SetFilePath(
96 prefs::kDownloadDefaultDirectory, download_save_dir);
97 prefs->SetInteger(prefs::kSaveFileType, save_type);
98 }
99
100 // Restores the default folder prefs.
101 void RestoreDirectoryPrefs(Profile* profile) {
102 DCHECK(profile);
103 PrefService* prefs = profile->GetPrefs();
104 prefs->SetFilePath(
105 prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
106 prefs->SetFilePath(
107 prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
108 prefs->SetInteger(prefs::kSaveFileType, prev_save_type_);
109 }
110
50 // Path to directory containing test data. 111 // Path to directory containing test data.
51 FilePath test_dir_; 112 FilePath test_dir_;
52 113
53 // Temporary directory we will save pages to. 114 // Temporary directory we will save pages to.
54 ScopedTempDir save_dir_; 115 ScopedTempDir save_dir_;
116
117 // Temporarily stores the default folder prefs.
118 FilePath prev_website_save_dir_;
119 FilePath prev_download_save_dir_;
120 SavePackage::SavePackageType prev_save_type_;
55 }; 121 };
56 122
123 } // namespace
124
57 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 125 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
58 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 126 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
59 GURL url = URLRequestMockHTTPJob::GetMockUrl( 127 GURL url = URLRequestMockHTTPJob::GetMockUrl(
60 FilePath(kTestDir).Append(file_name)); 128 FilePath(kTestDir).Append(file_name));
61 ui_test_utils::NavigateToURL(browser(), url); 129 ui_test_utils::NavigateToURL(browser(), url);
62 130
63 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 131 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
64 ASSERT_TRUE(current_tab); 132 ASSERT_TRUE(current_tab);
65 133
66 FilePath full_file_name = save_dir_.path().Append(file_name); 134 FilePath full_file_name = save_dir_.path().Append(file_name);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 202 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
135 full_file_name)); 203 full_file_name));
136 EXPECT_TRUE(file_util::ContentsEqual( 204 EXPECT_TRUE(file_util::ContentsEqual(
137 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 205 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
138 dir.AppendASCII("1.png"))); 206 dir.AppendASCII("1.png")));
139 EXPECT_TRUE(file_util::ContentsEqual( 207 EXPECT_TRUE(file_util::ContentsEqual(
140 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 208 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
141 dir.AppendASCII("1.css"))); 209 dir.AppendASCII("1.css")));
142 } 210 }
143 211
212 // Checks if an HTML page is saved to the default folder for saving HTML
213 // in the following situation:
214 // The default folder for saving HTML exists.
215 // The default folder for downloaded files exists.
216 // The user's "Downloads" folder exists.
217 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder1) {
218 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
219 GURL url = URLRequestMockHTTPJob::GetMockUrl(
220 FilePath(kTestDir).Append(file_name));
221 ui_test_utils::NavigateToURL(browser(), url);
222
223 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
224 ASSERT_TRUE(current_tab);
225 ASSERT_TRUE(current_tab->tab_contents());
226 ASSERT_TRUE(current_tab->tab_contents()->profile());
227
228 ScopedTempDir website_save_dir, download_save_dir, default_download_dir;
229 // Prepare the default folder for saving HTML.
230 ASSERT_TRUE(website_save_dir.CreateUniqueTempDir());
231 // Prepare the default folder for downloaded files.
232 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
233 // Prepare the user's "Downloads" folder.
234 ASSERT_TRUE(default_download_dir.CreateUniqueTempDir());
235
236 // Changes the default prefs.
237 ChangeDirectoryPrefs(
238 current_tab->tab_contents()->profile(),
239 website_save_dir.path(),
240 download_save_dir.path(),
241 SavePackage::SAVE_AS_ONLY_HTML);
242 // Overrides the user's "Downloads" folder.
243 download_test_util::ScopedDefaultDownloadDirectory
244 override_default_download(
245 default_download_dir.path(),
246 current_tab->tab_contents()->profile()->
247 GetDownloadManager()->download_prefs());
248
249 std::string title = UTF16ToASCII(
250 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
251 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
252 FilePath full_file_name =
253 website_save_dir.path().Append(FilePath(title + ".html"));
254
255 EXPECT_EQ(url, WaitForSavePackageToFinish());
256
257 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
258 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
259
260 // Is the file downloaded to the default folder for saving HTML?
261 EXPECT_TRUE(file_util::PathExists(full_file_name));
262 EXPECT_TRUE(file_util::ContentsEqual(
263 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
264 full_file_name));
265
266 RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
267 }
268
269 // Checks if an HTML page is saved to the default folder for downloaded files
270 // in the following situation:
271 // The default folder for saving HTML does not exist.
272 // The default folder for downloaded files exists.
273 // The user's "Downloads" folder exists.
274 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder2) {
275 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
276 GURL url = URLRequestMockHTTPJob::GetMockUrl(
277 FilePath(kTestDir).Append(file_name));
278 ui_test_utils::NavigateToURL(browser(), url);
279
280 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
281 ASSERT_TRUE(current_tab);
282 ASSERT_TRUE(current_tab->tab_contents());
283 ASSERT_TRUE(current_tab->tab_contents()->profile());
284
285 ScopedTempDir download_save_dir, default_download_dir;
286 // Prepare the default folder for saving downloaded files.
287 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
288 // Prepare the user's "Downloads" folder.
289 ASSERT_TRUE(default_download_dir.CreateUniqueTempDir());
290 // Prepare non-existent folder.
291 FilePath nonexistent_path("/tmp/koakuma_mikity_moemoe_nyannyan");
292 ASSERT_FALSE(file_util::PathExists(nonexistent_path));
293
294 // Changes the default prefs.
295 ChangeDirectoryPrefs(
296 current_tab->tab_contents()->profile(),
297 nonexistent_path,
298 download_save_dir.path(),
299 SavePackage::SAVE_AS_ONLY_HTML);
300 // Overrides the user's "Downloads" folder.
301 download_test_util::ScopedDefaultDownloadDirectory
302 override_default_download(
303 default_download_dir.path(),
304 current_tab->tab_contents()->profile()->
305 GetDownloadManager()->download_prefs());
306
307 std::string title = UTF16ToASCII(
308 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
309 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
310 FilePath full_file_name =
311 download_save_dir.path().Append(FilePath(title + ".html"));
312
313 EXPECT_EQ(url, WaitForSavePackageToFinish());
314
315 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
316 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
317
318 // Is the file downloaded to the default folder for downloaded files?
319 EXPECT_TRUE(file_util::PathExists(full_file_name));
320 EXPECT_FALSE(file_util::PathExists(nonexistent_path));
321 EXPECT_TRUE(file_util::ContentsEqual(
322 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
323 full_file_name));
324
325 RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
326 }
327
328 // Checks if an HTML page is saved to the user's "Downloads" directory
329 // in the following situation:
330 // The default folder for saving HTML does not exist.
331 // The default folder for downloaded files does not exist.
332 // The user's "Downloads" folder exists.
333 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder3) {
334 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
335 GURL url = URLRequestMockHTTPJob::GetMockUrl(
336 FilePath(kTestDir).Append(file_name));
337 ui_test_utils::NavigateToURL(browser(), url);
338
339 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
340 ASSERT_TRUE(current_tab);
341 ASSERT_TRUE(current_tab->tab_contents());
342 ASSERT_TRUE(current_tab->tab_contents()->profile());
343
344 ScopedTempDir default_download_dir;
345 // Prepare the user's "Downloads" folder.
346 ASSERT_TRUE(default_download_dir.CreateUniqueTempDir());
347 // Prepare non-existent folder.
348 FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
349 FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
350 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
351 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
352
353 // Changes the default prefs.
354 ChangeDirectoryPrefs(
355 current_tab->tab_contents()->profile(),
356 nonexistent_path1,
357 nonexistent_path2,
358 SavePackage::SAVE_AS_ONLY_HTML);
359 // Overrides the user's "Downloads" folder.
360 download_test_util::ScopedDefaultDownloadDirectory
361 override_default_download(
362 default_download_dir.path(),
363 current_tab->tab_contents()->profile()->
364 GetDownloadManager()->download_prefs());
365
366 std::string title = UTF16ToASCII(
367 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
368 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
369 FilePath full_file_name =
370 default_download_dir.path().Append(FilePath(title + ".html"));
371
372 EXPECT_EQ(url, WaitForSavePackageToFinish());
373
374 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
375 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
376
377 // Is the file downloaded to the user's "Downloads" directory?
378 EXPECT_TRUE(file_util::PathExists(full_file_name));
379 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
380 EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
381 EXPECT_TRUE(file_util::ContentsEqual(
382 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
383 full_file_name));
384
385 RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
386 }
387
388 // Checks if the default folder for downloaded files is created
389 // and an HTML page is saved to the folder in the following situation:
390 // The default folder for saving HTML does not exist.
391 // The default folder for downloaded files does not exist.
392 // The user's "Downloads" folder does not exist.
393 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder4) {
haraken1 2011/06/14 11:10:05 IMPORTANT: This test fails in this patch by the fo
Randy Smith (Not in Mondays) 2011/06/15 19:48:59 I'm torn. That's fine behavior, but there will be
haraken1 2011/06/22 18:01:58 I removed SavePageBrowserTest.SaveFolder4 and Down
394 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
395 GURL url = URLRequestMockHTTPJob::GetMockUrl(
396 FilePath(kTestDir).Append(file_name));
397 ui_test_utils::NavigateToURL(browser(), url);
398
399 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
400 ASSERT_TRUE(current_tab);
401 ASSERT_TRUE(current_tab->tab_contents());
402 ASSERT_TRUE(current_tab->tab_contents()->profile());
403
404 // Prepare non-existent folder.
405 FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
406 FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
407 FilePath nonexistent_path3("/tmp/koakuma_mikity_moemoe_kyunkyun");
408 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
409 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
410 ASSERT_FALSE(file_util::PathExists(nonexistent_path3));
411
412 // Changes the default prefs.
413 ChangeDirectoryPrefs(
414 current_tab->tab_contents()->profile(),
415 nonexistent_path1,
416 nonexistent_path2,
417 SavePackage::SAVE_AS_ONLY_HTML);
418 // Overrides the user's "Downloads" folder.
419 download_test_util::ScopedDefaultDownloadDirectory
420 override_default_download(
421 nonexistent_path3,
422 current_tab->tab_contents()->profile()->
423 GetDownloadManager()->download_prefs());
424
425 std::string title = UTF16ToASCII(
426 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
427 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
428 FilePath full_file_name =
429 nonexistent_path2.Append(FilePath(title + ".html"));
430
431 EXPECT_EQ(url, WaitForSavePackageToFinish());
432
433 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
434 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
435
436 // Is the file downloaded to the default folder for downloaded files?
437 EXPECT_TRUE(file_util::PathExists(full_file_name));
438 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
439 EXPECT_TRUE(file_util::PathExists(nonexistent_path2));
440 EXPECT_FALSE(file_util::PathExists(nonexistent_path3));
441 EXPECT_TRUE(file_util::ContentsEqual(
442 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
443 full_file_name));
444
445 ASSERT_TRUE(file_util::Delete(nonexistent_path2, true));
446
447 RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
448 }
449
144 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 450 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
145 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 451 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
146 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); 452 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
147 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); 453 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE));
148 } 454 }
149 455
150 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 456 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
151 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 457 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
152 458
153 GURL url = URLRequestMockHTTPJob::GetMockUrl( 459 GURL url = URLRequestMockHTTPJob::GetMockUrl(
(...skipping 21 matching lines...) Expand all
175 EXPECT_TRUE(file_util::TextContentsEqual( 481 EXPECT_TRUE(file_util::TextContentsEqual(
176 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 482 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
177 full_file_name)); 483 full_file_name));
178 EXPECT_TRUE(file_util::ContentsEqual( 484 EXPECT_TRUE(file_util::ContentsEqual(
179 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 485 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
180 dir.AppendASCII("1.png"))); 486 dir.AppendASCII("1.png")));
181 EXPECT_TRUE(file_util::ContentsEqual( 487 EXPECT_TRUE(file_util::ContentsEqual(
182 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 488 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
183 dir.AppendASCII("1.css"))); 489 dir.AppendASCII("1.css")));
184 } 490 }
185
186 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698