diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm index 00ba4ac38c..fc0e5db075 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm @@ -123,18 +123,24 @@ typedef _Nullable _NSResponderPtr (^NextResponderProvider)(); [FlutterMethodChannel methodChannelWithName:@"flutter/keyboard" binaryMessenger:[_viewDelegate getBinaryMessenger] codec:[FlutterStandardMethodCodec sharedInstance]]; + [keyboardChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { [self handleKeyboardMethodCall:call result:result]; }]; + _primaryResponders = [[NSMutableArray alloc] init]; + + __weak __typeof__(self) weakSelf = self; [self addPrimaryResponder:[[FlutterEmbedderKeyResponder alloc] initWithSendEvent:^(const FlutterKeyEvent& event, FlutterKeyEventCallback callback, void* userData) { - [_viewDelegate sendKeyEvent:event - callback:callback - userData:userData]; + __strong __typeof__(weakSelf) strongSelf = weakSelf; + [strongSelf.viewDelegate sendKeyEvent:event + callback:callback + userData:userData]; }]]; + [self addPrimaryResponder:[[FlutterChannelKeyResponder alloc] initWithChannel:[FlutterBasicMessageChannel @@ -143,6 +149,7 @@ typedef _Nullable _NSResponderPtr (^NextResponderProvider)(); getBinaryMessenger] codec:[FlutterJSONMessageCodec sharedInstance]]]]; + _pendingEvents = [[NSMutableArray alloc] init]; _layoutMap = [NSMutableDictionary dictionary]; [self buildLayout]; @@ -150,7 +157,6 @@ typedef _Nullable _NSResponderPtr (^NextResponderProvider)(); responder.layoutMap = _layoutMap; } - __weak __typeof__(self) weakSelf = self; [_viewDelegate subscribeToKeyboardLayoutChange:^() { [weakSelf buildLayout]; }]; diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerTest.mm index 5e57509f70..53e3144803 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerTest.mm @@ -426,9 +426,11 @@ void clearEvents(std::vector& events) { - (bool)keyboardChannelGetPressedState; - (bool)racingConditionBetweenKeyAndText; - (bool)correctLogicalKeyForLayouts; +- (bool)shouldNotHoldStrongReferenceToViewDelegate; @end namespace flutter::testing { + TEST(FlutterKeyboardManagerUnittests, SinglePrimaryResponder) { ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] singlePrimaryResponder]); } @@ -461,6 +463,11 @@ TEST(FlutterKeyboardManagerUnittests, CorrectLogicalKeyForLayouts) { ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] correctLogicalKeyForLayouts]); } +TEST(FlutterKeyboardManagerUnittests, ShouldNotHoldStrongReferenceToViewDelegate) { + ASSERT_TRUE( + [[FlutterKeyboardManagerUnittestsObjC alloc] shouldNotHoldStrongReferenceToViewDelegate]); +} + } // namespace flutter::testing @implementation FlutterKeyboardManagerUnittestsObjC @@ -809,4 +816,30 @@ TEST(FlutterKeyboardManagerUnittests, CorrectLogicalKeyForLayouts) { return TRUE; } +- (bool)shouldNotHoldStrongReferenceToViewDelegate { + __strong FlutterKeyboardManager* strongKeyboardManager; + __weak id weakViewDelegate; + + @autoreleasepool { + id binaryMessengerMock = OCMStrictProtocolMock(@protocol(FlutterBinaryMessenger)); + OCMStub([binaryMessengerMock setMessageHandlerOnChannel:[OCMArg any] + binaryMessageHandler:[OCMArg any]]); + + id viewDelegateMock = OCMStrictProtocolMock(@protocol(FlutterKeyboardViewDelegate)); + OCMStub([viewDelegateMock getBinaryMessenger]).andReturn(binaryMessengerMock); + OCMStub([viewDelegateMock subscribeToKeyboardLayoutChange:[OCMArg any]]); + + LayoutClue layoutClue; + OCMStub([viewDelegateMock lookUpLayoutForKeyCode:0 shift:NO]) + .ignoringNonObjectArgs() + .andReturn(layoutClue); + FlutterKeyboardManager* keyboardManager = + [[FlutterKeyboardManager alloc] initWithViewDelegate:viewDelegateMock]; + strongKeyboardManager = keyboardManager; + weakViewDelegate = viewDelegateMock; + } + + return weakViewDelegate == nil; +} + @end