OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/public/test/download_test_observer.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/stl_util.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/browser/download_url_parameters.h" | |
15 #include "content/public/test/test_utils.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace content { | |
19 | |
20 namespace { | |
21 | |
22 // These functions take scoped_refptr's to DownloadManager because they | |
23 // are posted to message queues, and hence may execute arbitrarily after | |
24 // their actual posting. Once posted, there is no connection between | |
25 // these routines and the DownloadTestObserver class from which | |
26 // they came, so the DownloadTestObserver's reference to the | |
27 // DownloadManager cannot be counted on to keep the DownloadManager around. | |
28 | |
29 // Fake user click on "Accept". | |
30 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, | |
31 int32 download_id) { | |
32 DownloadItem* download = download_manager->GetDownloadItem(download_id); | |
33 download->DangerousDownloadValidated(); | |
34 } | |
35 | |
36 // Fake user click on "Deny". | |
37 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, | |
38 int32 download_id) { | |
39 DownloadItem* download = download_manager->GetDownloadItem(download_id); | |
40 ASSERT_TRUE(download->IsPartialDownload()); | |
41 download->Cancel(true); | |
42 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 DownloadTestObserver::DownloadTestObserver( | |
48 DownloadManager* download_manager, | |
49 size_t wait_count, | |
50 DangerousDownloadAction dangerous_download_action) | |
51 : download_manager_(download_manager), | |
52 wait_count_(wait_count), | |
53 finished_downloads_at_construction_(0), | |
54 waiting_(false), | |
55 dangerous_download_action_(dangerous_download_action) { | |
56 } | |
57 | |
58 DownloadTestObserver::~DownloadTestObserver() { | |
59 for (DownloadSet::iterator it = downloads_observed_.begin(); | |
60 it != downloads_observed_.end(); ++it) | |
61 (*it)->RemoveObserver(this); | |
62 | |
63 download_manager_->RemoveObserver(this); | |
64 } | |
65 | |
66 void DownloadTestObserver::Init() { | |
67 download_manager_->AddObserver(this); // Will call initial ModelChanged(). | |
68 finished_downloads_at_construction_ = finished_downloads_.size(); | |
69 states_observed_.clear(); | |
70 } | |
71 | |
72 void DownloadTestObserver::WaitForFinished() { | |
73 if (!IsFinished()) { | |
74 waiting_ = true; | |
75 RunMessageLoop(); | |
76 waiting_ = false; | |
77 } | |
78 } | |
79 | |
80 bool DownloadTestObserver::IsFinished() const { | |
81 return (finished_downloads_.size() - finished_downloads_at_construction_ >= | |
82 wait_count_); | |
83 } | |
84 | |
85 void DownloadTestObserver::OnDownloadDestroyed(DownloadItem* download) { | |
86 // Stop observing. Do not do anything with it, as it is about to be gone. | |
87 DownloadSet::iterator it = downloads_observed_.find(download); | |
88 ASSERT_TRUE(it != downloads_observed_.end()); | |
89 downloads_observed_.erase(it); | |
90 download->RemoveObserver(this); | |
91 } | |
92 | |
93 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { | |
94 // Real UI code gets the user's response after returning from the observer. | |
95 if (download->GetSafetyState() == DownloadItem::DANGEROUS && | |
96 !ContainsKey(dangerous_downloads_seen_, download->GetId())) { | |
97 dangerous_downloads_seen_.insert(download->GetId()); | |
98 | |
99 // Calling DangerousDownloadValidated() at this point will | |
100 // cause the download to be completed twice. Do what the real UI | |
101 // code does: make the call as a delayed task. | |
102 switch (dangerous_download_action_) { | |
103 case ON_DANGEROUS_DOWNLOAD_ACCEPT: | |
104 // Fake user click on "Accept". Delay the actual click, as the | |
105 // real UI would. | |
106 BrowserThread::PostTask( | |
107 BrowserThread::UI, FROM_HERE, | |
108 base::Bind(&AcceptDangerousDownload, download_manager_, | |
109 download->GetId())); | |
110 break; | |
111 | |
112 case ON_DANGEROUS_DOWNLOAD_DENY: | |
113 // Fake a user click on "Deny". Delay the actual click, as the | |
114 // real UI would. | |
115 BrowserThread::PostTask( | |
116 BrowserThread::UI, FROM_HERE, | |
117 base::Bind(&DenyDangerousDownload, download_manager_, | |
118 download->GetId())); | |
119 break; | |
120 | |
121 case ON_DANGEROUS_DOWNLOAD_FAIL: | |
122 ADD_FAILURE() << "Unexpected dangerous download item."; | |
123 break; | |
124 | |
125 default: | |
126 NOTREACHED(); | |
127 } | |
128 } | |
129 | |
130 if (IsDownloadInFinalState(download)) | |
131 DownloadInFinalState(download); | |
132 } | |
133 | |
134 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { | |
135 DCHECK_EQ(manager, download_manager_); | |
136 | |
137 // Regenerate DownloadItem observers. If there are any download items | |
138 // in our final state, note them in |finished_downloads_| | |
139 // (done by |OnDownloadUpdated()|). | |
140 std::vector<DownloadItem*> downloads; | |
141 download_manager_->GetAllDownloads(FilePath(), &downloads); | |
142 // As a test class, we're generally interested in whatever's there, | |
143 // so we include temporary downloads. | |
144 download_manager_->GetTemporaryDownloads(FilePath(), &downloads); | |
145 | |
146 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); | |
147 it != downloads.end(); ++it) { | |
148 OnDownloadUpdated(*it); // Safe to call multiple times; checks state. | |
149 | |
150 DownloadSet::const_iterator finished_it(finished_downloads_.find(*it)); | |
151 DownloadSet::iterator observed_it(downloads_observed_.find(*it)); | |
152 | |
153 // If it isn't finished and we're aren't observing it, start. | |
154 if (finished_it == finished_downloads_.end() && | |
155 observed_it == downloads_observed_.end()) { | |
156 (*it)->AddObserver(this); | |
157 downloads_observed_.insert(*it); | |
158 continue; | |
159 } | |
160 | |
161 // If it is finished and we are observing it, stop. | |
162 if (finished_it != finished_downloads_.end() && | |
163 observed_it != downloads_observed_.end()) { | |
164 (*it)->RemoveObserver(this); | |
165 downloads_observed_.erase(observed_it); | |
166 continue; | |
167 } | |
168 } | |
169 } | |
170 | |
171 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { | |
172 return dangerous_downloads_seen_.size(); | |
173 } | |
174 | |
175 size_t DownloadTestObserver::NumDownloadsSeenInState( | |
176 DownloadItem::DownloadState state) const { | |
177 StateMap::const_iterator it = states_observed_.find(state); | |
178 | |
179 if (it == states_observed_.end()) | |
180 return 0; | |
181 | |
182 return it->second; | |
183 } | |
184 | |
185 void DownloadTestObserver::DownloadInFinalState(DownloadItem* download) { | |
186 if (finished_downloads_.find(download) != finished_downloads_.end()) { | |
187 // We've already seen the final state on this download. | |
188 return; | |
189 } | |
190 | |
191 // Record the transition. | |
192 finished_downloads_.insert(download); | |
193 | |
194 // Record the state. | |
195 states_observed_[download->GetState()]++; // Initializes to 0 the first time. | |
196 | |
197 SignalIfFinished(); | |
198 } | |
199 | |
200 void DownloadTestObserver::SignalIfFinished() { | |
201 if (waiting_ && IsFinished()) | |
202 MessageLoopForUI::current()->Quit(); | |
203 } | |
204 | |
205 DownloadTestObserverTerminal::DownloadTestObserverTerminal( | |
206 DownloadManager* download_manager, | |
207 size_t wait_count, | |
208 DangerousDownloadAction dangerous_download_action) | |
209 : DownloadTestObserver(download_manager, | |
210 wait_count, | |
211 dangerous_download_action) { | |
212 // You can't rely on overriden virtual functions in a base class constructor; | |
213 // the virtual function table hasn't been set up yet. So, we have to do any | |
214 // work that depends on those functions in the derived class constructor | |
215 // instead. In this case, it's because of |IsDownloadInFinalState()|. | |
216 Init(); | |
217 } | |
218 | |
219 DownloadTestObserverTerminal::~DownloadTestObserverTerminal() { | |
220 } | |
221 | |
222 | |
223 bool DownloadTestObserverTerminal::IsDownloadInFinalState( | |
224 DownloadItem* download) { | |
225 return (download->GetState() != DownloadItem::IN_PROGRESS); | |
226 } | |
227 | |
228 DownloadTestObserverInProgress::DownloadTestObserverInProgress( | |
229 DownloadManager* download_manager, | |
230 size_t wait_count) | |
231 : DownloadTestObserver(download_manager, | |
232 wait_count, | |
233 ON_DANGEROUS_DOWNLOAD_ACCEPT) { | |
234 // You can't override virtual functions in a base class constructor; the | |
235 // virtual function table hasn't been set up yet. So, we have to do any | |
236 // work that depends on those functions in the derived class constructor | |
237 // instead. In this case, it's because of |IsDownloadInFinalState()|. | |
238 Init(); | |
239 } | |
240 | |
241 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() { | |
242 } | |
243 | |
244 | |
245 bool DownloadTestObserverInProgress::IsDownloadInFinalState( | |
246 DownloadItem* download) { | |
247 return (download->GetState() == DownloadItem::IN_PROGRESS); | |
248 } | |
249 | |
250 DownloadTestFlushObserver::DownloadTestFlushObserver( | |
251 DownloadManager* download_manager) | |
252 : download_manager_(download_manager), | |
253 waiting_for_zero_inprogress_(true) {} | |
254 | |
255 void DownloadTestFlushObserver::WaitForFlush() { | |
256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
257 download_manager_->AddObserver(this); | |
258 RunMessageLoop(); | |
259 } | |
260 | |
261 void DownloadTestFlushObserver::ModelChanged(DownloadManager* manager) { | |
262 // Model has changed, so there may be more DownloadItems to observe. | |
263 CheckDownloadsInProgress(true); | |
264 } | |
265 | |
266 void DownloadTestFlushObserver::OnDownloadDestroyed(DownloadItem* download) { | |
267 // Stop observing. Do not do anything with it, as it is about to be gone. | |
268 DownloadSet::iterator it = downloads_observed_.find(download); | |
269 ASSERT_TRUE(it != downloads_observed_.end()); | |
270 downloads_observed_.erase(it); | |
271 download->RemoveObserver(this); | |
272 } | |
273 | |
274 void DownloadTestFlushObserver::OnDownloadUpdated(DownloadItem* download) { | |
275 // No change in DownloadItem set on manager. | |
276 CheckDownloadsInProgress(false); | |
277 } | |
278 | |
279 DownloadTestFlushObserver::~DownloadTestFlushObserver() { | |
280 download_manager_->RemoveObserver(this); | |
281 for (DownloadSet::iterator it = downloads_observed_.begin(); | |
282 it != downloads_observed_.end(); ++it) { | |
283 (*it)->RemoveObserver(this); | |
284 } | |
285 } | |
286 | |
287 // If we're waiting for that flush point, check the number | |
288 // of downloads in the IN_PROGRESS state and take appropriate | |
289 // action. If requested, also observes all downloads while iterating. | |
290 void DownloadTestFlushObserver::CheckDownloadsInProgress( | |
291 bool observe_downloads) { | |
292 if (waiting_for_zero_inprogress_) { | |
293 int count = 0; | |
294 | |
295 std::vector<DownloadItem*> downloads; | |
296 download_manager_->SearchDownloads(string16(), &downloads); | |
297 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); | |
298 it != downloads.end(); ++it) { | |
299 if ((*it)->GetState() == DownloadItem::IN_PROGRESS) | |
300 count++; | |
301 if (observe_downloads) { | |
302 if (downloads_observed_.find(*it) == downloads_observed_.end()) { | |
303 (*it)->AddObserver(this); | |
304 downloads_observed_.insert(*it); | |
305 } | |
306 // Download items are forever, and we don't want to make | |
307 // assumptions about future state transitions, so once we | |
308 // start observing them, we don't stop until destruction. | |
309 } | |
310 } | |
311 | |
312 if (count == 0) { | |
313 waiting_for_zero_inprogress_ = false; | |
314 // Stop observing DownloadItems. We maintain the observation | |
315 // of DownloadManager so that we don't have to independently track | |
316 // whether we are observing it for conditional destruction. | |
317 for (DownloadSet::iterator it = downloads_observed_.begin(); | |
318 it != downloads_observed_.end(); ++it) { | |
319 (*it)->RemoveObserver(this); | |
320 } | |
321 downloads_observed_.clear(); | |
322 | |
323 // Trigger next step. We need to go past the IO thread twice, as | |
324 // there's a self-task posting in the IO thread cancel path. | |
325 BrowserThread::PostTask( | |
326 BrowserThread::FILE, FROM_HERE, | |
327 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, 2)); | |
328 } | |
329 } | |
330 } | |
331 | |
332 void DownloadTestFlushObserver::PingFileThread(int cycle) { | |
333 BrowserThread::PostTask( | |
334 BrowserThread::IO, FROM_HERE, | |
335 base::Bind(&DownloadTestFlushObserver::PingIOThread, this, cycle)); | |
336 } | |
337 | |
338 void DownloadTestFlushObserver::PingIOThread(int cycle) { | |
339 if (--cycle) { | |
340 BrowserThread::PostTask( | |
341 BrowserThread::UI, FROM_HERE, | |
342 base::Bind(&DownloadTestFlushObserver::PingFileThread, this, cycle)); | |
343 } else { | |
344 BrowserThread::PostTask( | |
345 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | |
346 } | |
347 } | |
348 | |
349 DownloadTestItemCreationObserver::DownloadTestItemCreationObserver() | |
350 : download_id_(DownloadId::Invalid()), | |
351 error_(net::OK), | |
352 called_back_count_(0), | |
353 waiting_(false) { | |
354 } | |
355 | |
356 DownloadTestItemCreationObserver::~DownloadTestItemCreationObserver() { | |
357 } | |
358 | |
359 void DownloadTestItemCreationObserver::WaitForDownloadItemCreation() { | |
360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
361 | |
362 if (called_back_count_ == 0) { | |
363 waiting_ = true; | |
364 RunMessageLoop(); | |
365 waiting_ = false; | |
366 } | |
367 } | |
368 | |
369 void DownloadTestItemCreationObserver::DownloadItemCreationCallback( | |
370 DownloadId download_id, net::Error error) { | |
371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
372 | |
373 download_id_ = download_id; | |
374 error_ = error; | |
375 ++called_back_count_; | |
376 DCHECK_EQ(1u, called_back_count_); | |
377 | |
378 if (waiting_) | |
379 MessageLoopForUI::current()->Quit(); | |
380 } | |
381 | |
382 const DownloadUrlParameters::OnStartedCallback | |
383 DownloadTestItemCreationObserver::callback() { | |
384 return base::Bind( | |
385 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); | |
386 } | |
387 | |
388 } // namespace content | |
OLD | NEW |