This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathFlutterUndoManagerPluginTest.mm
More file actions
189 lines (157 loc) · 7.58 KB
/
FlutterUndoManagerPluginTest.mm
File metadata and controls
189 lines (157 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// 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 <OCMock/OCMock.h>
#import <XCTest/XCTest.h>
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"
FLUTTER_ASSERT_ARC
@interface FakeFlutterUndoManagerDelegate : NSObject <FlutterUndoManagerDelegate>
@property(readonly) NSUInteger undoCount;
@property(readonly) NSUInteger redoCount;
@property(nonatomic, nullable) NSUndoManager* undoManager;
@property(nonatomic, nullable) UIView<UITextInput>* activeTextInputView;
- (instancetype)initWithUndoManager:(NSUndoManager*)undoManager
activeTextInputView:(UIView<UITextInput>*)activeTextInputView;
@end
@implementation FakeFlutterUndoManagerDelegate
- (instancetype)initWithUndoManager:(NSUndoManager*)undoManager
activeTextInputView:(UIView<UITextInput>*)activeTextInputView {
self = [super init];
if (self) {
_undoManager = undoManager;
_activeTextInputView = activeTextInputView;
}
return self;
}
- (void)handleUndoWithDirection:(FlutterUndoRedoDirection)direction {
if (direction == FlutterUndoRedoDirectionUndo) {
_undoCount++;
} else {
_redoCount++;
}
}
@end
@interface FlutterUndoManagerPluginTest : XCTestCase
@property(nonatomic) FakeFlutterUndoManagerDelegate* undoManagerDelegate;
@property(nonatomic) FlutterUndoManagerPlugin* undoManagerPlugin;
@property(nonatomic) UIView<UITextInput>* activeTextInputView;
@property(nonatomic) NSUndoManager* undoManager;
@end
@implementation FlutterUndoManagerPluginTest
- (void)setUp {
[super setUp];
self.undoManager = OCMClassMock([NSUndoManager class]);
self.activeTextInputView = OCMClassMock([FlutterTextInputView class]);
self.undoManagerDelegate =
[[FakeFlutterUndoManagerDelegate alloc] initWithUndoManager:self.undoManager
activeTextInputView:self.activeTextInputView];
self.undoManagerPlugin =
[[FlutterUndoManagerPlugin alloc] initWithDelegate:self.undoManagerDelegate];
}
- (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 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(1UL, self.undoManagerDelegate.undoCount);
XCTAssertEqual(0UL, self.undoManagerDelegate.redoCount);
XCTAssertEqual(2, registerUndoCount);
// Invoking the redo handler will invoke the handleUndo delegate method with "redo".
undoHandler(self.undoManagerPlugin);
XCTAssertEqual(1UL, self.undoManagerDelegate.undoCount);
XCTAssertEqual(1UL, self.undoManagerDelegate.redoCount);
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(1UL, self.undoManagerDelegate.undoCount);
XCTAssertEqual(2UL, self.undoManagerDelegate.redoCount);
}
- (void)testSetUndoStateDoesInteractWithInputDelegate {
// Regression test for https://github.com/flutter/flutter/issues/133424
FlutterMethodCall* setUndoStateCall =
[FlutterMethodCall methodCallWithMethodName:@"UndoManager.setUndoState"
arguments:@{@"canUndo" : @NO, @"canRedo" : @NO}];
[self.undoManagerPlugin handleMethodCall:setUndoStateCall
result:^(id _Nullable result){
}];
OCMVerify(never(), [self.activeTextInputView inputDelegate]);
}
- (void)testDeallocRemovesAllUndoManagerActions {
__weak FlutterUndoManagerPlugin* weakUndoManagerPlugin;
// Use a real undo manager.
NSUndoManager* undoManager = [[NSUndoManager alloc] init];
@autoreleasepool {
id activeTextInputView = OCMClassMock([FlutterTextInputView class]);
FakeFlutterUndoManagerDelegate* undoManagerDelegate =
[[FakeFlutterUndoManagerDelegate alloc] initWithUndoManager:undoManager
activeTextInputView:activeTextInputView];
FlutterUndoManagerPlugin* undoManagerPlugin =
[[FlutterUndoManagerPlugin alloc] initWithDelegate:undoManagerDelegate];
weakUndoManagerPlugin = undoManagerPlugin;
FlutterMethodCall* setUndoStateCall =
[FlutterMethodCall methodCallWithMethodName:@"UndoManager.setUndoState"
arguments:@{@"canUndo" : @YES, @"canRedo" : @YES}];
[undoManagerPlugin handleMethodCall:setUndoStateCall
result:^(id _Nullable result){
}];
XCTAssertTrue(undoManager.canUndo);
XCTAssertTrue(undoManager.canRedo);
// Fake out the undoManager being nil, which happens when the FlutterViewController deallocs and
// the undo manager can't be fetched from the FlutterEngine delegate.
undoManagerDelegate.undoManager = nil;
}
XCTAssertNil(weakUndoManagerPlugin);
// Regression test for https://github.com/flutter/flutter/issues/150408.
// Undo manager undo and redo stack should be empty after the plugin deallocs.
XCTAssertFalse(undoManager.canUndo);
XCTAssertFalse(undoManager.canRedo);
}
@end