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

Unified Diff: chrome/browser/download/download_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: Overrides the user's "Downloads" folder in DownloadPrefs 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/download_browsertest.cc
diff --git a/chrome/browser/download/download_browsertest.cc b/chrome/browser/download/download_browsertest.cc
index c53817b5adad46732156ee7268a065d84fe52a52..5c3355e86c7842e0b3b84cae910c3aa599ed8090 100644
--- a/chrome/browser/download/download_browsertest.cc
+++ b/chrome/browser/download/download_browsertest.cc
@@ -15,6 +15,8 @@
#include "chrome/browser/download/download_manager.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_shelf.h"
+#include "chrome/browser/download/download_test_util.h"
+#include "chrome/browser/download/download_util.h"
#include "chrome/browser/history/download_history_info.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/net/url_request_mock_http_job.h"
@@ -90,6 +92,7 @@ class DownloadsObserver : public DownloadManager::Observer,
// State accessors.
bool select_file_dialog_seen() { return select_file_dialog_seen_; }
+ const FilePath& suggested_path() { return suggested_path_; }
// Wait for whatever state was specified in the constructor.
void WaitForFinished() {
@@ -151,8 +154,10 @@ class DownloadsObserver : public DownloadManager::Observer,
}
}
- virtual void SelectFileDialogDisplayed(int32 /* id */) {
+ virtual void SelectFileDialogDisplayed(
+ int32 /* id */, const FilePath& suggested_path) {
select_file_dialog_seen_ = true;
+ suggested_path_ = suggested_path;
SignalIfFinished();
}
@@ -218,6 +223,9 @@ class DownloadsObserver : public DownloadManager::Observer,
// True if we've seen the select file dialog.
bool select_file_dialog_seen_;
+ // The suggested file path in the select file dialog.
+ FilePath suggested_path_;
+
DISALLOW_COPY_AND_ASSIGN(DownloadsObserver);
};
@@ -393,12 +401,6 @@ class CancelTestDataCollector
class DownloadTest : public InProcessBrowserTest {
public:
- enum SelectExpectation {
- EXPECT_NO_SELECT_DIALOG = -1,
- EXPECT_NOTHING,
- EXPECT_SELECT_DIALOG
- };
-
DownloadTest() {
EnableDOMAutomation();
}
@@ -446,7 +448,7 @@ class DownloadTest : public InProcessBrowserTest {
// Location of the file destination (place to which it is downloaded).
FilePath DestinationFile(Browser* browser, FilePath file) {
- return GetDownloadDirectory(browser).Append(file);
+ return GetDownloadSaveDirectory(browser).Append(file);
}
// Must be called after browser creation. Creates a temporary
@@ -467,11 +469,16 @@ class DownloadTest : public InProcessBrowserTest {
return true;
}
+ // Delete the default folder for downloaded files.
+ bool DeleteDownloadsDirectory() {
+ return downloads_directory_.Delete();
+ }
+
DownloadPrefs* GetDownloadPrefs(Browser* browser) {
return browser->profile()->GetDownloadManager()->download_prefs();
}
- FilePath GetDownloadDirectory(Browser* browser) {
+ FilePath GetDownloadSaveDirectory(Browser* browser) {
DownloadManager* download_mananger =
browser->profile()->GetDownloadManager();
return download_mananger->download_prefs()->download_path();
@@ -503,16 +510,21 @@ class DownloadTest : public InProcessBrowserTest {
// Download |url|, then wait for the download to finish.
// |disposition| indicates where the navigation occurs (current tab, new
// foreground tab, etc).
- // |expectation| indicates whether or not a Select File dialog should be
+ // |expect_file_dialog| indicates whether a select file dialog should be
// open when the download is finished, or if we don't care.
- // If the dialog appears, the routine exits. The only effect |expectation|
- // has is whether or not the test succeeds.
+ // If the dialog appears, the routine exits. The only effect
+ // |expect_file_dialog| has is whether or not the test succeeds.
+ // |expected_suggested_path| is the path expected to be suggested in the
+ // select file dialog. This |expected_suggested_path| must be specified
+ // if |expect_file_dialog| is true. If |expect_file_dialog| is false,
+ // |expected_suggested_path| is ignored.
// |browser_test_flags| indicate what to wait for, and is an OR of 0 or more
// values in the ui_test_utils::BrowserTestWaitFlags enum.
void DownloadAndWaitWithDisposition(Browser* browser,
const GURL& url,
WindowOpenDisposition disposition,
- SelectExpectation expectation,
+ bool expect_file_dialog,
+ const FilePath& expected_suggested_path,
int browser_test_flags) {
// Setup notification, navigate, and block.
scoped_ptr<DownloadsObserver> observer(CreateWaiter(browser, 1));
@@ -525,22 +537,30 @@ class DownloadTest : public InProcessBrowserTest {
// Waits for the download to complete.
observer->WaitForFinished();
- // If specified, check the state of the select file dialog.
- if (expectation != EXPECT_NOTHING) {
- EXPECT_EQ(expectation == EXPECT_SELECT_DIALOG,
- observer->select_file_dialog_seen());
+ // Checks if the select file dialog was displayed as we expected.
+ // If displayed, checks the suggested path in the dialog.
+ if (expect_file_dialog) {
+ EXPECT_TRUE(observer->select_file_dialog_seen());
+ EXPECT_EQ(observer->suggested_path(), expected_suggested_path);
+ } else {
+ EXPECT_FALSE(observer->select_file_dialog_seen());
}
}
// Download a file in the current tab, then wait for the download to finish.
- void DownloadAndWait(Browser* browser,
- const GURL& url,
- SelectExpectation expectation) {
+ // Expect that no select file dialog is displayed.
+ void DownloadAndWait(Browser* browser, const GURL& url) {
DownloadAndWaitWithDisposition(
- browser,
- url,
- CURRENT_TAB,
- expectation,
+ browser, url, CURRENT_TAB, false, FilePath(),
+ ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
+ }
+
+ // Download a file in the current tab, then wait for the download to finish.
+ // Expect that a select file dialog suggesting || is displayed.
+ void DownloadAndWaitWithDialog(Browser* browser, const GURL& url,
+ const FilePath& expected_suggested_path) {
+ DownloadAndWaitWithDisposition(
+ browser, url, CURRENT_TAB, true, expected_suggested_path,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
}
@@ -782,8 +802,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
- // Download the file and wait. We do not expect the Select File dialog.
- DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
+ // Download the file and wait. We do not expect the select file dialog.
+ DownloadAndWait(browser(), url);
// Check state.
EXPECT_EQ(1, browser()->tab_count());
@@ -791,6 +811,64 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
CheckDownloadUIVisible(browser(), true, true);
}
+// Checks if a file is saved to the user's "Downloads" folder
+// in the following situation:
+// The default folder for downloaded files does not exist.
+// The user's "Downloads" folder exists.
+IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadFolder1) {
+ ASSERT_TRUE(InitialSetup(false));
+ FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
+ GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
+
+ // Temporarily change the user's "Downloads" folder.
+ ScopedTempDir default_download_dir;
+ ASSERT_TRUE(default_download_dir.CreateUniqueTempDir());
+ download_test_util::ScopedDefaultDownloadDirectory
+ override_default_download(
+ default_download_dir.path(), GetDownloadPrefs(browser()));
+ FilePath downloaded_file = default_download_dir.path().Append(file);
+
+ // Delete the default folder for downloaded files.
+ ASSERT_TRUE(DeleteDownloadsDirectory());
+ ASSERT_FALSE(file_util::PathExists(GetDownloadSaveDirectory(browser())));
+
+ // Download the file and wait. We expect the select file dialog.
+ DownloadAndWaitWithDialog(browser(), url, downloaded_file);
+
+ EXPECT_FALSE(file_util::PathExists(downloaded_file));
+ EXPECT_FALSE(file_util::PathExists(GetDownloadSaveDirectory(browser())));
+ EXPECT_EQ(1, browser()->tab_count());
+}
+
+// Checks if the default folder for downloaded files is created
+// and a file is saved to the folder in the following situation:
+// The default folder for downloaded files does not exist.
+// The user's "Downloads" folder does not exist.
+IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadFolder2) {
haraken1 2011/06/14 11:10:05 This test fails in this patch. Please see my comme
+ ASSERT_TRUE(InitialSetup(false));
+ FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
+ GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
+ FilePath downloaded_file(DestinationFile(browser(), file));
+
+ // Temporarily change the user's "Downloads" folder.
+ FilePath nonexistent_dir("/tmp/koakuma_andre_moemoe_nyannyan");
+ download_test_util::ScopedDefaultDownloadDirectory
+ override_default_download(
+ nonexistent_dir, GetDownloadPrefs(browser()));
+
+ // Delete the default folder for downloaded files.
+ ASSERT_TRUE(DeleteDownloadsDirectory());
+ ASSERT_FALSE(file_util::PathExists(GetDownloadSaveDirectory(browser())));
+
+ // Download the file and wait. We expect the select file dialog.
+ DownloadAndWaitWithDialog(browser(), url, downloaded_file);
+
+ EXPECT_FALSE(file_util::PathExists(downloaded_file));
+ EXPECT_FALSE(file_util::PathExists(nonexistent_dir));
+ EXPECT_TRUE(file_util::PathExists(GetDownloadSaveDirectory(browser())));
+ EXPECT_EQ(1, browser()->tab_count());
+}
+
#if defined(OS_WIN)
// Download a file and confirm that the zone identifier (on windows)
// is set to internet.
@@ -799,8 +877,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) {
FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
- // Download the file and wait. We do not expect the Select File dialog.
- DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
+ // Download the file and wait. We do not expect the select file dialog.
+ DownloadAndWait(browser(), url);
// Check state. Special file state must be checked before CheckDownload,
// as CheckDownload will delete the output file.
@@ -813,7 +891,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) {
}
#endif
-// Put up a Select File dialog when the file is downloaded, due to its MIME
+// Put up a select file dialog when the file is downloaded, due to its MIME
// type.
//
// This test runs correctly, but leaves behind turds in the test user's
@@ -824,14 +902,15 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
ASSERT_TRUE(InitialSetup(true));
FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
+ FilePath file_path(DestinationFile(browser(), file));
- // Download the file and wait. We expect the Select File dialog to appear
+ // Download the file and wait. We expect the select file dialog to appear
// due to the MIME type.
- DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
+ DownloadAndWaitWithDialog(browser(), url, file_path);
// Check state.
EXPECT_EQ(1, browser()->tab_count());
- // Since we exited while the Select File dialog was visible, there should not
+ // Since we exited while the select file dialog was visible, there should not
// be anything in the download shelf and so it should not be visible.
CheckDownloadUIVisible(browser(), false, false);
}
@@ -866,7 +945,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, ContentDisposition) {
FilePath download_file(FILE_PATH_LITERAL("download-test3-attachment.gif"));
// Download a file and wait.
- DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
+ DownloadAndWait(browser(), url);
CheckDownload(browser(), download_file, file);
@@ -886,7 +965,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, PerWindowShelf) {
FilePath download_file(FILE_PATH_LITERAL("download-test3-attachment.gif"));
// Download a file and wait.
- DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
+ DownloadAndWait(browser(), url);
CheckDownload(browser(), download_file, file);
@@ -951,7 +1030,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, IncognitoDownload) {
// Since |incognito| is a separate browser, we have to set it up explicitly.
incognito->profile()->GetPrefs()->SetBoolean(prefs::kPromptForDownload,
false);
- DownloadAndWait(incognito, url, EXPECT_NO_SELECT_DIALOG);
+ DownloadAndWait(incognito, url);
// We should still have 2 windows.
ExpectWindowCountAfterDownload(2);
@@ -1018,7 +1097,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CloseNewTab1) {
browser(),
url,
NEW_BACKGROUND_TAB,
- EXPECT_NO_SELECT_DIALOG,
+ false,
+ FilePath(),
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
// When the download finishes, we should still have one tab.
@@ -1050,7 +1130,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DontCloseNewTab2) {
DownloadAndWaitWithDisposition(browser(),
GURL("javascript:openNew()"),
CURRENT_TAB,
- EXPECT_NO_SELECT_DIALOG,
+ false,
+ FilePath(),
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
// When the download finishes, we should have two tabs.
@@ -1092,7 +1173,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DontCloseNewTab3) {
DownloadAndWaitWithDisposition(browser(),
url,
CURRENT_TAB,
- EXPECT_NO_SELECT_DIALOG,
+ false,
+ FilePath(),
ui_test_utils::BROWSER_TEST_NONE);
// When the download finishes, we should have two tabs.
@@ -1125,7 +1207,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CloseNewTab2) {
DownloadAndWaitWithDisposition(browser(),
GURL("javascript:openNew()"),
CURRENT_TAB,
- EXPECT_NO_SELECT_DIALOG,
+ false,
+ FilePath(),
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
// When the download finishes, we should still have one tab.
@@ -1160,7 +1243,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CloseNewTab3) {
browser(),
GURL("javascript:document.getElementById('form').submit()"),
CURRENT_TAB,
- EXPECT_NO_SELECT_DIALOG,
+ false,
+ FilePath(),
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
// When the download finishes, we should still have one tab.
@@ -1190,7 +1274,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, NewWindow) {
DownloadAndWaitWithDisposition(browser(),
url,
NEW_WINDOW,
- EXPECT_NO_SELECT_DIALOG,
+ false,
+ FilePath(),
ui_test_utils::BROWSER_TEST_NONE);
// When the download finishes, the download shelf SHOULD NOT be visible in
@@ -1297,8 +1382,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
int64 origin_size;
file_util::GetFileSize(origin_file, &origin_size);
- // Download the file and wait. We do not expect the Select File dialog.
- DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
+ // Download the file and wait. We do not expect the select file dialog.
+ DownloadAndWait(browser(), url);
// Get details of what downloads have just happened.
std::vector<DownloadItem*> downloads;
@@ -1335,7 +1420,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) {
GURL extensions_url(chrome::kChromeUIExtensionsURL);
ui_test_utils::NavigateToURL(browser(), flags_url);
- DownloadAndWait(browser(), download_url, EXPECT_NO_SELECT_DIALOG);
+ DownloadAndWait(browser(), download_url);
ui_test_utils::NavigateToURL(browser(), extensions_url);
TabContents* contents = browser()->GetSelectedTabContents();
ASSERT_TRUE(contents);
@@ -1372,7 +1457,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_BrowserCloseAfterDownload) {
&result));
EXPECT_TRUE(result);
- DownloadAndWait(browser(), download_url, EXPECT_NO_SELECT_DIALOG);
+ DownloadAndWait(browser(), download_url);
ui_test_utils::WindowedNotificationObserver signal(
NotificationType::BROWSER_CLOSED,
@@ -1394,7 +1479,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, AutoOpen) {
MockDownloadOpeningObserver observer(
browser()->profile()->GetDownloadManager());
- DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
+ DownloadAndWait(browser(), url);
// Find the download and confirm it was opened.
std::vector<DownloadItem*> downloads;

Powered by Google App Engine
This is Rietveld 408576698