From 2397cf14a96f63e2f21562a584d66079fef72fd0 Mon Sep 17 00:00:00 2001 From: Jami Couch Date: Wed, 11 May 2022 14:09:06 -0500 Subject: [PATCH] Add support for iOS UndoManager (flutter/engine#31415) --- .../ci/licenses_golden/licenses_flutter | 4 + .../shell/platform/darwin/ios/BUILD.gn | 4 + .../ios/framework/Source/FlutterEngine.mm | 37 +++++ .../Source/FlutterTextInputPlugin.mm | 3 +- .../Source/FlutterUndoManagerDelegate.h | 25 +++ .../Source/FlutterUndoManagerPlugin.h | 29 ++++ .../Source/FlutterUndoManagerPlugin.mm | 114 ++++++++++++++ .../Source/FlutterUndoManagerPluginTest.mm | 143 ++++++++++++++++++ .../IosUnitTests.xcodeproj/project.pbxproj | 2 + 9 files changed, 360 insertions(+), 1 deletion(-) create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.mm create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPluginTest.mm diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index bce31a41fc..9a77424a70 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1637,6 +1637,10 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInp FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUIPressProxy.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUIPressProxy.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUmbrellaImport.m +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPluginTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index d122820055..bfcbaab7fe 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -84,6 +84,9 @@ source_set("flutter_framework_source") { "framework/Source/FlutterTextInputPlugin.mm", "framework/Source/FlutterUIPressProxy.h", "framework/Source/FlutterUIPressProxy.mm", + "framework/Source/FlutterUndoManagerDelegate.h", + "framework/Source/FlutterUndoManagerPlugin.h", + "framework/Source/FlutterUndoManagerPlugin.mm", "framework/Source/FlutterView.h", "framework/Source/FlutterView.mm", "framework/Source/FlutterViewController.mm", @@ -262,6 +265,7 @@ shared_library("ios_test_flutter") { "framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm", "framework/Source/FlutterRestorationPluginTest.mm", "framework/Source/FlutterTextInputPluginTest.mm", + "framework/Source/FlutterUndoManagerPluginTest.mm", "framework/Source/FlutterViewControllerTest.mm", "framework/Source/SemanticsObjectTest.mm", "framework/Source/connection_collection_test.mm", 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 2105e4027a..e70a8a235e 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 @@ -25,6 +25,8 @@ #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterObservatoryPublisher.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" #import "flutter/shell/platform/darwin/ios/framework/Source/connection_collection.h" #import "flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h" @@ -77,6 +79,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; @end @interface FlutterEngine () // Maintains a dictionary of plugin names that have registered with the engine. Used by @@ -107,6 +110,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; // Channels fml::scoped_nsobject _platformPlugin; fml::scoped_nsobject _textInputPlugin; + fml::scoped_nsobject _undoManagerPlugin; fml::scoped_nsobject _restorationPlugin; fml::scoped_nsobject _localizationChannel; fml::scoped_nsobject _navigationChannel; @@ -114,6 +118,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; fml::scoped_nsobject _platformChannel; fml::scoped_nsobject _platformViewsChannel; fml::scoped_nsobject _textInputChannel; + fml::scoped_nsobject _undoManagerChannel; fml::scoped_nsobject _lifecycleChannel; fml::scoped_nsobject _systemChannel; fml::scoped_nsobject _settingsChannel; @@ -361,6 +366,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; self.iosPlatformView->SetOwnerViewController(_viewController); [self maybeSetupPlatformViewChannels]; _textInputPlugin.get().viewController = viewController; + _undoManagerPlugin.get().viewController = viewController; if (viewController) { __block FlutterEngine* blockSelf = self; @@ -396,6 +402,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; - (void)notifyViewControllerDeallocated { [[self lifecycleChannel] sendMessage:@"AppLifecycleState.detached"]; _textInputPlugin.get().viewController = nil; + _undoManagerPlugin.get().viewController = nil; if (!_allowHeadlessExecution) { [self destroyContext]; } else { @@ -433,6 +440,9 @@ static constexpr int kNumProfilerSamplesPerSec = 5; - (FlutterTextInputPlugin*)textInputPlugin { return _textInputPlugin.get(); } +- (FlutterUndoManagerPlugin*)undoManagerPlugin { + return _undoManagerPlugin.get(); +} - (FlutterRestorationPlugin*)restorationPlugin { return _restorationPlugin.get(); } @@ -451,6 +461,9 @@ static constexpr int kNumProfilerSamplesPerSec = 5; - (FlutterMethodChannel*)textInputChannel { return _textInputChannel.get(); } +- (FlutterMethodChannel*)undoManagerChannel { + return _undoManagerChannel.get(); +} - (FlutterBasicMessageChannel*)lifecycleChannel { return _lifecycleChannel.get(); } @@ -475,6 +488,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; _platformChannel.reset(); _platformViewsChannel.reset(); _textInputChannel.reset(); + _undoManagerChannel.reset(); _lifecycleChannel.reset(); _systemChannel.reset(); _settingsChannel.reset(); @@ -542,6 +556,11 @@ static constexpr int kNumProfilerSamplesPerSec = 5; binaryMessenger:self.binaryMessenger codec:[FlutterJSONMethodCodec sharedInstance]]); + _undoManagerChannel.reset([[FlutterMethodChannel alloc] + initWithName:@"flutter/undomanager" + binaryMessenger:self.binaryMessenger + codec:[FlutterJSONMethodCodec sharedInstance]]); + _lifecycleChannel.reset([[FlutterBasicMessageChannel alloc] initWithName:@"flutter/lifecycle" binaryMessenger:self.binaryMessenger @@ -567,6 +586,10 @@ static constexpr int kNumProfilerSamplesPerSec = 5; textInputPlugin.indirectScribbleDelegate = self; [textInputPlugin setupIndirectScribbleInteraction:self.viewController]; + FlutterUndoManagerPlugin* undoManagerPlugin = + [[FlutterUndoManagerPlugin alloc] initWithDelegate:self]; + _undoManagerPlugin.reset(undoManagerPlugin); + _platformPlugin.reset([[FlutterPlatformPlugin alloc] initWithEngine:[self getWeakPtr]]); _restorationPlugin.reset([[FlutterRestorationPlugin alloc] @@ -593,6 +616,12 @@ static constexpr int kNumProfilerSamplesPerSec = 5; [_textInputChannel.get() setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { [textInputPlugin handleMethodCall:call result:result]; }]; + + FlutterUndoManagerPlugin* undoManagerPlugin = _undoManagerPlugin.get(); + [_undoManagerChannel.get() + setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { + [undoManagerPlugin handleMethodCall:call result:result]; + }]; } } @@ -957,6 +986,14 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS arguments:@[ @(client) ]]; } +#pragma mark - Undo Manager Delegate + +- (void)flutterUndoManagerPlugin:(FlutterUndoManagerPlugin*)undoManagerPlugin + handleUndoWithDirection:(FlutterUndoRedoDirection)direction { + NSString* action = (direction == FlutterUndoRedoDirectionUndo) ? @"undo" : @"redo"; + [_undoManagerChannel.get() invokeMethod:@"UndoManagerClient.handleUndo" arguments:@[ action ]]; +} + #pragma mark - Screenshot Delegate - (flutter::Rasterizer::Screenshot)takeScreenshot:(flutter::Rasterizer::ScreenshotType)type 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 0e2f28635b..799afd5bb7 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 @@ -45,6 +45,7 @@ static NSString* const kSetEditableSizeAndTransformMethod = @"TextInput.setEditableSizeAndTransform"; static NSString* const kSetMarkedTextRectMethod = @"TextInput.setMarkedTextRect"; static NSString* const kFinishAutofillContextMethod = @"TextInput.finishAutofillContext"; +static NSString* const kSetSelectionRectsMethod = @"TextInput.setSelectionRects"; #pragma mark - TextInputConfiguration Field Names static NSString* const kSecureTextEntry = @"obscureText"; @@ -2085,7 +2086,7 @@ static BOOL IsSelectionRectCloserToPoint(CGPoint point, } else if ([method isEqualToString:kFinishAutofillContextMethod]) { [self triggerAutofillSave:[args boolValue]]; result(nil); - } else if ([method isEqualToString:@"TextInput.setSelectionRects"]) { + } else if ([method isEqualToString:kSetSelectionRectsMethod]) { [self setSelectionRects:args]; result(nil); } else { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h new file mode 100644 index 0000000000..2157820c85 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h @@ -0,0 +1,25 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERUNDOMANAGERDELEGATE_H_ +#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERUNDOMANAGERDELEGATE_H_ + +#import + +NS_ASSUME_NONNULL_BEGIN + +typedef NS_ENUM(NSInteger, FlutterUndoRedoDirection) { + FlutterUndoRedoDirectionUndo, + FlutterUndoRedoDirectionRedo, +}; + +@class FlutterUndoManagerPlugin; + +@protocol FlutterUndoManagerDelegate +- (void)flutterUndoManagerPlugin:(FlutterUndoManagerPlugin*)undoManagerPlugin + handleUndoWithDirection:(FlutterUndoRedoDirection)direction; +@end +NS_ASSUME_NONNULL_END + +#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERUNDOMANAGERDELEGATE_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h new file mode 100644 index 0000000000..8e38f76e5c --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h @@ -0,0 +1,29 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERUNDOMANAGERPLUGIN_H_ +#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERUNDOMANAGERPLUGIN_H_ + +#import + +#import "flutter/fml/memory/weak_ptr.h" +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h" + +@interface FlutterUndoManagerPlugin : NSObject + +@property(nonatomic, assign) FlutterViewController* viewController; + +- (instancetype)init NS_UNAVAILABLE; ++ (instancetype)new NS_UNAVAILABLE; + +- (instancetype)initWithDelegate:(id)undoManagerDelegate + NS_DESIGNATED_INITIALIZER; + +- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; + +@end + +#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERUNDOMANAGERPLUGIN_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.mm new file mode 100644 index 0000000000..488704ea4a --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.mm @@ -0,0 +1,114 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h" + +#import +#import + +#include "flutter/fml/logging.h" + +#pragma mark - UndoManager channel method names. +static NSString* const kSetUndoStateMethod = @"UndoManager.setUndoState"; + +#pragma mark - Undo State field names +static NSString* const kCanUndo = @"canUndo"; +static NSString* const kCanRedo = @"canRedo"; + +@implementation FlutterUndoManagerPlugin { + id _undoManagerDelegate; +} + +- (instancetype)initWithDelegate:(id)undoManagerDelegate { + self = [super init]; + + if (self) { + // `_undoManagerDelegate` is a weak reference because it should retain FlutterUndoManagerPlugin. + _undoManagerDelegate = undoManagerDelegate; + } + + return self; +} + +- (void)dealloc { + [self resetUndoManager]; + [super dealloc]; +} + +- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { + NSString* method = call.method; + id args = call.arguments; + if ([method isEqualToString:kSetUndoStateMethod]) { + [self setUndoState:args]; + result(nil); + } else { + result(FlutterMethodNotImplemented); + } +} + +- (NSUndoManager*)undoManager { + return _viewController.undoManager; +} + +- (void)resetUndoManager API_AVAILABLE(ios(9.0)) { + [[self undoManager] removeAllActionsWithTarget:self]; +} + +- (void)registerUndoWithDirection:(FlutterUndoRedoDirection)direction API_AVAILABLE(ios(9.0)) { + [[self undoManager] beginUndoGrouping]; + [[self undoManager] registerUndoWithTarget:self + handler:^(id target) { + // Register undo with opposite direction. + FlutterUndoRedoDirection newDirection = + (direction == FlutterUndoRedoDirectionRedo) + ? FlutterUndoRedoDirectionUndo + : FlutterUndoRedoDirectionRedo; + [target registerUndoWithDirection:newDirection]; + // Invoke method on delegate. + [_undoManagerDelegate flutterUndoManagerPlugin:self + handleUndoWithDirection:direction]; + }]; + [[self undoManager] endUndoGrouping]; +} + +- (void)registerRedo API_AVAILABLE(ios(9.0)) { + [[self undoManager] beginUndoGrouping]; + [[self undoManager] + registerUndoWithTarget:self + handler:^(id target) { + // Register undo with opposite direction. + [target registerUndoWithDirection:FlutterUndoRedoDirectionRedo]; + }]; + [[self undoManager] endUndoGrouping]; + [[self undoManager] undo]; +} + +- (void)setUndoState:(NSDictionary*)dictionary API_AVAILABLE(ios(9.0)) { + BOOL groupsByEvent = [self undoManager].groupsByEvent; + [self undoManager].groupsByEvent = NO; + BOOL canUndo = [dictionary[kCanUndo] boolValue]; + BOOL canRedo = [dictionary[kCanRedo] boolValue]; + + [self resetUndoManager]; + + if (canUndo) { + [self registerUndoWithDirection:FlutterUndoRedoDirectionUndo]; + } + if (canRedo) { + [self registerRedo]; + } + + if (_viewController.engine.textInputPlugin.textInputView != nil) { + // This is needed to notify the iPadOS keyboard that it needs to update the + // state of the UIBarButtons. Otherwise, the state changes to NSUndoManager + // will not show up until the next keystroke (or other trigger). + id inputDelegate = + _viewController.engine.textInputPlugin.textInputView.inputDelegate; + [inputDelegate selectionDidChange:_viewController.engine.textInputPlugin.textInputView]; + } + [self undoManager].groupsByEvent = groupsByEvent; +} + +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPluginTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPluginTest.mm new file mode 100644 index 0000000000..0a30cb1dfb --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPluginTest.mm @@ -0,0 +1,143 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h" + +#import +#import + +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h" +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" + +FLUTTER_ASSERT_ARC + +@interface FlutterEngine () +- (nonnull FlutterUndoManagerPlugin*)undoManagerPlugin; +@end + +@interface FlutterUndoManagerPluginForTest : FlutterUndoManagerPlugin +@property(nonatomic, assign) NSUndoManager* undoManager; +@end + +@implementation FlutterUndoManagerPluginForTest { +} +@end + +@interface FlutterUndoManagerPluginTest : XCTestCase +@property(nonatomic, strong) id engine; +@property(nonatomic, strong) FlutterUndoManagerPluginForTest* undoManagerPlugin; +@property(nonatomic, strong) FlutterViewController* viewController; +@property(nonatomic, strong) NSUndoManager* undoManager; +@end + +@implementation FlutterUndoManagerPluginTest { +} + +- (void)setUp { + [super setUp]; + self.engine = OCMClassMock([FlutterEngine class]); + + self.undoManagerPlugin = [[FlutterUndoManagerPluginForTest alloc] initWithDelegate:self.engine]; + + self.viewController = [FlutterViewController new]; + self.undoManagerPlugin.viewController = self.viewController; + + self.undoManager = OCMClassMock([NSUndoManager class]); + self.undoManagerPlugin.undoManager = self.undoManager; +} + +- (void)tearDown { + [self.undoManager removeAllActionsWithTarget:self.undoManagerPlugin]; + self.undoManagerPlugin = nil; + self.engine = nil; + self.viewController = nil; + self.undoManager = nil; + [super tearDown]; +} + +- (void)testSetUndoState { + __block int registerUndoCount = 0; + __block void (^undoHandler)(id target); + OCMStub([self.undoManager registerUndoWithTarget:self.undoManagerPlugin handler:[OCMArg any]]) + .andDo(^(NSInvocation* invocation) { + registerUndoCount++; + __weak void (^handler)(id target); + [invocation retainArguments]; + [invocation getArgument:&handler atIndex:3]; + undoHandler = handler; + }); + __block int removeAllActionsCount = 0; + OCMStub([self.undoManager removeAllActionsWithTarget:self.undoManagerPlugin]) + .andDo(^(NSInvocation* invocation) { + removeAllActionsCount++; + }); + __block int delegateUndoCount = 0; + OCMStub([self.engine flutterUndoManagerPlugin:[OCMArg any] + handleUndoWithDirection:FlutterUndoRedoDirectionUndo]) + .andDo(^(NSInvocation* invocation) { + delegateUndoCount++; + }); + __block int delegateRedoCount = 0; + OCMStub([self.engine flutterUndoManagerPlugin:[OCMArg any] + handleUndoWithDirection:FlutterUndoRedoDirectionRedo]) + .andDo(^(NSInvocation* invocation) { + delegateRedoCount++; + }); + __block int undoCount = 0; + OCMStub([self.undoManager undo]).andDo(^(NSInvocation* invocation) { + undoCount++; + undoHandler(self.undoManagerPlugin); + }); + + // If canUndo and canRedo are false, only removeAllActionsWithTarget is called. + FlutterMethodCall* setUndoStateCall = + [FlutterMethodCall methodCallWithMethodName:@"UndoManager.setUndoState" + arguments:@{@"canUndo" : @NO, @"canRedo" : @NO}]; + [self.undoManagerPlugin handleMethodCall:setUndoStateCall + result:^(id _Nullable result){ + }]; + XCTAssertEqual(1, removeAllActionsCount); + XCTAssertEqual(0, registerUndoCount); + + // If canUndo is true, an undo will be registered. + setUndoStateCall = + [FlutterMethodCall methodCallWithMethodName:@"UndoManager.setUndoState" + arguments:@{@"canUndo" : @YES, @"canRedo" : @NO}]; + [self.undoManagerPlugin handleMethodCall:setUndoStateCall + result:^(id _Nullable result){ + }]; + XCTAssertEqual(2, removeAllActionsCount); + XCTAssertEqual(1, registerUndoCount); + + // Invoking the undo handler will invoke the handleUndo delegate method with "undo". + undoHandler(self.undoManagerPlugin); + XCTAssertEqual(1, delegateUndoCount); + XCTAssertEqual(0, delegateRedoCount); + XCTAssertEqual(2, registerUndoCount); + + // Invoking the redo handler will invoke the handleUndo delegate method with "redo". + undoHandler(self.undoManagerPlugin); + XCTAssertEqual(1, delegateUndoCount); + XCTAssertEqual(1, delegateRedoCount); + XCTAssertEqual(3, registerUndoCount); + + // If canRedo is true, an undo will be registered and undo will be called. + setUndoStateCall = + [FlutterMethodCall methodCallWithMethodName:@"UndoManager.setUndoState" + arguments:@{@"canUndo" : @NO, @"canRedo" : @YES}]; + [self.undoManagerPlugin handleMethodCall:setUndoStateCall + result:^(id _Nullable result){ + }]; + XCTAssertEqual(3, removeAllActionsCount); + XCTAssertEqual(5, registerUndoCount); + XCTAssertEqual(1, undoCount); + + // Invoking the redo handler will invoke the handleUndo delegate method with "redo". + undoHandler(self.undoManagerPlugin); + XCTAssertEqual(1, delegateUndoCount); + XCTAssertEqual(2, delegateRedoCount); +} + +@end diff --git a/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj b/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj index fc3929cfcf..d2f9e6ee03 100644 --- a/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj +++ b/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj @@ -70,6 +70,7 @@ 0D6AB6CF22BB05E200EEE540 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 0D6AB73E22BD8F0200EEE540 /* FlutterEngineConfig.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = FlutterEngineConfig.xcconfig; sourceTree = ""; }; 68B6091227F62F990036AC78 /* VsyncWaiterIosTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = VsyncWaiterIosTest.mm; sourceTree = ""; }; + 3DD7D38C27D2B81000DA365C /* FlutterUndoManagerPluginTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterUndoManagerPluginTest.mm; sourceTree = ""; }; F7521D7226BB671E005F15C5 /* libios_test_flutter.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libios_test_flutter.dylib; path = "../../../../out/$(FLUTTER_ENGINE)/libios_test_flutter.dylib"; sourceTree = ""; }; F7521D7526BB673E005F15C5 /* libocmock_shared.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libocmock_shared.dylib; path = "../../../../out/$(FLUTTER_ENGINE)/libocmock_shared.dylib"; sourceTree = ""; }; F77E081726FA9CE6003E6E4C /* Flutter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Flutter.framework; path = "../../../../out/$(FLUTTER_ENGINE)/Flutter.framework"; sourceTree = ""; }; @@ -98,6 +99,7 @@ isa = PBXGroup; children = ( 68B6091227F62F990036AC78 /* VsyncWaiterIosTest.mm */, + 3DD7D38C27D2B81000DA365C /* FlutterUndoManagerPluginTest.mm */, F7A3FDE026B9E0A300EADD61 /* FlutterAppDelegateTest.mm */, 0AC232F424BA71D300A85907 /* SemanticsObjectTest.mm */, 0AC232F724BA71D300A85907 /* FlutterEngineTest.mm */,