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 "content/public/test/download_test_observer.h" | |
| 6 | |
| 5 #include <vector> | 7 #include <vector> |
| 6 | 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 10 #include "base/stl_util.h" | 12 #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" | 13 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/download_url_parameters.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" | |
| 18 | 17 |
| 19 using content::BrowserThread; | 18 using content::BrowserThread; |
| 20 using content::DownloadItem; | 19 using content::DownloadItem; |
| 21 using content::DownloadManager; | 20 using content::DownloadManager; |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 // These functions take scoped_refptr's to DownloadManager because they | 24 // These functions take scoped_refptr's to DownloadManager because they |
| 26 // are posted to message queues, and hence may execute arbitrarily after | 25 // are posted to message queues, and hence may execute arbitrarily after |
| 27 // their actual posting. Once posted, there is no connection between | 26 // their actual posting. Once posted, there is no connection between |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 40 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, | 39 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, |
| 41 int32 download_id) { | 40 int32 download_id) { |
| 42 DownloadItem* download = download_manager->GetDownloadItem(download_id); | 41 DownloadItem* download = download_manager->GetDownloadItem(download_id); |
| 43 ASSERT_TRUE(download->IsPartialDownload()); | 42 ASSERT_TRUE(download->IsPartialDownload()); |
| 44 download->Cancel(true); | 43 download->Cancel(true); |
| 45 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 44 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 46 } | 45 } |
| 47 | 46 |
| 48 } // namespace | 47 } // namespace |
| 49 | 48 |
| 49 namespace content { | |
| 50 | |
| 50 DownloadTestObserver::DownloadTestObserver( | 51 DownloadTestObserver::DownloadTestObserver( |
| 51 DownloadManager* download_manager, | 52 DownloadManager* download_manager, |
| 52 size_t wait_count, | 53 size_t wait_count, |
| 53 DangerousDownloadAction dangerous_download_action) | 54 DangerousDownloadAction dangerous_download_action) |
| 54 : download_manager_(download_manager), | 55 : download_manager_(download_manager), |
| 55 wait_count_(wait_count), | 56 wait_count_(wait_count), |
| 56 finished_downloads_at_construction_(0), | 57 finished_downloads_at_construction_(0), |
| 57 waiting_(false), | 58 waiting_(false), |
| 58 dangerous_download_action_(dangerous_download_action) { | 59 dangerous_download_action_(dangerous_download_action) { |
| 59 } | 60 } |
| 60 | 61 |
| 61 DownloadTestObserver::~DownloadTestObserver() { | 62 DownloadTestObserver::~DownloadTestObserver() { |
| 62 for (DownloadSet::iterator it = downloads_observed_.begin(); | 63 for (DownloadSet::iterator it = downloads_observed_.begin(); |
| 63 it != downloads_observed_.end(); ++it) | 64 it != downloads_observed_.end(); ++it) |
| 64 (*it)->RemoveObserver(this); | 65 (*it)->RemoveObserver(this); |
| 65 | 66 |
| 66 download_manager_->RemoveObserver(this); | 67 download_manager_->RemoveObserver(this); |
| 67 } | 68 } |
| 68 | 69 |
| 69 void DownloadTestObserver::Init() { | 70 void DownloadTestObserver::Init() { |
| 70 download_manager_->AddObserver(this); // Will call initial ModelChanged(). | 71 download_manager_->AddObserver(this); // Will call initial ModelChanged(). |
| 71 finished_downloads_at_construction_ = finished_downloads_.size(); | 72 finished_downloads_at_construction_ = finished_downloads_.size(); |
| 72 states_observed_.clear(); | 73 states_observed_.clear(); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void DownloadTestObserver::WaitForFinished() { | 76 void DownloadTestObserver::WaitForFinished() { |
| 76 if (!IsFinished()) { | 77 if (!IsFinished()) { |
| 77 waiting_ = true; | 78 waiting_ = true; |
| 78 content::RunMessageLoop(); | 79 content::RunMessageLoop(); |
|
jam
2012/08/13 16:00:21
ditto
Randy Smith (Not in Mondays)
2012/08/13 18:57:49
Done.
| |
| 79 waiting_ = false; | 80 waiting_ = false; |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool DownloadTestObserver::IsFinished() const { | 84 bool DownloadTestObserver::IsFinished() const { |
| 84 return (finished_downloads_.size() - finished_downloads_at_construction_ >= | 85 return (finished_downloads_.size() - finished_downloads_at_construction_ >= |
| 85 wait_count_); | 86 wait_count_); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void DownloadTestObserver::OnDownloadDestroyed(DownloadItem* download) { | 89 void DownloadTestObserver::OnDownloadDestroyed(DownloadItem* download) { |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 381 if (waiting_) | 382 if (waiting_) |
| 382 MessageLoopForUI::current()->Quit(); | 383 MessageLoopForUI::current()->Quit(); |
| 383 } | 384 } |
| 384 | 385 |
| 385 const content::DownloadUrlParameters::OnStartedCallback | 386 const content::DownloadUrlParameters::OnStartedCallback |
| 386 DownloadTestItemCreationObserver::callback() { | 387 DownloadTestItemCreationObserver::callback() { |
| 387 return base::Bind( | 388 return base::Bind( |
| 388 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); | 389 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); |
| 389 } | 390 } |
| 390 | 391 |
| 391 namespace internal { | 392 } // namespace content |
| 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 |