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 weak_ptr_factory_(this) {
29 if (!profile
->IsOffTheRecord())
30 GetDownloadIdReceiverCallback().Run(
31 content::DownloadItem::kInvalidId
+ 1);
34 ~MockDownloadManagerDelegate() override
{}
36 void EnableFileChooser(bool enable
) {
37 file_chooser_enabled_
= enable
;
40 bool TestAndResetDidShowFileChooser() {
41 bool did_show
= file_chooser_displayed_
;
42 file_chooser_displayed_
= false;
46 base::WeakPtr
<MockDownloadManagerDelegate
> GetWeakPtr() {
47 return weak_ptr_factory_
.GetWeakPtr();
51 void PromptUserForDownloadPath(
52 content::DownloadItem
* item
,
53 const base::FilePath
& suggested_path
,
54 const FileSelectedCallback
& callback
) override
{
55 file_chooser_displayed_
= true;
56 base::MessageLoop::current()->PostTask(
57 FROM_HERE
, base::Bind(callback
, (file_chooser_enabled_
? suggested_path
58 : base::FilePath())));
61 void OpenDownload(content::DownloadItem
* item
) override
{}
64 bool file_chooser_enabled_
;
65 bool file_chooser_displayed_
;
66 base::WeakPtrFactory
<MockDownloadManagerDelegate
> weak_ptr_factory_
;
69 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver(
71 scoped_ptr
<MockDownloadManagerDelegate
> mock_delegate(
72 new MockDownloadManagerDelegate(profile
));
73 test_delegate_
= mock_delegate
->GetWeakPtr();
74 DownloadServiceFactory::GetForBrowserContext(profile
)
75 ->SetDownloadManagerDelegateForTesting(mock_delegate
.Pass());
78 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() {
81 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable
) {
82 if (test_delegate_
.get())
83 test_delegate_
->EnableFileChooser(enable
);
86 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() {
87 return test_delegate_
.get() &&
88 test_delegate_
->TestAndResetDidShowFileChooser();