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

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 SavePageTest.SavedFolder4 and DownloadTest.DownloadedFolder2 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"
10 #include "chrome/browser/net/url_request_mock_http_job.h" 14 #include "chrome/browser/net/url_request_mock_http_job.h"
15 #include "chrome/browser/prefs/pref_service.h"
16 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h" 18 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/download/download_tab_helper.h" 19 #include "chrome/browser/ui/download/download_tab_helper.h"
14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
15 #include "chrome/common/chrome_paths.h" 21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h" 23 #include "chrome/common/url_constants.h"
17 #include "chrome/test/in_process_browser_test.h" 24 #include "chrome/test/in_process_browser_test.h"
18 #include "chrome/test/ui_test_utils.h" 25 #include "chrome/test/ui_test_utils.h"
19 #include "content/browser/tab_contents/tab_contents.h" 26 #include "content/browser/tab_contents/tab_contents.h"
20 #include "content/common/notification_service.h" 27 #include "content/common/notification_service.h"
21 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
22 29
23 namespace { 30 namespace {
24 31
25 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); 32 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page");
(...skipping 14 matching lines...) Expand all
40 } 47 }
41 48
42 GURL WaitForSavePackageToFinish() { 49 GURL WaitForSavePackageToFinish() {
43 ui_test_utils::TestNotificationObserver observer; 50 ui_test_utils::TestNotificationObserver observer;
44 ui_test_utils::RegisterAndWait(&observer, 51 ui_test_utils::RegisterAndWait(&observer,
45 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, 52 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
46 NotificationService::AllSources()); 53 NotificationService::AllSources());
47 return *Details<GURL>(observer.details()).ptr(); 54 return *Details<GURL>(observer.details()).ptr();
48 } 55 }
49 56
57 // Changes the default folder prefs. This method saves the current folder
58 // for saving HTML, the current folder for saving downloaded files,
59 // the current user's "Downloads" folder and a save type (HTML only or
60 // complete HTML files), and then changes them to |website_save_dir|,
61 // |download_save_dir|, |default_downloads_dir| and |save_type|, respectively.
62 // If we call this method, we must call RestoreSaveDirectoryPrefs()
63 // after the test to restore the default folder prefs.
64 void ChangeSaveDirectoryPrefs(
65 Profile* profile,
66 const FilePath& website_save_dir,
67 const FilePath& download_save_dir,
68 const FilePath& default_downloads_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 if (!PathService::Get(
88 chrome::DIR_DEFAULT_DOWNLOADS, &prev_default_downloads_dir_)) {
89 LOG(ERROR) << "Cannot find the user's \"Downloads\" folder.";
90 prev_default_downloads_dir_.clear();
91 }
92
93 DownloadPrefs* download_prefs =
94 profile->GetDownloadManager()->download_prefs();
95 prev_save_type_ =
96 static_cast<SavePackage::SavePackageType>
97 (download_prefs->save_file_type());
98
99 prefs->SetFilePath(
100 prefs::kSaveFileDefaultDirectory, website_save_dir);
101 prefs->SetFilePath(
102 prefs::kDownloadDefaultDirectory, download_save_dir);
103 PathService::Set(chrome::DIR_DEFAULT_DOWNLOADS, default_downloads_dir);
104 prefs->SetInteger(prefs::kSaveFileType, save_type);
105 }
106
107 // Restores the default folder prefs.
108 void RestoreSaveDirectoryPrefs(Profile* profile) {
109 DCHECK(profile);
110 PrefService* prefs = profile->GetPrefs();
111 prefs->SetFilePath(
112 prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
113 prefs->SetFilePath(
114 prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
115 // Restores DIR_DEFAULT_DOWNLOADS only when we could get
116 // |prev_default_downloads_dir_| successfully in ChangeSaveDirectoryPrefs().
117 if (!prev_default_downloads_dir_.empty())
118 PathService::Set(
119 chrome::DIR_DEFAULT_DOWNLOADS, prev_default_downloads_dir_);
120 prefs->SetInteger(prefs::kSaveFileType, prev_save_type_);
121 }
122
50 // Path to directory containing test data. 123 // Path to directory containing test data.
51 FilePath test_dir_; 124 FilePath test_dir_;
52 125
53 // Temporary directory we will save pages to. 126 // Temporary directory we will save pages to.
54 ScopedTempDir save_dir_; 127 ScopedTempDir save_dir_;
128
129 // Temporarily stores the default folder prefs.
130 FilePath prev_website_save_dir_;
131 FilePath prev_download_save_dir_;
132 FilePath prev_default_downloads_dir_;
133 SavePackage::SavePackageType prev_save_type_;
55 }; 134 };
56 135
136 } // namespace
137
57 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 138 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
58 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 139 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
59 GURL url = URLRequestMockHTTPJob::GetMockUrl( 140 GURL url = URLRequestMockHTTPJob::GetMockUrl(
60 FilePath(kTestDir).Append(file_name)); 141 FilePath(kTestDir).Append(file_name));
61 ui_test_utils::NavigateToURL(browser(), url); 142 ui_test_utils::NavigateToURL(browser(), url);
62 143
63 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 144 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
64 ASSERT_TRUE(current_tab); 145 ASSERT_TRUE(current_tab);
65 146
66 FilePath full_file_name = save_dir_.path().Append(file_name); 147 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"), 215 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
135 full_file_name)); 216 full_file_name));
136 EXPECT_TRUE(file_util::ContentsEqual( 217 EXPECT_TRUE(file_util::ContentsEqual(
137 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 218 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
138 dir.AppendASCII("1.png"))); 219 dir.AppendASCII("1.png")));
139 EXPECT_TRUE(file_util::ContentsEqual( 220 EXPECT_TRUE(file_util::ContentsEqual(
140 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 221 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
141 dir.AppendASCII("1.css"))); 222 dir.AppendASCII("1.css")));
142 } 223 }
143 224
225 // Checks if an HTML page is saved to the default folder for saving HTML
226 // in the following situation:
227 // The default folder for saving HTML exists.
228 // The default folder for downloaded files exists.
229 // The user's "Downloads" folder exists.
230 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder1) {
231 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
232 GURL url = URLRequestMockHTTPJob::GetMockUrl(
233 FilePath(kTestDir).Append(file_name));
234 ui_test_utils::NavigateToURL(browser(), url);
235
236 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
237 ASSERT_TRUE(current_tab);
238 ASSERT_TRUE(current_tab->tab_contents());
239 ASSERT_TRUE(current_tab->tab_contents()->profile());
240
241 ScopedTempDir website_save_dir, download_save_dir, default_downloads_dir;
242 // Prepare the default folder for saving HTML.
243 ASSERT_TRUE(website_save_dir.CreateUniqueTempDir());
244 // Prepare the default folder for downloaded files.
245 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
246 // Prepare the user's "Downloads" folder.
247 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
248
249 // Changes the default prefs.
250 ChangeSaveDirectoryPrefs(
251 current_tab->tab_contents()->profile(),
252 website_save_dir.path(),
253 download_save_dir.path(),
254 default_downloads_dir.path(),
255 SavePackage::SAVE_AS_ONLY_HTML);
256
257 std::string title = UTF16ToASCII(
258 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
259 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
260 FilePath full_file_name =
261 website_save_dir.path().Append(FilePath(title + ".html"));
262
263 EXPECT_EQ(url, WaitForSavePackageToFinish());
264
265 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
266 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
267
268 // Is the file downloaded to the default folder for saving HTML?
269 EXPECT_TRUE(file_util::PathExists(full_file_name));
270 EXPECT_TRUE(file_util::ContentsEqual(
271 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
272 full_file_name));
273
274 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
275 }
276
277 // Checks if an HTML page is saved to the default folder for downloaded files
278 // in the following situation:
279 // The default folder for saving HTML does not exist.
280 // The default folder for downloaded files exists.
281 // The user's "Downloads" folder exists.
282 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder2) {
283 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
284 GURL url = URLRequestMockHTTPJob::GetMockUrl(
285 FilePath(kTestDir).Append(file_name));
286 ui_test_utils::NavigateToURL(browser(), url);
287
288 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
289 ASSERT_TRUE(current_tab);
290 ASSERT_TRUE(current_tab->tab_contents());
291 ASSERT_TRUE(current_tab->tab_contents()->profile());
292
293 ScopedTempDir download_save_dir, default_downloads_dir;
294 // Prepare the default folder for saving downloaded files.
295 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
296 // Prepare the user's "Downloads" folder.
297 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
298 // Prepare non-existent folder.
299 FilePath nonexistent_path("/tmp/koakuma_mikity_moemoe_nyannyan");
300 ASSERT_FALSE(file_util::PathExists(nonexistent_path));
301
302 // Changes the default prefs.
303 ChangeSaveDirectoryPrefs(
304 current_tab->tab_contents()->profile(),
305 nonexistent_path,
306 download_save_dir.path(),
307 default_downloads_dir.path(),
308 SavePackage::SAVE_AS_ONLY_HTML);
309
310 std::string title = UTF16ToASCII(
311 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
312 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
313 FilePath full_file_name =
314 download_save_dir.path().Append(FilePath(title + ".html"));
315
316 EXPECT_EQ(url, WaitForSavePackageToFinish());
317
318 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
319 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
320
321 // Is the file downloaded to the default folder for downloaded files?
322 EXPECT_TRUE(file_util::PathExists(full_file_name));
323 EXPECT_FALSE(file_util::PathExists(nonexistent_path));
324 EXPECT_TRUE(file_util::ContentsEqual(
325 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
326 full_file_name));
327
328 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
329 }
330
331 // Checks if an HTML page is saved to the user's "Downloads" directory
332 // in the following situation:
333 // The default folder for saving HTML does not exist.
334 // The default folder for downloaded files does not exist.
335 // The user's "Downloads" folder exists.
336 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder3) {
337 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
338 GURL url = URLRequestMockHTTPJob::GetMockUrl(
339 FilePath(kTestDir).Append(file_name));
340 ui_test_utils::NavigateToURL(browser(), url);
341
342 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
343 ASSERT_TRUE(current_tab);
344 ASSERT_TRUE(current_tab->tab_contents());
345 ASSERT_TRUE(current_tab->tab_contents()->profile());
346
347 ScopedTempDir default_downloads_dir;
348 // Prepare the user's "Downloads" folder.
349 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
350 // Prepare non-existent folder.
351 FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
352 FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
353 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
354 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
355
356 // Changes the default prefs.
357 ChangeSaveDirectoryPrefs(
358 current_tab->tab_contents()->profile(),
359 nonexistent_path1,
360 nonexistent_path2,
361 default_downloads_dir.path(),
362 SavePackage::SAVE_AS_ONLY_HTML);
363
364 std::string title = UTF16ToASCII(
365 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
366 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
367 FilePath full_file_name =
368 default_downloads_dir.path().Append(FilePath(title + ".html"));
369
370 EXPECT_EQ(url, WaitForSavePackageToFinish());
371
372 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
373 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
374
375 // Is the file downloaded to the user's "Downloads" directory?
376 EXPECT_TRUE(file_util::PathExists(full_file_name));
377 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
378 EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
379 EXPECT_TRUE(file_util::ContentsEqual(
380 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
381 full_file_name));
382
383 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
384 }
385
386 // Checks if the default folder for downloaded files is created
387 // and an HTML page is saved to the folder in the following situation:
388 // The default folder for saving HTML does not exist.
389 // The default folder for downloaded files does not exist.
390 // The user's "Downloads" folder does not exist.
391 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder4) {
392 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
393 GURL url = URLRequestMockHTTPJob::GetMockUrl(
394 FilePath(kTestDir).Append(file_name));
395 ui_test_utils::NavigateToURL(browser(), url);
396
397 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
398 ASSERT_TRUE(current_tab);
399 ASSERT_TRUE(current_tab->tab_contents());
400 ASSERT_TRUE(current_tab->tab_contents()->profile());
401
402 // Prepare non-existent folder.
403 FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
404 FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
405 FilePath nonexistent_path3("/tmp/koakuma_mikity_moemoe_kyankyan");
406 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
407 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
408 ASSERT_FALSE(file_util::PathExists(nonexistent_path3));
409
410 // Changes the default prefs.
411 ChangeSaveDirectoryPrefs(
412 current_tab->tab_contents()->profile(),
413 nonexistent_path1,
414 nonexistent_path2,
415 nonexistent_path3,
416 SavePackage::SAVE_AS_ONLY_HTML);
417
418 std::string title = UTF16ToASCII(
419 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
420 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
421 FilePath full_file_name =
422 nonexistent_path2.Append(FilePath(title + ".html"));
423
424 EXPECT_EQ(url, WaitForSavePackageToFinish());
425
426 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
427 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
428
429 // Is the file downloaded to the default folder for downloaded files?
430 EXPECT_TRUE(file_util::PathExists(full_file_name));
431 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
432 EXPECT_TRUE(file_util::PathExists(nonexistent_path2));
433 EXPECT_FALSE(file_util::PathExists(nonexistent_path3));
434 EXPECT_TRUE(file_util::ContentsEqual(
435 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
436 full_file_name));
437
438 ASSERT_TRUE(file_util::Delete(nonexistent_path2, true));
439
440 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
441 }
442
144 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 443 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
145 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 444 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
146 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); 445 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
147 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); 446 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE));
148 } 447 }
149 448
150 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 449 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
151 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 450 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
152 451
153 GURL url = URLRequestMockHTTPJob::GetMockUrl( 452 GURL url = URLRequestMockHTTPJob::GetMockUrl(
(...skipping 21 matching lines...) Expand all
175 EXPECT_TRUE(file_util::TextContentsEqual( 474 EXPECT_TRUE(file_util::TextContentsEqual(
176 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 475 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
177 full_file_name)); 476 full_file_name));
178 EXPECT_TRUE(file_util::ContentsEqual( 477 EXPECT_TRUE(file_util::ContentsEqual(
179 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 478 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
180 dir.AppendASCII("1.png"))); 479 dir.AppendASCII("1.png")));
181 EXPECT_TRUE(file_util::ContentsEqual( 480 EXPECT_TRUE(file_util::ContentsEqual(
182 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 481 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
183 dir.AppendASCII("1.css"))); 482 dir.AppendASCII("1.css")));
184 } 483 }
185
186 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698