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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/download/save_page_browsertest.cc
diff --git a/chrome/browser/download/save_page_browsertest.cc b/chrome/browser/download/save_page_browsertest.cc
index c2420ba5eb08c07de08f4fedb52d396cbf998351..d2efbb691a26b00272de5efe87815cfbc4d390cb 100644
--- a/chrome/browser/download/save_page_browsertest.cc
+++ b/chrome/browser/download/save_page_browsertest.cc
@@ -4,15 +4,23 @@
#include "base/file_path.h"
#include "base/file_util.h"
+#include "base/i18n/file_util_icu.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
+#include "base/string_util.h"
#include "chrome/app/chrome_command_ids.h"
+#include "chrome/browser/download/download_manager.h"
+#include "chrome/browser/download/download_prefs.h"
+#include "chrome/browser/download/download_util.h"
#include "chrome/browser/net/url_request_mock_http_job.h"
+#include "chrome/browser/prefs/pref_service.h"
+#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/download/download_tab_helper.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/common/chrome_paths.h"
+#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
@@ -47,13 +55,76 @@ class SavePageBrowserTest : public InProcessBrowserTest {
return *Details<GURL>(observer.details()).ptr();
}
+ // Changes the default folder prefs. This method saves the current folder
+ // for saving HTML, the current folder for saving downloaded files,
+ // the current user's "Downloads" folder and a save type (HTML only or
+ // complete HTML files), and then changes them to |website_save_dir|,
+ // |download_save_dir|, |default_downloads_dir| and |save_type|, respectively.
+ // If we call this method, we must call RestoreSaveDirectoryPrefs()
+ // after the test to restore the default folder prefs.
+ void ChangeSaveDirectoryPrefs(
+ Profile* profile,
+ const FilePath& website_save_dir,
+ const FilePath& download_save_dir,
+ const FilePath& default_downloads_dir,
+ const SavePackage::SavePackageType save_type) {
+ DCHECK(profile);
+ PrefService* prefs = profile->GetPrefs();
+
+ DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory));
+ prev_download_save_dir_ = prefs->GetFilePath(
+ prefs::kDownloadDefaultDirectory);
+
+ // Check whether the preference has the default folder for saving HTML.
+ // If not, initialize it with the default folder for downloaded files.
+ if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) {
+ prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory,
+ prev_download_save_dir_,
+ PrefService::UNSYNCABLE_PREF);
+ }
+ prev_website_save_dir_ = prefs->GetFilePath(
+ prefs::kSaveFileDefaultDirectory);
+
+ DownloadPrefs* download_prefs =
+ profile->GetDownloadManager()->download_prefs();
+ prev_save_type_ =
+ static_cast<SavePackage::SavePackageType>
+ (download_prefs->save_file_type());
+
+ prefs->SetFilePath(
+ prefs::kSaveFileDefaultDirectory, website_save_dir);
+ prefs->SetFilePath(
+ prefs::kDownloadDefaultDirectory, download_save_dir);
+ download_util::DefaultDownloadDirectory::Override(default_downloads_dir);
+ prefs->SetInteger(prefs::kSaveFileType, save_type);
+ }
+
+ // Restores the default folder prefs.
+ void RestoreSaveDirectoryPrefs(Profile* profile) {
+ DCHECK(profile);
+ PrefService* prefs = profile->GetPrefs();
+ prefs->SetFilePath(
+ prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
+ prefs->SetFilePath(
+ prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
+ download_util::DefaultDownloadDirectory::UnOverride();
+ prefs->SetInteger(prefs::kSaveFileType, prev_save_type_);
+ }
+
// Path to directory containing test data.
FilePath test_dir_;
// Temporary directory we will save pages to.
ScopedTempDir save_dir_;
+
+ // Temporarily stores the default folder prefs.
+ FilePath prev_website_save_dir_;
+ FilePath prev_download_save_dir_;
+ SavePackage::SavePackageType prev_save_type_;
};
+} // namespace
+
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
FilePath file_name(FILE_PATH_LITERAL("a.htm"));
GURL url = URLRequestMockHTTPJob::GetMockUrl(
@@ -141,6 +212,224 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
dir.AppendASCII("1.css")));
}
+// Checks if an HTML page is saved to the default folder for saving HTML
+// in the following situation:
+// The default folder for saving HTML exists.
+// The default folder for downloaded files exists.
+// The user's "Downloads" folder exists.
+IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder1) {
+ FilePath file_name(FILE_PATH_LITERAL("a.htm"));
+ GURL url = URLRequestMockHTTPJob::GetMockUrl(
+ FilePath(kTestDir).Append(file_name));
+ ui_test_utils::NavigateToURL(browser(), url);
+
+ TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
+ ASSERT_TRUE(current_tab);
+ ASSERT_TRUE(current_tab->tab_contents());
+ ASSERT_TRUE(current_tab->tab_contents()->profile());
+
+ ScopedTempDir website_save_dir, download_save_dir, default_downloads_dir;
+ // Prepare the default folder for saving HTML.
+ ASSERT_TRUE(website_save_dir.CreateUniqueTempDir());
+ // Prepare the default folder for downloaded files.
+ ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
+ // Prepare the user's "Downloads" folder.
+ ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
+
+ // Changes the default prefs.
+ ChangeSaveDirectoryPrefs(
+ current_tab->tab_contents()->profile(),
+ website_save_dir.path(),
+ download_save_dir.path(),
+ default_downloads_dir.path(),
+ SavePackage::SAVE_AS_ONLY_HTML);
+
+ std::string title = UTF16ToASCII(
+ current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
+ file_util::ReplaceIllegalCharactersInPath(&title, ' ');
+ FilePath full_file_name =
+ website_save_dir.path().Append(FilePath(title + ".html"));
+
+ EXPECT_EQ(url, WaitForSavePackageToFinish());
+
+ if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
+ EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
+
+ // Is the file downloaded to the default folder for saving HTML?
+ EXPECT_TRUE(file_util::PathExists(full_file_name));
+ EXPECT_TRUE(file_util::ContentsEqual(
+ test_dir_.Append(FilePath(kTestDir)).Append(file_name),
+ full_file_name));
+
+ RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
+}
+
+// Checks if an HTML page is saved to the default folder for downloaded files
+// in the following situation:
+// The default folder for saving HTML does not exist.
+// The default folder for downloaded files exists.
+// The user's "Downloads" folder exists.
+IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder2) {
+ FilePath file_name(FILE_PATH_LITERAL("a.htm"));
+ GURL url = URLRequestMockHTTPJob::GetMockUrl(
+ FilePath(kTestDir).Append(file_name));
+ ui_test_utils::NavigateToURL(browser(), url);
+
+ TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
+ ASSERT_TRUE(current_tab);
+ ASSERT_TRUE(current_tab->tab_contents());
+ ASSERT_TRUE(current_tab->tab_contents()->profile());
+
+ ScopedTempDir download_save_dir, default_downloads_dir;
+ // Prepare the default folder for saving downloaded files.
+ ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
+ // Prepare the user's "Downloads" folder.
+ ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
+ // Prepare non-existent folder.
+ FilePath nonexistent_path("/tmp/koakuma_mikity_moemoe_nyannyan");
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path));
+
+ // Changes the default prefs.
+ ChangeSaveDirectoryPrefs(
+ current_tab->tab_contents()->profile(),
+ nonexistent_path,
+ download_save_dir.path(),
+ default_downloads_dir.path(),
+ SavePackage::SAVE_AS_ONLY_HTML);
+
+ std::string title = UTF16ToASCII(
+ current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
+ file_util::ReplaceIllegalCharactersInPath(&title, ' ');
+ FilePath full_file_name =
+ download_save_dir.path().Append(FilePath(title + ".html"));
+
+ EXPECT_EQ(url, WaitForSavePackageToFinish());
+
+ if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
+ EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
+
+ // Is the file downloaded to the default folder for downloaded files?
+ EXPECT_TRUE(file_util::PathExists(full_file_name));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_path));
+ EXPECT_TRUE(file_util::ContentsEqual(
+ test_dir_.Append(FilePath(kTestDir)).Append(file_name),
+ full_file_name));
+
+ RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
+}
+
+// Checks if an HTML page is saved to the user's "Downloads" directory
+// in the following situation:
+// The default folder for saving HTML does not exist.
+// The default folder for downloaded files does not exist.
+// The user's "Downloads" folder exists.
+IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder3) {
+ FilePath file_name(FILE_PATH_LITERAL("a.htm"));
+ GURL url = URLRequestMockHTTPJob::GetMockUrl(
+ FilePath(kTestDir).Append(file_name));
+ ui_test_utils::NavigateToURL(browser(), url);
+
+ TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
+ ASSERT_TRUE(current_tab);
+ ASSERT_TRUE(current_tab->tab_contents());
+ ASSERT_TRUE(current_tab->tab_contents()->profile());
+
+ ScopedTempDir default_downloads_dir;
+ // Prepare the user's "Downloads" folder.
+ ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir());
+ // Prepare non-existent folder.
+ FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
+ FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
+
+ // Changes the default prefs.
+ ChangeSaveDirectoryPrefs(
+ current_tab->tab_contents()->profile(),
+ nonexistent_path1,
+ nonexistent_path2,
+ default_downloads_dir.path(),
+ SavePackage::SAVE_AS_ONLY_HTML);
+
+ std::string title = UTF16ToASCII(
+ current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
+ file_util::ReplaceIllegalCharactersInPath(&title, ' ');
+ FilePath full_file_name =
+ default_downloads_dir.path().Append(FilePath(title + ".html"));
+
+ EXPECT_EQ(url, WaitForSavePackageToFinish());
+
+ if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
+ EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
+
+ // Is the file downloaded to the user's "Downloads" directory?
+ EXPECT_TRUE(file_util::PathExists(full_file_name));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
+ EXPECT_TRUE(file_util::ContentsEqual(
+ test_dir_.Append(FilePath(kTestDir)).Append(file_name),
+ full_file_name));
+
+ RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
+}
+
+// Checks if the default folder for downloaded files is created
+// and an HTML page is saved to the folder in the following situation:
+// The default folder for saving HTML does not exist.
+// The default folder for downloaded files does not exist.
+// The user's "Downloads" folder does not exist.
+IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavedFolder4) {
+ FilePath file_name(FILE_PATH_LITERAL("a.htm"));
+ GURL url = URLRequestMockHTTPJob::GetMockUrl(
+ FilePath(kTestDir).Append(file_name));
+ ui_test_utils::NavigateToURL(browser(), url);
+
+ TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper();
+ ASSERT_TRUE(current_tab);
+ ASSERT_TRUE(current_tab->tab_contents());
+ ASSERT_TRUE(current_tab->tab_contents()->profile());
+
+ // Prepare non-existent folder.
+ FilePath nonexistent_path1("/tmp/koakuma_mikity_moemoe_nyannyan");
+ FilePath nonexistent_path2("/tmp/koakuma_mikity_moemoe_pyonpyon");
+ FilePath nonexistent_path3("/tmp/koakuma_mikity_moemoe_kyunkyun");
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path3));
+
+ // Changes the default prefs.
+ ChangeSaveDirectoryPrefs(
+ current_tab->tab_contents()->profile(),
+ nonexistent_path1,
+ nonexistent_path2,
+ nonexistent_path3,
+ SavePackage::SAVE_AS_ONLY_HTML);
+
+ std::string title = UTF16ToASCII(
+ current_tab->download_tab_helper()->SavePageBasedOnDefaultPrefs());
+ file_util::ReplaceIllegalCharactersInPath(&title, ' ');
+ FilePath full_file_name =
+ nonexistent_path2.Append(FilePath(title + ".html"));
+
+ EXPECT_EQ(url, WaitForSavePackageToFinish());
+
+ if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
+ EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
+
+ // Is the file downloaded to the default folder for downloaded files?
+ EXPECT_TRUE(file_util::PathExists(full_file_name));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
+ EXPECT_TRUE(file_util::PathExists(nonexistent_path2));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_path3));
+ EXPECT_TRUE(file_util::ContentsEqual(
+ test_dir_.Append(FilePath(kTestDir)).Append(file_name),
+ full_file_name));
+
+ ASSERT_TRUE(file_util::Delete(nonexistent_path2, true));
+
+ RestoreSaveDirectoryPrefs(current_tab->tab_contents()->profile());
+}
+
IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL));
ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE));
@@ -182,5 +471,3 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"),
dir.AppendASCII("1.css")));
}
-
-} // namespace

Powered by Google App Engine
This is Rietveld 408576698