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 "chrome/browser/download/download_test_file_chooser_observer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 10 #include "chrome/browser/download/download_service.h" |
| 11 #include "chrome/browser/download/download_service_factory.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 |
| 14 namespace content { |
| 15 class DownloadItem; |
| 16 } |
| 17 |
| 18 namespace internal { |
| 19 |
| 20 // Test ChromeDownloadManagerDelegate that controls whether how file chooser |
| 21 // dialogs are handled. By default, file chooser dialogs are disabled. |
| 22 class MockFileChooserDownloadManagerDelegate |
| 23 : public ChromeDownloadManagerDelegate { |
| 24 public: |
| 25 explicit MockFileChooserDownloadManagerDelegate(Profile* profile) |
| 26 : ChromeDownloadManagerDelegate(profile), |
| 27 file_chooser_enabled_(false), |
| 28 file_chooser_displayed_(false) {} |
| 29 |
| 30 void EnableFileChooser(bool enable) { |
| 31 file_chooser_enabled_ = enable; |
| 32 } |
| 33 |
| 34 bool TestAndResetDidShowFileChooser() { |
| 35 bool did_show = file_chooser_displayed_; |
| 36 file_chooser_displayed_ = false; |
| 37 return did_show; |
| 38 } |
| 39 |
| 40 protected: |
| 41 |
| 42 virtual void ChooseDownloadPath(content::DownloadItem* item, |
| 43 const FilePath& suggested_path, |
| 44 const FileSelectedCallback& |
| 45 callback) OVERRIDE { |
| 46 file_chooser_displayed_ = true; |
| 47 MessageLoop::current()->PostTask( |
| 48 FROM_HERE, |
| 49 base::Bind(callback, |
| 50 (file_chooser_enabled_ ? suggested_path : FilePath()))); |
| 51 } |
| 52 |
| 53 private: |
| 54 virtual ~MockFileChooserDownloadManagerDelegate() {} |
| 55 |
| 56 bool file_chooser_enabled_; |
| 57 bool file_chooser_displayed_; |
| 58 }; |
| 59 |
| 60 } // namespace internal |
| 61 |
| 62 DownloadTestFileChooserObserver::DownloadTestFileChooserObserver( |
| 63 Profile* profile) { |
| 64 test_delegate_ = |
| 65 new internal::MockFileChooserDownloadManagerDelegate(profile); |
| 66 DownloadServiceFactory::GetForProfile(profile)-> |
| 67 SetDownloadManagerDelegateForTesting(test_delegate_.get()); |
| 68 } |
| 69 |
| 70 DownloadTestFileChooserObserver::~DownloadTestFileChooserObserver() { |
| 71 } |
| 72 |
| 73 void DownloadTestFileChooserObserver::EnableFileChooser(bool enable) { |
| 74 test_delegate_->EnableFileChooser(enable); |
| 75 } |
| 76 |
| 77 bool DownloadTestFileChooserObserver::TestAndResetDidShowFileChooser() { |
| 78 return test_delegate_->TestAndResetDidShowFileChooser(); |
| 79 } |
| 80 |
OLD | NEW |