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

Unified Diff: chrome/browser/download/download_browsertest.cc

Issue 7294013: Modified cancel and interrupt processing to avoid race with history. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 a486ae811df1d72dddf3aedef3b733c95f5807b8..b6456648e6a197f81f9c700be083130295bd3417 100644
--- a/chrome/browser/download/download_browsertest.cc
+++ b/chrome/browser/download/download_browsertest.cc
@@ -56,6 +56,7 @@ const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx"));
enum DangerousDownloadAction {
ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download
ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download
+ ON_DANGEROUS_DOWNLOAD_IGNORE, // Don't do anything; calling code will handle.
ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen
};
@@ -71,7 +72,7 @@ void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager,
int32 download_id) {
DownloadItem* download = download_manager->GetDownloadItem(download_id);
ASSERT_TRUE(download->IsPartialDownload());
- download->Cancel(true);
+ download->Cancel();
download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
}
@@ -90,8 +91,10 @@ void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager,
class DownloadsObserver : public DownloadManager::Observer,
public DownloadItem::Observer {
public:
+ typedef std::set<DownloadItem::DownloadState> StateSet;
+
// Create an object that will be considered finished when |wait_count|
- // download items have entered state |download_finished_state|.
+ // download items have entered any states in |download_finished_states|.
// If |finish_on_select_file| is true, the object will also be
// considered finished if the DownloadManager raises a
// SelectFileDialogDisplayed() notification.
@@ -100,20 +103,21 @@ class DownloadsObserver : public DownloadManager::Observer,
// to treat as completion events.
DownloadsObserver(DownloadManager* download_manager,
size_t wait_count,
- DownloadItem::DownloadState download_finished_state,
+ StateSet download_finished_states,
bool finish_on_select_file,
DangerousDownloadAction dangerous_download_action)
: download_manager_(download_manager),
wait_count_(wait_count),
finished_downloads_at_construction_(0),
waiting_(false),
- download_finished_state_(download_finished_state),
+ download_finished_states_(download_finished_states),
finish_on_select_file_(finish_on_select_file),
select_file_dialog_seen_(false),
dangerous_download_action_(dangerous_download_action) {
download_manager_->AddObserver(this); // Will call initial ModelChanged().
finished_downloads_at_construction_ = finished_downloads_.size();
- EXPECT_NE(DownloadItem::REMOVING, download_finished_state)
+ EXPECT_TRUE(download_finished_states.find(DownloadItem::REMOVING) ==
+ download_finished_states.end())
<< "Waiting for REMOVING is not supported. Try COMPLETE.";
}
@@ -192,12 +196,16 @@ class DownloadsObserver : public DownloadManager::Observer,
ADD_FAILURE() << "Unexpected dangerous download item.";
break;
+ case ON_DANGEROUS_DOWNLOAD_IGNORE:
+ break;
+
default:
NOTREACHED();
}
}
- if (download->state() == download_finished_state_) {
+ if (download_finished_states_.find(download->state()) !=
+ download_finished_states_.end()) {
DownloadInFinalState(download);
}
}
@@ -300,8 +308,8 @@ class DownloadsObserver : public DownloadManager::Observer,
// all downloads completing.
bool waiting_;
- // The state on which to consider the DownloadItem finished.
- DownloadItem::DownloadState download_finished_state_;
+ // The states on which to consider the DownloadItem finished.
+ StateSet download_finished_states_;
// True if we should transition the DownloadsObserver to finished if
// the select file dialog comes up.
@@ -489,6 +497,16 @@ class CancelTestDataCollector
DISALLOW_COPY_AND_ASSIGN(CancelTestDataCollector);
};
+static const DownloadItem::DownloadState kTerminalStates[] = {
+ DownloadItem::CANCELLED,
+ DownloadItem::INTERRUPTED,
+ DownloadItem::COMPLETE,
+};
+
+static const DownloadItem::DownloadState kInProgressStates[] = {
+ DownloadItem::IN_PROGRESS,
+};
+
class DownloadTest : public InProcessBrowserTest {
public:
enum SelectExpectation {
@@ -565,6 +583,12 @@ class DownloadTest : public InProcessBrowserTest {
return true;
}
+ // For tests that want to test system reaction to files
+ // going away underneath them.
+ void DeleteDownloadsDirectory() {
+ EXPECT_TRUE(downloads_directory_.Delete());
+ }
+
DownloadPrefs* GetDownloadPrefs(Browser* browser) {
return browser->profile()->GetDownloadManager()->download_prefs();
}
@@ -582,12 +606,29 @@ class DownloadTest : public InProcessBrowserTest {
browser->profile()->GetDownloadManager();
return new DownloadsObserver(
download_manager, num_downloads,
- DownloadItem::COMPLETE, // Really done
- false, // Bail on select file
+ DownloadsObserver::StateSet(
+ kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
+ true, // Bail on select file
ON_DANGEROUS_DOWNLOAD_FAIL);
}
// Create a DownloadsObserver that will wait for the
+ // specified number of downloads to finish, and is
+ // ok with dangerous downloads. Note that use of this
+ // waiter is conditional on accepting the dangerous download.
+ DownloadsObserver* CreateDangerousWaiter(
+ Browser* browser, int num_downloads) {
+ DownloadManager* download_manager =
+ browser->profile()->GetDownloadManager();
+ return new DownloadsObserver(
+ download_manager, num_downloads,
+ DownloadsObserver::StateSet(
+ kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
+ true, // Bail on select file
+ ON_DANGEROUS_DOWNLOAD_IGNORE);
+ }
+
+ // Create a DownloadsObserver that will wait for the
// specified number of downloads to start.
DownloadsObserver* CreateInProgressWaiter(Browser* browser,
int num_downloads) {
@@ -595,9 +636,11 @@ class DownloadTest : public InProcessBrowserTest {
browser->profile()->GetDownloadManager();
return new DownloadsObserver(
download_manager, num_downloads,
- DownloadItem::IN_PROGRESS, // Has started
+ DownloadsObserver::StateSet(
+ kInProgressStates,
+ kInProgressStates + arraysize(kInProgressStates)),
true, // Bail on select file
- ON_DANGEROUS_DOWNLOAD_FAIL);
+ ON_DANGEROUS_DOWNLOAD_IGNORE);
}
// Create a DownloadsObserver that will wait for the
@@ -609,11 +652,14 @@ class DownloadTest : public InProcessBrowserTest {
DownloadItem::DownloadState final_state,
DangerousDownloadAction dangerous_download_action) {
+ DownloadsObserver::StateSet states;
+ states.insert(final_state);
+
DownloadManager* download_manager =
browser->profile()->GetDownloadManager();
return new DownloadsObserver(
download_manager, num_downloads,
- final_state,
+ states,
true, // Bail on select file
dangerous_download_action);
}
@@ -965,13 +1011,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) {
#endif
// 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
-// download directory because of http://crbug.com/62099. No big loss; it
-// was primarily confirming DownloadsObserver wait on select file dialog
-// functionality anyway.
-IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
+// type. Confirm that we can cancel the download in that state.
+IN_PROC_BROWSER_TEST_F(DownloadTest, CancelFromFileSelection) {
ASSERT_TRUE(InitialSetup(true));
FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
@@ -980,6 +1021,107 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
// due to the MIME type.
DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
+ std::vector<DownloadItem*> active_downloads, history_downloads;
+ browser()->profile()->GetDownloadManager()->GetInProgressDownloads(
+ &active_downloads);
+ ASSERT_EQ(1u, active_downloads.size());
+ EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &history_downloads);
+ EXPECT_EQ(0u, history_downloads.size());
+
+ // This should remove the download as it hasn't yet been entered into
+ // the history.
+ active_downloads[0]->Cancel();
+
+ active_downloads.clear();
+ history_downloads.clear();
+ browser()->profile()->GetDownloadManager()->GetInProgressDownloads(
+ &active_downloads);
+ EXPECT_EQ(0u, active_downloads.size());
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &history_downloads);
+ EXPECT_EQ(0u, history_downloads.size());
+
+ // Check state.
+ EXPECT_EQ(1, browser()->tab_count());
+ // 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);
+}
+
+// Put up a Select File dialog when the file is downloaded, due to its MIME
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 These comments about select file dialog appearing
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+// type. Confirm that we can remove the download in that state.
+IN_PROC_BROWSER_TEST_F(DownloadTest, RemoveFromFileSelection) {
+ ASSERT_TRUE(InitialSetup(true));
+ FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
+ GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
+
+ // Download the file and wait. We expect the Select File dialog to appear
+ // due to the MIME type.
+ DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
+
+ std::vector<DownloadItem*> active_downloads, history_downloads;
+ browser()->profile()->GetDownloadManager()->GetInProgressDownloads(
+ &active_downloads);
+ ASSERT_EQ(1u, active_downloads.size());
+ EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &history_downloads);
+ EXPECT_EQ(0u, history_downloads.size());
+
+ active_downloads[0]->Remove();
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Comment: Confirm the file can be successfully remo
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+
+ active_downloads.clear();
+ history_downloads.clear();
+ browser()->profile()->GetDownloadManager()->GetInProgressDownloads(
+ &active_downloads);
+ EXPECT_EQ(0u, active_downloads.size());
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &history_downloads);
+ EXPECT_EQ(0u, history_downloads.size());
+
+ // Check state.
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Remove comment.
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+ EXPECT_EQ(1, browser()->tab_count());
+ // 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);
+}
+
+// Put up a Select File dialog when the file is downloaded, due to its MIME
+// type. Confirm that an error coming in from the network works properly
+// when in that state.
+IN_PROC_BROWSER_TEST_F(DownloadTest, InterruptFromFileSelection) {
+ ASSERT_TRUE(InitialSetup(true));
+ GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
+
+ // Download the file and wait. We expect the Select File dialog to appear
+ // due to the MIME type.
+ DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
+
+ std::vector<DownloadItem*> active_downloads, history_downloads;
+ browser()->profile()->GetDownloadManager()->GetInProgressDownloads(
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Consider making a download manager accessor. A bi
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Created new and used existing accessors for what I
+ &active_downloads);
+ ASSERT_EQ(1u, active_downloads.size());
+ EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &history_downloads);
+ EXPECT_EQ(0u, history_downloads.size());
+
+ // Complete the download with error.
+ GURL error_url(URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
+ ui_test_utils::NavigateToURL(browser(), error_url);
+ MessageLoopForUI::current()->RunAllPending();
+
+ active_downloads.clear();
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Comment: Confirm that a download error before entr
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+ history_downloads.clear();
+ browser()->profile()->GetDownloadManager()->GetInProgressDownloads(
+ &active_downloads);
+ EXPECT_EQ(0u, active_downloads.size());
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &history_downloads);
+ EXPECT_EQ(0u, history_downloads.size());
+
// Check state.
EXPECT_EQ(1, browser()->tab_count());
// Since we exited while the Select File dialog was visible, there should not
@@ -1440,6 +1582,184 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadCancelled) {
CheckDownloadUIVisible(browser(), false, true);
}
+IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerous) {
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Comment explaining test.
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+ ASSERT_TRUE(InitialSetup(false));
+ FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
+ GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
+
+ EXPECT_EQ(1, browser()->tab_count());
+
+ scoped_ptr<DownloadsObserver> observer(
+ CreateInProgressWaiter(browser(), 1));
+ ui_test_utils::NavigateToURL(browser(), url);
+ observer->WaitForFinished();
+
+ // We should have one download, in history, and it should
+ // still be dangerous.
+ std::vector<DownloadItem*> downloads;
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(1u, downloads.size());
+ DownloadItem* download = downloads[0];
+ EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
+ EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
+ EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
+ CheckDownloadUIVisible(browser(), true, true);
+
+ // See if accepting it does the right thing.
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Specify what the right thing is.
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+ scoped_ptr<DownloadsObserver> completion_observer(
+ CreateDangerousWaiter(browser(), 1));
+ AcceptDangerousDownload(browser()->profile()->GetDownloadManager(),
+ download->id());
+ completion_observer->WaitForFinished();
+
+ downloads.clear();
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(1u, downloads.size());
+ ASSERT_EQ(downloads[0], download);
+ EXPECT_EQ(DownloadItem::COMPLETE, download->state());
+ EXPECT_EQ(DownloadItem::DANGEROUS_BUT_VALIDATED, download->safety_state());
+ CheckDownloadUIVisible(browser(), true, true);
+}
+
+IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerousFileError) {
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Comment describing test.
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+ ASSERT_TRUE(InitialSetup(false));
+ FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
+ GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
+
+ EXPECT_EQ(1, browser()->tab_count());
+
+ scoped_ptr<DownloadsObserver> observer(
+ CreateInProgressWaiter(browser(), 1));
+ ui_test_utils::NavigateToURL(browser(), url);
+ observer->WaitForFinished();
+
+ // We should have one download, in history, and it should
+ // still be dangerous.
+ std::vector<DownloadItem*> downloads;
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(1u, downloads.size());
+ DownloadItem* download = downloads[0];
+ EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
+ EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
+ EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
+ CheckDownloadUIVisible(browser(), true, true);
+
+ // Accept it after nuking the directory into which it's being downloaded;
+ // that should complete the download with an error.
+ DeleteDownloadsDirectory();
+ scoped_ptr<DownloadsObserver> completion_observer(
+ CreateDangerousWaiter(browser(), 1));
+ AcceptDangerousDownload(browser()->profile()->GetDownloadManager(),
+ download->id());
+ completion_observer->WaitForFinished();
+
+ downloads.clear();
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(1u, downloads.size());
+ ASSERT_EQ(downloads[0], download);
+ // Persistent errors currently -> CANCELLED.
+ EXPECT_EQ(DownloadItem::CANCELLED, download->state());
+ EXPECT_EQ(DownloadItem::DANGEROUS_BUT_VALIDATED, download->safety_state());
+ CheckDownloadUIVisible(browser(), true, true);
+}
+
+IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerousDecline) {
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Comment explaining test.
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+ ASSERT_TRUE(InitialSetup(false));
+ FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
+ GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
+
+ EXPECT_EQ(1, browser()->tab_count());
+
+ scoped_ptr<DownloadsObserver> observer(
+ CreateInProgressWaiter(browser(), 1));
+ ui_test_utils::NavigateToURL(browser(), url);
+ observer->WaitForFinished();
+
+ // We should have one download, in history, and it should
+ // still be dangerous.
+ std::vector<DownloadItem*> downloads;
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(1u, downloads.size());
+ DownloadItem* download = downloads[0];
+ EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
+ EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
+ EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
+ CheckDownloadUIVisible(browser(), true, true);
+
+ // See if declining it does the right thing.
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 Specify what the right thing is.
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Just deleted comment; initial comment has all need
+ DenyDangerousDownload(browser()->profile()->GetDownloadManager(),
+ download->id());
+
+ downloads.clear();
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(0u, downloads.size());
+ CheckDownloadUIVisible(browser(), false, true);
+}
+
+
+
+// Fail a download with a network error partway through, and make sure the
+// correct things happen.
Randy Smith (Not in Mondays) 2011/06/30 23:05:13 More specific about what the correct things are.
Randy Smith (Not in Mondays) 2011/07/05 20:28:44 Done.
+IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadInterrupted) {
+ ASSERT_TRUE(InitialSetup(false));
+ GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
+
+ scoped_ptr<DownloadsObserver> observer(
+ CreateInProgressWaiter(browser(), 1));
+ ui_test_utils::NavigateToURL(browser(), url);
+ observer->WaitForFinished();
+
+ std::vector<DownloadItem*> downloads;
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(1u, downloads.size());
+ ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state());
+ CheckDownloadUIVisible(browser(), true, true);
+
+ // Fail the download
+ GURL error_url(URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
+ ui_test_utils::NavigateToURL(browser(), error_url);
+ MessageLoopForUI::current()->RunAllPending();
+
+ // Should still be visible, with INTERRUPTED state.
+ downloads.clear();
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(1u, downloads.size());
+ DownloadItem* download = downloads[0];
+ ASSERT_EQ(DownloadItem::INTERRUPTED, download->state());
+ CheckDownloadUIVisible(browser(), true, true);
+
+ // Confirm cancel does nothing.
+ download->Cancel();
+ MessageLoopForUI::current()->RunAllPending();
+
+ downloads.clear();
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(1u, downloads.size());
+ ASSERT_EQ(download, downloads[0]);
+ ASSERT_EQ(DownloadItem::INTERRUPTED, download->state());
+ CheckDownloadUIVisible(browser(), true, true);
+
+ // Confirm remove gets rid of it.
+ download->Remove();
+ download = NULL;
+ MessageLoopForUI::current()->RunAllPending();
+
+ downloads.clear();
+ browser()->profile()->GetDownloadManager()->SearchDownloads(
+ string16(), &downloads);
+ ASSERT_EQ(0u, downloads.size());
+ CheckDownloadUIVisible(browser(), false, true);
+}
+
// Confirm a download makes it into the history properly.
IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
ASSERT_TRUE(InitialSetup(false));

Powered by Google App Engine
This is Rietveld 408576698