Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
test_keyboard.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_KEYBOARD_H_
6#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_KEYBOARD_H_
7
8#include <windows.h>
9
10#include <functional>
11#include <string>
12
13#include "flutter/fml/macros.h"
14#include "flutter/shell/platform/embedder/embedder.h"
15#include "flutter/shell/platform/windows/testing/engine_modifier.h"
16#include "flutter/shell/platform/windows/testing/wm_builders.h"
17
18#include "gtest/gtest.h"
19
22 void* user_data;
23};
24
25namespace flutter {
26namespace testing {
27::testing::AssertionResult _EventEquals(const char* expr_event,
28 const char* expr_expected,
30 const FlutterKeyEvent& expected);
31
32// Clone string onto the heap.
33//
34// If #string is nullptr, returns nullptr. Otherwise, the returned pointer must
35// be freed with delete[].
36char* clone_string(const char* string);
37
38// Creates a valid Windows LPARAM for WM_KEYDOWN and WM_CHAR from parameters
39// given.
40//
41// While |CreateKeyEventLparam| is flexible, it's recommended to use dedicated
42// functions in wm_builders.h, such as |WmKeyDownInfo|.
44 bool extended,
45 bool was_down,
46 USHORT repeat_count = 1,
47 bool context_code = 0,
48 bool transition_state = 1);
49
51 public:
52 using ResponseCallback = std::function<void(bool)>;
54 std::function<void(const FlutterKeyEvent*, ResponseCallback)>;
55 using ChannelCallbackHandler = std::function<void(ResponseCallback)>;
57 std::function<void(std::unique_ptr<rapidjson::Document>)>;
58
60 : channel_response_(ChannelRespondFalse),
61 embedder_response_(EmbedderRespondFalse),
62 text_input_response_(
63 [](std::unique_ptr<rapidjson::Document> document) {}) {}
64
66 channel_response_ = std::move(handler);
67 }
68
70 embedder_response_ = std::move(handler);
71 }
72
74 text_input_response_ = std::move(handler);
75 }
76
78 channel_response_(callback);
79 }
80
83 embedder_response_(event, std::move(callback));
84 }
85
86 void HandleTextInputMessage(std::unique_ptr<rapidjson::Document> document) {
87 text_input_response_(std::move(document));
88 }
89
90 private:
91 EmbedderCallbackHandler embedder_response_;
92 ChannelCallbackHandler channel_response_;
93 TextInputCallbackHandler text_input_response_;
94
95 static void ChannelRespondFalse(ResponseCallback callback) {
96 callback(false);
97 }
98
99 static void EmbedderRespondFalse(const FlutterKeyEvent* event,
101 callback(false);
102 }
103
105};
106
108 EngineModifier& modifier,
109 std::shared_ptr<MockKeyResponseController> response_controller);
110
111// Simulate a message queue for WM messages.
112//
113// Subclasses must implement |Win32SendMessage| for how dispatched messages are
114// processed.
116 protected:
117 // Push a message to the message queue without dispatching it.
118 void PushBack(const Win32Message* message);
119
120 // Dispatch the first message of the message queue and return its result.
121 //
122 // This method asserts that the queue is not empty.
124
125 // Peak the next message in the message queue.
126 //
127 // See Win32's |PeekMessage| for documentation.
129 UINT wMsgFilterMin,
130 UINT wMsgFilterMax,
131 UINT wRemoveMsg);
132
133 // Simulate dispatching a message to the system.
135 WPARAM const wparam,
136 LPARAM const lparam) = 0;
137
138 std::list<Win32Message> _pending_messages;
139 std::list<Win32Message> _sent_messages;
140};
141
142} // namespace testing
143} // namespace flutter
144
145// Expect the |_target| FlutterKeyEvent has the required properties.
146#define EXPECT_EVENT_EQUALS(_target, _type, _physical, _logical, _character, \
147 _synthesized) \
148 EXPECT_PRED_FORMAT2( \
149 _EventEquals, _target, \
150 (FlutterKeyEvent{ \
151 /* struct_size = */ sizeof(FlutterKeyEvent), \
152 /* timestamp = */ 0, \
153 /* type = */ _type, \
154 /* physical = */ _physical, \
155 /* logical = */ _logical, \
156 /* character = */ _character, \
157 /* synthesized = */ _synthesized, \
158 /* device_type = */ kFlutterKeyEventDeviceTypeKeyboard, \
159 }));
160
161#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_KEYBOARD_H_
void HandleChannelMessage(ResponseCallback callback)
std::function< void(const FlutterKeyEvent *, ResponseCallback)> EmbedderCallbackHandler
void SetEmbedderResponse(EmbedderCallbackHandler handler)
void SetChannelResponse(ChannelCallbackHandler handler)
void SetTextInputResponse(TextInputCallbackHandler handler)
std::function< void(ResponseCallback)> ChannelCallbackHandler
std::function< void(std::unique_ptr< rapidjson::Document >)> TextInputCallbackHandler
void HandleTextInputMessage(std::unique_ptr< rapidjson::Document > document)
void HandleEmbedderMessage(const FlutterKeyEvent *event, ResponseCallback callback)
std::function< void(bool)> ResponseCallback
std::list< Win32Message > _pending_messages
std::list< Win32Message > _sent_messages
void PushBack(const Win32Message *message)
virtual LRESULT Win32SendMessage(UINT const message, WPARAM const wparam, LPARAM const lparam)=0
BOOL Win32PeekMessage(LPMSG lpMsg, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
FlKeyEvent * event
void(* FlutterDesktopBinaryReply)(const uint8_t *data, size_t data_size, void *user_data)
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
Win32Message message
::testing::AssertionResult _EventEquals(const char *expr_event, const char *expr_expected, const FlutterKeyEvent &event, const FlutterKeyEvent &expected)
void MockEmbedderApiForKeyboard(EngineModifier &modifier, std::shared_ptr< MockKeyResponseController > response_controller)
LPARAM CreateKeyEventLparam(USHORT scancode, bool extended, bool was_down, USHORT repeat_count, bool context_code, bool transition_state)
char * clone_string(const char *string)
Definition ref_ptr.h:256
FlutterDesktopBinaryReply callback
int BOOL
struct tagMSG * LPMSG
LONG_PTR LRESULT
unsigned int UINT
LONG_PTR LPARAM
UINT_PTR WPARAM