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

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: Added URLRequestMockHTTPJob.test_dir_ and URLRequestMockHTTPJob.temp_dir_ 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 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 8830fa71109dd460ec067789ff039957ac09d5f2..e0d9286f520e790db3b91f6708ec241f2763988c 100644
--- a/chrome/browser/download/save_page_browsertest.cc
+++ b/chrome/browser/download/save_page_browsertest.cc
@@ -4,14 +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 "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/download/download_item.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/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/webui/active_downloads_ui.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"
@@ -25,6 +34,10 @@ namespace {
static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page");
+// The contents of a downloaded file.
+static const char kContentsOfTestHTML[] =
+ "<html><head><title>test page</title></head><body>test</body></html>";
+
static const char* kAppendedExtension =
#if defined(OS_WIN)
".htm";
@@ -72,13 +85,73 @@ class SavePageBrowserTest : public InProcessBrowserTest {
#endif
}
+ // 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| and |save_type|, respectively.
+ // If we call this method, we must call RestoreDirectoryPrefs()
+ // after the test to restore the default folder prefs.
+ void ChangeDirectoryPrefs(
+ Profile* profile,
+ const FilePath& website_save_dir,
+ const FilePath& download_save_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);
+ prefs->SetInteger(prefs::kSaveFileType, save_type);
+ }
+
+ // Restores the default folder prefs.
+ void RestoreDirectoryPrefs(Profile* profile) {
+ DCHECK(profile);
+ PrefService* prefs = profile->GetPrefs();
+ prefs->SetFilePath(
+ prefs::kSaveFileDefaultDirectory, prev_website_save_dir_);
+ prefs->SetFilePath(
+ prefs::kDownloadDefaultDirectory, prev_download_save_dir_);
+ 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(
@@ -163,6 +236,213 @@ 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, SaveFolder1) {
+ FilePath file(FILE_PATH_LITERAL("a.htm"));
+ GURL url = URLRequestMockHTTPJob::GetMockUrl(
+ FilePath(kTestDir).Append(file));
+ ui_test_utils::NavigateToURL(browser(), url);
+
+ TabContents* current_tab = browser()->GetSelectedTabContents();
+ ASSERT_TRUE(current_tab);
+ ASSERT_TRUE(current_tab->profile());
+
+ ScopedTempDir website_save_dir, download_save_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());
+
+ // Changes the default prefs.
+ ChangeDirectoryPrefs(
+ current_tab->profile(),
+ website_save_dir.path(),
+ download_save_dir.path(),
+ SavePackage::SAVE_AS_ONLY_HTML);
+
+ string16 title = current_tab->SavePageBasedOnDefaultPrefs();
+ FilePath::StringType basename;
+#if defined(OS_WIN)
+ basename = UTF16ToWide(title);
+ basename.append(FILE_PATH_LITERAL(".htm"));
+#else
+ basename = UTF16ToASCII(title);
+ basename.append(FILE_PATH_LITERAL(".html"));
+#endif
+ file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
+ FilePath downloaded_file = website_save_dir.path().Append(FilePath(basename));
+
+ EXPECT_EQ(url, WaitForSavePackageToFinish());
+
+ CheckDownloadUI(downloaded_file);
+
+ // Is the file downloaded to the default folder for saving HTML?
+ EXPECT_TRUE(file_util::PathExists(downloaded_file));
+ EXPECT_TRUE(file_util::ContentsEqual(
+ test_dir_.Append(FilePath(kTestDir)).Append(file),
+ downloaded_file));
+
+ RestoreDirectoryPrefs(current_tab->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, SaveFolder2) {
+ FilePath file(FILE_PATH_LITERAL("a.htm"));
+ GURL url = URLRequestMockHTTPJob::GetMockUrl(
+ FilePath(kTestDir).Append(file));
+ ui_test_utils::NavigateToURL(browser(), url);
+
+ TabContents* current_tab = browser()->GetSelectedTabContents();
+ ASSERT_TRUE(current_tab);
+ ASSERT_TRUE(current_tab->profile());
+
+ ScopedTempDir download_save_dir;
+ // Prepare the default folder for saving downloaded files.
+ ASSERT_TRUE(download_save_dir.CreateUniqueTempDir());
+ // Prepare non-existent folder.
+ FilePath nonexistent_path(
+ FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan"));
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path));
+
+ // Changes the default prefs.
+ ChangeDirectoryPrefs(
+ current_tab->profile(),
+ nonexistent_path,
+ download_save_dir.path(),
+ SavePackage::SAVE_AS_ONLY_HTML);
+
+ string16 title = current_tab->SavePageBasedOnDefaultPrefs();
+ FilePath::StringType basename;
+#if defined(OS_WIN)
+ basename = UTF16ToWide(title);
+ basename.append(FILE_PATH_LITERAL(".htm"));
+#else
+ basename = UTF16ToASCII(title);
+ basename.append(FILE_PATH_LITERAL(".html"));
+#endif
+ file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
+ FilePath downloaded_file =
+ download_save_dir.path().Append(FilePath(basename));
+
+ EXPECT_EQ(url, WaitForSavePackageToFinish());
+
+ CheckDownloadUI(downloaded_file);
+
+ // Is the file downloaded to the default folder for downloaded files?
+ EXPECT_TRUE(file_util::PathExists(downloaded_file));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_path));
+ EXPECT_TRUE(file_util::ContentsEqual(
+ test_dir_.Append(FilePath(kTestDir)).Append(file),
+ downloaded_file));
+
+ RestoreDirectoryPrefs(current_tab->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.
+//
+// This test creates and deletes a file on the user's "Downloads" folder,
+// which is globally shared among all tests on the environment. Therefore,
+// if we run browser tests in parallel, the file created by one browser test
+// may be deleted by another brower test in case where the file name conflicts.
+// In order to avoid this problem, we create a temporary file with a unique name
+// in chrome/test/data/ folder and use the temporary file for this test.
+//
+// Ideally, in the first place, we should not use the user's "Downloads" folder.
+// Instead, we should create a temporary "Downloads" folder for each test.
+// However, we concluded that creating temporary "Downloads" folder for each
+// test requires very invasive code changes to many places. See also here:
+// http://codereview.chromium.org/6973052/
+IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder3) {
+ ScopedTempDir scoped_temp_dir;
+ ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
+ // Creates a temporary file with a unique name.
+ FilePath unique_file;
+ ASSERT_TRUE(file_util::CreateTemporaryFileInDir(
+ scoped_temp_dir.path(), &unique_file));
+ // Adds "test" prefix to the temporary file, so that the file name starts
+ // with an ascii character and thus the file is handled as a normal file.
+ // Adds ".htm" extension to the temporary file, so that the file is
+ // handled as an HTML file.
+ FilePath file = unique_file.DirName().
+ Append(FILE_PATH_LITERAL("test") + unique_file.BaseName().value() +
+ FILE_PATH_LITERAL(".htm"));
+ ASSERT_TRUE(file_util::Move(unique_file, file));
+ ASSERT_TRUE(file_util::WriteFile(file,
+ kContentsOfTestHTML,
+ ARRAYSIZE_UNSAFE(kContentsOfTestHTML)));
+ GURL url(URLRequestMockHTTPJob::GetMockUrlOfTempDir(
+ scoped_temp_dir.path().BaseName().Append(file.BaseName())));
+ ui_test_utils::NavigateToURL(browser(), url);
+
+ TabContents* current_tab = browser()->GetSelectedTabContents();
+ ASSERT_TRUE(current_tab);
+ ASSERT_TRUE(current_tab->profile());
+
+ // Prepare non-existent folder.
+ FilePath nonexistent_path1(
+ FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan"));
+ FilePath nonexistent_path2(
+ FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_pyonpyon"));
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path1));
+ ASSERT_FALSE(file_util::PathExists(nonexistent_path2));
+
+ // Changes the default prefs.
+ ChangeDirectoryPrefs(
+ current_tab->profile(),
+ nonexistent_path1,
+ nonexistent_path2,
+ SavePackage::SAVE_AS_ONLY_HTML);
+
+ string16 title = current_tab->SavePageBasedOnDefaultPrefs();
+ FilePath::StringType basename;
+#if defined(OS_WIN)
+ basename = UTF16ToWide(title);
+ basename.append(FILE_PATH_LITERAL(".htm"));
+#else
+ basename = UTF16ToASCII(title);
+ basename.append(FILE_PATH_LITERAL(".html"));
+#endif
+ file_util::ReplaceIllegalCharactersInPath(&basename, ' ');
+ FilePath default_download_dir =
+ download_util::GetDefaultDownloadDirectoryFromPathService();
+ FilePath downloaded_file =
+ default_download_dir.Append(FilePath(basename));
+ // Make sure that the target file does not exist.
+ file_util::Delete(downloaded_file, false);
+ // Make sure that the temporary file does not exist.
+ FilePath temporary_file =
+ downloaded_file.Append(FILE_PATH_LITERAL(".crdownload"));
+ file_util::Delete(temporary_file, false);
+
+ EXPECT_EQ(url, WaitForSavePackageToFinish());
+
+ CheckDownloadUI(downloaded_file);
+
+ // Is the file downloaded to the user's "Downloads" directory?
+ EXPECT_TRUE(file_util::PathExists(downloaded_file));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_path1));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_path2));
+ EXPECT_TRUE(file_util::ContentsEqual(file, downloaded_file));
+
+ // Clean up the generated files.
+ file_util::Delete(downloaded_file, false);
+ file_util::Delete(temporary_file, false);
+
+ RestoreDirectoryPrefs(current_tab->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));
@@ -203,5 +483,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