From b0de1fa20ef9fd0ff22106ffd53631ae6e64b9de Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Mon, 28 Mar 2022 18:39:44 -0700 Subject: [PATCH] [macOS, Keyboard] Channel responder calls callback on duplicate modifiers (flutter/engine#32302) --- .../framework/Source/FlutterChannelKeyResponder.mm | 5 ++++- .../Source/FlutterChannelKeyResponderUnittests.mm | 12 +++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.mm index 9ef4ff54aa..c57003a569 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.mm @@ -57,11 +57,14 @@ } else { // ignore duplicate modifiers; This can happen in situations like switching // between application windows when MacOS only sends the up event to new window. + callback(true); return; } break; - default: + default: { NSAssert(false, @"Unexpected key event type (got %lu).", event.type); + callback(false); + } } _previouslyPressedFlags = modifierFlags; NSMutableDictionary* keyMessage = [@{ diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponderUnittests.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponderUnittests.mm index c7c2891230..05ad871860 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponderUnittests.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponderUnittests.mm @@ -56,7 +56,8 @@ TEST(FlutterChannelKeyResponderUnittests, BasicKeyEvent) { [[FlutterChannelKeyResponder alloc] initWithChannel:mockKeyEventChannel]; // Initial empty modifiers. This can happen when user opens window while modifier key is pressed - // and then releases the modifier. Shouldn't result in an event being sent. + // and then releases the modifier. No events should be sent, but the callback + // should still be called. // Regression test for https://github.com/flutter/flutter/issues/87339. [responder handleEvent:keyEvent(NSEventTypeFlagsChanged, 0x100, @"", @"", FALSE, 60) callback:^(BOOL handled) { @@ -64,6 +65,9 @@ TEST(FlutterChannelKeyResponderUnittests, BasicKeyEvent) { }]; EXPECT_EQ([messages count], 0u); + EXPECT_EQ([responses count], 1u); + EXPECT_EQ([responses[0] boolValue], TRUE); + [responses removeAllObjects]; // Key down [responder handleEvent:keyEvent(NSEventTypeKeyDown, 0x100, @"a", @"a", FALSE, 0) @@ -182,7 +186,8 @@ TEST(FlutterChannelKeyResponderUnittests, BasicKeyEvent) { [messages removeAllObjects]; [responses removeAllObjects]; - // RShift up again, should be ignored and not produce a keydown event. + // RShift up again, should be ignored and not produce a keydown event, but the + // callback should be called. next_response = false; [responder handleEvent:keyEvent(NSEventTypeFlagsChanged, 0x100, @"", @"", FALSE, 60) callback:^(BOOL handled) { @@ -190,7 +195,8 @@ TEST(FlutterChannelKeyResponderUnittests, BasicKeyEvent) { }]; EXPECT_EQ([messages count], 0u); - EXPECT_EQ([responses count], 0u); + EXPECT_EQ([responses count], 1u); + EXPECT_EQ([responses[0] boolValue], TRUE); } TEST(FlutterChannelKeyResponderUnittests, EmptyResponseIsTakenAsHandled) {