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/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.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/profiles/profile.h"
20 // Test ChromeDownloadManagerDelegate that controls whether how file chooser
21 // dialogs are handled, and how files are opend.
22 // By default, file chooser dialogs are disabled.
23 class DownloadTestFileActivityObserver::MockDownloadManagerDelegate
24 : public ChromeDownloadManagerDelegate
{
26 explicit MockDownloadManagerDelegate(Profile
* profile
)
27 : ChromeDownloadManagerDelegate(profile
),
28 file_chooser_enabled_(false),
29 file_chooser_displayed_(false),
30 weak_ptr_factory_(this) {
31 if (!profile
->IsOffTheRecord())
32 GetDownloadIdReceiverCallback().Run(
33 content::DownloadItem::kInvalidId
+ 1);
36 ~MockDownloadManagerDelegate() override
{}
38 void EnableFileChooser(bool enable
) {
39 file_chooser_enabled_
= enable
;
42 bool TestAndResetDidShowFileChooser() {
43 bool did_show
= file_chooser_displayed_
;
44 file_chooser_displayed_
= false;
48 base::WeakPtr
<MockDownloadManagerDelegate
> GetWeakPtr() {
49 return weak_ptr_factory_
.GetWeakPtr();
53 void PromptUserForDownloadPath(
54 content::DownloadItem
* item
,
55 const base::FilePath
& suggested_path
,
56 const FileSelectedCallback
& callback
) override
{
57 file_chooser_displayed_
= true;
58 base::ThreadTaskRunnerHandle::Get()->PostTask(
60 base::Bind(callback
, (file_chooser_enabled_
? suggested_path
61 : base::FilePath())));
64 void OpenDownload(content::DownloadItem
* item
) override
{}
67 bool file_chooser_enabled_
;
68 bool file_chooser_displayed_
;
69 base::WeakPtrFactory
<MockDownloadManagerDelegate
> weak_ptr_factory_
;
72 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver(
74 scoped_ptr
<MockDownloadManagerDelegate
> mock_delegate(
75 new MockDownloadManagerDelegate(profile
));
76 test_delegate_
= mock_delegate
->GetWeakPtr();
77 DownloadServiceFactory::GetForBrowserContext(profile
)
78 ->SetDownloadManagerDelegateForTesting(mock_delegate
.Pass());
81 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() {
84 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable
) {
85 if (test_delegate_
.get())
86 test_delegate_
->EnableFileChooser(enable
);
89 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() {
90 return test_delegate_
.get() &&
91 test_delegate_
->TestAndResetDidShowFileChooser();