summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/forms/mock_file_chooser.h
blob: 2058d12e4be010fc812ad1531093bf7a631c6e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_MOCK_FILE_CHOOSER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_MOCK_FILE_CHOOSER_H_

#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/mojom/choosers/file_chooser.mojom-blink.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"

namespace blink {

class MockFileChooser : public mojom::blink::FileChooser {
  using FileChooser = mojom::blink::FileChooser;
  using FileChooserFileInfoPtr = mojom::blink::FileChooserFileInfoPtr;
  using FileChooserParamsPtr = mojom::blink::FileChooserParamsPtr;
  using FileChooserRequest = mojom::blink::FileChooserRequest;

 public:
  // |reached_callback| is called when OpenFileChooser() or
  // |EnumerateChosenDirectory() is called.
  MockFileChooser(service_manager::InterfaceProvider* provider,
                  base::OnceClosure reached_callback)
      : provider_(provider), reached_callback_(std::move(reached_callback)) {
    service_manager::InterfaceProvider::TestApi test_api(provider);
    test_api.SetBinderForName(
        FileChooser::Name_,
        WTF::BindRepeating(&MockFileChooser::OnFileChooserRequest,
                           WTF::Unretained(this)));
  }

  ~MockFileChooser() override {
    service_manager::InterfaceProvider::TestApi test_api(provider_);
    test_api.ClearBinderForName(FileChooser::Name_);
  }

  void ResponseOnOpenFileChooser(FileChooserFileInfoList files) {
    DCHECK(callback_)
        << "OpenFileChooser() or EnumerateChosenDirectory() should "
           "be called beforehand.";
    std::move(callback_).Run(mojom::blink::FileChooserResult::New(
        std::move(files), base::FilePath()));
    bindings_.FlushForTesting();
  }

 private:
  void OnFileChooserRequest(mojo::ScopedMessagePipeHandle handle) {
    bindings_.AddBinding(this, FileChooserRequest(std::move(handle)));
  }

  void OpenFileChooser(FileChooserParamsPtr params,
                       OpenFileChooserCallback callback) override {
    DCHECK(!callback_);
    callback_ = std::move(callback);
    if (reached_callback_)
      std::move(reached_callback_).Run();
  }

  void EnumerateChosenDirectory(
      const base::FilePath& directory_path,
      EnumerateChosenDirectoryCallback callback) override {
    DCHECK(!callback_);
    callback_ = std::move(callback);
    if (reached_callback_)
      std::move(reached_callback_).Run();
  }

  service_manager::InterfaceProvider* provider_;
  mojo::BindingSet<FileChooser> bindings_;
  OpenFileChooserCallback callback_;
  FileChooserParamsPtr params_;
  base::OnceClosure reached_callback_;
};

}  // namespace blink
#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_MOCK_FILE_CHOOSER_H_