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 24 matching lines...) Expand all Loading... | |
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(). | 54 download_manager_->AddObserver(this); // Will call initial ModelChanged(). |
57 finished_downloads_at_construction_ = finished_downloads_.size(); | 55 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 } | 56 } |
61 | 57 |
62 DownloadTestObserver::~DownloadTestObserver() { | 58 DownloadTestObserver::~DownloadTestObserver() { |
63 for (DownloadSet::iterator it = downloads_observed_.begin(); | 59 for (DownloadSet::iterator it = downloads_observed_.begin(); |
64 it != downloads_observed_.end(); ++it) | 60 it != downloads_observed_.end(); ++it) |
65 (*it)->RemoveObserver(this); | 61 (*it)->RemoveObserver(this); |
66 | 62 |
67 download_manager_->RemoveObserver(this); | 63 download_manager_->RemoveObserver(this); |
68 } | 64 } |
69 | 65 |
70 void DownloadTestObserver::WaitForFinished() { | 66 void DownloadTestObserver::WaitForFinished() { |
71 if (!IsFinished()) { | 67 if (!IsFinished()) { |
72 waiting_ = true; | 68 waiting_ = true; |
73 ui_test_utils::RunMessageLoop(); | 69 ui_test_utils::RunMessageLoop(); |
74 waiting_ = false; | 70 waiting_ = false; |
75 } | 71 } |
76 } | 72 } |
77 | 73 |
78 bool DownloadTestObserver::IsFinished() const { | 74 bool DownloadTestObserver::IsFinished() const { |
79 if (finished_downloads_.size() - finished_downloads_at_construction_ >= | 75 size_t finished_download_count = finished_downloads_.size() - |
80 wait_count_) | 76 finished_downloads_at_construction_; |
77 if (finished_download_count >= wait_count_) | |
Randy Smith (Not in Mondays)
2012/03/06 21:49:58
Why the change? It looks functionally the same--a
ahendrickson
2012/03/07 15:38:58
Sorry, that was left over from a previous patch.
| |
81 return true; | 78 return true; |
82 return (finish_on_select_file_ && select_file_dialog_seen_); | 79 return (finish_on_select_file_ && select_file_dialog_seen_); |
83 } | 80 } |
84 | 81 |
85 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { | 82 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { |
86 // The REMOVING state indicates that the download is being destroyed. | 83 // 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. | 84 // Stop observing. Do not do anything with it, as it is about to be gone. |
88 if (download->GetState() == DownloadItem::REMOVING) { | 85 if (download->GetState() == DownloadItem::REMOVING) { |
89 DownloadSet::iterator it = downloads_observed_.find(download); | 86 DownloadSet::iterator it = downloads_observed_.find(download); |
90 ASSERT_TRUE(it != downloads_observed_.end()); | 87 ASSERT_TRUE(it != downloads_observed_.end()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 | 119 |
123 case ON_DANGEROUS_DOWNLOAD_FAIL: | 120 case ON_DANGEROUS_DOWNLOAD_FAIL: |
124 ADD_FAILURE() << "Unexpected dangerous download item."; | 121 ADD_FAILURE() << "Unexpected dangerous download item."; |
125 break; | 122 break; |
126 | 123 |
127 default: | 124 default: |
128 NOTREACHED(); | 125 NOTREACHED(); |
129 } | 126 } |
130 } | 127 } |
131 | 128 |
132 if (download->GetState() == download_finished_state_) { | 129 if (IsDownloadInFinalState(download)) |
133 DownloadInFinalState(download); | 130 DownloadInFinalState(download); |
134 } | |
135 } | 131 } |
136 | 132 |
137 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { | 133 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { |
138 DCHECK_EQ(manager, download_manager_); | 134 DCHECK_EQ(manager, download_manager_); |
139 | 135 |
140 // Regenerate DownloadItem observers. If there are any download items | 136 // Regenerate DownloadItem observers. If there are any download items |
141 // in our final state, note them in |finished_downloads_| | 137 // in our final state, note them in |finished_downloads_| |
142 // (done by |OnDownloadUpdated()|). | 138 // (done by |OnDownloadUpdated()|). |
143 std::vector<DownloadItem*> downloads; | 139 std::vector<DownloadItem*> downloads; |
144 download_manager_->GetAllDownloads(FilePath(), &downloads); | 140 download_manager_->GetAllDownloads(FilePath(), &downloads); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 select_file_dialog_seen_ = true; | 173 select_file_dialog_seen_ = true; |
178 SignalIfFinished(); | 174 SignalIfFinished(); |
179 } | 175 } |
180 | 176 |
181 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { | 177 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { |
182 return dangerous_downloads_seen_.size(); | 178 return dangerous_downloads_seen_.size(); |
183 } | 179 } |
184 | 180 |
185 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { | 181 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { |
186 if (finished_downloads_.find(download) != finished_downloads_.end()) { | 182 if (finished_downloads_.find(download) != finished_downloads_.end()) { |
187 // We've already seen terminal state on this download. | 183 // We've already seen the final state on this download. |
188 return; | 184 return; |
189 } | 185 } |
190 | 186 |
191 // Record the transition. | 187 // Record the transition. |
192 finished_downloads_.insert(download); | 188 finished_downloads_.insert(download); |
193 | 189 |
194 SignalIfFinished(); | 190 SignalIfFinished(); |
195 } | 191 } |
196 | 192 |
193 bool DownloadTestObserver::IsDownloadInFinalState( | |
194 content::DownloadItem* download) { | |
195 return (download->GetState() != DownloadItem::IN_PROGRESS); | |
196 } | |
197 | |
197 void DownloadTestObserver::SignalIfFinished() { | 198 void DownloadTestObserver::SignalIfFinished() { |
198 if (waiting_ && IsFinished()) | 199 if (waiting_ && IsFinished()) |
199 MessageLoopForUI::current()->Quit(); | 200 MessageLoopForUI::current()->Quit(); |
200 } | 201 } |
201 | 202 |
203 DownloadTestObserverInProgress::DownloadTestObserverInProgress( | |
204 content::DownloadManager* download_manager, | |
205 size_t wait_count) | |
206 : DownloadTestObserver(download_manager, | |
207 wait_count, | |
208 true, | |
209 ON_DANGEROUS_DOWNLOAD_FAIL) { | |
210 } | |
211 | |
212 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() { | |
213 } | |
214 | |
215 | |
216 bool DownloadTestObserverInProgress::IsDownloadInFinalState( | |
217 content::DownloadItem* download) { | |
218 return (download->GetState() == DownloadItem::IN_PROGRESS); | |
219 } | |
220 | |
202 DownloadTestFlushObserver::DownloadTestFlushObserver( | 221 DownloadTestFlushObserver::DownloadTestFlushObserver( |
203 DownloadManager* download_manager) | 222 DownloadManager* download_manager) |
204 : download_manager_(download_manager), | 223 : download_manager_(download_manager), |
205 waiting_for_zero_inprogress_(true) {} | 224 waiting_for_zero_inprogress_(true) {} |
206 | 225 |
207 void DownloadTestFlushObserver::WaitForFlush() { | 226 void DownloadTestFlushObserver::WaitForFlush() { |
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
209 download_manager_->AddObserver(this); | 228 download_manager_->AddObserver(this); |
210 ui_test_utils::RunMessageLoop(); | 229 ui_test_utils::RunMessageLoop(); |
211 } | 230 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 void DownloadTestFlushObserver::PingIOThread(int cycle) { | 311 void DownloadTestFlushObserver::PingIOThread(int cycle) { |
293 if (--cycle) { | 312 if (--cycle) { |
294 BrowserThread::PostTask( | 313 BrowserThread::PostTask( |
295 BrowserThread::UI, FROM_HERE, | 314 BrowserThread::UI, FROM_HERE, |
296 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); | 315 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); |
297 } else { | 316 } else { |
298 BrowserThread::PostTask( | 317 BrowserThread::PostTask( |
299 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 318 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
300 } | 319 } |
301 } | 320 } |
OLD | NEW |