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

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

Issue 7324031: Revert 91861 - When the download folder does not exist, change the download folder to a user's "D... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 5 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"
8 #include "base/path_service.h" 7 #include "base/path_service.h"
9 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
10 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/download/download_item.h" 10 #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"
19 #include "chrome/browser/ui/browser.h" 11 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_window.h" 12 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/download/download_tab_helper.h" 13 #include "chrome/browser/ui/download/download_tab_helper.h"
22 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
23 #include "chrome/browser/ui/webui/active_downloads_ui.h" 15 #include "chrome/browser/ui/webui/active_downloads_ui.h"
24 #include "chrome/common/chrome_paths.h" 16 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h" 17 #include "chrome/common/url_constants.h"
27 #include "chrome/test/in_process_browser_test.h" 18 #include "chrome/test/in_process_browser_test.h"
28 #include "chrome/test/ui_test_utils.h" 19 #include "chrome/test/ui_test_utils.h"
29 #include "content/browser/net/url_request_mock_http_job.h" 20 #include "content/browser/net/url_request_mock_http_job.h"
30 #include "content/browser/tab_contents/tab_contents.h" 21 #include "content/browser/tab_contents/tab_contents.h"
31 #include "content/common/notification_service.h" 22 #include "content/common/notification_service.h"
32 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
33 24
34 namespace { 25 namespace {
35 26
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 found = true; 66 found = true;
76 break; 67 break;
77 } 68 }
78 } 69 }
79 EXPECT_TRUE(found); 70 EXPECT_TRUE(found);
80 #else 71 #else
81 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 72 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
82 #endif 73 #endif
83 } 74 }
84 75
85 // Changes the default folder prefs. This method saves the current folder
86 // for saving HTML, the current folder for saving downloaded files,
87 // the current user's "Downloads" folder and a save type (HTML only or
88 // complete HTML files), and then changes them to |website_save_dir|,
89 // |download_save_dir| and |save_type|, respectively.
90 // If we call this method, we must call RestoreDirectoryPrefs()
91 // after the test to restore the default folder prefs.
92 void ChangeDirectoryPrefs(
93 Profile* profile,
94 const FilePath& website_save_dir,
95 const FilePath& download_save_dir,
96 const SavePackage::SavePackageType save_type) {
97 DCHECK(profile);
98 PrefService* prefs = profile->GetPrefs();
99
100 DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory));
101 prev_download_save_dir_ = prefs->GetFilePath(
102 prefs::kDownloadDefaultDirectory);
103
104 // Check whether the preference has the default folder for saving HTML.
105 // If not, initialize it with the default folder for downloaded files.
106 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) {
107 prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory,
108 prev_download_save_dir_,
109 PrefService::UNSYNCABLE_PREF);
110 }
111 prev_website_save_dir_ = prefs->GetFilePath(
112 prefs::kSaveFileDefaultDirectory);
113
114 DownloadPrefs* download_prefs =
115 profile->GetDownloadManager()->download_prefs();
116 prev_save_type_ =
117 static_cast<SavePackage::SavePackageType>
118 (download_prefs->save_file_type());
119
120 prefs->SetFilePath(
121 prefs::kSaveFileDefaultDirectory, website_save_dir);
122 prefs->SetFilePath(
123 prefs::kDownloadDefaultDirectory, download_save_dir);
124 prefs->SetInteger(prefs::kSaveFileType, save_type);
125 }
126
127 // Restores the default folder prefs.
128 void RestoreDirectoryPrefs(Profile* profile) {
129 DCHECK(profile);
130 PrefService* prefs = profile->GetPrefs();
131 prefs->SetFilePath(
132 prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
133 prefs->SetFilePath(
134 prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
135 prefs->SetInteger(prefs::kSaveFileType, prev_save_type_);
136 }
137
138 // Path to directory containing test data. 76 // Path to directory containing test data.
139 FilePath test_dir_; 77 FilePath test_dir_;
140 78
141 // Temporary directory we will save pages to. 79 // Temporary directory we will save pages to.
142 ScopedTempDir save_dir_; 80 ScopedTempDir save_dir_;
143
144 // Temporarily stores the default folder prefs.
145 FilePath prev_website_save_dir_;
146 FilePath prev_download_save_dir_;
147 SavePackage::SavePackageType prev_save_type_;
148 }; 81 };
149 82
150 } // namespace
151
152 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { 83 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
153 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 84 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
154 GURL url = URLRequestMockHTTPJob::GetMockUrl( 85 GURL url = URLRequestMockHTTPJob::GetMockUrl(
155 FilePath(kTestDir).Append(file_name)); 86 FilePath(kTestDir).Append(file_name));
156 ui_test_utils::NavigateToURL(browser(), url); 87 ui_test_utils::NavigateToURL(browser(), url);
157 88
158 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); 89 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
159 ASSERT_TRUE(current_tab); 90 ASSERT_TRUE(current_tab);
160 91
161 FilePath full_file_name = save_dir_.path().Append(file_name); 92 FilePath full_file_name = save_dir_.path().Append(file_name);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 157 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"),
227 full_file_name)); 158 full_file_name));
228 EXPECT_TRUE(file_util::ContentsEqual( 159 EXPECT_TRUE(file_util::ContentsEqual(
229 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 160 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
230 dir.AppendASCII("1.png"))); 161 dir.AppendASCII("1.png")));
231 EXPECT_TRUE(file_util::ContentsEqual( 162 EXPECT_TRUE(file_util::ContentsEqual(
232 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 163 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
233 dir.AppendASCII("1.css"))); 164 dir.AppendASCII("1.css")));
234 } 165 }
235 166
236 // Checks if an HTML page is saved to the default folder for saving HTML
237 // in the following situation:
238 // The default folder for saving HTML exists.
239 // The default folder for downloaded files exists.
240 // The user's "Downloads" folder exists.
241 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder1) {
242 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
243 GURL url = URLRequestMockHTTPJob::GetMockUrl(
244 FilePath(kTestDir).Append(file_name));
245 ui_test_utils::NavigateToURL(browser(), url);
246
247 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
248 ASSERT_TRUE(current_tab);
249 ASSERT_TRUE(current_tab->tab_contents());
250 ASSERT_TRUE(current_tab->tab_contents()->profile());
251
252 ScopedTempDir website_save_dir, download_save_dir;
253 // Prepare the default folder for saving HTML.
254 ASSERT_TRUE(website_save_dir.CreateUniqueTempDir());
255 // Prepare the default folder for downloaded files.
256 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
257
258 // Changes the default prefs.
259 ChangeDirectoryPrefs(
260 current_tab->tab_contents()->profile(),
261 website_save_dir.path(),
262 download_save_dir.path(),
263 SavePackage::SAVE_AS_ONLY_HTML);
264
265
266 std::string ascii_basename =
267 UTF16ToASCII(current_tab->download_tab_helper()->
268 SavePageBasedOnDefaultPrefs()) + ".html";
269 FilePath::StringType basename;
270 #if defined(OS_WIN)
271 basename = ASCIIToWide(ascii_basename);
272 #else
273 basename = ascii_basename;
274 #endif
275 file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
276 FilePath full_file_name = website_save_dir.path().Append(FilePath(basename));
277
278 EXPECT_EQ(url, WaitForSavePackageToFinish());
279
280 CheckDownloadUI(full_file_name);
281
282 // Is the file downloaded to the default folder for saving HTML?
283 EXPECT_TRUE(file_util::PathExists(full_file_name));
284 EXPECT_TRUE(file_util::ContentsEqual(
285 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
286 full_file_name));
287
288 RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
289 }
290
291 // Checks if an HTML page is saved to the default folder for downloaded files
292 // in the following situation:
293 // The default folder for saving HTML does not exist.
294 // The default folder for downloaded files exists.
295 // The user's "Downloads" folder exists.
296 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder2) {
297 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
298 GURL url = URLRequestMockHTTPJob::GetMockUrl(
299 FilePath(kTestDir).Append(file_name));
300 ui_test_utils::NavigateToURL(browser(), url);
301
302 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
303 ASSERT_TRUE(current_tab);
304 ASSERT_TRUE(current_tab->tab_contents());
305 ASSERT_TRUE(current_tab->tab_contents()->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->tab_contents()->profile(),
318 nonexistent_path,
319 download_save_dir.path(),
320 SavePackage::SAVE_AS_ONLY_HTML);
321
322 std::string ascii_basename =
323 UTF16ToASCII(current_tab->download_tab_helper()->
324 SavePageBasedOnDefaultPrefs()) + ".html";
325 FilePath::StringType basename;
326 #if defined(OS_WIN)
327 basename = ASCIIToWide(ascii_basename);
328 #else
329 basename = ascii_basename;
330 #endif
331 file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
332 FilePath full_file_name = download_save_dir.path().Append(FilePath(basename));
333
334 EXPECT_EQ(url, WaitForSavePackageToFinish());
335
336 CheckDownloadUI(full_file_name);
337
338 // Is the file downloaded to the default folder for downloaded files?
339 EXPECT_TRUE(file_util::PathExists(full_file_name));
340 EXPECT_FALSE(file_util::PathExists(nonexistent_path));
341 EXPECT_TRUE(file_util::ContentsEqual(
342 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
343 full_file_name));
344
345 RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
346 }
347
348 // Checks if an HTML page is saved to the user's "Downloads" directory
349 // in the following situation:
350 // The default folder for saving HTML does not exist.
351 // The default folder for downloaded files does not exist.
352 // The user's "Downloads" folder exists.
353 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder3) {
354 FilePath file_name(FILE_PATH_LITERAL("a.htm"));
355 GURL url = URLRequestMockHTTPJob::GetMockUrl(
356 FilePath(kTestDir).Append(file_name));
357 ui_test_utils::NavigateToURL(browser(), url);
358
359 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
360 ASSERT_TRUE(current_tab);
361 ASSERT_TRUE(current_tab->tab_contents());
362 ASSERT_TRUE(current_tab->tab_contents()->profile());
363
364 // Prepare non-existent folder.
365 FilePath nonexistent_path1(
366 FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan"));
367 FilePath nonexistent_path2(
368 FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_pyonpyon"));
369 ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
370 ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
371
372 // Changes the default prefs.
373 ChangeDirectoryPrefs(
374 current_tab->tab_contents()->profile(),
375 nonexistent_path1,
376 nonexistent_path2,
377 SavePackage::SAVE_AS_ONLY_HTML);
378
379 std::string ascii_basename =
380 UTF16ToASCII(current_tab->download_tab_helper()->
381 SavePageBasedOnDefaultPrefs()) + ".html";
382 FilePath::StringType basename;
383 #if defined(OS_WIN)
384 basename = ASCIIToWide(ascii_basename);
385 #else
386 basename = ascii_basename;
387 #endif
388 file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
389 FilePath default_download_dir =
390 download_util::GetDefaultDownloadDirectoryFromPathService();
391 FilePath full_file_name =
392 default_download_dir.Append(FilePath(basename));
393
394 EXPECT_EQ(url, WaitForSavePackageToFinish());
395
396 CheckDownloadUI(full_file_name);
397
398 // Is the file downloaded to the user's "Downloads" directory?
399 EXPECT_TRUE(file_util::PathExists(full_file_name));
400 EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
401 EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
402 EXPECT_TRUE(file_util::ContentsEqual(
403 test_dir_.Append(FilePath(kTestDir)).Append(file_name),
404 full_file_name));
405
406 RestoreDirectoryPrefs(current_tab->tab_contents()->profile());
407 }
408
409 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 167 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
410 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); 168 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
411 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); 169 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
412 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); 170 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE));
413 } 171 }
414 172
415 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { 173 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
416 FilePath file_name(FILE_PATH_LITERAL("b.htm")); 174 FilePath file_name(FILE_PATH_LITERAL("b.htm"));
417 175
418 GURL url = URLRequestMockHTTPJob::GetMockUrl( 176 GURL url = URLRequestMockHTTPJob::GetMockUrl(
(...skipping 20 matching lines...) Expand all
439 EXPECT_TRUE(file_util::TextContentsEqual( 197 EXPECT_TRUE(file_util::TextContentsEqual(
440 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 198 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"),
441 full_file_name)); 199 full_file_name));
442 EXPECT_TRUE(file_util::ContentsEqual( 200 EXPECT_TRUE(file_util::ContentsEqual(
443 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), 201 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"),
444 dir.AppendASCII("1.png"))); 202 dir.AppendASCII("1.png")));
445 EXPECT_TRUE(file_util::ContentsEqual( 203 EXPECT_TRUE(file_util::ContentsEqual(
446 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), 204 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
447 dir.AppendASCII("1.css"))); 205 dir.AppendASCII("1.css")));
448 } 206 }
207
208 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/download/save_package.cc ('k') | chrome/browser/ui/download/download_tab_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698