From db6f967f47650b0cc8c4665b5aa44269d6486f76 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Fri, 7 Jan 2022 13:15:03 -0800 Subject: [PATCH] [iOS Text Input] Always forward press events to the view controller (flutter/engine#30580) --- .../ios/framework/Source/FlutterEngine.mm | 8 +- .../framework/Source/FlutterTextInputPlugin.h | 18 +- .../Source/FlutterTextInputPlugin.mm | 101 +++++--- .../Source/FlutterTextInputPluginTest.mm | 218 +++++++++++------- 4 files changed, 220 insertions(+), 125 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index 03fe0a06e8..0a7d8ddc4b 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -530,10 +530,10 @@ static constexpr int kNumProfilerSamplesPerSec = 5; binaryMessenger:self.binaryMessenger codec:[FlutterJSONMessageCodec sharedInstance]]); - _textInputPlugin.reset([[FlutterTextInputPlugin alloc] init]); - _textInputPlugin.get().textInputDelegate = self; - _textInputPlugin.get().indirectScribbleDelegate = self; - [_textInputPlugin.get() setupIndirectScribbleInteraction:self.viewController]; + FlutterTextInputPlugin* textInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:self]; + _textInputPlugin.reset(textInputPlugin); + textInputPlugin.indirectScribbleDelegate = self; + [textInputPlugin setupIndirectScribbleInteraction:self.viewController]; _platformPlugin.reset([[FlutterPlatformPlugin alloc] initWithEngine:[self getWeakPtr]]); diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h index fcc3017ce5..2a6970c61b 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h @@ -7,7 +7,8 @@ #import -#include "flutter/shell/platform/common/text_editing_delta.h" +#import "flutter/fml/memory/weak_ptr.h" +#import "flutter/shell/platform/common/text_editing_delta.h" #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterIndirectScribbleDelegate.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterKeySecondaryResponder.h" @@ -29,11 +30,17 @@ typedef NS_ENUM(NSInteger, FlutterScribbleInteractionStatus) { @interface FlutterTextInputPlugin : NSObject -@property(nonatomic, assign) id textInputDelegate; @property(nonatomic, assign) UIViewController* viewController; @property(nonatomic, assign) id indirectScribbleDelegate; @property(nonatomic, strong) NSMutableDictionary* scribbleElements; + +- (instancetype)init NS_UNAVAILABLE; ++ (instancetype)new NS_UNAVAILABLE; + +- (instancetype)initWithDelegate:(id)textInputDelegate + NS_DESIGNATED_INITIALIZER; + - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; /** @@ -134,7 +141,6 @@ FLUTTER_DARWIN_EXPORT @property(nonatomic) UITextSmartDashesType smartDashesType API_AVAILABLE(ios(11.0)); @property(nonatomic, copy) UITextContentType textContentType API_AVAILABLE(ios(10.0)); -@property(nonatomic, assign) id textInputDelegate; @property(nonatomic, assign) UIAccessibilityElement* backingTextInputAccessibilityObject; // Scribble Support @@ -143,5 +149,11 @@ FLUTTER_DARWIN_EXPORT @property(nonatomic, strong) NSArray* selectionRects; - (void)resetScribbleInteractionStatusIfEnding; +- (instancetype)init NS_UNAVAILABLE; ++ (instancetype)new NS_UNAVAILABLE; +- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE; +- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; +- (instancetype)initWithOwner:(FlutterTextInputPlugin*)textInputPlugin NS_DESIGNATED_INITIALIZER; + @end #endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTINPUTPLUGIN_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index 74dc151244..1bc6e9b8db 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -710,6 +710,11 @@ static BOOL IsScribbleAvailable() { @end +@interface FlutterTextInputPlugin () +@property(nonatomic, readonly) fml::WeakPtr weakPtr; +@property(nonatomic, readonly) id textInputDelegate; +@end + @interface FlutterTextInputView () @property(nonatomic, copy) NSString* autofillId; @property(nonatomic, readonly) CATransform3D editableTransform; @@ -734,6 +739,7 @@ static BOOL IsScribbleAvailable() { // when the app shows its own in-flutter keyboard. bool _isSystemKeyboardEnabled; bool _isFloatingCursorActive; + fml::WeakPtr _textInputPlugin; // The view has reached end of life, and is no longer // allowed to access its textInputDelegate. BOOL _decommissioned; @@ -741,9 +747,10 @@ static BOOL IsScribbleAvailable() { @synthesize tokenizer = _tokenizer; -- (instancetype)init { - self = [super init]; +- (instancetype)initWithOwner:(FlutterTextInputPlugin*)textInputPlugin { + self = [super initWithFrame:CGRectZero]; if (self) { + _textInputPlugin = textInputPlugin.weakPtr; _textInputClient = 0; _selectionAffinity = _kTextAffinityUpstream; @@ -870,7 +877,7 @@ static BOOL IsScribbleAvailable() { } - (id)textInputDelegate { - return _decommissioned ? nil : _textInputDelegate; + return _textInputPlugin.get().textInputDelegate; } // Declares that the view has reached end of life, and @@ -1009,13 +1016,13 @@ static BOOL IsScribbleAvailable() { - (void)scribbleInteractionWillBeginWriting:(UIScribbleInteraction*)interaction API_AVAILABLE(ios(14.0)) { _scribbleInteractionStatus = FlutterScribbleInteractionStatusStarted; - [_textInputDelegate flutterTextInputViewScribbleInteractionBegan:self]; + [self.textInputDelegate flutterTextInputViewScribbleInteractionBegan:self]; } - (void)scribbleInteractionDidFinishWriting:(UIScribbleInteraction*)interaction API_AVAILABLE(ios(14.0)) { _scribbleInteractionStatus = FlutterScribbleInteractionStatusEnding; - [_textInputDelegate flutterTextInputViewScribbleInteractionFinished:self]; + [self.textInputDelegate flutterTextInputViewScribbleInteractionFinished:self]; } - (BOOL)scribbleInteraction:(UIScribbleInteraction*)interaction @@ -1122,7 +1129,7 @@ static BOOL IsScribbleAvailable() { @"Expected a FlutterTextRange for range (got %@).", [selectedTextRange class]); FlutterTextRange* flutterTextRange = (FlutterTextRange*)selectedTextRange; if (flutterTextRange.range.length > 0) { - [_textInputDelegate flutterTextInputView:self showToolbar:_textInputClient]; + [self.textInputDelegate flutterTextInputView:self showToolbar:_textInputClient]; } } @@ -1516,10 +1523,10 @@ static BOOL IsScribbleAvailable() { if (_scribbleInteractionStatus == FlutterScribbleInteractionStatusNone && _scribbleFocusStatus == FlutterScribbleFocusStatusUnfocused) { - [_textInputDelegate flutterTextInputView:self - showAutocorrectionPromptRectForStart:start - end:end - withClient:_textInputClient]; + [self.textInputDelegate flutterTextInputView:self + showAutocorrectionPromptRectForStart:start + end:end + withClient:_textInputClient]; } NSUInteger first = start; @@ -1828,16 +1835,16 @@ static BOOL IsScribbleAvailable() { } - (UITextPlaceholder*)insertTextPlaceholderWithSize:(CGSize)size API_AVAILABLE(ios(13.0)) { - [_textInputDelegate flutterTextInputView:self - insertTextPlaceholderWithSize:size - withClient:_textInputClient]; + [self.textInputDelegate flutterTextInputView:self + insertTextPlaceholderWithSize:size + withClient:_textInputClient]; _hasPlaceholder = YES; return [[[FlutterTextPlaceholder alloc] init] autorelease]; } - (void)removeTextPlaceholder:(UITextPlaceholder*)textPlaceholder API_AVAILABLE(ios(13.0)) { _hasPlaceholder = NO; - [_textInputDelegate flutterTextInputView:self removeTextPlaceholder:_textInputClient]; + [self.textInputDelegate flutterTextInputView:self removeTextPlaceholder:_textInputClient]; } - (void)deleteBackward { @@ -1897,6 +1904,27 @@ static BOOL IsScribbleAvailable() { } } +#pragma mark - Key Events Handling +- (void)pressesBegan:(NSSet*)presses + withEvent:(UIPressesEvent*)event API_AVAILABLE(ios(9.0)) { + [_textInputPlugin.get().viewController pressesBegan:presses withEvent:event]; +} + +- (void)pressesChanged:(NSSet*)presses + withEvent:(UIPressesEvent*)event API_AVAILABLE(ios(9.0)) { + [_textInputPlugin.get().viewController pressesChanged:presses withEvent:event]; +} + +- (void)pressesEnded:(NSSet*)presses + withEvent:(UIPressesEvent*)event API_AVAILABLE(ios(9.0)) { + [_textInputPlugin.get().viewController pressesEnded:presses withEvent:event]; +} + +- (void)pressesCancelled:(NSSet*)presses + withEvent:(UIPressesEvent*)event API_AVAILABLE(ios(9.0)) { + [_textInputPlugin.get().viewController pressesCancelled:presses withEvent:event]; +} + @end /** @@ -1964,36 +1992,30 @@ static BOOL IsScribbleAvailable() { @implementation FlutterTextInputPlugin { NSTimer* _enableFlutterTextInputViewAccessibilityTimer; + std::unique_ptr> _weakFactory; + id _textInputDelegate; } -@synthesize textInputDelegate = _textInputDelegate; - -- (instancetype)init { +- (instancetype)initWithDelegate:(id)textInputDelegate { self = [super init]; if (self) { + // `_textInputDelegate` is a weak reference because it should retain FlutterTextInputPlugin. + _textInputDelegate = textInputDelegate; + _weakFactory = std::make_unique>(self); _autofillContext = [[NSMutableDictionary alloc] init]; _inputHider = [[FlutterTextInputViewAccessibilityHider alloc] init]; _scribbleElements = [[NSMutableDictionary alloc] init]; - // Initialize activeView with a dummy view to keep tests - // passing. This dummy view needs to be replace once the - // framework initializes an input connection, and thus - // should never have access to the textInputDelegate. - _activeView = [[FlutterTextInputView alloc] init]; - [_activeView decommission]; } return self; } - (void)dealloc { + _weakFactory.reset(); [self hideTextInput]; - _activeView.textInputDelegate = nil; [_activeView release]; [_inputHider release]; - for (FlutterTextInputView* autofillView in _autofillContext.allValues) { - autofillView.textInputDelegate = nil; - } [_autofillContext release]; [_scribbleElements release]; [super dealloc]; @@ -2011,6 +2033,14 @@ static BOOL IsScribbleAvailable() { return _activeView; } +- (id)textInputDelegate { + return _textInputDelegate; +} + +- (fml::WeakPtr)weakPtr { + return _weakFactory->GetWeakPtr(); +} + - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { NSString* method = call.method; id args = call.arguments; @@ -2086,7 +2116,6 @@ static BOOL IsScribbleAvailable() { } - (void)showTextInput { - _activeView.textInputDelegate = _textInputDelegate; _activeView.viewResponder = _viewResponder; [self addToInputParentViewIfNeeded:_activeView]; // Adds a delay to prevent the text view from receiving accessibility @@ -2190,10 +2219,9 @@ static BOOL IsScribbleAvailable() { if (autofillId) { [_autofillContext removeObjectForKey:autofillId]; } - FlutterTextInputView* newView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* newView = [[FlutterTextInputView alloc] initWithOwner:self]; [newView configureWithDictionary:configuration]; [self addToInputParentViewIfNeeded:newView]; - newView.textInputDelegate = _textInputDelegate; for (NSDictionary* field in configuration[kAssociatedAutofillFields]) { NSString* autofillId = AutofillIdFromDictionary(field); @@ -2256,21 +2284,22 @@ static BOOL IsScribbleAvailable() { if (!inputView) { inputView = needsPasswordAutofill ? [FlutterSecureTextInputView alloc] : [FlutterTextInputView alloc]; - inputView = [[inputView init] autorelease]; + inputView = [[inputView initWithOwner:self] autorelease]; [self addToInputParentViewIfNeeded:inputView]; } - inputView.textInputDelegate = _textInputDelegate; [inputView configureWithDictionary:field]; return inputView; } // The UIView to add FlutterTextInputViews to. - (UIView*)hostView { - NSAssert(self.viewController.view != nullptr, - @"The application must have a HostView since the keyboard client " - @"must be part of the responder chain to function"); - return self.viewController.view; + UIView* host = _viewController.view; + NSAssert(host != nullptr, + @"The application must have a host view since the keyboard client " + @"must be part of the responder chain to function. The host view controller is %@", + _viewController); + return host; } // The UIView to add FlutterTextInputViews to. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index 6289c07d1e..d988354446 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -24,6 +24,7 @@ FLUTTER_ASSERT_ARC - (void)setMarkedRect:(CGRect)markedRect; - (void)updateEditingState; - (BOOL)isVisibleToAutofill; +- (id)textInputDelegate; @end @@ -64,6 +65,7 @@ FLUTTER_ASSERT_ARC delayRemoval:(BOOL)delayRemoval; - (NSArray*)textInputViews; - (UIView*)hostView; +- (fml::WeakPtr)getWeakPtr; - (void)addToInputParentViewIfNeeded:(FlutterTextInputView*)inputView; @end @@ -75,15 +77,16 @@ FLUTTER_ASSERT_ARC NSDictionary* _passwordTemplate; id engine; FlutterTextInputPlugin* textInputPlugin; + FlutterViewController* viewController; } - (void)setUp { [super setUp]; - engine = OCMClassMock([FlutterEngine class]); - textInputPlugin = [[FlutterTextInputPlugin alloc] init]; - textInputPlugin.textInputDelegate = engine; + + textInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:engine]; + viewController = [FlutterViewController new]; textInputPlugin.viewController = viewController; @@ -92,9 +95,7 @@ FLUTTER_ASSERT_ARC } - (void)tearDown { - for (FlutterTextInputView* autofillView in textInputPlugin.autofillContext.allValues) { - autofillView.textInputDelegate = nil; - } + textInputPlugin = nil; engine = nil; [textInputPlugin.autofillContext removeAllObjects]; [textInputPlugin cleanUpViewHierarchy:YES clearText:YES delayRemoval:NO]; @@ -162,6 +163,42 @@ FLUTTER_ASSERT_ARC } #pragma mark - Tests +- (void)testNoDanglingEnginePointer { + __weak FlutterTextInputPlugin* weakFlutterTextInputPlugin; + FlutterViewController* flutterViewController = [FlutterViewController new]; + __weak FlutterEngine* weakFlutterEngine; + + FlutterTextInputView* currentView; + + // The engine instance will be deallocated after the autorelease pool is drained. + @autoreleasepool { + FlutterEngine* flutterEngine = OCMClassMock([FlutterEngine class]); + weakFlutterEngine = flutterEngine; + NSAssert(weakFlutterEngine, @"flutter engine must not be nil"); + FlutterTextInputPlugin* flutterTextInputPlugin = [[FlutterTextInputPlugin alloc] + initWithDelegate:(id)flutterEngine]; + weakFlutterTextInputPlugin = flutterTextInputPlugin; + flutterTextInputPlugin.viewController = flutterViewController; + + // Set client so the text input plugin has an active view. + NSDictionary* config = self.mutableTemplateCopy; + FlutterMethodCall* setClientCall = + [FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient" + arguments:@[ [NSNumber numberWithInt:123], config ]]; + [flutterTextInputPlugin handleMethodCall:setClientCall + result:^(id _Nullable result){ + }]; + currentView = flutterTextInputPlugin.activeView; + } + + NSAssert(!weakFlutterEngine, @"flutter engine must be nil"); + NSAssert(currentView, @"current view must not be nil"); + + XCTAssertNil(weakFlutterTextInputPlugin); + // Verify that the view can no longer access the deallocated engine/text input plugin + // instance. + XCTAssertNil(currentView.textInputDelegate); +} - (void)testSecureInput { NSDictionary* config = self.mutableTemplateCopy; @@ -222,8 +259,7 @@ FLUTTER_ASSERT_ARC } - (void)testAutocorrectionPromptRectAppears { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithFrame:CGRectZero]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView firstRectForRange:[FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)]]; // Verify behavior. @@ -235,8 +271,7 @@ FLUTTER_ASSERT_ARC - (void)testAutocorrectionPromptRectDoesNotAppearDuringScribble { if (@available(iOS 14.0, *)) { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithFrame:CGRectZero]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; __block int callCount = 0; OCMStub([engine flutterTextInputView:inputView @@ -286,7 +321,7 @@ FLUTTER_ASSERT_ARC } - (void)testTextRangeFromPositionMatchesUITextViewBehavior { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithFrame:CGRectZero]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; FlutterTextPosition* fromPosition = [[FlutterTextPosition alloc] initWithIndex:2]; FlutterTextPosition* toPosition = [[FlutterTextPosition alloc] initWithIndex:0]; @@ -357,7 +392,8 @@ FLUTTER_ASSERT_ARC - (void)testNoZombies { // Regression test for https://github.com/flutter/flutter/issues/62501. - FlutterSecureTextInputView* passwordView = [[FlutterSecureTextInputView alloc] init]; + FlutterSecureTextInputView* passwordView = + [[FlutterSecureTextInputView alloc] initWithOwner:textInputPlugin]; @autoreleasepool { // Initialize the lazy textField. @@ -369,12 +405,11 @@ FLUTTER_ASSERT_ARC - (void)testInputViewCrash { FlutterTextInputView* activeView = nil; @autoreleasepool { - FlutterTextInputPlugin* inputPlugin = [[FlutterTextInputPlugin alloc] init]; - activeView = inputPlugin.activeView; FlutterEngine* flutterEngine = [[FlutterEngine alloc] init]; - activeView.textInputDelegate = (id)flutterEngine; + FlutterTextInputPlugin* inputPlugin = [[FlutterTextInputPlugin alloc] + initWithDelegate:(id)flutterEngine]; + activeView = inputPlugin.activeView; } - XCTAssert(!activeView.textInputDelegate); [activeView updateEditingState]; } @@ -389,48 +424,79 @@ FLUTTER_ASSERT_ARC XCTAssertNotEqual(currentView, textInputPlugin.activeView); } -- (void)testNoDanglingEnginePointer { - NSDictionary* config = self.mutableTemplateCopy; - [self setClientId:123 configuration:config]; - - // We'll hold onto the current view and try to access the engine - // after changing the active view. - FlutterTextInputView* currentView = textInputPlugin.activeView; - [self setClientId:456 configuration:config]; - XCTAssertNotNil(currentView); - XCTAssertNotNil(textInputPlugin.activeView); - XCTAssertNotEqual(currentView, textInputPlugin.activeView); - - // Verify that the view can no longer access the engine - // instance. - XCTAssertNil(currentView.textInputDelegate); -} - - (void)ensureOnlyActiveViewCanBecomeFirstResponder { for (FlutterTextInputView* inputView in self.installedInputViews) { XCTAssertEqual(inputView.canBecomeFirstResponder, inputView == textInputPlugin.activeView); } } -- (void)testPropagatePressEventsToNextResponder { +- (void)testPropagatePressEventsToViewController { + FlutterViewController* mockViewController = OCMPartialMock(viewController); + OCMStub([mockViewController pressesBegan:[OCMArg isNotNil] withEvent:[OCMArg isNotNil]]); + OCMStub([mockViewController pressesEnded:[OCMArg isNotNil] withEvent:[OCMArg isNotNil]]); + + textInputPlugin.viewController = mockViewController; + NSDictionary* config = self.mutableTemplateCopy; [self setClientId:123 configuration:config]; - FlutterTextInputView* currentView = textInputPlugin.activeView; - UIView* mockCurrentView = OCMPartialMock(currentView); + [self setTextInputShow]; - [mockCurrentView pressesBegan:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil] - withEvent:OCMClassMock([UIPressesEvent class])]; + [currentView pressesBegan:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil] + withEvent:OCMClassMock([UIPressesEvent class])]; - // The event should be propagated to the next responder instead - // of the engine. - OCMVerify([mockCurrentView nextResponder]); + OCMVerify(times(1), [mockViewController pressesBegan:[OCMArg isNotNil] + withEvent:[OCMArg isNotNil]]); + OCMVerify(times(0), [mockViewController pressesEnded:[OCMArg isNotNil] + withEvent:[OCMArg isNotNil]]); + + [currentView pressesEnded:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil] + withEvent:OCMClassMock([UIPressesEvent class])]; + + OCMVerify(times(1), [mockViewController pressesBegan:[OCMArg isNotNil] + withEvent:[OCMArg isNotNil]]); + OCMVerify(times(1), [mockViewController pressesEnded:[OCMArg isNotNil] + withEvent:[OCMArg isNotNil]]); +} + +- (void)testPropagatePressEventsToViewController2 { + FlutterViewController* mockViewController = OCMPartialMock(viewController); + OCMStub([mockViewController pressesBegan:[OCMArg isNotNil] withEvent:[OCMArg isNotNil]]); + OCMStub([mockViewController pressesEnded:[OCMArg isNotNil] withEvent:[OCMArg isNotNil]]); + + textInputPlugin.viewController = mockViewController; + + NSDictionary* config = self.mutableTemplateCopy; + [self setClientId:123 configuration:config]; + [self setTextInputShow]; + FlutterTextInputView* currentView = textInputPlugin.activeView; + + [currentView pressesBegan:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil] + withEvent:OCMClassMock([UIPressesEvent class])]; + + OCMVerify(times(1), [mockViewController pressesBegan:[OCMArg isNotNil] + withEvent:[OCMArg isNotNil]]); + OCMVerify(times(0), [mockViewController pressesEnded:[OCMArg isNotNil] + withEvent:[OCMArg isNotNil]]); + + // Switch focus to a different view. + [self setClientId:321 configuration:config]; + [self setTextInputShow]; + NSAssert(textInputPlugin.activeView, @"active view must not be nil"); + NSAssert(textInputPlugin.activeView != currentView, @"active view must change"); + currentView = textInputPlugin.activeView; + [currentView pressesEnded:[NSSet setWithObjects:OCMClassMock([UIPress class]), nil] + withEvent:OCMClassMock([UIPressesEvent class])]; + + OCMVerify(times(1), [mockViewController pressesBegan:[OCMArg isNotNil] + withEvent:[OCMArg isNotNil]]); + OCMVerify(times(1), [mockViewController pressesEnded:[OCMArg isNotNil] + withEvent:[OCMArg isNotNil]]); } #pragma mark - TextEditingDelta tests - (void)testTextEditingDeltasAreGeneratedOnTextInput { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; inputView.enableDeltaModel = YES; __block int updateCount = 0; @@ -533,8 +599,7 @@ FLUTTER_ASSERT_ARC } - (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextReplacement { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; inputView.enableDeltaModel = YES; __block int updateCount = 0; @@ -568,8 +633,7 @@ FLUTTER_ASSERT_ARC } - (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextInsertion { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; inputView.enableDeltaModel = YES; __block int updateCount = 0; @@ -603,8 +667,7 @@ FLUTTER_ASSERT_ARC } - (void)testTextEditingDeltasAreGeneratedOnSetMarkedTextDeletion { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; inputView.enableDeltaModel = YES; __block int updateCount = 0; @@ -640,8 +703,7 @@ FLUTTER_ASSERT_ARC #pragma mark - EditingState tests - (void)testUITextInputCallsUpdateEditingStateOnce { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; __block int updateCount = 0; OCMStub([engine flutterTextInputView:inputView updateEditingClient:0 withState:[OCMArg isNotNil]]) @@ -671,8 +733,7 @@ FLUTTER_ASSERT_ARC } - (void)testUITextInputCallsUpdateEditingStateWithDeltaOnce { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; inputView.enableDeltaModel = YES; __block int updateCount = 0; @@ -703,8 +764,7 @@ FLUTTER_ASSERT_ARC } - (void)testTextChangesDoNotTriggerUpdateEditingClient { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; __block int updateCount = 0; OCMStub([engine flutterTextInputView:inputView updateEditingClient:0 withState:[OCMArg isNotNil]]) @@ -744,8 +804,7 @@ FLUTTER_ASSERT_ARC } - (void)testTextChangesDoNotTriggerUpdateEditingClientWithDelta { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; inputView.enableDeltaModel = YES; __block int updateCount = 0; @@ -786,8 +845,7 @@ FLUTTER_ASSERT_ARC } - (void)testUITextInputAvoidUnnecessaryUndateEditingClientCalls { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; __block int updateCount = 0; OCMStub([engine flutterTextInputView:inputView updateEditingClient:0 withState:[OCMArg isNotNil]]) @@ -810,8 +868,7 @@ FLUTTER_ASSERT_ARC - (void)testSetMarkedTextDuringScribbleDoesNotTriggerUpdateEditingClient { if (@available(iOS 14.0, *)) { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; __block int updateCount = 0; OCMStub([engine flutterTextInputView:inputView @@ -859,8 +916,7 @@ FLUTTER_ASSERT_ARC } - (void)testUpdateEditingClientNegativeSelection { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView.text setString:@"SELECTION"]; inputView.markedTextRange = nil; @@ -903,8 +959,7 @@ FLUTTER_ASSERT_ARC - (void)testUpdateEditingClientSelectionClamping { // Regression test for https://github.com/flutter/flutter/issues/62992. - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView.text setString:@"SELECTION"]; inputView.markedTextRange = nil; @@ -964,7 +1019,7 @@ FLUTTER_ASSERT_ARC #pragma mark - UITextInput methods - Tests - (void)testUpdateFirstRectForRange { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView setTextInputState:@{@"text" : @"COMPOSING", @"composingBase" : @1, @"composingExtent" : @3}]; @@ -1009,7 +1064,7 @@ FLUTTER_ASSERT_ARC } - (void)testFirstRectForRangeReturnsCorrectSelectionRect { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView setTextInputState:@{@"text" : @"COMPOSING"}]; FlutterTextRange* range = [FlutterTextRange rangeWithNSRange:NSMakeRange(1, 1)]; @@ -1027,7 +1082,7 @@ FLUTTER_ASSERT_ARC } - (void)testClosestPositionToPoint { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView setTextInputState:@{@"text" : @"COMPOSING"}]; // Minimize the vertical distance from the center of the rects first @@ -1075,7 +1130,7 @@ FLUTTER_ASSERT_ARC } - (void)testSelectionRectsForRange { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView setTextInputState:@{@"text" : @"COMPOSING"}]; CGRect testRect0 = CGRectMake(100, 100, 100, 100); @@ -1102,7 +1157,7 @@ FLUTTER_ASSERT_ARC } - (void)testClosestPositionToPointWithinRange { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView setTextInputState:@{@"text" : @"COMPOSING"}]; // Do not return a position before the start of the range @@ -1136,14 +1191,14 @@ FLUTTER_ASSERT_ARC - (void)testInputViewsHaveUIInteractions { if (@available(iOS 13.0, *)) { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; XCTAssertGreaterThan(inputView.interactions.count, 0ul); } } - (void)testFloatingCursorDoesNotThrow { // The keyboard implementation may send unbalanced calls to the input view. - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView beginFloatingCursorAtPoint:CGPointMake(123, 321)]; [inputView beginFloatingCursorAtPoint:CGPointMake(123, 321)]; [inputView endFloatingCursor]; @@ -1152,7 +1207,7 @@ FLUTTER_ASSERT_ARC } - (void)testBoundsForFloatingCursor { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; CGRect initialBounds = inputView.bounds; // Make sure the initial bounds.size is not as large. @@ -1192,7 +1247,7 @@ FLUTTER_ASSERT_ARC #pragma mark - UIKeyInput Overrides - Tests - (void)testInsertTextAddsPlaceholderSelectionRects { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView setTextInputState:@{@"text" : @"test", @"selectionBase" : @1, @"selectionExtent" : @1}]; @@ -1606,7 +1661,6 @@ FLUTTER_ASSERT_ARC } - (void)testInitialActiveViewCantAccessTextInputDelegate { - textInputPlugin.activeView.textInputDelegate = engine; // Before the framework sends the first text input configuration, // the dummy "activeView" we use should never have access to // its textInputDelegate. @@ -1616,6 +1670,8 @@ FLUTTER_ASSERT_ARC #pragma mark - Accessibility - Tests - (void)testUITextInputAccessibilityNotHiddenWhenShowed { + [self setClientId:123 configuration:self.mutableTemplateCopy]; + // Send show text input method call. [self setTextInputShow]; // Find all the FlutterTextInputViews we created. @@ -1634,8 +1690,8 @@ FLUTTER_ASSERT_ARC } - (void)testFlutterTextInputViewDirectFocusToBackingTextInput { - FlutterTextInputViewSpy* inputView = [[FlutterTextInputViewSpy alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputViewSpy* inputView = + [[FlutterTextInputViewSpy alloc] initWithOwner:textInputPlugin]; UIView* container = [[UIView alloc] init]; UIAccessibilityElement* backing = [[UIAccessibilityElement alloc] initWithAccessibilityContainer:container]; @@ -1649,8 +1705,7 @@ FLUTTER_ASSERT_ARC } - (void)testFlutterTokenizerCanParseLines { - FlutterTextInputView* inputView = [[FlutterTextInputView alloc] init]; - inputView.textInputDelegate = engine; + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; id tokenizer = [inputView tokenizer]; // The tokenizer returns zero range When text is empty. @@ -1687,8 +1742,7 @@ FLUTTER_ASSERT_ARC - (void)testFlutterTextInputPluginRetainsFlutterTextInputView { FlutterViewController* flutterViewController = [FlutterViewController new]; - FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] init]; - myInputPlugin.textInputDelegate = engine; + FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:engine]; myInputPlugin.viewController = flutterViewController; __weak UIView* activeView; @@ -1714,7 +1768,7 @@ FLUTTER_ASSERT_ARC } - (void)testFlutterTextInputPluginHostViewNilCrash { - FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] init]; + FlutterTextInputPlugin* myInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:engine]; myInputPlugin.viewController = nil; XCTAssertThrows([myInputPlugin hostView], @"Throws exception if host view is nil"); }