Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 DangerousDownloadAction dangerous_download_action) | 47 DangerousDownloadAction dangerous_download_action) |
| 48 : download_manager_(download_manager), | 48 : download_manager_(download_manager), |
| 49 wait_count_(wait_count), | 49 wait_count_(wait_count), |
| 50 finished_downloads_at_construction_(0), | 50 finished_downloads_at_construction_(0), |
| 51 waiting_(false), | 51 waiting_(false), |
| 52 download_finished_state_(download_finished_state), | 52 download_finished_state_(download_finished_state), |
| 53 finish_on_select_file_(finish_on_select_file), | 53 finish_on_select_file_(finish_on_select_file), |
| 54 select_file_dialog_seen_(false), | 54 select_file_dialog_seen_(false), |
| 55 dangerous_download_action_(dangerous_download_action) { | 55 dangerous_download_action_(dangerous_download_action) { |
| 56 download_manager_->AddObserver(this); // Will call initial ModelChanged(). | 56 download_manager_->AddObserver(this); // Will call initial ModelChanged(). |
| 57 finished_downloads_at_construction_ = finished_downloads_.size(); | 57 finished_downloads_at_construction_ = finished_downloads_.size(); |
|
Randy Smith (Not in Mondays)
2012/03/01 21:07:53
This member variable is now redundant with keeping
ahendrickson
2012/03/02 17:29:41
The list excludes items that match download_finish
| |
| 58 // Move the finished other downloads to an exclusion list. | |
| 59 finished_other_downloads_.swap(previously_finished_downloads_); | |
| 58 EXPECT_NE(DownloadItem::REMOVING, download_finished_state) | 60 EXPECT_NE(DownloadItem::REMOVING, download_finished_state) |
| 59 << "Waiting for REMOVING is not supported. Try COMPLETE."; | 61 << "Waiting for REMOVING is not supported. Try COMPLETE."; |
| 60 } | 62 } |
| 61 | 63 |
| 62 DownloadTestObserver::~DownloadTestObserver() { | 64 DownloadTestObserver::~DownloadTestObserver() { |
| 63 for (DownloadSet::iterator it = downloads_observed_.begin(); | 65 for (DownloadSet::iterator it = downloads_observed_.begin(); |
| 64 it != downloads_observed_.end(); ++it) | 66 it != downloads_observed_.end(); ++it) |
| 65 (*it)->RemoveObserver(this); | 67 (*it)->RemoveObserver(this); |
| 66 | 68 |
| 67 download_manager_->RemoveObserver(this); | 69 download_manager_->RemoveObserver(this); |
| 68 } | 70 } |
| 69 | 71 |
| 70 void DownloadTestObserver::WaitForFinished() { | 72 void DownloadTestObserver::WaitForFinished() { |
| 71 if (!IsFinished()) { | 73 if (!IsFinished()) { |
| 72 waiting_ = true; | 74 waiting_ = true; |
| 73 ui_test_utils::RunMessageLoop(); | 75 ui_test_utils::RunMessageLoop(); |
| 74 waiting_ = false; | 76 waiting_ = false; |
| 75 } | 77 } |
| 78 | |
| 79 // We do not expect to see downloads finishing in the wrong state. | |
| 80 EXPECT_EQ(0u, finished_other_downloads_.size()); | |
|
Randy Smith (Not in Mondays)
2012/03/01 21:07:53
This is an extra constraint on behavior. We may n
ahendrickson
2012/03/02 17:29:41
Replaced with a logging statement.
| |
| 76 } | 81 } |
| 77 | 82 |
| 78 bool DownloadTestObserver::IsFinished() const { | 83 bool DownloadTestObserver::IsFinished() const { |
| 79 if (finished_downloads_.size() - finished_downloads_at_construction_ >= | 84 size_t finished_download_count = |
| 80 wait_count_) | 85 finished_downloads_.size() + |
| 86 finished_other_downloads_.size() - | |
| 87 finished_downloads_at_construction_; | |
| 88 if (finished_download_count >= wait_count_) | |
| 81 return true; | 89 return true; |
| 82 return (finish_on_select_file_ && select_file_dialog_seen_); | 90 return (finish_on_select_file_ && select_file_dialog_seen_); |
| 83 } | 91 } |
| 84 | 92 |
| 85 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { | 93 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { |
| 86 // The REMOVING state indicates that the download is being destroyed. | 94 // The REMOVING state indicates that the download is being destroyed. |
| 87 // Stop observing. Do not do anything with it, as it is about to be gone. | 95 // Stop observing. Do not do anything with it, as it is about to be gone. |
| 88 if (download->GetState() == DownloadItem::REMOVING) { | 96 if (download->GetState() == DownloadItem::REMOVING) { |
| 89 DownloadSet::iterator it = downloads_observed_.find(download); | 97 DownloadSet::iterator it = downloads_observed_.find(download); |
| 90 ASSERT_TRUE(it != downloads_observed_.end()); | 98 ASSERT_TRUE(it != downloads_observed_.end()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 ADD_FAILURE() << "Unexpected dangerous download item."; | 132 ADD_FAILURE() << "Unexpected dangerous download item."; |
| 125 break; | 133 break; |
| 126 | 134 |
| 127 default: | 135 default: |
| 128 NOTREACHED(); | 136 NOTREACHED(); |
| 129 } | 137 } |
| 130 } | 138 } |
| 131 | 139 |
| 132 if (download->GetState() == download_finished_state_) { | 140 if (download->GetState() == download_finished_state_) { |
| 133 DownloadInFinalState(download); | 141 DownloadInFinalState(download); |
| 142 } else if (download->GetState() != DownloadItem::IN_PROGRESS) { | |
| 143 DownloadInTerminalState(download); | |
| 134 } | 144 } |
| 135 } | 145 } |
| 136 | 146 |
| 137 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { | 147 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { |
| 138 DCHECK_EQ(manager, download_manager_); | 148 DCHECK_EQ(manager, download_manager_); |
| 139 | 149 |
| 140 // Regenerate DownloadItem observers. If there are any download items | 150 // Regenerate DownloadItem observers. If there are any download items |
| 141 // in our final state, note them in |finished_downloads_| | 151 // in our final state, note them in |finished_downloads_| |
| 142 // (done by |OnDownloadUpdated()|). | 152 // (done by |OnDownloadUpdated()|). |
| 143 std::vector<DownloadItem*> downloads; | 153 std::vector<DownloadItem*> downloads; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 DownloadManager* manager, int32 /* id */) { | 185 DownloadManager* manager, int32 /* id */) { |
| 176 DCHECK_EQ(manager, download_manager_); | 186 DCHECK_EQ(manager, download_manager_); |
| 177 select_file_dialog_seen_ = true; | 187 select_file_dialog_seen_ = true; |
| 178 SignalIfFinished(); | 188 SignalIfFinished(); |
| 179 } | 189 } |
| 180 | 190 |
| 181 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { | 191 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { |
| 182 return dangerous_downloads_seen_.size(); | 192 return dangerous_downloads_seen_.size(); |
| 183 } | 193 } |
| 184 | 194 |
| 195 size_t DownloadTestObserver::NumOtherDownloadsSeen() const { | |
| 196 return finished_other_downloads_.size(); | |
| 197 } | |
| 198 | |
| 185 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { | 199 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { |
| 186 if (finished_downloads_.find(download) != finished_downloads_.end()) { | 200 if (finished_downloads_.find(download) != finished_downloads_.end()) { |
| 187 // We've already seen terminal state on this download. | 201 // We've already seen the final state on this download. |
| 188 return; | 202 return; |
| 189 } | 203 } |
| 190 | 204 |
| 191 // Record the transition. | 205 // Record the transition. |
| 192 finished_downloads_.insert(download); | 206 finished_downloads_.insert(download); |
| 193 | 207 |
| 194 SignalIfFinished(); | 208 SignalIfFinished(); |
| 195 } | 209 } |
| 196 | 210 |
| 211 void DownloadTestObserver::DownloadInTerminalState(DownloadItem* download) { | |
| 212 if (finished_downloads_.find(download) != finished_downloads_.end()) { | |
| 213 // We've already seen the final state on this download. | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 if (previously_finished_downloads_.find(download) != | |
| 218 previously_finished_downloads_.end()) { | |
| 219 // These occurred before we were constructed. | |
| 220 return; | |
| 221 } | |
| 222 | |
| 223 if (finished_other_downloads_.find(download) != | |
| 224 finished_other_downloads_.end()) { | |
| 225 // We've already seen the terminal state on this download. | |
| 226 return; | |
| 227 } | |
| 228 | |
| 229 // Record the transition to a final state. | |
| 230 finished_other_downloads_.insert(download); | |
| 231 | |
| 232 LOG(WARNING) << " " << __FUNCTION__ << "()" | |
| 233 << " download = " << download->DebugString(true); | |
| 234 | |
| 235 SignalIfFinished(); | |
| 236 } | |
| 237 | |
| 238 | |
| 197 void DownloadTestObserver::SignalIfFinished() { | 239 void DownloadTestObserver::SignalIfFinished() { |
| 198 if (waiting_ && IsFinished()) | 240 if (waiting_ && IsFinished()) |
| 199 MessageLoopForUI::current()->Quit(); | 241 MessageLoopForUI::current()->Quit(); |
| 200 } | 242 } |
| 201 | 243 |
| 202 DownloadTestFlushObserver::DownloadTestFlushObserver( | 244 DownloadTestFlushObserver::DownloadTestFlushObserver( |
| 203 DownloadManager* download_manager) | 245 DownloadManager* download_manager) |
| 204 : download_manager_(download_manager), | 246 : download_manager_(download_manager), |
| 205 waiting_for_zero_inprogress_(true) {} | 247 waiting_for_zero_inprogress_(true) {} |
| 206 | 248 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 292 void DownloadTestFlushObserver::PingIOThread(int cycle) { | 334 void DownloadTestFlushObserver::PingIOThread(int cycle) { |
| 293 if (--cycle) { | 335 if (--cycle) { |
| 294 BrowserThread::PostTask( | 336 BrowserThread::PostTask( |
| 295 BrowserThread::UI, FROM_HERE, | 337 BrowserThread::UI, FROM_HERE, |
| 296 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); | 338 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); |
| 297 } else { | 339 } else { |
| 298 BrowserThread::PostTask( | 340 BrowserThread::PostTask( |
| 299 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 341 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 300 } | 342 } |
| 301 } | 343 } |
| OLD | NEW |