Chromium Code Reviews| Index: chrome/browser/download/download_test_observer.cc |
| diff --git a/chrome/browser/download/download_test_observer.cc b/chrome/browser/download/download_test_observer.cc |
| index 0cbcd0cea8b94433513d612add42506e05ecc87b..32bd770f960a215ac442a8f83f8edc5f22eec497 100644 |
| --- a/chrome/browser/download/download_test_observer.cc |
| +++ b/chrome/browser/download/download_test_observer.cc |
| @@ -19,9 +19,9 @@ using content::DownloadManager; |
| // These functions take scoped_refptr's to DownloadManager because they |
| // are posted to message queues, and hence may execute arbitrarily after |
| // their actual posting. Once posted, there is no connection between |
| -// these routines and the DownloadTestObserver class from which they came, |
| -// so the DownloadTestObserver's reference to the DownloadManager cannot |
| -// be counted on to keep the DownloadManager around. |
| +// these routines and the DownloadTestObserver class from which |
| +// they came, so the DownloadTestObserver's reference to the |
| +// DownloadManager cannot be counted on to keep the DownloadManager around. |
| // Fake user click on "Accept". |
| void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, |
| @@ -42,21 +42,15 @@ void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, |
| DownloadTestObserver::DownloadTestObserver( |
| DownloadManager* download_manager, |
| size_t wait_count, |
| - DownloadItem::DownloadState download_finished_state, |
| 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), |
| 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) |
| - << "Waiting for REMOVING is not supported. Try COMPLETE."; |
| } |
| DownloadTestObserver::~DownloadTestObserver() { |
| @@ -67,6 +61,12 @@ DownloadTestObserver::~DownloadTestObserver() { |
| download_manager_->RemoveObserver(this); |
| } |
| +void DownloadTestObserver::Init() { |
| + download_manager_->AddObserver(this); // Will call initial ModelChanged(). |
| + finished_downloads_at_construction_ = finished_downloads_.size(); |
| + states_observed_.clear(); |
| +} |
| + |
| void DownloadTestObserver::WaitForFinished() { |
| if (!IsFinished()) { |
| waiting_ = true; |
| @@ -129,9 +129,8 @@ void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { |
| } |
| } |
| - if (download->GetState() == download_finished_state_) { |
| + if (IsDownloadInFinalState(download)) |
| DownloadInFinalState(download); |
| - } |
| } |
| void DownloadTestObserver::ModelChanged(DownloadManager* manager) { |
| @@ -182,15 +181,28 @@ size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { |
| return dangerous_downloads_seen_.size(); |
| } |
| +size_t DownloadTestObserver::NumDownloadsSeenInState( |
| + content::DownloadItem::DownloadState state) const { |
| + StateMap::const_iterator it = states_observed_.find(state); |
| + |
| + if (it == states_observed_.end()) |
| + return 0; |
| + |
| + return it->second; |
| +} |
| + |
| void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { |
| if (finished_downloads_.find(download) != finished_downloads_.end()) { |
| - // We've already seen terminal state on this download. |
| + // We've already seen the final state on this download. |
| return; |
| } |
| // Record the transition. |
| finished_downloads_.insert(download); |
| + // Record the state. |
| + states_observed_[download->GetState()]++; // Initializes to 0 the first time. |
| + |
| SignalIfFinished(); |
| } |
| @@ -199,6 +211,55 @@ void DownloadTestObserver::SignalIfFinished() { |
| MessageLoopForUI::current()->Quit(); |
| } |
| +DownloadTestObserverTerminal::DownloadTestObserverTerminal( |
| + content::DownloadManager* download_manager, |
| + size_t wait_count, |
| + bool finish_on_select_file, |
| + DangerousDownloadAction dangerous_download_action) |
| + : DownloadTestObserver(download_manager, |
| + wait_count, |
| + finish_on_select_file, |
| + dangerous_download_action) { |
| + // You can't override virtual functions in a base class constructor; the |
|
Randy Smith (Not in Mondays)
2012/03/12 01:52:15
wording nit: "You can't rely on overridden functio
ahendrickson
2012/03/12 03:13:54
Done.
|
| + // virtual function table hasn't been set up yet. So, we have to do any |
| + // work that depends on those functions in the derived class constructor |
| + // instead. In this case, it's because of |IsDownloadInFinalState()|. |
| + Init(); |
| +} |
| + |
| +DownloadTestObserverTerminal::~DownloadTestObserverTerminal() { |
| +} |
| + |
| + |
| +bool DownloadTestObserverTerminal::IsDownloadInFinalState( |
| + content::DownloadItem* download) { |
| + return (download->GetState() != DownloadItem::IN_PROGRESS); |
| +} |
| + |
| +DownloadTestObserverInProgress::DownloadTestObserverInProgress( |
| + content::DownloadManager* download_manager, |
| + size_t wait_count, |
| + bool finish_on_select_file) |
| + : DownloadTestObserver(download_manager, |
| + wait_count, |
| + finish_on_select_file, |
| + ON_DANGEROUS_DOWNLOAD_ACCEPT) { |
| + // You can't override virtual functions in a base class constructor; the |
| + // virtual function table hasn't been set up yet. So, we have to do any |
| + // work that depends on those functions in the derived class constructor |
| + // instead. In this case, it's because of |IsDownloadInFinalState()|. |
| + Init(); |
| +} |
| + |
| +DownloadTestObserverInProgress::~DownloadTestObserverInProgress() { |
| +} |
| + |
| + |
| +bool DownloadTestObserverInProgress::IsDownloadInFinalState( |
| + content::DownloadItem* download) { |
| + return (download->GetState() == DownloadItem::IN_PROGRESS); |
| +} |
| + |
| DownloadTestFlushObserver::DownloadTestFlushObserver( |
| DownloadManager* download_manager) |
| : download_manager_(download_manager), |