7 #include "flutter/shell/platform/windows/testing/mock_direct_manipulation.h"
8 #include "flutter/shell/platform/windows/testing/mock_text_input_manager.h"
9 #include "flutter/shell/platform/windows/testing/mock_window.h"
10 #include "flutter/shell/platform/windows/testing/mock_windows_proc_table.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
16 using testing::InSequence;
17 using testing::Invoke;
18 using testing::Return;
23 TEST(MockWindow, CreateDestroy) {
28 TEST(MockWindow, GetDpiAfterCreate) {
30 ASSERT_TRUE(window.GetDpi() > 0);
33 TEST(MockWindow, VerticalScroll) {
35 const int scroll_amount = 10;
38 EXPECT_CALL(window, OnScroll(0, -scroll_amount / 120.0,
39 kFlutterPointerDeviceKindMouse, 0))
42 window.InjectWindowMessage(WM_MOUSEWHEEL, MAKEWPARAM(0, scroll_amount), 0);
45 TEST(MockWindow, OnImeCompositionCompose) {
46 auto windows_proc_table = std::make_unique<MockWindowsProcTable>();
47 auto* text_input_manager =
new MockTextInputManager();
48 std::unique_ptr<TextInputManager> text_input_manager_ptr(text_input_manager);
49 MockWindow window(std::move(windows_proc_table),
50 std::move(text_input_manager_ptr));
51 EXPECT_CALL(*text_input_manager, GetComposingString())
53 Return(std::optional<std::u16string>(std::u16string(u
"nihao"))));
54 EXPECT_CALL(*text_input_manager, GetResultString())
56 Return(std::optional<std::u16string>(std::u16string(u
"`}"))));
57 EXPECT_CALL(*text_input_manager, GetComposingCursorPosition())
58 .WillRepeatedly(Return((
int)0));
60 EXPECT_CALL(window, OnComposeChange(std::u16string(u
"nihao"), 0)).Times(1);
61 EXPECT_CALL(window, OnComposeChange(std::u16string(u
"`}"), 0)).Times(0);
62 EXPECT_CALL(window, OnComposeCommit()).Times(0);
63 ON_CALL(window, OnImeComposition)
64 .WillByDefault(Invoke(&window, &MockWindow::CallOnImeComposition));
65 EXPECT_CALL(window, OnImeComposition(_, _, _)).Times(1);
68 window.InjectWindowMessage(WM_IME_COMPOSITION, 0, GCS_COMPSTR);
71 TEST(MockWindow, OnImeCompositionResult) {
72 auto windows_proc_table = std::make_unique<MockWindowsProcTable>();
73 auto* text_input_manager =
new MockTextInputManager();
74 std::unique_ptr<TextInputManager> text_input_manager_ptr(text_input_manager);
75 MockWindow window(std::move(windows_proc_table),
76 std::move(text_input_manager_ptr));
77 EXPECT_CALL(*text_input_manager, GetComposingString())
79 Return(std::optional<std::u16string>(std::u16string(u
"nihao"))));
80 EXPECT_CALL(*text_input_manager, GetResultString())
82 Return(std::optional<std::u16string>(std::u16string(u
"`}"))));
83 EXPECT_CALL(*text_input_manager, GetComposingCursorPosition())
84 .WillRepeatedly(Return((
int)0));
86 EXPECT_CALL(window, OnComposeChange(std::u16string(u
"nihao"), 0)).Times(0);
87 EXPECT_CALL(window, OnComposeChange(std::u16string(u
"`}"), 0)).Times(1);
88 EXPECT_CALL(window, OnComposeCommit()).Times(1);
89 ON_CALL(window, OnImeComposition)
90 .WillByDefault(Invoke(&window, &MockWindow::CallOnImeComposition));
91 EXPECT_CALL(window, OnImeComposition(_, _, _)).Times(1);
94 window.InjectWindowMessage(WM_IME_COMPOSITION, 0, GCS_RESULTSTR);
97 TEST(MockWindow, OnImeCompositionResultAndCompose) {
98 auto windows_proc_table = std::make_unique<MockWindowsProcTable>();
99 auto* text_input_manager =
new MockTextInputManager();
100 std::unique_ptr<TextInputManager> text_input_manager_ptr(text_input_manager);
101 MockWindow window(std::move(windows_proc_table),
102 std::move(text_input_manager_ptr));
108 EXPECT_CALL(*text_input_manager, GetResultString())
110 Return(std::optional<std::u16string>(std::u16string(u
"今日"))));
111 EXPECT_CALL(*text_input_manager, GetComposingString())
113 Return(std::optional<std::u16string>(std::u16string(u
"は"))));
117 EXPECT_CALL(window, OnComposeChange(std::u16string(u
"今日"), 0)).Times(1);
118 EXPECT_CALL(window, OnComposeCommit()).Times(1);
119 EXPECT_CALL(window, OnComposeChange(std::u16string(u
"は"), 0)).Times(1);
122 EXPECT_CALL(*text_input_manager, GetComposingCursorPosition())
123 .WillRepeatedly(Return((
int)0));
125 ON_CALL(window, OnImeComposition)
126 .WillByDefault(Invoke(&window, &MockWindow::CallOnImeComposition));
127 EXPECT_CALL(window, OnImeComposition(_, _, _)).Times(1);
131 window.InjectWindowMessage(WM_IME_COMPOSITION, 0,
132 GCS_COMPSTR | GCS_RESULTSTR);
135 TEST(MockWindow, OnImeCompositionClearChange) {
136 auto windows_proc_table = std::make_unique<MockWindowsProcTable>();
137 auto* text_input_manager =
new MockTextInputManager();
138 std::unique_ptr<TextInputManager> text_input_manager_ptr(text_input_manager);
139 MockWindow window(std::move(windows_proc_table),
140 std::move(text_input_manager_ptr));
141 EXPECT_CALL(window, OnComposeChange(std::u16string(u
""), 0)).Times(1);
142 EXPECT_CALL(window, OnComposeCommit()).Times(1);
143 ON_CALL(window, OnImeComposition)
144 .WillByDefault(Invoke(&window, &MockWindow::CallOnImeComposition));
145 EXPECT_CALL(window, OnImeComposition(_, _, _)).Times(1);
149 window.InjectWindowMessage(WM_IME_COMPOSITION, 0, 0);
152 TEST(MockWindow, HorizontalScroll) {
154 const int scroll_amount = 10;
156 EXPECT_CALL(window, OnScroll(scroll_amount / 120.0, 0,
157 kFlutterPointerDeviceKindMouse, 0))
160 window.InjectWindowMessage(WM_MOUSEHWHEEL, MAKEWPARAM(0, scroll_amount), 0);
165 const double mouse_x = 10.0;
166 const double mouse_y = 20.0;
168 EXPECT_CALL(window, OnPointerMove(mouse_x, mouse_y,
169 kFlutterPointerDeviceKindMouse, 0, 0))
171 EXPECT_CALL(window, OnPointerLeave(mouse_x, mouse_y,
172 kFlutterPointerDeviceKindMouse, 0))
175 window.InjectWindowMessage(WM_MOUSEMOVE, 0, MAKELPARAM(mouse_x, mouse_y));
176 window.InjectWindowMessage(WM_MOUSELEAVE, 0, 0);
181 EXPECT_CALL(window, OnKey(_, _, _, _, _, _, _)).Times(1);
182 LPARAM lparam = CreateKeyEventLparam(42,
false,
false);
184 window.InjectWindowMessage(WM_KEYDOWN, 16, lparam);
189 EXPECT_CALL(window, OnKey(_, _, _, _, _, _, _)).Times(1);
190 LPARAM lparam = CreateKeyEventLparam(42,
false,
true);
192 window.InjectWindowMessage(WM_KEYUP, 16, lparam);
197 EXPECT_CALL(window, OnKey(_, _, _, _, _, _, _)).Times(1);
198 LPARAM lparam = CreateKeyEventLparam(42,
false,
false);
200 window.InjectWindowMessage(WM_SYSKEYDOWN, 16, lparam);
205 EXPECT_CALL(window, OnKey(_, _, _, _, _, _, _)).Times(1);
206 LPARAM lparam = CreateKeyEventLparam(42,
false,
true);
208 window.InjectWindowMessage(WM_SYSKEYUP, 16, lparam);
211 TEST(MockWindow, KeyDownPrintable) {
213 LPARAM lparam = CreateKeyEventLparam(30,
false,
false);
217 std::function<void(
bool)>
callback) {
220 EXPECT_CALL(window, OnKey(65, 30, WM_KEYDOWN, 0,
false,
false, _))
222 .WillOnce(respond_false);
223 EXPECT_CALL(window, OnText(_)).Times(1);
224 std::array<Win32Message, 2> messages = {
225 Win32Message{WM_KEYDOWN, 65, lparam, kWmResultDontCheck},
226 Win32Message{WM_CHAR, 65, lparam, kWmResultDontCheck}};
227 window.InjectMessageList(2, messages.data());
230 TEST(MockWindow, KeyDownWithCtrl) {
234 std::array<BYTE, 256> keyboard_state;
235 keyboard_state[VK_CONTROL] = -1;
236 SetKeyboardState(keyboard_state.data());
238 LPARAM lparam = CreateKeyEventLparam(30,
false,
false);
242 EXPECT_CALL(window, OnKey(65, 30, WM_KEYDOWN, 0,
false,
false, _)).Times(1);
243 EXPECT_CALL(window, OnText(_)).Times(0);
245 window.InjectWindowMessage(WM_KEYDOWN, 65, lparam);
247 keyboard_state.fill(0);
248 SetKeyboardState(keyboard_state.data());
251 TEST(MockWindow, KeyDownWithCtrlToggled) {
256 std::function<void(
bool)>
callback) {
261 std::array<BYTE, 256> keyboard_state;
262 keyboard_state[VK_CONTROL] = 1;
263 SetKeyboardState(keyboard_state.data());
265 LPARAM lparam = CreateKeyEventLparam(30,
false,
false);
267 EXPECT_CALL(window, OnKey(65, 30, WM_KEYDOWN, 0,
false,
false, _))
269 .WillOnce(respond_false);
270 EXPECT_CALL(window, OnText(_)).Times(1);
273 Win32Message messages[] = {{WM_KEYDOWN, 65, lparam, kWmResultDontCheck},
274 {WM_CHAR, 65, lparam, kWmResultDontCheck}};
275 window.InjectMessageList(2, messages);
277 keyboard_state.fill(0);
278 SetKeyboardState(keyboard_state.data());
283 EXPECT_CALL(window, OnPaint()).Times(1);
284 window.InjectWindowMessage(WM_PAINT, 0, 0);
288 TEST(MockWindow, PointerHitTest) {
289 UINT32 pointer_id = 123;
290 auto windows_proc_table = std::make_unique<MockWindowsProcTable>();
291 auto text_input_manager = std::make_unique<MockTextInputManager>();
293 EXPECT_CALL(*windows_proc_table, GetPointerType(Eq(pointer_id), _))
295 .WillOnce([](UINT32 pointer_id, POINTER_INPUT_TYPE*
type) {
300 MockWindow window(std::move(windows_proc_table),
301 std::move(text_input_manager));
303 auto direct_manipulation =
304 std::make_unique<MockDirectManipulationOwner>(&window);
306 EXPECT_CALL(*direct_manipulation, SetContact).Times(0);
308 window.SetDirectManipulationOwner(std::move(direct_manipulation));
309 window.InjectWindowMessage(DM_POINTERHITTEST, MAKEWPARAM(pointer_id, 0), 0);
313 TEST(MockWindow, TouchPadHitTest) {
314 UINT32 pointer_id = 123;
315 auto windows_proc_table = std::make_unique<MockWindowsProcTable>();
316 auto text_input_manager = std::make_unique<MockTextInputManager>();
318 EXPECT_CALL(*windows_proc_table, GetPointerType(Eq(pointer_id), _))
320 .WillOnce([](UINT32 pointer_id, POINTER_INPUT_TYPE*
type) {
325 MockWindow window(std::move(windows_proc_table),
326 std::move(text_input_manager));
328 auto direct_manipulation =
329 std::make_unique<MockDirectManipulationOwner>(&window);
331 EXPECT_CALL(*direct_manipulation, SetContact(Eq(pointer_id))).Times(1);
333 window.SetDirectManipulationOwner(std::move(direct_manipulation));
334 window.InjectWindowMessage(DM_POINTERHITTEST, MAKEWPARAM(pointer_id, 0), 0);
341 TEST(MockWindow, UnknownPointerTypeSkipsDirectManipulation) {
342 UINT32 pointer_id = 123;
343 auto windows_proc_table = std::make_unique<MockWindowsProcTable>();
344 auto text_input_manager = std::make_unique<MockTextInputManager>();
346 EXPECT_CALL(*windows_proc_table, GetPointerType(Eq(pointer_id), _))
349 [](UINT32 pointer_id, POINTER_INPUT_TYPE*
type) {
return FALSE; });
351 MockWindow window(std::move(windows_proc_table),
352 std::move(text_input_manager));
354 auto direct_manipulation =
355 std::make_unique<MockDirectManipulationOwner>(&window);
357 EXPECT_CALL(*direct_manipulation, SetContact).Times(0);
359 window.SetDirectManipulationOwner(std::move(direct_manipulation));
360 window.InjectWindowMessage(DM_POINTERHITTEST, MAKEWPARAM(pointer_id, 0), 0);
364 TEST(MockWindow, DISABLED_GetObjectUia) {
366 bool uia_called =
false;
367 ON_CALL(window, OnGetObject)
368 .WillByDefault(Invoke([&uia_called](UINT msg, WPARAM wpar, LPARAM lpar) {
369 #ifdef FLUTTER_ENGINE_USE_UIA
371 #endif // FLUTTER_ENGINE_USE_UIA
372 return static_cast<LRESULT
>(0);
374 EXPECT_CALL(window, OnGetObject).Times(1);
376 window.InjectWindowMessage(WM_GETOBJECT, 0, UiaRootObjectId);
378 EXPECT_TRUE(uia_called);