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
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298

content / test / mock_widget_input_handler.h [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_TEST_MOCK_WIDGET_INPUT_HANDLER_H_
#define CONTENT_TEST_MOCK_WIDGET_INPUT_HANDLER_H_

#include <stddef.h>

#include <memory>
#include <utility>

#include "build/build_config.h"
#include "cc/input/browser_controls_offset_tags_info.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/mojom/input/input_handler.mojom.h"

namespace content {

class MockWidgetInputHandler : public blink::mojom::WidgetInputHandler {
 public:
  MockWidgetInputHandler();
  MockWidgetInputHandler(
      mojo::PendingReceiver<blink::mojom::WidgetInputHandler> receiver,
      mojo::PendingRemote<blink::mojom::WidgetInputHandlerHost> host);

  MockWidgetInputHandler(const MockWidgetInputHandler&) = delete;
  MockWidgetInputHandler& operator=(const MockWidgetInputHandler&) = delete;

  ~MockWidgetInputHandler() override;

  class DispatchedEditCommandMessage;
  class DispatchedEventMessage;
  class DispatchedFocusMessage;
  class DispatchedIMEMessage;
  class DispatchedRequestCompositionUpdatesMessage;
  class DispatchedFinishComposingMessage;

  // Abstract storage of a received call on the MockWidgetInputHandler
  // interface.
  class DispatchedMessage {
   public:
    explicit DispatchedMessage(const std::string& name);

    DispatchedMessage(const DispatchedMessage&) = delete;
    DispatchedMessage& operator=(const DispatchedMessage&) = delete;

    virtual ~DispatchedMessage();

    // Cast this to a DispatchedEditCommandMessage if it is one, null
    // otherwise.
    virtual DispatchedEditCommandMessage* ToEditCommand();

    // Cast this to a DispatchedEventMessage if it is one, null otherwise.
    virtual DispatchedEventMessage* ToEvent();

    // Cast this to an DispatchedFocusMessage if it is one, null otherwise.
    virtual DispatchedFocusMessage* ToFocus();

    // Cast this to an DispatchedIMEMessage if it is one, null otherwise.
    virtual DispatchedIMEMessage* ToIME();

    // Cast this to a DispatchedRequestCompositionUpdateMessage if it is one,
    // null otherwise.
    virtual DispatchedRequestCompositionUpdatesMessage*
    ToRequestCompositionUpdates();

    // Cast this to a DispatchedFinishComposingMessage if it is one,
    // null otherwise.
    virtual DispatchedFinishComposingMessage* ToFinishComposing();

    // Return the name associated with this message. It will either match
    // the message call name (eg. MouseCaptureLost) or the name of an
    // input event (eg. GestureScrollBegin).
    const std::string& name() const { return name_; }

   private:
    std::string name_;
  };

  // A DispatchedMessage that stores the IME compositing parameters
  // that were invoked with.
  class DispatchedIMEMessage : public DispatchedMessage {
   public:
    DispatchedIMEMessage(const std::string& name,
                         const std::u16string& text,
                         const std::vector<ui::ImeTextSpan>& ime_text_spans,
                         const gfx::Range& range,
                         int32_t start,
                         int32_t end);

    DispatchedIMEMessage(const DispatchedIMEMessage&) = delete;
    DispatchedIMEMessage& operator=(const DispatchedIMEMessage&) = delete;

    ~DispatchedIMEMessage() override;

    // Override and return |this|.
    DispatchedIMEMessage* ToIME() override;

    // Returns if this message matches the parameters passed in.
    bool Matches(const std::u16string& text,
                 const std::vector<ui::ImeTextSpan>& ime_text_spans,
                 const gfx::Range& range,
                 int32_t start,
                 int32_t end) const;

   private:
    std::u16string text_;
    std::vector<ui::ImeTextSpan> text_spans_;
    gfx::Range range_;
    int32_t start_;
    int32_t end_;
  };

  // A DispatchedMessage that stores the IME compositing parameters
  // that were invoked with.
  class DispatchedEditCommandMessage : public DispatchedMessage {
   public:
    explicit DispatchedEditCommandMessage(
        std::vector<blink::mojom::EditCommandPtr> commands);

    DispatchedEditCommandMessage(const DispatchedEditCommandMessage&) = delete;
    DispatchedEditCommandMessage& operator=(
        const DispatchedEditCommandMessage&) = delete;

    ~DispatchedEditCommandMessage() override;

    // Override and return |this|.
    DispatchedEditCommandMessage* ToEditCommand() override;

    const std::vector<blink::mojom::EditCommandPtr>& Commands() const;

   private:
    std::vector<blink::mojom::EditCommandPtr> commands_;
  };

  // A DispatchedMessage that stores the focus parameters
  // that were invoked with.
  class DispatchedFocusMessage : public DispatchedMessage {
   public:
    explicit DispatchedFocusMessage(bool focused);

    DispatchedFocusMessage(const DispatchedFocusMessage&) = delete;
    DispatchedFocusMessage& operator=(const DispatchedFocusMessage&) = delete;

    ~DispatchedFocusMessage() override;

    // Override and return |this|.
    DispatchedFocusMessage* ToFocus() override;

    bool focused() const { return focused_; }

   private:
    const bool focused_;
  };

  // A DispatchedMessage that stores the InputEvent and callback
  // that was passed to the MockWidgetInputHandler interface.
  class DispatchedEventMessage : public DispatchedMessage {
   public:
    DispatchedEventMessage(std::unique_ptr<blink::WebCoalescedInputEvent> event,
                           DispatchEventCallback callback);

    DispatchedEventMessage(const DispatchedEventMessage&) = delete;
    DispatchedEventMessage& operator=(const DispatchedEventMessage&) = delete;

    ~DispatchedEventMessage() override;

    // Override and return |this|.
    DispatchedEventMessage* ToEvent() override;

    // Invoke the callback on this object with the passed in |state|.
    // The callback is called with default values for the other fields.
    void CallCallback(blink::mojom::InputEventResultState state);

    // Invoke a callback with all the arguments provided.
    void CallCallback(blink::mojom::InputEventResultSource source,
                      const ui::LatencyInfo& latency_info,
                      blink::mojom::InputEventResultState state,
                      blink::mojom::DidOverscrollParamsPtr overscroll,
                      blink::mojom::TouchActionOptionalPtr touch_action);

    // Return if the callback is set.
    bool HasCallback() const;

    // Return the associated event.
    const blink::WebCoalescedInputEvent* Event() const;

   private:
    std::unique_ptr<blink::WebCoalescedInputEvent> event_;
    DispatchEventCallback callback_;
  };

  // A DispatchedMessage that stores the RequestCompositionUpdates parameters
  // that were invoked with.
  class DispatchedRequestCompositionUpdatesMessage : public DispatchedMessage {
   public:
    DispatchedRequestCompositionUpdatesMessage(bool immediate_request,
                                               bool monitor_request);

    DispatchedRequestCompositionUpdatesMessage(
        const DispatchedRequestCompositionUpdatesMessage&) = delete;
    DispatchedRequestCompositionUpdatesMessage& operator=(
        const DispatchedRequestCompositionUpdatesMessage&) = delete;

    ~DispatchedRequestCompositionUpdatesMessage() override;

    // Override and return |this|.
    DispatchedRequestCompositionUpdatesMessage* ToRequestCompositionUpdates()
        override;

    bool immediate_request() const { return immediate_request_; }
    bool monitor_request() const { return monitor_request_; }

   private:
    const bool immediate_request_;
    const bool monitor_request_;
  };

  // A DispatchedMessage that stores the FinishComposingText parameters
  // that were invoked with.
  class DispatchedFinishComposingMessage : public DispatchedMessage {
   public:
    explicit DispatchedFinishComposingMessage(bool keep_selection);
    DispatchedFinishComposingMessage(const DispatchedFinishComposingMessage&) =
        delete;
    DispatchedFinishComposingMessage& operator=(
        const DispatchedFinishComposingMessage&) = delete;
    ~DispatchedFinishComposingMessage() override;

    // Override and return |this|.
    DispatchedFinishComposingMessage* ToFinishComposing() override;

    bool keep_selection() const { return keep_selection_; }

   private:
    const bool keep_selection_;
  };

  // blink::mojom::WidgetInputHandler override.
  void SetFocus(blink::mojom::FocusState focus_state) override;
  void MouseCaptureLost() override;
  void SetEditCommandsForNextKeyEvent(
      std::vector<blink::mojom::EditCommandPtr> commands) override;
  void CursorVisibilityChanged(bool visible) override;
  void ImeSetComposition(const std::u16string& text,
                         const std::vector<ui::ImeTextSpan>& ime_text_spans,
                         const gfx::Range& range,
                         int32_t start,
                         int32_t end,
                         ImeSetCompositionCallback callback) override;
  void ImeCommitText(const std::u16string& text,
                     const std::vector<ui::ImeTextSpan>& ime_text_spans,
                     const gfx::Range& range,
                     int32_t relative_cursor_position,
                     ImeCommitTextCallback callback) override;
  void ImeFinishComposingText(bool keep_selection) override;
  void RequestTextInputStateUpdate() override;
  void RequestCompositionUpdates(bool immediate_request,
                                 bool monitor_request) override;

  void DispatchEvent(std::unique_ptr<blink::WebCoalescedInputEvent> event,
                     DispatchEventCallback callback) override;
  void DispatchNonBlockingEvent(
      std::unique_ptr<blink::WebCoalescedInputEvent> event) override;
  void WaitForInputProcessed(WaitForInputProcessedCallback callback) override;
#if BUILDFLAG(IS_ANDROID)
  void AttachSynchronousCompositor(
      mojo::PendingRemote<blink::mojom::SynchronousCompositorControlHost>
          control_host,
      mojo::PendingAssociatedRemote<blink::mojom::SynchronousCompositorHost>
          host,
      mojo::PendingAssociatedReceiver<blink::mojom::SynchronousCompositor>
          compositor_request) override;
#endif
  void GetFrameWidgetInputHandler(
      mojo::PendingAssociatedReceiver<blink::mojom::FrameWidgetInputHandler>
          interface_request) override;
  void UpdateBrowserControlsState(
      cc::BrowserControlsState constraints,
      cc::BrowserControlsState current,
      bool animate,
      const std::optional<cc::BrowserControlsOffsetTagsInfo>& offset_tags_info)
      override;

  using MessageVector = std::vector<std::unique_ptr<DispatchedMessage>>;
  MessageVector GetAndResetDispatchedMessages();

 private:
  mojo::Receiver<blink::mojom::WidgetInputHandler> receiver_{this};
  mojo::Remote<blink::mojom::WidgetInputHandlerHost> host_;
  MessageVector dispatched_messages_;
};

}  // namespace content

#endif  // CONTENT_TEST_MOCK_WIDGET_INPUT_HANDLER_H_