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

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: Add ChooseSavableDirectory() and ScopedDefaultDownloadDirectory 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_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|, |default_downloads_dir| and |save_type|, respectively.
63 // If we call this method, we must call RestoreSaveDirectoryPrefs()
64 // after the test to restore the default folder prefs.
65 void ChangeSaveDirectoryPrefs(
66 Profile* profile,
67 const FilePath& website_save_dir,
68 const FilePath& download_save_dir,
69 const FilePath& default_downloads_dir,
70 const SavePackage::SavePackageType save_type) {
71 DCHECK(profile);
72 PrefService* prefs = profile->GetPrefs();
73
74 DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory));
75 prev_download_save_dir_ = prefs->GetFilePath(
76 prefs::kDownloadDefaultDirectory);
77
78 // Check whether the preference has the default folder for saving HTML.
79 // If not, initialize it with the default folder for downloaded files.
80 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) {
81 prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory,
82 prev_download_save_dir_,
83 PrefService::UNSYNCABLE_PREF);
84 }
85 prev_website_save_dir_ = prefs->GetFilePath(
86 prefs::kSaveFileDefaultDirectory);
87
88 DownloadPrefs* download_prefs =
89 profile->GetDownloadManager()->download_prefs();
90 prev_save_type_ =
91 static_cast<SavePackage::SavePackageType>
92 (download_prefs->save_file_type());
93
94 prefs->SetFilePath(
95 prefs::kSaveFileDefaultDirectory, website_save_dir);
96 prefs->SetFilePath(
97 prefs::kDownloadDefaultDirectory, download_save_dir);
98 override_default_downloads_.Override(default_downloads_dir);
99 prefs->SetInteger(prefs::kSaveFileType, save_type);
100 }
101
102 // Restores the default folder prefs.
103 void RestoreSaveDirectoryPrefs(Profile* profile) {
104 DCHECK(profile);
105 PrefService* prefs = profile->GetPrefs();
106 prefs->SetFilePath(
107 prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
108 prefs->SetFilePath(
109 prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
110 override_default_downloads_.UnOverride();
111 prefs->SetInteger(prefs::kSaveFileType, prev_save_type_);
112 }
113
50 // Path to directory containing test data. 114 // Path to directory containing test data.
51 FilePath test_dir_; 115 FilePath test_dir_;
52 116
53 // Temporary directory we will save pages to. 117 // Temporary directory we will save pages to.
54 ScopedTempDir save_dir_; 118 ScopedTempDir save_dir_;
119
120 // Temporarily stores the default folder prefs.
121 FilePath prev_website_save_dir_;
122 FilePath prev_download_save_dir_;
123 SavePackage::SavePackageType prev_save_type_;
124 download_util::ScopedDefaultDownloadDirectory override_default_downloads_;
55 }; 125 };
56 126
127 } // namespace
128
57 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 129 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
58 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 130 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
59 GURL url = URLRequestMockHTTPJob::GetMockUrl( 131 GURL url = URLRequestMockHTTPJob::GetMockUrl(
60 FilePath(kTestDir).Append(file_name)); 132 FilePath(kTestDir).Append(file_name));
61 ui_test_utils::NavigateToURL(browser(), url); 133 ui_test_utils::NavigateToURL(browser(), url);
62 134
63 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 135 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
64 ASSERT_TRUE(current_tab); 136 ASSERT_TRUE(current_tab);
65 137
66 FilePath full_file_name = save_dir_.path().Append(file_name); 138 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"), 206 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
135 full_file_name)); 207 full_file_name));
136 EXPECT_TRUE(file_util::ContentsEqual( 208 EXPECT_TRUE(file_util::ContentsEqual(
137 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 209 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
138 dir.AppendASCII("1.png"))); 210 dir.AppendASCII("1.png")));
139 EXPECT_TRUE(file_util::ContentsEqual( 211 EXPECT_TRUE(file_util::ContentsEqual(
140 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 212 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
141 dir.AppendASCII("1.css"))); 213 dir.AppendASCII("1.css")));
142 } 214 }
143 215
216 // Checks if an HTML page is saved to the default folder for saving HTML
217 // in the following situation:
218 // The default folder for saving HTML exists.
219 // The default folder for downloaded files exists.
220 // The user's "Downloads" folder exists.
221 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder1) {
222 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
223 GURL url = URLRequestMockHTTPJob::GetMockUrl(
224 FilePath(kTestDir).Append(file_name));
225 ui_test_utils::NavigateToURL(browser(), url);
226
227 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
228 ASSERT_TRUE(current_tab);
229 ASSERT_TRUE(current_tab->tab_contents());
230 ASSERT_TRUE(current_tab->tab_contents()->profile());
231
232 ScopedTempDir website_save_dir, download_save_dir, default_downloads_dir;
233 // Prepare the default folder for saving HTML.
234 ASSERT_TRUE(website_save_dir.CreateUniqueTempDir());
235 // Prepare the default folder for downloaded files.
236 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
237 // Prepare the user's "Downloads" folder.
238 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
239
240 // Changes the default prefs.
241 ChangeSaveDirectoryPrefs(
242 current_tab->tab_contents()->profile(),
243 website_save_dir.path(),
244 download_save_dir.path(),
245 default_downloads_dir.path(),
246 SavePackage::SAVE_AS_ONLY_HTML);
247
248 std::string title = UTF16ToASCII(
249 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
250 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
251 FilePath full_file_name =
252 website_save_dir.path().Append(FilePath(title + ".html"));
253
254 EXPECT_EQ(url, WaitForSavePackageToFinish());
255
256 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
257 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
258
259 // Is the file downloaded to the default folder for saving HTML?
260 EXPECT_TRUE(file_util::PathExists(full_file_name));
261 EXPECT_TRUE(file_util::ContentsEqual(
262 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
263 full_file_name));
264
265 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
266 }
267
268 // Checks if an HTML page is saved to the default folder for downloaded files
269 // in the following situation:
270 // The default folder for saving HTML does not exist.
271 // The default folder for downloaded files exists.
272 // The user's "Downloads" folder exists.
273 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder2) {
274 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
275 GURL url = URLRequestMockHTTPJob::GetMockUrl(
276 FilePath(kTestDir).Append(file_name));
277 ui_test_utils::NavigateToURL(browser(), url);
278
279 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
280 ASSERT_TRUE(current_tab);
281 ASSERT_TRUE(current_tab->tab_contents());
282 ASSERT_TRUE(current_tab->tab_contents()->profile());
283
284 ScopedTempDir download_save_dir, default_downloads_dir;
285 // Prepare the default folder for saving downloaded files.
286 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
287 // Prepare the user's "Downloads" folder.
288 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
289 // Prepare non-existent folder.
290 FilePath nonexistent_path("/tmp/koakuma_mikity_moemoe_nyannyan");
291 ASSERT_FALSE(file_util::PathExists(nonexistent_path));
292
293 // Changes the default prefs.
294 ChangeSaveDirectoryPrefs(
295 current_tab->tab_contents()->profile(),
296 nonexistent_path,
297 download_save_dir.path(),
298 default_downloads_dir.path(),
299 SavePackage::SAVE_AS_ONLY_HTML);
300
301 std::string title = UTF16ToASCII(
302 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
303 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
304 FilePath full_file_name =
305 download_save_dir.path().Append(FilePath(title + ".html"));
306
307 EXPECT_EQ(url, WaitForSavePackageToFinish());
308
309 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
310 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
311
312 // Is the file downloaded to the default folder for downloaded files?
313 EXPECT_TRUE(file_util::PathExists(full_file_name));
314 EXPECT_FALSE(file_util::PathExists(nonexistent_path));
315 EXPECT_TRUE(file_util::ContentsEqual(
316 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
317 full_file_name));
318
319 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
320 }
321
322 // Checks if an HTML page is saved to the user's "Downloads" directory
323 // in the following situation:
324 // The default folder for saving HTML does not exist.
325 // The default folder for downloaded files does not exist.
326 // The user's "Downloads" folder exists.
327 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder3) {
328 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
329 GURL url = URLRequestMockHTTPJob::GetMockUrl(
330 FilePath(kTestDir).Append(file_name));
331 ui_test_utils::NavigateToURL(browser(), url);
332
333 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
334 ASSERT_TRUE(current_tab);
335 ASSERT_TRUE(current_tab->tab_contents());
336 ASSERT_TRUE(current_tab->tab_contents()->profile());
337
338 ScopedTempDir default_downloads_dir;
339 // Prepare the user's "Downloads" folder.
340 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
341 // Prepare non-existent folder.
342 FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
343 FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
344 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
345 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
346
347 // Changes the default prefs.
348 ChangeSaveDirectoryPrefs(
349 current_tab->tab_contents()->profile(),
350 nonexistent_path1,
351 nonexistent_path2,
352 default_downloads_dir.path(),
353 SavePackage::SAVE_AS_ONLY_HTML);
354
355 std::string title = UTF16ToASCII(
356 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
357 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
358 FilePath full_file_name =
359 default_downloads_dir.path().Append(FilePath(title + ".html"));
360
361 EXPECT_EQ(url, WaitForSavePackageToFinish());
362
363 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
364 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
365
366 // Is the file downloaded to the user's "Downloads" directory?
367 EXPECT_TRUE(file_util::PathExists(full_file_name));
368 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
369 EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
370 EXPECT_TRUE(file_util::ContentsEqual(
371 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
372 full_file_name));
373
374 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
375 }
376
377 // Checks if the default folder for downloaded files is created
378 // and an HTML page is saved to the folder in the following situation:
379 // The default folder for saving HTML does not exist.
380 // The default folder for downloaded files does not exist.
381 // The user's "Downloads" folder does not exist.
382 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder4) {
383 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
384 GURL url = URLRequestMockHTTPJob::GetMockUrl(
385 FilePath(kTestDir).Append(file_name));
386 ui_test_utils::NavigateToURL(browser(), url);
387
388 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
389 ASSERT_TRUE(current_tab);
390 ASSERT_TRUE(current_tab->tab_contents());
391 ASSERT_TRUE(current_tab->tab_contents()->profile());
392
393 // Prepare non-existent folder.
394 FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
395 FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
396 FilePath nonexistent_path3("/tmp/koakuma_mikity_moemoe_kyunkyun");
397 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
398 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
399 ASSERT_FALSE(file_util::PathExists(nonexistent_path3));
400
401 // Changes the default prefs.
402 ChangeSaveDirectoryPrefs(
403 current_tab->tab_contents()->profile(),
404 nonexistent_path1,
405 nonexistent_path2,
406 nonexistent_path3,
407 SavePackage::SAVE_AS_ONLY_HTML);
408
409 std::string title = UTF16ToASCII(
410 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
411 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
412 FilePath full_file_name =
413 nonexistent_path2.Append(FilePath(title + ".html"));
414
415 EXPECT_EQ(url, WaitForSavePackageToFinish());
416
417 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
418 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
419
420 // Is the file downloaded to the default folder for downloaded files?
421 EXPECT_TRUE(file_util::PathExists(full_file_name));
422 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
423 EXPECT_TRUE(file_util::PathExists(nonexistent_path2));
424 EXPECT_FALSE(file_util::PathExists(nonexistent_path3));
425 EXPECT_TRUE(file_util::ContentsEqual(
426 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
427 full_file_name));
428
429 ASSERT_TRUE(file_util::Delete(nonexistent_path2, true));
430
431 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
432 }
433
144 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 434 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
145 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 435 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
146 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); 436 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
147 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); 437 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE));
148 } 438 }
149 439
150 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 440 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
151 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 441 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
152 442
153 GURL url = URLRequestMockHTTPJob::GetMockUrl( 443 GURL url = URLRequestMockHTTPJob::GetMockUrl(
(...skipping 21 matching lines...) Expand all
175 EXPECT_TRUE(file_util::TextContentsEqual( 465 EXPECT_TRUE(file_util::TextContentsEqual(
176 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 466 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
177 full_file_name)); 467 full_file_name));
178 EXPECT_TRUE(file_util::ContentsEqual( 468 EXPECT_TRUE(file_util::ContentsEqual(
179 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 469 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
180 dir.AppendASCII("1.png"))); 470 dir.AppendASCII("1.png")));
181 EXPECT_TRUE(file_util::ContentsEqual( 471 EXPECT_TRUE(file_util::ContentsEqual(
182 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 472 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
183 dir.AppendASCII("1.css"))); 473 dir.AppendASCII("1.css")));
184 } 474 }
185
186 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698