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" |
| 11 #include "chrome/browser/download/download_test_observer.h" | 11 #include "chrome/browser/download/download_test_observer.h" |
| 12 #include "chrome/test/base/ui_test_utils.h" | 12 #include "chrome/test/base/ui_test_utils.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 | 14 |
| 15 using content::BrowserThread; | 15 using content::BrowserThread; |
| 16 using content::DownloadItem; | 16 using content::DownloadItem; |
| 17 using content::DownloadManager; | 17 using content::DownloadManager; |
| 18 | 18 |
| 19 // These functions take scoped_refptr's to DownloadManager because they | 19 // These functions take scoped_refptr's to DownloadManager because they |
| 20 // are posted to message queues, and hence may execute arbitrarily after | 20 // are posted to message queues, and hence may execute arbitrarily after |
| 21 // their actual posting. Once posted, there is no connection between | 21 // their actual posting. Once posted, there is no connection between |
| 22 // these routines and the DownloadTestObserver class from which they came, | 22 // these routines and the DownloadTestObserver class from which |
| 23 // so the DownloadTestObserver's reference to the DownloadManager cannot | 23 // they came, so the DownloadTestObserver's reference to the |
| 24 // be counted on to keep the DownloadManager around. | 24 // DownloadManager cannot be counted on to keep the DownloadManager around. |
| 25 | 25 |
| 26 // Fake user click on "Accept". | 26 // Fake user click on "Accept". |
| 27 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, | 27 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, |
| 28 int32 download_id) { | 28 int32 download_id) { |
| 29 DownloadItem* download = download_manager->GetDownloadItem(download_id); | 29 DownloadItem* download = download_manager->GetDownloadItem(download_id); |
| 30 download->DangerousDownloadValidated(); | 30 download->DangerousDownloadValidated(); |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Fake user click on "Deny". | 33 // Fake user click on "Deny". |
| 34 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, | 34 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, |
| 35 int32 download_id) { | 35 int32 download_id) { |
| 36 DownloadItem* download = download_manager->GetDownloadItem(download_id); | 36 DownloadItem* download = download_manager->GetDownloadItem(download_id); |
| 37 ASSERT_TRUE(download->IsPartialDownload()); | 37 ASSERT_TRUE(download->IsPartialDownload()); |
| 38 download->Cancel(true); | 38 download->Cancel(true); |
| 39 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 39 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 40 } | 40 } |
| 41 | 41 |
| 42 DownloadTestObserver::DownloadTestObserver( | 42 DownloadTestObserver::DownloadTestObserver( |
| 43 DownloadManager* download_manager, | 43 DownloadManager* download_manager, |
| 44 size_t wait_count, | 44 size_t wait_count, |
| 45 DownloadItem::DownloadState download_finished_state, | |
| 46 bool finish_on_select_file, | 45 bool finish_on_select_file, |
| 47 DangerousDownloadAction dangerous_download_action) | 46 DangerousDownloadAction dangerous_download_action) |
| 48 : download_manager_(download_manager), | 47 : download_manager_(download_manager), |
| 49 wait_count_(wait_count), | 48 wait_count_(wait_count), |
| 50 finished_downloads_at_construction_(0), | 49 finished_downloads_at_construction_(0), |
| 51 waiting_(false), | 50 waiting_(false), |
| 52 download_finished_state_(download_finished_state), | |
| 53 finish_on_select_file_(finish_on_select_file), | 51 finish_on_select_file_(finish_on_select_file), |
| 54 select_file_dialog_seen_(false), | 52 select_file_dialog_seen_(false), |
| 55 dangerous_download_action_(dangerous_download_action) { | 53 dangerous_download_action_(dangerous_download_action) { |
| 56 download_manager_->AddObserver(this); // Will call initial ModelChanged(). | |
| 57 finished_downloads_at_construction_ = finished_downloads_.size(); | |
| 58 EXPECT_NE(DownloadItem::REMOVING, download_finished_state) | |
| 59 << "Waiting for REMOVING is not supported. Try COMPLETE."; | |
| 60 } | 54 } |
| 61 | 55 |
| 62 DownloadTestObserver::~DownloadTestObserver() { | 56 DownloadTestObserver::~DownloadTestObserver() { |
| 63 for (DownloadSet::iterator it = downloads_observed_.begin(); | 57 for (DownloadSet::iterator it = downloads_observed_.begin(); |
| 64 it != downloads_observed_.end(); ++it) | 58 it != downloads_observed_.end(); ++it) |
| 65 (*it)->RemoveObserver(this); | 59 (*it)->RemoveObserver(this); |
| 66 | 60 |
| 67 download_manager_->RemoveObserver(this); | 61 download_manager_->RemoveObserver(this); |
| 68 } | 62 } |
| 69 | 63 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 | 116 |
| 123 case ON_DANGEROUS_DOWNLOAD_FAIL: | 117 case ON_DANGEROUS_DOWNLOAD_FAIL: |
| 124 ADD_FAILURE() << "Unexpected dangerous download item."; | 118 ADD_FAILURE() << "Unexpected dangerous download item."; |
| 125 break; | 119 break; |
| 126 | 120 |
| 127 default: | 121 default: |
| 128 NOTREACHED(); | 122 NOTREACHED(); |
| 129 } | 123 } |
| 130 } | 124 } |
| 131 | 125 |
| 132 if (download->GetState() == download_finished_state_) { | 126 if (IsDownloadInFinalState(download)) |
| 133 DownloadInFinalState(download); | 127 DownloadInFinalState(download); |
| 134 } | |
| 135 } | 128 } |
| 136 | 129 |
| 137 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { | 130 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { |
| 138 DCHECK_EQ(manager, download_manager_); | 131 DCHECK_EQ(manager, download_manager_); |
| 139 | 132 |
| 140 // Regenerate DownloadItem observers. If there are any download items | 133 // Regenerate DownloadItem observers. If there are any download items |
| 141 // in our final state, note them in |finished_downloads_| | 134 // in our final state, note them in |finished_downloads_| |
| 142 // (done by |OnDownloadUpdated()|). | 135 // (done by |OnDownloadUpdated()|). |
| 143 std::vector<DownloadItem*> downloads; | 136 std::vector<DownloadItem*> downloads; |
| 144 download_manager_->GetAllDownloads(FilePath(), &downloads); | 137 download_manager_->GetAllDownloads(FilePath(), &downloads); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 175 DownloadManager* manager, int32 /* id */) { | 168 DownloadManager* manager, int32 /* id */) { |
| 176 DCHECK_EQ(manager, download_manager_); | 169 DCHECK_EQ(manager, download_manager_); |
| 177 select_file_dialog_seen_ = true; | 170 select_file_dialog_seen_ = true; |
| 178 SignalIfFinished(); | 171 SignalIfFinished(); |
| 179 } | 172 } |
| 180 | 173 |
| 181 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { | 174 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { |
| 182 return dangerous_downloads_seen_.size(); | 175 return dangerous_downloads_seen_.size(); |
| 183 } | 176 } |
| 184 | 177 |
| 178 size_t DownloadTestObserver::NumDownloadsSeenInState( | |
| 179 content::DownloadItem::DownloadState state) const { | |
| 180 StateMap::const_iterator it = states_observed_.find(state); | |
| 181 | |
| 182 if (it == states_observed_.end()) | |
| 183 return 0; | |
| 184 | |
| 185 return it->second; | |
| 186 } | |
| 187 | |
| 185 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { | 188 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { |
| 186 if (finished_downloads_.find(download) != finished_downloads_.end()) { | 189 if (finished_downloads_.find(download) != finished_downloads_.end()) { |
| 187 // We've already seen terminal state on this download. | 190 // We've already seen the final state on this download. |
| 188 return; | 191 return; |
| 189 } | 192 } |
| 190 | 193 |
| 191 // Record the transition. | 194 // Record the transition. |
| 192 finished_downloads_.insert(download); | 195 finished_downloads_.insert(download); |
| 193 | 196 |
| 197 // Record the state. | |
| 198 states_observed_[download->GetState()]++; // Initializes to 0 the first time. | |
| 199 | |
| 194 SignalIfFinished(); | 200 SignalIfFinished(); |
| 195 } | 201 } |
| 196 | 202 |
| 197 void DownloadTestObserver::SignalIfFinished() { | 203 void DownloadTestObserver::SignalIfFinished() { |
| 198 if (waiting_ && IsFinished()) | 204 if (waiting_ && IsFinished()) |
| 199 MessageLoopForUI::current()->Quit(); | 205 MessageLoopForUI::current()->Quit(); |
| 200 } | 206 } |
| 201 | 207 |
| 208 DownloadTestObserverTerminal::DownloadTestObserverTerminal( | |
| 209 content::DownloadManager* download_manager, | |
| 210 size_t wait_count, | |
| 211 bool finish_on_select_file, | |
| 212 DangerousDownloadAction dangerous_download_action) | |
| 213 : DownloadTestObserver(download_manager, | |
| 214 wait_count, | |
| 215 finish_on_select_file, | |
| 216 dangerous_download_action) { | |
| 217 download_manager_->AddObserver(this); // Will call initial ModelChanged(). | |
| 218 finished_downloads_at_construction_ = finished_downloads_.size(); | |
| 219 states_observed_.clear(); | |
|
Randy Smith (Not in Mondays)
2012/03/08 18:25:31
Completely up to you, but why duplicate this code
ahendrickson
2012/03/08 20:56:34
This one is a bit tricky: In the base class const
Randy Smith (Not in Mondays)
2012/03/09 18:39:38
Ah, good catch. Yikes. Would you comment that?
ahendrickson
2012/03/09 20:07:23
I *had* comments to this effect, but they seem to
| |
| 220 } | |
| 221 | |
| 222 DownloadTestObserverTerminal::~DownloadTestObserverTerminal() { | |
| 223 } | |
| 224 | |
| 225 | |
| 226 bool DownloadTestObserverTerminal::IsDownloadInFinalState( | |
| 227 content::DownloadItem* download) { | |
| 228 return (download->GetState() != DownloadItem::IN_PROGRESS); | |
| 229 } | |
| 230 | |
| 231 DownloadTestObserverInProgress::DownloadTestObserverInProgress( | |
| 232 content::DownloadManager* download_manager, | |
| 233 size_t wait_count) | |
| 234 : DownloadTestObserver(download_manager, | |
| 235 wait_count, | |
| 236 true, | |
| 237 ON_DANGEROUS_DOWNLOAD_FAIL) { | |
|
Randy Smith (Not in Mondays)
2012/03/08 18:25:31
Why FAIL rather than IGNORE? Dangerous downloads
ahendrickson
2012/03/08 20:56:34
I don't think a download class can be declared dan
Randy Smith (Not in Mondays)
2012/03/09 18:39:38
My model of the transitions say that it can happen
ahendrickson
2012/03/09 20:07:23
Your model is correct.
The header file has the co
| |
| 238 download_manager_->AddObserver(this); // Will call initial ModelChanged(). | |
| 239 finished_downloads_at_construction_ = finished_downloads_.size(); | |
| 240 states_observed_.clear(); | |
| 241 } | |
| 242 | |
| 243 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() { | |
| 244 } | |
| 245 | |
| 246 | |
| 247 bool DownloadTestObserverInProgress::IsDownloadInFinalState( | |
| 248 content::DownloadItem* download) { | |
| 249 return (download->GetState() == DownloadItem::IN_PROGRESS); | |
| 250 } | |
| 251 | |
| 202 DownloadTestFlushObserver::DownloadTestFlushObserver( | 252 DownloadTestFlushObserver::DownloadTestFlushObserver( |
| 203 DownloadManager* download_manager) | 253 DownloadManager* download_manager) |
| 204 : download_manager_(download_manager), | 254 : download_manager_(download_manager), |
| 205 waiting_for_zero_inprogress_(true) {} | 255 waiting_for_zero_inprogress_(true) {} |
| 206 | 256 |
| 207 void DownloadTestFlushObserver::WaitForFlush() { | 257 void DownloadTestFlushObserver::WaitForFlush() { |
| 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 209 download_manager_->AddObserver(this); | 259 download_manager_->AddObserver(this); |
| 210 ui_test_utils::RunMessageLoop(); | 260 ui_test_utils::RunMessageLoop(); |
| 211 } | 261 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 292 void DownloadTestFlushObserver::PingIOThread(int cycle) { | 342 void DownloadTestFlushObserver::PingIOThread(int cycle) { |
| 293 if (--cycle) { | 343 if (--cycle) { |
| 294 BrowserThread::PostTask( | 344 BrowserThread::PostTask( |
| 295 BrowserThread::UI, FROM_HERE, | 345 BrowserThread::UI, FROM_HERE, |
| 296 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); | 346 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); |
| 297 } else { | 347 } else { |
| 298 BrowserThread::PostTask( | 348 BrowserThread::PostTask( |
| 299 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 349 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 300 } | 350 } |
| 301 } | 351 } |
| OLD | NEW |