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_activity_observer.h"
6
7 #include "base/bind.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"
13
14 namespace content {
15 class DownloadItem;
16 }
17
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 {
23 public:
MockDownloadManagerDelegate(Profile * profile)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);
32 }
33
~MockDownloadManagerDelegate()34 virtual ~MockDownloadManagerDelegate() {}
35
EnableFileChooser(bool enable)36 void EnableFileChooser(bool enable) {
37 file_chooser_enabled_ = enable;
38 }
39
TestAndResetDidShowFileChooser()40 bool TestAndResetDidShowFileChooser() {
41 bool did_show = file_chooser_displayed_;
42 file_chooser_displayed_ = false;
43 return did_show;
44 }
45
GetWeakPtr()46 base::WeakPtr<MockDownloadManagerDelegate> GetWeakPtr() {
47 return weak_ptr_factory_.GetWeakPtr();
48 }
49
50 protected:
51
PromptUserForDownloadPath(content::DownloadItem * item,const base::FilePath & suggested_path,const FileSelectedCallback & callback)52 virtual void PromptUserForDownloadPath(content::DownloadItem* item,
53 const base::FilePath& suggested_path,
54 const FileSelectedCallback&
55 callback) OVERRIDE {
56 file_chooser_displayed_ = true;
57 base::MessageLoop::current()->PostTask(
58 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path
59 : base::FilePath())));
60 }
61
OpenDownload(content::DownloadItem * item)62 virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {}
63
64 private:
65 bool file_chooser_enabled_;
66 bool file_chooser_displayed_;
67 base::WeakPtrFactory<MockDownloadManagerDelegate> weak_ptr_factory_;
68 };
69
DownloadTestFileActivityObserver(Profile * profile)70 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver(
71 Profile* profile) {
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>());
78 }
79
~DownloadTestFileActivityObserver()80 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() {
81 }
82
EnableFileChooser(bool enable)83 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) {
84 if (test_delegate_.get())
85 test_delegate_->EnableFileChooser(enable);
86 }
87
TestAndResetDidShowFileChooser()88 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() {
89 return test_delegate_.get() &&
90 test_delegate_->TestAndResetDidShowFileChooser();
91 }
92