Chromium Code Reviews| Index: chrome/browser/download/download_browsertest.cc |
| diff --git a/chrome/browser/download/download_browsertest.cc b/chrome/browser/download/download_browsertest.cc |
| index d34ab507c2289fab88dcbec397ad2b8da7654c5f..3b51dbf0439cf483b4b836a079cf6f8f6565ee34 100644 |
| --- a/chrome/browser/download/download_browsertest.cc |
| +++ b/chrome/browser/download/download_browsertest.cc |
| @@ -15,6 +15,7 @@ |
| #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_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 +91,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 +153,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 +222,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 +400,6 @@ class CancelTestDataCollector |
| class DownloadTest : public InProcessBrowserTest { |
| public: |
| - enum SelectExpectation { |
| - EXPECT_NO_SELECT_DIALOG = -1, |
| - EXPECT_NOTHING, |
| - EXPECT_SELECT_DIALOG |
| - }; |
| - |
| DownloadTest() { |
| EnableDOMAutomation(); |
| } |
| @@ -467,6 +468,11 @@ 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(); |
| } |
| @@ -503,16 +509,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 +536,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 DownloadAndWaitWithoutDialog(Browser* browser, const GURL& url) { |
|
Randy Smith (Not in Mondays)
2011/06/08 22:31:18
I think that this is the common case, so I'd leave
haraken1
2011/06/09 10:16:56
Done.
|
| 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 +801,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. |
| + DownloadAndWaitWithoutDialog(browser(), url); |
| // Check state. |
| EXPECT_EQ(1, browser()->tab_count()); |
| @@ -791,6 +810,67 @@ 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, DownloadedFolder1) { |
| + 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_downloads_dir; |
| + ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir()); |
| + download_util::DefaultDownloadDirectory::Override( |
| + default_downloads_dir.path()); |
| + FilePath downloaded_file = default_downloads_dir.path().Append(file); |
| + |
| + // Delete the default folder for downloaded files. |
| + ASSERT_TRUE(DeleteDownloadsDirectory()); |
| + ASSERT_FALSE(file_util::PathExists(GetDownloadDirectory(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(GetDownloadDirectory(browser()))); |
| + EXPECT_EQ(1, browser()->tab_count()); |
| + |
| + // Restore the user's "Downloads" folder. |
| + download_util::DefaultDownloadDirectory::UnOverride(); |
|
Paweł Hajdan Jr.
2011/06/08 09:50:23
I think this should be handled by a scoped object
haraken1
2011/06/09 10:16:56
Done. I created a new class ScopedDefaultDownloadD
|
| +} |
| + |
| +// 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, DownloadedFolder2) { |
| + 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_util::DefaultDownloadDirectory::Override(nonexistent_dir); |
| + |
| + // Delete the default folder for downloaded files. |
| + ASSERT_TRUE(DeleteDownloadsDirectory()); |
| + ASSERT_FALSE(file_util::PathExists(GetDownloadDirectory(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(GetDownloadDirectory(browser()))); |
| + EXPECT_EQ(1, browser()->tab_count()); |
| + |
| + // Restore the user's "Downloads" folder. |
| + download_util::DefaultDownloadDirectory::UnOverride(); |
| +} |
| + |
| #if defined(OS_WIN) |
| // Download a file and confirm that the zone identifier (on windows) |
| // is set to internet. |
| @@ -799,8 +879,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. |
| + DownloadAndWaitWithoutDialog(browser(), url); |
| // Check state. Special file state must be checked before CheckDownload, |
| // as CheckDownload will delete the output file. |
| @@ -813,7 +893,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 +904,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 +947,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); |
| + DownloadAndWaitWithoutDialog(browser(), url); |
| CheckDownload(browser(), download_file, file); |
| @@ -886,7 +967,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); |
| + DownloadAndWaitWithoutDialog(browser(), url); |
| CheckDownload(browser(), download_file, file); |
| @@ -951,7 +1032,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); |
| + DownloadAndWaitWithoutDialog(incognito, url); |
| // We should still have 2 windows. |
| ExpectWindowCountAfterDownload(2); |
| @@ -1018,7 +1099,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 +1132,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 +1175,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 +1209,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 +1245,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 +1276,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 +1384,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. |
| + DownloadAndWaitWithoutDialog(browser(), url); |
| // Get details of what downloads have just happened. |
| std::vector<DownloadItem*> downloads; |
| @@ -1335,7 +1422,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); |
| + DownloadAndWaitWithoutDialog(browser(), download_url); |
| ui_test_utils::NavigateToURL(browser(), extensions_url); |
| TabContents* contents = browser()->GetSelectedTabContents(); |
| ASSERT_TRUE(contents); |
| @@ -1372,7 +1459,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_BrowserCloseAfterDownload) { |
| &result)); |
| EXPECT_TRUE(result); |
| - DownloadAndWait(browser(), download_url, EXPECT_NO_SELECT_DIALOG); |
| + DownloadAndWaitWithoutDialog(browser(), download_url); |
| ui_test_utils::WindowedNotificationObserver signal( |
| NotificationType::BROWSER_CLOSED, |
| @@ -1394,7 +1481,7 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, AutoOpen) { |
| MockDownloadOpeningObserver observer( |
| browser()->profile()->GetDownloadManager()); |
| - DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); |
| + DownloadAndWaitWithoutDialog(browser(), url); |
| // Find the download and confirm it was opened. |
| std::vector<DownloadItem*> downloads; |