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

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: Remove PathServiceWrapper and add DefaultDownloadDirectory 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 download_util::DefaultDownloadDirectory::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 download_util::DefaultDownloadDirectory::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_;
55 }; 124 };
56 125
126 } // namespace
127
57 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 128 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
58 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 129 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
59 GURL url = URLRequestMockHTTPJob::GetMockUrl( 130 GURL url = URLRequestMockHTTPJob::GetMockUrl(
60 FilePath(kTestDir).Append(file_name)); 131 FilePath(kTestDir).Append(file_name));
61 ui_test_utils::NavigateToURL(browser(), url); 132 ui_test_utils::NavigateToURL(browser(), url);
62 133
63 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 134 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
64 ASSERT_TRUE(current_tab); 135 ASSERT_TRUE(current_tab);
65 136
66 FilePath full_file_name = save_dir_.path().Append(file_name); 137 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"), 205 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
135 full_file_name)); 206 full_file_name));
136 EXPECT_TRUE(file_util::ContentsEqual( 207 EXPECT_TRUE(file_util::ContentsEqual(
137 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 208 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
138 dir.AppendASCII("1.png"))); 209 dir.AppendASCII("1.png")));
139 EXPECT_TRUE(file_util::ContentsEqual( 210 EXPECT_TRUE(file_util::ContentsEqual(
140 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 211 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
141 dir.AppendASCII("1.css"))); 212 dir.AppendASCII("1.css")));
142 } 213 }
143 214
215 // Checks if an HTML page is saved to the default folder for saving HTML
216 // in the following situation:
217 // The default folder for saving HTML exists.
218 // The default folder for downloaded files exists.
219 // The user's "Downloads" folder exists.
220 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder1) {
221 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
222 GURL url = URLRequestMockHTTPJob::GetMockUrl(
223 FilePath(kTestDir).Append(file_name));
224 ui_test_utils::NavigateToURL(browser(), url);
225
226 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
227 ASSERT_TRUE(current_tab);
228 ASSERT_TRUE(current_tab->tab_contents());
229 ASSERT_TRUE(current_tab->tab_contents()->profile());
230
231 ScopedTempDir website_save_dir, download_save_dir, default_downloads_dir;
232 // Prepare the default folder for saving HTML.
233 ASSERT_TRUE(website_save_dir.CreateUniqueTempDir());
234 // Prepare the default folder for downloaded files.
235 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
236 // Prepare the user's "Downloads" folder.
237 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
238
239 // Changes the default prefs.
240 ChangeSaveDirectoryPrefs(
241 current_tab->tab_contents()->profile(),
242 website_save_dir.path(),
243 download_save_dir.path(),
244 default_downloads_dir.path(),
245 SavePackage::SAVE_AS_ONLY_HTML);
246
247 std::string title = UTF16ToASCII(
248 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
249 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
250 FilePath full_file_name =
251 website_save_dir.path().Append(FilePath(title + ".html"));
252
253 EXPECT_EQ(url, WaitForSavePackageToFinish());
254
255 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
256 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
257
258 // Is the file downloaded to the default folder for saving HTML?
259 EXPECT_TRUE(file_util::PathExists(full_file_name));
260 EXPECT_TRUE(file_util::ContentsEqual(
261 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
262 full_file_name));
263
264 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
265 }
266
267 // Checks if an HTML page is saved to the default folder for downloaded files
268 // in the following situation:
269 // The default folder for saving HTML does not exist.
270 // The default folder for downloaded files exists.
271 // The user's "Downloads" folder exists.
272 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder2) {
273 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
274 GURL url = URLRequestMockHTTPJob::GetMockUrl(
275 FilePath(kTestDir).Append(file_name));
276 ui_test_utils::NavigateToURL(browser(), url);
277
278 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
279 ASSERT_TRUE(current_tab);
280 ASSERT_TRUE(current_tab->tab_contents());
281 ASSERT_TRUE(current_tab->tab_contents()->profile());
282
283 ScopedTempDir download_save_dir, default_downloads_dir;
284 // Prepare the default folder for saving downloaded files.
285 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
286 // Prepare the user's "Downloads" folder.
287 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
288 // Prepare non-existent folder.
289 FilePath nonexistent_path("/tmp/koakuma_mikity_moemoe_nyannyan");
290 ASSERT_FALSE(file_util::PathExists(nonexistent_path));
291
292 // Changes the default prefs.
293 ChangeSaveDirectoryPrefs(
294 current_tab->tab_contents()->profile(),
295 nonexistent_path,
296 download_save_dir.path(),
297 default_downloads_dir.path(),
298 SavePackage::SAVE_AS_ONLY_HTML);
299
300 std::string title = UTF16ToASCII(
301 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
302 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
303 FilePath full_file_name =
304 download_save_dir.path().Append(FilePath(title + ".html"));
305
306 EXPECT_EQ(url, WaitForSavePackageToFinish());
307
308 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
309 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
310
311 // Is the file downloaded to the default folder for downloaded files?
312 EXPECT_TRUE(file_util::PathExists(full_file_name));
313 EXPECT_FALSE(file_util::PathExists(nonexistent_path));
314 EXPECT_TRUE(file_util::ContentsEqual(
315 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
316 full_file_name));
317
318 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
319 }
320
321 // Checks if an HTML page is saved to the user's "Downloads" directory
322 // in the following situation:
323 // The default folder for saving HTML does not exist.
324 // The default folder for downloaded files does not exist.
325 // The user's "Downloads" folder exists.
326 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder3) {
327 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
328 GURL url = URLRequestMockHTTPJob::GetMockUrl(
329 FilePath(kTestDir).Append(file_name));
330 ui_test_utils::NavigateToURL(browser(), url);
331
332 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
333 ASSERT_TRUE(current_tab);
334 ASSERT_TRUE(current_tab->tab_contents());
335 ASSERT_TRUE(current_tab->tab_contents()->profile());
336
337 ScopedTempDir default_downloads_dir;
338 // Prepare the user's "Downloads" folder.
339 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
340 // Prepare non-existent folder.
341 FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
342 FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
343 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
344 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
345
346 // Changes the default prefs.
347 ChangeSaveDirectoryPrefs(
348 current_tab->tab_contents()->profile(),
349 nonexistent_path1,
350 nonexistent_path2,
351 default_downloads_dir.path(),
352 SavePackage::SAVE_AS_ONLY_HTML);
353
354 std::string title = UTF16ToASCII(
355 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
356 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
357 FilePath full_file_name =
358 default_downloads_dir.path().Append(FilePath(title + ".html"));
359
360 EXPECT_EQ(url, WaitForSavePackageToFinish());
361
362 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
363 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
364
365 // Is the file downloaded to the user's "Downloads" directory?
366 EXPECT_TRUE(file_util::PathExists(full_file_name));
367 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
368 EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
369 EXPECT_TRUE(file_util::ContentsEqual(
370 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
371 full_file_name));
372
373 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
374 }
375
376 // Checks if the default folder for downloaded files is created
377 // and an HTML page is saved to the folder in the following situation:
378 // The default folder for saving HTML does not exist.
379 // The default folder for downloaded files does not exist.
380 // The user's "Downloads" folder does not exist.
381 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder4) {
382 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
383 GURL url = URLRequestMockHTTPJob::GetMockUrl(
384 FilePath(kTestDir).Append(file_name));
385 ui_test_utils::NavigateToURL(browser(), url);
386
387 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
388 ASSERT_TRUE(current_tab);
389 ASSERT_TRUE(current_tab->tab_contents());
390 ASSERT_TRUE(current_tab->tab_contents()->profile());
391
392 // Prepare non-existent folder.
393 FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
394 FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
395 FilePath nonexistent_path3("/tmp/koakuma_mikity_moemoe_kyunkyun");
396 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
397 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
398 ASSERT_FALSE(file_util::PathExists(nonexistent_path3));
399
400 // Changes the default prefs.
401 ChangeSaveDirectoryPrefs(
402 current_tab->tab_contents()->profile(),
403 nonexistent_path1,
404 nonexistent_path2,
405 nonexistent_path3,
406 SavePackage::SAVE_AS_ONLY_HTML);
407
408 std::string title = UTF16ToASCII(
409 current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
410 file_util::ReplaceIllegalCharactersInPath(&title, ' ');
411 FilePath full_file_name =
412 nonexistent_path2.Append(FilePath(title + ".html"));
413
414 EXPECT_EQ(url, WaitForSavePackageToFinish());
415
416 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
417 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
418
419 // Is the file downloaded to the default folder for downloaded files?
420 EXPECT_TRUE(file_util::PathExists(full_file_name));
421 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
422 EXPECT_TRUE(file_util::PathExists(nonexistent_path2));
423 EXPECT_FALSE(file_util::PathExists(nonexistent_path3));
424 EXPECT_TRUE(file_util::ContentsEqual(
425 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
426 full_file_name));
427
428 ASSERT_TRUE(file_util::Delete(nonexistent_path2, true));
429
430 RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
431 }
432
144 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 433 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
145 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 434 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
146 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); 435 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
147 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); 436 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE));
148 } 437 }
149 438
150 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 439 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
151 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 440 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
152 441
153 GURL url = URLRequestMockHTTPJob::GetMockUrl( 442 GURL url = URLRequestMockHTTPJob::GetMockUrl(
(...skipping 21 matching lines...) Expand all
175 EXPECT_TRUE(file_util::TextContentsEqual( 464 EXPECT_TRUE(file_util::TextContentsEqual(
176 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 465 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
177 full_file_name)); 466 full_file_name));
178 EXPECT_TRUE(file_util::ContentsEqual( 467 EXPECT_TRUE(file_util::ContentsEqual(
179 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 468 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
180 dir.AppendASCII("1.png"))); 469 dir.AppendASCII("1.png")));
181 EXPECT_TRUE(file_util::ContentsEqual( 470 EXPECT_TRUE(file_util::ContentsEqual(
182 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 471 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
183 dir.AppendASCII("1.css"))); 472 dir.AppendASCII("1.css")));
184 } 473 }
185
186 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698