Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 73b1723

Browse files
authored
[Win32, Keyboard] Migrate FlutterWindowWin32 keyboard tests to keyboard_win32_unittests (#30808)
* Until AltLeft * Alt right * Meta keys * Remove tests * Format
1 parent 84f7112 commit 73b1723

File tree

6 files changed

+421
-703
lines changed

6 files changed

+421
-703
lines changed

shell/platform/windows/flutter_window_win32_unittests.cc

Lines changed: 0 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -328,216 +328,6 @@ TEST(FlutterWindowWin32Test, CreateDestroy) {
328328
ASSERT_TRUE(TRUE);
329329
}
330330

331-
// Tests key event propagation of non-printable, non-modifier key down events.
332-
TEST(FlutterWindowWin32Test, NonPrintableKeyDownPropagation) {
333-
::testing::InSequence in_sequence;
334-
335-
constexpr WPARAM virtual_key = VK_LEFT;
336-
constexpr WPARAM scan_code = 10;
337-
constexpr char32_t character = 0;
338-
MockFlutterWindowWin32 win32window;
339-
auto window_binding_handler =
340-
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
341-
TestFlutterWindowsView flutter_windows_view(
342-
std::move(window_binding_handler), virtual_key, false /* is_printable */);
343-
win32window.SetView(&flutter_windows_view);
344-
LPARAM lparam = CreateKeyEventLparam(scan_code, false, false);
345-
346-
// Test an event not handled by the framework
347-
{
348-
test_response = false;
349-
flutter_windows_view.SetEngine(std::move(GetTestEngine()));
350-
EXPECT_CALL(*flutter_windows_view.key_event_handler,
351-
KeyboardHook(virtual_key, scan_code, WM_KEYDOWN, character,
352-
false /* extended */, _))
353-
.Times(2)
354-
.RetiresOnSaturation();
355-
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
356-
KeyboardHook(_, _, _, _, _, _))
357-
.Times(1)
358-
.RetiresOnSaturation();
359-
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_)).Times(0);
360-
win32window.InjectMessages(1,
361-
Win32Message{WM_KEYDOWN, virtual_key, lparam});
362-
flutter_windows_view.InjectPendingEvents(&win32window);
363-
}
364-
365-
// Test an event handled by the framework
366-
{
367-
test_response = true;
368-
EXPECT_CALL(*flutter_windows_view.key_event_handler,
369-
KeyboardHook(virtual_key, scan_code, WM_KEYDOWN, character,
370-
false /* extended */, false /* PrevState */))
371-
.Times(1)
372-
.RetiresOnSaturation();
373-
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
374-
KeyboardHook(_, _, _, _, _, _))
375-
.Times(0);
376-
win32window.InjectMessages(1,
377-
Win32Message{WM_KEYDOWN, virtual_key, lparam});
378-
flutter_windows_view.InjectPendingEvents(&win32window);
379-
}
380-
}
381-
382-
// Tests key event propagation of system (WM_SYSKEYDOWN) key down events.
383-
TEST(FlutterWindowWin32Test, SystemKeyDownPropagation) {
384-
::testing::InSequence in_sequence;
385-
386-
constexpr WPARAM virtual_key = VK_LEFT;
387-
constexpr WPARAM scan_code = 10;
388-
constexpr char32_t character = 0;
389-
MockFlutterWindowWin32 win32window;
390-
auto window_binding_handler =
391-
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
392-
TestFlutterWindowsView flutter_windows_view(
393-
std::move(window_binding_handler), virtual_key, false /* is_printable */);
394-
win32window.SetView(&flutter_windows_view);
395-
LPARAM lparam = CreateKeyEventLparam(scan_code, false, false);
396-
397-
// Test an event not handled by the framework
398-
{
399-
test_response = false;
400-
flutter_windows_view.SetEngine(std::move(GetTestEngine()));
401-
EXPECT_CALL(*flutter_windows_view.key_event_handler,
402-
KeyboardHook(virtual_key, scan_code, WM_SYSKEYDOWN, character,
403-
false /* extended */, _))
404-
.Times(2)
405-
.RetiresOnSaturation();
406-
// Syskey events are not redispatched, so TextInputPlugin, which relies on
407-
// them to receive events, no longer works.
408-
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
409-
KeyboardHook(_, _, _, _, _, _))
410-
.Times(0)
411-
.RetiresOnSaturation();
412-
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_)).Times(0);
413-
win32window.InjectMessages(
414-
1, Win32Message{WM_SYSKEYDOWN, virtual_key, lparam, kWmResultDefault});
415-
flutter_windows_view.InjectPendingEvents(&win32window);
416-
}
417-
418-
// Test an event handled by the framework
419-
{
420-
test_response = true;
421-
EXPECT_CALL(*flutter_windows_view.key_event_handler,
422-
KeyboardHook(_, _, _, _, _, _))
423-
.Times(0);
424-
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
425-
KeyboardHook(_, _, _, _, _, _))
426-
.Times(0);
427-
win32window.InjectMessages(
428-
1, Win32Message{WM_SYSKEYDOWN, virtual_key, lparam, kWmResultDefault});
429-
flutter_windows_view.InjectPendingEvents(&win32window);
430-
}
431-
}
432-
433-
// Tests key event propagation of printable character key down events. These
434-
// differ from non-printable characters in that they follow a different code
435-
// path in the WndProc (HandleMessage), producing a follow-on WM_CHAR event.
436-
TEST(FlutterWindowWin32Test, CharKeyDownPropagation) {
437-
::testing::InSequence in_sequence;
438-
439-
constexpr WPARAM virtual_key = 65; // The "A" key, which produces a character
440-
constexpr WPARAM scan_code = 30;
441-
constexpr char32_t character = 65;
442-
443-
MockFlutterWindowWin32 win32window;
444-
auto window_binding_handler =
445-
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
446-
TestFlutterWindowsView flutter_windows_view(
447-
std::move(window_binding_handler), virtual_key, true /* is_printable */);
448-
win32window.SetView(&flutter_windows_view);
449-
LPARAM lparam = CreateKeyEventLparam(scan_code, false, false);
450-
flutter_windows_view.SetEngine(std::move(GetTestEngine()));
451-
452-
// Test an event not handled by the framework
453-
{
454-
test_response = false;
455-
EXPECT_CALL(*flutter_windows_view.key_event_handler,
456-
KeyboardHook(virtual_key, scan_code, WM_KEYDOWN, character,
457-
false, false))
458-
.Times(2)
459-
.RetiresOnSaturation();
460-
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
461-
KeyboardHook(_, _, _, _, _, _))
462-
.Times(1)
463-
.RetiresOnSaturation();
464-
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_))
465-
.Times(1)
466-
.RetiresOnSaturation();
467-
win32window.InjectMessages(2, Win32Message{WM_KEYDOWN, virtual_key, lparam},
468-
Win32Message{WM_CHAR, virtual_key, lparam});
469-
flutter_windows_view.InjectPendingEvents(&win32window);
470-
}
471-
472-
// Test an event handled by the framework
473-
{
474-
test_response = true;
475-
EXPECT_CALL(*flutter_windows_view.key_event_handler,
476-
KeyboardHook(virtual_key, scan_code, WM_KEYDOWN, character,
477-
false, false))
478-
.Times(1)
479-
.RetiresOnSaturation();
480-
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
481-
KeyboardHook(_, _, _, _, _, _))
482-
.Times(0);
483-
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_)).Times(0);
484-
win32window.InjectMessages(2, Win32Message{WM_KEYDOWN, virtual_key, lparam},
485-
Win32Message{WM_CHAR, virtual_key, lparam});
486-
flutter_windows_view.InjectPendingEvents(&win32window);
487-
}
488-
}
489-
490-
// Tests key event propagation of modifier key down events. This is different
491-
// from non-printable events in that they call MapVirtualKey, resulting in a
492-
// slightly different code path.
493-
TEST(FlutterWindowWin32Test, ModifierKeyDownPropagation) {
494-
constexpr WPARAM virtual_key = VK_LSHIFT;
495-
constexpr WPARAM scan_code = 0x2a;
496-
constexpr char32_t character = 0;
497-
MockFlutterWindowWin32 win32window;
498-
auto window_binding_handler =
499-
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
500-
TestFlutterWindowsView flutter_windows_view(
501-
std::move(window_binding_handler), virtual_key, false /* is_printable */);
502-
win32window.SetView(&flutter_windows_view);
503-
LPARAM lparam = CreateKeyEventLparam(scan_code, false, false);
504-
505-
// Test an event not handled by the framework
506-
{
507-
test_response = false;
508-
flutter_windows_view.SetEngine(std::move(GetTestEngine()));
509-
EXPECT_CALL(*flutter_windows_view.key_event_handler,
510-
KeyboardHook(virtual_key, scan_code, WM_KEYDOWN, character,
511-
false /* extended */, false))
512-
.Times(2)
513-
.RetiresOnSaturation();
514-
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
515-
KeyboardHook(_, _, _, _, _, _))
516-
.Times(1)
517-
.RetiresOnSaturation();
518-
EXPECT_CALL(*flutter_windows_view.text_input_plugin, TextHook(_)).Times(0);
519-
EXPECT_EQ(win32window.InjectWindowMessage(WM_KEYDOWN, virtual_key, lparam),
520-
0);
521-
flutter_windows_view.InjectPendingEvents(&win32window);
522-
}
523-
524-
// Test an event handled by the framework
525-
{
526-
test_response = true;
527-
EXPECT_CALL(*flutter_windows_view.key_event_handler,
528-
KeyboardHook(virtual_key, scan_code, WM_KEYDOWN, character,
529-
false /* extended */, false))
530-
.Times(1)
531-
.RetiresOnSaturation();
532-
EXPECT_CALL(*flutter_windows_view.text_input_plugin,
533-
KeyboardHook(_, _, _, _, _, _))
534-
.Times(0);
535-
EXPECT_EQ(win32window.InjectWindowMessage(WM_KEYDOWN, virtual_key, lparam),
536-
0);
537-
flutter_windows_view.InjectPendingEvents(&win32window);
538-
}
539-
}
540-
541331
// Tests that composing rect updates are transformed from Flutter logical
542332
// coordinates to device coordinates and passed to the text input manager
543333
// when the DPI scale is 100% (96 DPI).

shell/platform/windows/keyboard_key_handler.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ KeyboardKeyHandler::KeyboardKeyHandlerDelegate::~KeyboardKeyHandlerDelegate() =
105105
KeyboardKeyHandler::KeyboardKeyHandler(EventDispatcher dispatch_event)
106106
: dispatch_event_(dispatch_event),
107107
last_sequence_id_(1),
108-
last_key_is_ctrl_left_down(false) {}
108+
last_key_is_ctrl_left_down(false),
109+
should_synthesize_ctrl_left_up(false) {}
109110

110111
KeyboardKeyHandler::~KeyboardKeyHandler() = default;
111112

0 commit comments

Comments
 (0)