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