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 virtual ~MockDownloadManagerDelegate() {}
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();
52 virtual void PromptUserForDownloadPath(content::DownloadItem
* item
,
53 const base::FilePath
& suggested_path
,
54 const FileSelectedCallback
&
56 file_chooser_displayed_
= true;
57 base::MessageLoop::current()->PostTask(
58 FROM_HERE
, base::Bind(callback
, (file_chooser_enabled_
? suggested_path
59 : base::FilePath())));
62 virtual void OpenDownload(content::DownloadItem
* item
) OVERRIDE
{}
65 bool file_chooser_enabled_
;
66 bool file_chooser_displayed_
;
67 base::WeakPtrFactory
<MockDownloadManagerDelegate
> weak_ptr_factory_
;
70 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver(
72 scoped_ptr
<MockDownloadManagerDelegate
> mock_delegate(
73 new MockDownloadManagerDelegate(profile
));
74 test_delegate_
= mock_delegate
->GetWeakPtr();
75 DownloadServiceFactory::GetForBrowserContext(profile
)->
76 SetDownloadManagerDelegateForTesting(
77 mock_delegate
.PassAs
<ChromeDownloadManagerDelegate
>());
80 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() {
83 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable
) {
84 if (test_delegate_
.get())
85 test_delegate_
->EnableFileChooser(enable
);
88 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() {
89 return test_delegate_
.get() &&
90 test_delegate_
->TestAndResetDidShowFileChooser();