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

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: Added URLRequestMockHTTPJob.test_dir_ and URLRequestMockHTTPJob.temp_dir_ Created 9 years, 4 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"
11 #include "base/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h" 12 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/download/download_item.h" 13 #include "chrome/browser/download/download_item.h"
14 #include "chrome/browser/download/download_manager.h"
15 #include "chrome/browser/download/download_prefs.h"
16 #include "chrome/browser/download/download_util.h"
17 #include "chrome/browser/prefs/pref_service.h"
18 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h" 20 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/webui/active_downloads_ui.h" 21 #include "chrome/browser/ui/webui/active_downloads_ui.h"
14 #include "chrome/common/chrome_paths.h" 22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/pref_names.h"
15 #include "chrome/common/url_constants.h" 24 #include "chrome/common/url_constants.h"
16 #include "chrome/test/in_process_browser_test.h" 25 #include "chrome/test/in_process_browser_test.h"
17 #include "chrome/test/ui_test_utils.h" 26 #include "chrome/test/ui_test_utils.h"
18 #include "content/browser/net/url_request_mock_http_job.h" 27 #include "content/browser/net/url_request_mock_http_job.h"
19 #include "content/browser/tab_contents/tab_contents.h" 28 #include "content/browser/tab_contents/tab_contents.h"
20 #include "content/common/content_notification_types.h" 29 #include "content/common/content_notification_types.h"
21 #include "content/common/notification_service.h" 30 #include "content/common/notification_service.h"
22 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
23 32
24 namespace { 33 namespace {
25 34
26 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); 35 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page");
27 36
37 // The contents of a downloaded file.
38 static const char kContentsOfTestHTML[] =
39 "<html><head><title>test page</title></head><body>test</body></html>";
40
28 static const char* kAppendedExtension = 41 static const char* kAppendedExtension =
29 #if defined(OS_WIN) 42 #if defined(OS_WIN)
30 ".htm"; 43 ".htm";
31 #else 44 #else
32 ".html"; 45 ".html";
33 #endif 46 #endif
34 47
35 class SavePageBrowserTest : public InProcessBrowserTest { 48 class SavePageBrowserTest : public InProcessBrowserTest {
36 protected: 49 protected:
37 void SetUp() { 50 void SetUp() {
(...skipping 27 matching lines...) Expand all
65 found = true; 78 found = true;
66 break; 79 break;
67 } 80 }
68 } 81 }
69 EXPECT_TRUE(found); 82 EXPECT_TRUE(found);
70 #else 83 #else
71 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 84 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
72 #endif 85 #endif
73 } 86 }
74 87
88 // Changes the default folder prefs. This method saves the current folder
89 // for saving HTML, the current folder for saving downloaded files,
90 // the current user's "Downloads" folder and a save type (HTML only or
91 // complete HTML files), and then changes them to |website_save_dir|,
92 // |download_save_dir| and |save_type|, respectively.
93 // If we call this method, we must call RestoreDirectoryPrefs()
94 // after the test to restore the default folder prefs.
95 void ChangeDirectoryPrefs(
96 Profile* profile,
97 const FilePath& website_save_dir,
98 const FilePath& download_save_dir,
99 const SavePackage::SavePackageType save_type) {
100 DCHECK(profile);
101 PrefService* prefs = profile->GetPrefs();
102
103 DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory));
104 prev_download_save_dir_ = prefs->GetFilePath(
105 prefs::kDownloadDefaultDirectory);
106
107 // Check whether the preference has the default folder for saving HTML.
108 // If not, initialize it with the default folder for downloaded files.
109 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) {
110 prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory,
111 prev_download_save_dir_,
112 PrefService::UNSYNCABLE_PREF);
113 }
114 prev_website_save_dir_ = prefs->GetFilePath(
115 prefs::kSaveFileDefaultDirectory);
116
117 DownloadPrefs* download_prefs =
118 profile->GetDownloadManager()->download_prefs();
119 prev_save_type_ =
120 static_cast<SavePackage::SavePackageType>
121 (download_prefs->save_file_type());
122
123 prefs->SetFilePath(
124 prefs::kSaveFileDefaultDirectory, website_save_dir);
125 prefs->SetFilePath(
126 prefs::kDownloadDefaultDirectory, download_save_dir);
127 prefs->SetInteger(prefs::kSaveFileType, save_type);
128 }
129
130 // Restores the default folder prefs.
131 void RestoreDirectoryPrefs(Profile* profile) {
132 DCHECK(profile);
133 PrefService* prefs = profile->GetPrefs();
134 prefs->SetFilePath(
135 prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
136 prefs->SetFilePath(
137 prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
138 prefs->SetInteger(prefs::kSaveFileType, prev_save_type_);
139 }
140
75 // Path to directory containing test data. 141 // Path to directory containing test data.
76 FilePath test_dir_; 142 FilePath test_dir_;
77 143
78 // Temporary directory we will save pages to. 144 // Temporary directory we will save pages to.
79 ScopedTempDir save_dir_; 145 ScopedTempDir save_dir_;
146
147 // Temporarily stores the default folder prefs.
148 FilePath prev_website_save_dir_;
149 FilePath prev_download_save_dir_;
150 SavePackage::SavePackageType prev_save_type_;
80 }; 151 };
81 152
153 } // namespace
154
82 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 155 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
83 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 156 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
84 GURL url = URLRequestMockHTTPJob::GetMockUrl( 157 GURL url = URLRequestMockHTTPJob::GetMockUrl(
85 FilePath(kTestDir).Append(file_name)); 158 FilePath(kTestDir).Append(file_name));
86 ui_test_utils::NavigateToURL(browser(), url); 159 ui_test_utils::NavigateToURL(browser(), url);
87 160
88 TabContents* current_tab = browser()->GetSelectedTabContents(); 161 TabContents* current_tab = browser()->GetSelectedTabContents();
89 ASSERT_TRUE(current_tab); 162 ASSERT_TRUE(current_tab);
90 163
91 FilePath full_file_name = save_dir_.path().Append(file_name); 164 FilePath full_file_name = save_dir_.path().Append(file_name);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 229 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
157 full_file_name)); 230 full_file_name));
158 EXPECT_TRUE(file_util::ContentsEqual( 231 EXPECT_TRUE(file_util::ContentsEqual(
159 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 232 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
160 dir.AppendASCII("1.png"))); 233 dir.AppendASCII("1.png")));
161 EXPECT_TRUE(file_util::ContentsEqual( 234 EXPECT_TRUE(file_util::ContentsEqual(
162 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 235 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
163 dir.AppendASCII("1.css"))); 236 dir.AppendASCII("1.css")));
164 } 237 }
165 238
239 // Checks if an HTML page is saved to the default folder for saving HTML
240 // in the following situation:
241 // The default folder for saving HTML exists.
242 // The default folder for downloaded files exists.
243 // The user's "Downloads" folder exists.
244 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder1) {
245 FilePath file(FILE_PATH_LITERAL("a.htm"));
246 GURL url = URLRequestMockHTTPJob::GetMockUrl(
247 FilePath(kTestDir).Append(file));
248 ui_test_utils::NavigateToURL(browser(), url);
249
250 TabContents* current_tab = browser()->GetSelectedTabContents();
251 ASSERT_TRUE(current_tab);
252 ASSERT_TRUE(current_tab->profile());
253
254 ScopedTempDir website_save_dir, download_save_dir;
255 // Prepare the default folder for saving HTML.
256 ASSERT_TRUE(website_save_dir.CreateUniqueTempDir());
257 // Prepare the default folder for downloaded files.
258 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
259
260 // Changes the default prefs.
261 ChangeDirectoryPrefs(
262 current_tab->profile(),
263 website_save_dir.path(),
264 download_save_dir.path(),
265 SavePackage::SAVE_AS_ONLY_HTML);
266
267 string16 title = current_tab->SavePageBasedOnDefaultPrefs();
268 FilePath::StringType basename;
269 #if defined(OS_WIN)
270 basename = UTF16ToWide(title);
271 basename.append(FILE_PATH_LITERAL(".htm"));
272 #else
273 basename = UTF16ToASCII(title);
274 basename.append(FILE_PATH_LITERAL(".html"));
275 #endif
276 file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
277 FilePath downloaded_file = website_save_dir.path().Append(FilePath(basename));
278
279 EXPECT_EQ(url, WaitForSavePackageToFinish());
280
281 CheckDownloadUI(downloaded_file);
282
283 // Is the file downloaded to the default folder for saving HTML?
284 EXPECT_TRUE(file_util::PathExists(downloaded_file));
285 EXPECT_TRUE(file_util::ContentsEqual(
286 test_dir_.Append(FilePath(kTestDir)).Append(file),
287 downloaded_file));
288
289 RestoreDirectoryPrefs(current_tab->profile());
290 }
291
292 // Checks if an HTML page is saved to the default folder for downloaded files
293 // in the following situation:
294 // The default folder for saving HTML does not exist.
295 // The default folder for downloaded files exists.
296 // The user's "Downloads" folder exists.
297 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder2) {
298 FilePath file(FILE_PATH_LITERAL("a.htm"));
299 GURL url = URLRequestMockHTTPJob::GetMockUrl(
300 FilePath(kTestDir).Append(file));
301 ui_test_utils::NavigateToURL(browser(), url);
302
303 TabContents* current_tab = browser()->GetSelectedTabContents();
304 ASSERT_TRUE(current_tab);
305 ASSERT_TRUE(current_tab->profile());
306
307 ScopedTempDir download_save_dir;
308 // Prepare the default folder for saving downloaded files.
309 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
310 // Prepare non-existent folder.
311 FilePath nonexistent_path(
312 FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan"));
313 ASSERT_FALSE(file_util::PathExists(nonexistent_path));
314
315 // Changes the default prefs.
316 ChangeDirectoryPrefs(
317 current_tab->profile(),
318 nonexistent_path,
319 download_save_dir.path(),
320 SavePackage::SAVE_AS_ONLY_HTML);
321
322 string16 title = current_tab->SavePageBasedOnDefaultPrefs();
323 FilePath::StringType basename;
324 #if defined(OS_WIN)
325 basename = UTF16ToWide(title);
326 basename.append(FILE_PATH_LITERAL(".htm"));
327 #else
328 basename = UTF16ToASCII(title);
329 basename.append(FILE_PATH_LITERAL(".html"));
330 #endif
331 file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
332 FilePath downloaded_file =
333 download_save_dir.path().Append(FilePath(basename));
334
335 EXPECT_EQ(url, WaitForSavePackageToFinish());
336
337 CheckDownloadUI(downloaded_file);
338
339 // Is the file downloaded to the default folder for downloaded files?
340 EXPECT_TRUE(file_util::PathExists(downloaded_file));
341 EXPECT_FALSE(file_util::PathExists(nonexistent_path));
342 EXPECT_TRUE(file_util::ContentsEqual(
343 test_dir_.Append(FilePath(kTestDir)).Append(file),
344 downloaded_file));
345
346 RestoreDirectoryPrefs(current_tab->profile());
347 }
348
349 // Checks if an HTML page is saved to the user's "Downloads" directory
350 // in the following situation:
351 // The default folder for saving HTML does not exist.
352 // The default folder for downloaded files does not exist.
353 // The user's "Downloads" folder exists.
354 //
355 // This test creates and deletes a file on the user's "Downloads" folder,
356 // which is globally shared among all tests on the environment. Therefore,
357 // if we run browser tests in parallel, the file created by one browser test
358 // may be deleted by another brower test in case where the file name conflicts.
359 // In order to avoid this problem, we create a temporary file with a unique name
360 // in chrome/test/data/ folder and use the temporary file for this test.
361 //
362 // Ideally, in the first place, we should not use the user's "Downloads" folder.
363 // Instead, we should create a temporary "Downloads" folder for each test.
364 // However, we concluded that creating temporary "Downloads" folder for each
365 // test requires very invasive code changes to many places. See also here:
366 // http://codereview.chromium.org/6973052/
367 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder3) {
368 ScopedTempDir scoped_temp_dir;
369 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
370 // Creates a temporary file with a unique name.
371 FilePath unique_file;
372 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
373 scoped_temp_dir.path(), &unique_file));
374 // Adds "test" prefix to the temporary file, so that the file name starts
375 // with an ascii character and thus the file is handled as a normal file.
376 // Adds ".htm" extension to the temporary file, so that the file is
377 // handled as an HTML file.
378 FilePath file = unique_file.DirName().
379 Append(FILE_PATH_LITERAL("test") + unique_file.BaseName().value() +
380 FILE_PATH_LITERAL(".htm"));
381 ASSERT_TRUE(file_util::Move(unique_file, file));
382 ASSERT_TRUE(file_util::WriteFile(file,
383 kContentsOfTestHTML,
384 ARRAYSIZE_UNSAFE(kContentsOfTestHTML)));
385 GURL url(URLRequestMockHTTPJob::GetMockUrlOfTempDir(
386 scoped_temp_dir.path().BaseName().Append(file.BaseName())));
387 ui_test_utils::NavigateToURL(browser(), url);
388
389 TabContents* current_tab = browser()->GetSelectedTabContents();
390 ASSERT_TRUE(current_tab);
391 ASSERT_TRUE(current_tab->profile());
392
393 // Prepare non-existent folder.
394 FilePath nonexistent_path1(
395 FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan"));
396 FilePath nonexistent_path2(
397 FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_pyonpyon"));
398 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
399 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
400
401 // Changes the default prefs.
402 ChangeDirectoryPrefs(
403 current_tab->profile(),
404 nonexistent_path1,
405 nonexistent_path2,
406 SavePackage::SAVE_AS_ONLY_HTML);
407
408 string16 title = current_tab->SavePageBasedOnDefaultPrefs();
409 FilePath::StringType basename;
410 #if defined(OS_WIN)
411 basename = UTF16ToWide(title);
412 basename.append(FILE_PATH_LITERAL(".htm"));
413 #else
414 basename = UTF16ToASCII(title);
415 basename.append(FILE_PATH_LITERAL(".html"));
416 #endif
417 file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
418 FilePath default_download_dir =
419 download_util::GetDefaultDownloadDirectoryFromPathService();
420 FilePath downloaded_file =
421 default_download_dir.Append(FilePath(basename));
422 // Make sure that the target file does not exist.
423 file_util::Delete(downloaded_file, false);
424 // Make sure that the temporary file does not exist.
425 FilePath temporary_file =
426 downloaded_file.Append(FILE_PATH_LITERAL(".crdownload"));
427 file_util::Delete(temporary_file, false);
428
429 EXPECT_EQ(url, WaitForSavePackageToFinish());
430
431 CheckDownloadUI(downloaded_file);
432
433 // Is the file downloaded to the user's "Downloads" directory?
434 EXPECT_TRUE(file_util::PathExists(downloaded_file));
435 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
436 EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
437 EXPECT_TRUE(file_util::ContentsEqual(file, downloaded_file));
438
439 // Clean up the generated files.
440 file_util::Delete(downloaded_file, false);
441 file_util::Delete(temporary_file, false);
442
443 RestoreDirectoryPrefs(current_tab->profile());
444 }
445
166 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 446 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
167 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 447 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
168 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); 448 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
169 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); 449 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE));
170 } 450 }
171 451
172 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 452 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
173 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 453 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
174 454
175 GURL url = URLRequestMockHTTPJob::GetMockUrl( 455 GURL url = URLRequestMockHTTPJob::GetMockUrl(
(...skipping 20 matching lines...) Expand all
196 EXPECT_TRUE(file_util::TextContentsEqual( 476 EXPECT_TRUE(file_util::TextContentsEqual(
197 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 477 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
198 full_file_name)); 478 full_file_name));
199 EXPECT_TRUE(file_util::ContentsEqual( 479 EXPECT_TRUE(file_util::ContentsEqual(
200 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 480 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
201 dir.AppendASCII("1.png"))); 481 dir.AppendASCII("1.png")));
202 EXPECT_TRUE(file_util::ContentsEqual( 482 EXPECT_TRUE(file_util::ContentsEqual(
203 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 483 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
204 dir.AppendASCII("1.css"))); 484 dir.AppendASCII("1.css")));
205 } 485 }
206
207 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698