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.
5 #include "chrome/browser/download/download_test_file_activity_observer.h"
8 #include "base/message_loop/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"
18 // Test ChromeDownloadManagerDelegate that controls whether how file chooser
19 // dialogs are handled, and how files are opend.
20 // By default, file chooser dialogs are disabled.
21 class DownloadTestFileActivityObserver::MockDownloadManagerDelegate
22 : public ChromeDownloadManagerDelegate {
24 explicit MockDownloadManagerDelegate(Profile* profile)
25 : ChromeDownloadManagerDelegate(profile),
26 file_chooser_enabled_(false),
27 file_chooser_displayed_(false) {
28 if (!profile->IsOffTheRecord())
29 SetNextId(content::DownloadItem::kInvalidId + 1);
32 void EnableFileChooser(bool enable) {
33 file_chooser_enabled_ = enable;
36 bool TestAndResetDidShowFileChooser() {
37 bool did_show = file_chooser_displayed_;
38 file_chooser_displayed_ = false;
44 virtual void PromptUserForDownloadPath(content::DownloadItem* item,
45 const base::FilePath& suggested_path,
46 const FileSelectedCallback&
48 file_chooser_displayed_ = true;
49 base::MessageLoop::current()->PostTask(
50 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path
51 : base::FilePath())));
54 virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {}
57 virtual ~MockDownloadManagerDelegate() {}
59 bool file_chooser_enabled_;
60 bool file_chooser_displayed_;
63 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver(
65 test_delegate_ = new MockDownloadManagerDelegate(profile);
66 DownloadServiceFactory::GetForBrowserContext(profile)->
67 SetDownloadManagerDelegateForTesting(test_delegate_.get());
70 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() {
73 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) {
74 test_delegate_->EnableFileChooser(enable);
77 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() {
78 return test_delegate_->TestAndResetDidShowFileChooser();