download_test_file_activity_observer.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.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
14namespace content {
15class 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.
21class DownloadTestFileActivityObserver::MockDownloadManagerDelegate
22    : public ChromeDownloadManagerDelegate {
23 public:
24  explicit MockDownloadManagerDelegate(Profile* profile)
25      : ChromeDownloadManagerDelegate(profile),
26        file_chooser_enabled_(false),
27        file_chooser_displayed_(false) {}
28
29  void EnableFileChooser(bool enable) {
30    file_chooser_enabled_ = enable;
31  }
32
33  bool TestAndResetDidShowFileChooser() {
34    bool did_show = file_chooser_displayed_;
35    file_chooser_displayed_ = false;
36    return did_show;
37  }
38
39 protected:
40
41  virtual void ChooseDownloadPath(content::DownloadItem* item,
42                                  const base::FilePath& suggested_path,
43                                  const FileSelectedCallback&
44                                      callback) OVERRIDE {
45    file_chooser_displayed_ = true;
46    MessageLoop::current()->PostTask(
47        FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path
48                                         : base::FilePath())));
49  }
50
51  virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {}
52
53 private:
54  virtual ~MockDownloadManagerDelegate() {}
55
56  bool file_chooser_enabled_;
57  bool file_chooser_displayed_;
58};
59
60DownloadTestFileActivityObserver::DownloadTestFileActivityObserver(
61    Profile* profile) {
62  test_delegate_ = new MockDownloadManagerDelegate(profile);
63  DownloadServiceFactory::GetForProfile(profile)->
64      SetDownloadManagerDelegateForTesting(test_delegate_.get());
65}
66
67DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() {
68}
69
70void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) {
71  test_delegate_->EnableFileChooser(enable);
72}
73
74bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() {
75  return test_delegate_->TestAndResetDidShowFileChooser();
76}
77