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

Unified Diff: chrome/browser/download/download_test_observer.h

Issue 9568003: Fixed issue with DownloadTestObserver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Split DownloadTestObserver further. Created 8 years, 9 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_test_observer.h
diff --git a/chrome/browser/download/download_test_observer.h b/chrome/browser/download/download_test_observer.h
index 3404f425e3c9d6fc57beb11ee7ca6e49536a251b..9cafbd570982ef0e3df530325566e24337180ac7 100644
--- a/chrome/browser/download/download_test_observer.h
+++ b/chrome/browser/download/download_test_observer.h
@@ -13,12 +13,12 @@
#include "content/public/browser/download_item.h"
#include "content/public/browser/download_manager.h"
-// Construction of this class defines a system state, based on some number
-// of downloads being seen in a particular state + other events that
-// may occur in the download system. That state will be recorded if it
-// occurs at any point after construction. When that state occurs, the class
-// is considered finished. Callers may either probe for the finished state, or
-// wait on it.
+// Detects changes to the downloads after construction.
+// Finishes when one of the following happens:
+// - A specified number of downloads change to a terminal state (defined
+// in derived classes).
+// - Specific events, such as a select file dialog.
+// Callers may either probe for the finished state, or wait on it.
//
// TODO(rdsmith): Detect manager going down, remove pointer to
// DownloadManager, transition to finished. (For right now we
@@ -36,17 +36,13 @@ class DownloadTestObserver : public content::DownloadManager::Observer,
};
// Create an object that will be considered finished when |wait_count|
- // download items have entered state |download_finished_state|.
+ // download items have entered a terminal state.
// If |finish_on_select_file| is true, the object will also be
// considered finished if the DownloadManager raises a
// SelectFileDialogDisplayed() notification.
-
- // TODO(rdsmith): Consider rewriting the interface to take a list of events
- // to treat as completion events.
DownloadTestObserver(
content::DownloadManager* download_manager,
size_t wait_count,
- content::DownloadItem::DownloadState download_finished_state,
bool finish_on_select_file,
DangerousDownloadAction dangerous_download_action);
@@ -55,7 +51,7 @@ class DownloadTestObserver : public content::DownloadManager::Observer,
// State accessors.
bool select_file_dialog_seen() const { return select_file_dialog_seen_; }
- // Wait for whatever state was specified in the constructor.
+ // Wait for the requested number of downloads to enter a terminal state.
void WaitForFinished();
// Return true if everything's happened that we're configured for.
@@ -73,16 +69,17 @@ class DownloadTestObserver : public content::DownloadManager::Observer,
size_t NumDangerousDownloadsSeen() const;
- private:
+ size_t NumDownloadsSeenInState(
+ content::DownloadItem::DownloadState state) const;
Randy Smith (Not in Mondays) 2012/03/08 18:25:31 Up to you, but I'd be more inclined to leave this
ahendrickson 2012/03/08 20:56:34 It's here specifically for IN_PROGRESS tests, whic
Randy Smith (Not in Mondays) 2012/03/09 18:39:38 Ah, makes sense. Ok.
+
+ protected:
typedef std::set<content::DownloadItem*> DownloadSet;
- // Called when we know that a download item is in a final state.
- // Note that this is not the same as it first transitioning in to the
- // final state; multiple notifications may occur once the item is in
- // that state. So we keep our own track of transitions into final.
- void DownloadInFinalState(content::DownloadItem* download);
+ // Maps states to the number of times they have been encountered
+ typedef std::map<content::DownloadItem::DownloadState, size_t> StateMap;
- void SignalIfFinished();
+ // Called to see if a download item is in a final state.
+ virtual bool IsDownloadInFinalState(content::DownloadItem* download) = 0;
// The observed download manager.
scoped_refptr<content::DownloadManager> download_manager_;
benjhayden 2012/03/08 20:30:02 Would it be too much trouble to keep member fields
ahendrickson 2012/03/08 21:28:55 My preference would be to punt this to another CL,
Randy Smith (Not in Mondays) 2012/03/09 18:39:38 For this kind of issue, it's useful to look at whe
ahendrickson 2012/03/09 20:07:23 OK, done.
@@ -92,13 +89,11 @@ class DownloadTestObserver : public content::DownloadManager::Observer,
// reaches wait_count_, we're done.
DownloadSet finished_downloads_;
- // The set of DownloadItem's we are currently observing. Generally there
- // won't be any overlap with the above; once we see the final state
- // on a DownloadItem, we'll stop observing it.
- DownloadSet downloads_observed_;
-
- // The number of downloads to wait on completing.
- size_t wait_count_;
+ // The map of states to the number of times they have been observed since
+ // we started looking.
+ // Recorded at the time downloads_observed_ is recorded, but cleared in the
+ // constructor to exclude pre-existing states.
+ StateMap states_observed_;
// The number of downloads entered in final state in initial
// ModelChanged(). We use |finished_downloads_| to track the incoming
@@ -110,13 +105,27 @@ class DownloadTestObserver : public content::DownloadManager::Observer,
// to finished.
int finished_downloads_at_construction_;
+ private:
+ // Called when we know that a download item is in a final state.
+ // Note that this is not the same as it first transitioning in to the
+ // final state; multiple notifications may occur once the item is in
+ // that state. So we keep our own track of transitions into final.
+ void DownloadInFinalState(content::DownloadItem* download);
+
+ void SignalIfFinished();
+
+ // The set of DownloadItem's we are currently observing. Generally there
+ // won't be any overlap with the above; once we see the final state
+ // on a DownloadItem, we'll stop observing it.
+ DownloadSet downloads_observed_;
+
+ // The number of downloads to wait on completing.
+ size_t wait_count_;
+
// Whether an internal message loop has been started and must be quit upon
// all downloads completing.
bool waiting_;
- // The state on which to consider the DownloadItem finished.
- content::DownloadItem::DownloadState download_finished_state_;
-
// True if we should transition the DownloadTestObserver to finished if
// the select file dialog comes up.
bool finish_on_select_file_;
@@ -133,6 +142,49 @@ class DownloadTestObserver : public content::DownloadManager::Observer,
DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver);
};
+class DownloadTestObserverTerminal : public DownloadTestObserver {
+ public:
+ // Create an object that will be considered finished when |wait_count|
+ // download items have entered a terminal state (any but IN_PROGRESS).
+ // If |finish_on_select_file| is true, the object will also be
+ // considered finished if the DownloadManager raises a
+ // SelectFileDialogDisplayed() notification.
+ DownloadTestObserverTerminal(
+ content::DownloadManager* download_manager,
+ size_t wait_count,
+ bool finish_on_select_file,
+ DangerousDownloadAction dangerous_download_action);
+
+ virtual ~DownloadTestObserverTerminal();
+
+ private:
+ virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE;
+
+ DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal);
+};
+
+// Detects changes to the downloads after construction.
+// Finishes when one of the following happens:
+// - A specified number of downloads change to the IN_PROGRESS state.
Randy Smith (Not in Mondays) 2012/03/08 18:25:31 You haven't specified the behavior on a dangerous
ahendrickson 2012/03/08 20:56:34 I think we're in trouble if the select file dialog
Randy Smith (Not in Mondays) 2012/03/09 18:39:38 Confused, and I'd like to be unconfused before we
ahendrickson 2012/03/09 20:07:23 OK, added the parameter to DownloadObserverTestInP
+// Callers may either probe for the finished state, or wait on it.
+//
Randy Smith (Not in Mondays) 2012/03/08 18:25:31 nit: Could you nuke the blank comment line? I thi
ahendrickson 2012/03/08 20:56:34 Done.
+class DownloadTestObserverInProgress : public DownloadTestObserver {
+ public:
+ // Create an object that will be considered finished when |wait_count|
+ // download items have entered state |IN_PROGRESS|.
+
+ DownloadTestObserverInProgress(
+ content::DownloadManager* download_manager,
+ size_t wait_count);
+
+ virtual ~DownloadTestObserverInProgress();
+
+ private:
+ virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE;
+
+ DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress);
+};
+
// The WaitForFlush() method on this class returns after:
// * There are no IN_PROGRESS download items remaining on the
// DownloadManager.

Powered by Google App Engine
This is Rietveld 408576698