forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlutterViewControllerTest.mm
More file actions
1759 lines (1535 loc) · 86.9 KB
/
FlutterViewControllerTest.mm
File metadata and controls
1759 lines (1535 loc) · 86.9 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <OCMock/OCMock.h>
#import <XCTest/XCTest.h>
#include "flutter/fml/platform/darwin/message_loop_darwin.h"
#import "flutter/lib/ui/window/platform_configuration.h"
#include "flutter/lib/ui/window/pointer_data.h"
#import "flutter/lib/ui/window/viewport_metrics.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEmbedderKeyResponder.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterFakeKeyEvents.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
#import "flutter/shell/platform/embedder/embedder.h"
#import "flutter/third_party/spring_animation/spring_animation.h"
FLUTTER_ASSERT_ARC
@interface FlutterEngine ()
- (FlutterTextInputPlugin*)textInputPlugin;
- (void)sendKeyEvent:(const FlutterKeyEvent&)event
callback:(nullable FlutterKeyEventCallback)callback
userData:(nullable void*)userData;
- (fml::RefPtr<fml::TaskRunner>)uiTaskRunner;
@end
/// Sometimes we have to use a custom mock to avoid retain cycles in OCMock.
/// Used for testing low memory notification.
@interface FlutterEnginePartialMock : FlutterEngine
@property(nonatomic, strong) FlutterBasicMessageChannel* lifecycleChannel;
@property(nonatomic, strong) FlutterBasicMessageChannel* keyEventChannel;
@property(nonatomic, weak) FlutterViewController* viewController;
@property(nonatomic, strong) FlutterTextInputPlugin* textInputPlugin;
@property(nonatomic, assign) BOOL didCallNotifyLowMemory;
- (FlutterTextInputPlugin*)textInputPlugin;
- (void)sendKeyEvent:(const FlutterKeyEvent&)event
callback:(nullable FlutterKeyEventCallback)callback
userData:(nullable void*)userData;
@end
@implementation FlutterEnginePartialMock
@synthesize viewController;
@synthesize lifecycleChannel;
@synthesize keyEventChannel;
@synthesize textInputPlugin;
- (void)notifyLowMemory {
_didCallNotifyLowMemory = YES;
}
- (void)sendKeyEvent:(const FlutterKeyEvent&)event
callback:(FlutterKeyEventCallback)callback
userData:(void*)userData API_AVAILABLE(ios(9.0)) {
if (callback == nil)
return;
// NSAssert(callback != nullptr, @"Invalid callback");
// Response is async, so we have to post it to the run loop instead of calling
// it directly.
CFRunLoopPerformBlock(CFRunLoopGetCurrent(), fml::MessageLoopDarwin::kMessageLoopCFRunLoopMode,
^() {
callback(true, userData);
});
}
@end
@interface FlutterEngine ()
- (BOOL)createShell:(NSString*)entrypoint
libraryURI:(NSString*)libraryURI
initialRoute:(NSString*)initialRoute;
- (void)dispatchPointerDataPacket:(std::unique_ptr<flutter::PointerDataPacket>)packet;
- (void)updateViewportMetrics:(flutter::ViewportMetrics)viewportMetrics;
- (void)attachView;
@end
@interface FlutterEngine (TestLowMemory)
- (void)notifyLowMemory;
@end
extern NSNotificationName const FlutterViewControllerWillDealloc;
/// A simple mock class for FlutterEngine.
///
/// OCMClassMock can't be used for FlutterEngine sometimes because OCMock retains arguments to
/// invocations and since the init for FlutterViewController calls a method on the
/// FlutterEngine it creates a retain cycle that stops us from testing behaviors related to
/// deleting FlutterViewControllers.
///
/// Used for testing deallocation.
@interface MockEngine : NSObject
@property(nonatomic, strong) FlutterDartProject* project;
@end
@implementation MockEngine
- (FlutterViewController*)viewController {
return nil;
}
- (void)setViewController:(FlutterViewController*)viewController {
// noop
}
@end
@interface FlutterKeyboardManager (Tests)
@property(nonatomic, retain, readonly)
NSMutableArray<id<FlutterKeyPrimaryResponder>>* primaryResponders;
@end
@interface FlutterEmbedderKeyResponder (Tests)
@property(nonatomic, copy, readonly) FlutterSendKeyEvent sendEvent;
@end
@interface FlutterViewController (Tests)
@property(nonatomic, assign) double targetViewInsetBottom;
@property(nonatomic, assign) BOOL isKeyboardInOrTransitioningFromBackground;
@property(nonatomic, assign) BOOL keyboardAnimationIsShowing;
- (void)createTouchRateCorrectionVSyncClientIfNeeded;
- (void)surfaceUpdated:(BOOL)appeared;
- (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences;
- (void)handlePressEvent:(FlutterUIPressProxy*)press
nextAction:(void (^)())next API_AVAILABLE(ios(13.4));
- (void)discreteScrollEvent:(UIPanGestureRecognizer*)recognizer;
- (void)updateViewportMetricsIfNeeded;
- (void)onUserSettingsChanged:(NSNotification*)notification;
- (void)applicationWillTerminate:(NSNotification*)notification;
- (void)goToApplicationLifecycle:(nonnull NSString*)state;
- (UIScreen*)mainScreenIfViewLoaded;
- (void)handleKeyboardNotification:(NSNotification*)notification;
- (CGFloat)calculateKeyboardInset:(CGRect)keyboardFrame keyboardMode:(int)keyboardMode;
- (BOOL)shouldIgnoreKeyboardNotification:(NSNotification*)notification;
- (FlutterKeyboardMode)calculateKeyboardAttachMode:(NSNotification*)notification;
- (CGFloat)calculateMultitaskingAdjustment:(CGRect)screenRect keyboardFrame:(CGRect)keyboardFrame;
- (void)startKeyBoardAnimation:(NSTimeInterval)duration;
- (UIView*)keyboardAnimationView;
- (SpringAnimation*)keyboardSpringAnimation;
- (void)setupKeyboardSpringAnimationIfNeeded:(CAAnimation*)keyboardAnimation;
- (void)setupKeyboardAnimationVsyncClient:
(FlutterKeyboardAnimationCallback)keyboardAnimationCallback;
- (void)ensureViewportMetricsIsCorrect;
- (void)invalidateKeyboardAnimationVSyncClient;
- (void)addInternalPlugins;
- (flutter::PointerData)generatePointerDataForFake;
- (void)sharedSetupWithProject:(nullable FlutterDartProject*)project
initialRoute:(nullable NSString*)initialRoute;
@end
@interface FlutterViewControllerTest : XCTestCase
@property(nonatomic, strong) id mockEngine;
@property(nonatomic, strong) id mockTextInputPlugin;
@property(nonatomic, strong) id messageSent;
- (void)sendMessage:(id _Nullable)message reply:(FlutterReply _Nullable)callback;
@end
@implementation FlutterViewControllerTest
- (void)setUp {
self.mockEngine = OCMClassMock([FlutterEngine class]);
self.mockTextInputPlugin = OCMClassMock([FlutterTextInputPlugin class]);
OCMStub([self.mockEngine textInputPlugin]).andReturn(self.mockTextInputPlugin);
self.messageSent = nil;
}
- (void)tearDown {
// We stop mocking here to avoid retain cycles that stop
// FlutterViewControllers from deallocing.
[self.mockEngine stopMocking];
self.mockEngine = nil;
self.mockTextInputPlugin = nil;
self.messageSent = nil;
}
- (id)setupMockMainScreenAndView:(FlutterViewController*)viewControllerMock
viewFrame:(CGRect)viewFrame
convertedFrame:(CGRect)convertedFrame {
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
id mockView = OCMClassMock([UIView class]);
OCMStub([mockView frame]).andReturn(viewFrame);
OCMStub([mockView convertRect:viewFrame toCoordinateSpace:[OCMArg any]])
.andReturn(convertedFrame);
OCMStub([viewControllerMock viewIfLoaded]).andReturn(mockView);
return mockView;
}
- (void)testViewDidLoadWillInvokeCreateTouchRateCorrectionVSyncClient {
FlutterEngine* engine = [[FlutterEngine alloc] init];
[engine runWithEntrypoint:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
[viewControllerMock loadView];
[viewControllerMock viewDidLoad];
OCMVerify([viewControllerMock createTouchRateCorrectionVSyncClientIfNeeded]);
}
- (void)testStartKeyboardAnimationWillInvokeSetupKeyboardSpringAnimationIfNeeded {
FlutterEngine* engine = [[FlutterEngine alloc] init];
[engine runWithEntrypoint:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
viewControllerMock.targetViewInsetBottom = 100;
[viewControllerMock startKeyBoardAnimation:0.25];
CAAnimation* keyboardAnimation =
[[viewControllerMock keyboardAnimationView].layer animationForKey:@"position"];
OCMVerify([viewControllerMock setupKeyboardSpringAnimationIfNeeded:keyboardAnimation]);
}
- (void)testSetupKeyboardSpringAnimationIfNeeded {
FlutterEngine* engine = [[FlutterEngine alloc] init];
[engine runWithEntrypoint:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
CGRect viewFrame = UIScreen.mainScreen.bounds;
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
// Null check.
[viewControllerMock setupKeyboardSpringAnimationIfNeeded:nil];
SpringAnimation* keyboardSpringAnimation = [viewControllerMock keyboardSpringAnimation];
XCTAssertTrue(keyboardSpringAnimation == nil);
// CAAnimation that is not a CASpringAnimation.
CABasicAnimation* nonSpringAnimation = [CABasicAnimation animation];
nonSpringAnimation.duration = 1.0;
nonSpringAnimation.fromValue = [NSNumber numberWithFloat:0.0];
nonSpringAnimation.toValue = [NSNumber numberWithFloat:1.0];
nonSpringAnimation.keyPath = @"position";
[viewControllerMock setupKeyboardSpringAnimationIfNeeded:nonSpringAnimation];
keyboardSpringAnimation = [viewControllerMock keyboardSpringAnimation];
XCTAssertTrue(keyboardSpringAnimation == nil);
// CASpringAnimation.
CASpringAnimation* springAnimation = [CASpringAnimation animation];
springAnimation.mass = 1.0;
springAnimation.stiffness = 100.0;
springAnimation.damping = 10.0;
springAnimation.keyPath = @"position";
springAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
[viewControllerMock setupKeyboardSpringAnimationIfNeeded:springAnimation];
keyboardSpringAnimation = [viewControllerMock keyboardSpringAnimation];
XCTAssertTrue(keyboardSpringAnimation != nil);
}
- (void)testKeyboardAnimationIsShowingAndCompounding {
FlutterEngine* engine = [[FlutterEngine alloc] init];
[engine runWithEntrypoint:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
CGRect viewFrame = UIScreen.mainScreen.bounds;
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
BOOL isLocal = YES;
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.height;
// Start show keyboard animation.
CGRect initialShowKeyboardBeginFrame = CGRectMake(0, screenHeight, screenWidth, 250);
CGRect initialShowKeyboardEndFrame = CGRectMake(0, screenHeight - 250, screenWidth, 500);
NSNotification* fakeNotification = [NSNotification
notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameBeginUserInfoKey" : @(initialShowKeyboardBeginFrame),
@"UIKeyboardFrameEndUserInfoKey" : @(initialShowKeyboardEndFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @(0.25),
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
viewControllerMock.targetViewInsetBottom = 0;
[viewControllerMock handleKeyboardNotification:fakeNotification];
BOOL isShowingAnimation1 = viewControllerMock.keyboardAnimationIsShowing;
XCTAssertTrue(isShowingAnimation1);
// Start compounding show keyboard animation.
CGRect compoundingShowKeyboardBeginFrame = CGRectMake(0, screenHeight - 250, screenWidth, 250);
CGRect compoundingShowKeyboardEndFrame = CGRectMake(0, screenHeight - 500, screenWidth, 500);
fakeNotification = [NSNotification
notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameBeginUserInfoKey" : @(compoundingShowKeyboardBeginFrame),
@"UIKeyboardFrameEndUserInfoKey" : @(compoundingShowKeyboardEndFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @(0.25),
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
[viewControllerMock handleKeyboardNotification:fakeNotification];
BOOL isShowingAnimation2 = viewControllerMock.keyboardAnimationIsShowing;
XCTAssertTrue(isShowingAnimation2);
XCTAssertTrue(isShowingAnimation1 == isShowingAnimation2);
// Start hide keyboard animation.
CGRect initialHideKeyboardBeginFrame = CGRectMake(0, screenHeight - 500, screenWidth, 250);
CGRect initialHideKeyboardEndFrame = CGRectMake(0, screenHeight - 250, screenWidth, 500);
fakeNotification = [NSNotification
notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameBeginUserInfoKey" : @(initialHideKeyboardBeginFrame),
@"UIKeyboardFrameEndUserInfoKey" : @(initialHideKeyboardEndFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @(0.25),
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
[viewControllerMock handleKeyboardNotification:fakeNotification];
BOOL isShowingAnimation3 = viewControllerMock.keyboardAnimationIsShowing;
XCTAssertFalse(isShowingAnimation3);
XCTAssertTrue(isShowingAnimation2 != isShowingAnimation3);
// Start compounding hide keyboard animation.
CGRect compoundingHideKeyboardBeginFrame = CGRectMake(0, screenHeight - 250, screenWidth, 250);
CGRect compoundingHideKeyboardEndFrame = CGRectMake(0, screenHeight, screenWidth, 500);
fakeNotification = [NSNotification
notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameBeginUserInfoKey" : @(compoundingHideKeyboardBeginFrame),
@"UIKeyboardFrameEndUserInfoKey" : @(compoundingHideKeyboardEndFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @(0.25),
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
[viewControllerMock handleKeyboardNotification:fakeNotification];
BOOL isShowingAnimation4 = viewControllerMock.keyboardAnimationIsShowing;
XCTAssertFalse(isShowingAnimation4);
XCTAssertTrue(isShowingAnimation3 == isShowingAnimation4);
}
- (void)testShouldIgnoreKeyboardNotification {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
CGRect viewFrame = UIScreen.mainScreen.bounds;
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
CGRect emptyKeyboard = CGRectZero;
CGRect zeroHeightKeyboard = CGRectMake(0, 0, screenWidth, 0);
CGRect validKeyboardEndFrame = CGRectMake(0, screenHeight - 320, screenWidth, 320);
BOOL isLocal = NO;
// Hide notification, valid keyboard
NSNotification* notification =
[NSNotification notificationWithName:UIKeyboardWillHideNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(validKeyboardEndFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
BOOL shouldIgnore = [viewControllerMock shouldIgnoreKeyboardNotification:notification];
XCTAssertTrue(shouldIgnore == NO);
// All zero keyboard
isLocal = YES;
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(emptyKeyboard),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
shouldIgnore = [viewControllerMock shouldIgnoreKeyboardNotification:notification];
XCTAssertTrue(shouldIgnore == YES);
// Zero height keyboard
isLocal = NO;
notification =
[NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(zeroHeightKeyboard),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
shouldIgnore = [viewControllerMock shouldIgnoreKeyboardNotification:notification];
XCTAssertTrue(shouldIgnore == NO);
// Valid keyboard, triggered from another app
isLocal = NO;
notification =
[NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(validKeyboardEndFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
shouldIgnore = [viewControllerMock shouldIgnoreKeyboardNotification:notification];
XCTAssertTrue(shouldIgnore == YES);
// Valid keyboard
isLocal = YES;
notification =
[NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(validKeyboardEndFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
shouldIgnore = [viewControllerMock shouldIgnoreKeyboardNotification:notification];
XCTAssertTrue(shouldIgnore == NO);
if (@available(iOS 13.0, *)) {
// noop
} else {
// Valid keyboard, keyboard is in background
OCMStub([viewControllerMock isKeyboardInOrTransitioningFromBackground]).andReturn(YES);
isLocal = YES;
notification =
[NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(validKeyboardEndFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
shouldIgnore = [viewControllerMock shouldIgnoreKeyboardNotification:notification];
XCTAssertTrue(shouldIgnore == YES);
}
}
- (void)testKeyboardAnimationWillWaitUIThreadVsync {
// We need to make sure the new viewport metrics get sent after the
// begin frame event has processed. And this test is to expect that the callback
// will sync with UI thread. So just simulate a lot of works on UI thread and
// test the keyboard animation callback will execute until UI task completed.
// Related issue: https://github.com/flutter/flutter/issues/120555.
FlutterEngine* engine = [[FlutterEngine alloc] init];
[engine runWithEntrypoint:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
// Post a task to UI thread to block the thread.
const int delayTime = 1;
[engine uiTaskRunner]->PostTask([] { sleep(delayTime); });
XCTestExpectation* expectation = [self expectationWithDescription:@"keyboard animation callback"];
__block CFTimeInterval fulfillTime;
FlutterKeyboardAnimationCallback callback = ^(fml::TimePoint targetTime) {
fulfillTime = CACurrentMediaTime();
[expectation fulfill];
};
CFTimeInterval startTime = CACurrentMediaTime();
[viewController setupKeyboardAnimationVsyncClient:callback];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
XCTAssertTrue(fulfillTime - startTime > delayTime);
}
- (void)testCalculateKeyboardAttachMode {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
CGRect viewFrame = UIScreen.mainScreen.bounds;
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
// hide notification
CGRect keyboardFrame = CGRectZero;
NSNotification* notification =
[NSNotification notificationWithName:UIKeyboardWillHideNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
FlutterKeyboardMode keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeHidden);
// all zeros
keyboardFrame = CGRectZero;
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeFloating);
// 0 height
keyboardFrame = CGRectMake(0, 0, screenWidth, 0);
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeHidden);
// floating
keyboardFrame = CGRectMake(0, 0, 320, 320);
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeFloating);
// undocked
keyboardFrame = CGRectMake(0, 0, screenWidth, 320);
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeFloating);
// docked
keyboardFrame = CGRectMake(0, screenHeight - 320, screenWidth, 320);
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeDocked);
// docked - rounded values
CGFloat longDecimalHeight = 320.666666666666666;
keyboardFrame = CGRectMake(0, screenHeight - longDecimalHeight, screenWidth, longDecimalHeight);
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeDocked);
// hidden - rounded values
keyboardFrame = CGRectMake(0, screenHeight - .0000001, screenWidth, longDecimalHeight);
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeHidden);
// hidden
keyboardFrame = CGRectMake(0, screenHeight, screenWidth, 320);
notification = [NSNotification notificationWithName:UIKeyboardWillChangeFrameNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(YES)
}];
keyboardMode = [viewControllerMock calculateKeyboardAttachMode:notification];
XCTAssertTrue(keyboardMode == FlutterKeyboardModeHidden);
}
- (void)testCalculateMultitaskingAdjustment {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
CGRect screenRect = UIScreen.mainScreen.bounds;
CGRect viewOrigFrame = CGRectMake(0, 0, 320, screenHeight - 40);
CGRect convertedViewFrame = CGRectMake(20, 20, 320, screenHeight - 40);
CGRect keyboardFrame = CGRectMake(20, screenHeight - 320, screenWidth, 300);
id mockView = [self setupMockMainScreenAndView:viewControllerMock
viewFrame:viewOrigFrame
convertedFrame:convertedViewFrame];
id mockTraitCollection = OCMClassMock([UITraitCollection class]);
OCMStub([mockTraitCollection userInterfaceIdiom]).andReturn(UIUserInterfaceIdiomPad);
OCMStub([mockTraitCollection horizontalSizeClass]).andReturn(UIUserInterfaceSizeClassCompact);
OCMStub([mockTraitCollection verticalSizeClass]).andReturn(UIUserInterfaceSizeClassRegular);
OCMStub([mockView traitCollection]).andReturn(mockTraitCollection);
CGFloat adjustment = [viewControllerMock calculateMultitaskingAdjustment:screenRect
keyboardFrame:keyboardFrame];
XCTAssertTrue(adjustment == 20);
}
- (void)testCalculateKeyboardInset {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
CGRect viewOrigFrame = CGRectMake(0, 0, 320, screenHeight - 40);
CGRect convertedViewFrame = CGRectMake(20, 20, 320, screenHeight - 40);
CGRect keyboardFrame = CGRectMake(20, screenHeight - 320, screenWidth, 300);
[self setupMockMainScreenAndView:viewControllerMock
viewFrame:viewOrigFrame
convertedFrame:convertedViewFrame];
CGFloat inset = [viewControllerMock calculateKeyboardInset:keyboardFrame
keyboardMode:FlutterKeyboardModeDocked];
XCTAssertTrue(inset == 300 * UIScreen.mainScreen.scale);
}
- (void)testHandleKeyboardNotification {
FlutterEngine* engine = [[FlutterEngine alloc] init];
[engine runWithEntrypoint:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
// keyboard is empty
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
CGRect keyboardFrame = CGRectMake(0, screenHeight - 320, screenWidth, 320);
CGRect viewFrame = UIScreen.mainScreen.bounds;
BOOL isLocal = YES;
NSNotification* notification =
[NSNotification notificationWithName:UIKeyboardWillShowNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @0.25,
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
viewControllerMock.targetViewInsetBottom = 0;
XCTestExpectation* expectation = [self expectationWithDescription:@"update viewport"];
OCMStub([viewControllerMock updateViewportMetricsIfNeeded]).andDo(^(NSInvocation* invocation) {
[expectation fulfill];
});
[viewControllerMock handleKeyboardNotification:notification];
XCTAssertTrue(viewControllerMock.targetViewInsetBottom == 320 * UIScreen.mainScreen.scale);
OCMVerify([viewControllerMock startKeyBoardAnimation:0.25]);
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
- (void)testEnsureBottomInsetIsZeroWhenKeyboardDismissed {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
CGRect keyboardFrame = CGRectZero;
BOOL isLocal = YES;
NSNotification* fakeNotification =
[NSNotification notificationWithName:UIKeyboardWillHideNotification
object:nil
userInfo:@{
@"UIKeyboardFrameEndUserInfoKey" : @(keyboardFrame),
@"UIKeyboardAnimationDurationUserInfoKey" : @(0.25),
@"UIKeyboardIsLocalUserInfoKey" : @(isLocal)
}];
viewControllerMock.targetViewInsetBottom = 10;
[viewControllerMock handleKeyboardNotification:fakeNotification];
XCTAssertTrue(viewControllerMock.targetViewInsetBottom == 0);
}
- (void)testEnsureViewportMetricsWillInvokeAndDisplayLinkWillInvalidateInViewDidDisappear {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
id viewControllerMock = OCMPartialMock(viewController);
[viewControllerMock viewDidDisappear:YES];
OCMVerify([viewControllerMock ensureViewportMetricsIsCorrect]);
OCMVerify([viewControllerMock invalidateKeyboardAnimationVSyncClient]);
}
- (void)testViewDidDisappearDoesntPauseEngineWhenNotTheViewController {
id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]);
FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init];
mockEngine.lifecycleChannel = lifecycleChannel;
FlutterViewController* viewControllerA =
[[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil];
FlutterViewController* viewControllerB =
[[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil bundle:nil];
id viewControllerMock = OCMPartialMock(viewControllerA);
OCMStub([viewControllerMock surfaceUpdated:NO]);
mockEngine.viewController = viewControllerB;
[viewControllerA viewDidDisappear:NO];
OCMReject([lifecycleChannel sendMessage:@"AppLifecycleState.paused"]);
OCMReject([viewControllerMock surfaceUpdated:[OCMArg any]]);
}
- (void)testAppWillTerminateViewDidDestroyTheEngine {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
id viewControllerMock = OCMPartialMock(viewController);
OCMStub([viewControllerMock goToApplicationLifecycle:@"AppLifecycleState.detached"]);
OCMStub([mockEngine destroyContext]);
[viewController applicationWillTerminate:nil];
OCMVerify([viewControllerMock goToApplicationLifecycle:@"AppLifecycleState.detached"]);
OCMVerify([mockEngine destroyContext]);
}
- (void)testViewDidDisappearDoesPauseEngineWhenIsTheViewController {
id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]);
FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init];
mockEngine.lifecycleChannel = lifecycleChannel;
__weak FlutterViewController* weakViewController;
@autoreleasepool {
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
weakViewController = viewController;
id viewControllerMock = OCMPartialMock(viewController);
OCMStub([viewControllerMock surfaceUpdated:NO]);
[viewController viewDidDisappear:NO];
OCMVerify([lifecycleChannel sendMessage:@"AppLifecycleState.paused"]);
OCMVerify([viewControllerMock surfaceUpdated:NO]);
}
XCTAssertNil(weakViewController);
}
- (void)
testEngineConfigSyncMethodWillExecuteWhenViewControllerInEngineIsCurrentViewControllerInViewWillAppear {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
[viewController viewWillAppear:YES];
OCMVerify([viewController onUserSettingsChanged:nil]);
}
- (void)
testEngineConfigSyncMethodWillNotExecuteWhenViewControllerInEngineIsNotCurrentViewControllerInViewWillAppear {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewControllerA = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = nil;
FlutterViewController* viewControllerB = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = nil;
mockEngine.viewController = viewControllerB;
[viewControllerA viewWillAppear:YES];
OCMVerify(never(), [viewControllerA onUserSettingsChanged:nil]);
}
- (void)
testEngineConfigSyncMethodWillExecuteWhenViewControllerInEngineIsCurrentViewControllerInViewDidAppear {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
[viewController viewDidAppear:YES];
OCMVerify([viewController onUserSettingsChanged:nil]);
}
- (void)
testEngineConfigSyncMethodWillNotExecuteWhenViewControllerInEngineIsNotCurrentViewControllerInViewDidAppear {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewControllerA = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = nil;
FlutterViewController* viewControllerB = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = nil;
mockEngine.viewController = viewControllerB;
[viewControllerA viewDidAppear:YES];
OCMVerify(never(), [viewControllerA onUserSettingsChanged:nil]);
}
- (void)
testEngineConfigSyncMethodWillExecuteWhenViewControllerInEngineIsCurrentViewControllerInViewWillDisappear {
id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]);
FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init];
mockEngine.lifecycleChannel = lifecycleChannel;
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = viewController;
[viewController viewWillDisappear:NO];
OCMVerify([lifecycleChannel sendMessage:@"AppLifecycleState.inactive"]);
}
- (void)
testEngineConfigSyncMethodWillNotExecuteWhenViewControllerInEngineIsNotCurrentViewControllerInViewWillDisappear {
id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]);
FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init];
mockEngine.lifecycleChannel = lifecycleChannel;
FlutterViewController* viewControllerA = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerB = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = viewControllerB;
[viewControllerA viewDidDisappear:NO];
OCMReject([lifecycleChannel sendMessage:@"AppLifecycleState.inactive"]);
}
- (void)testUpdateViewportMetricsIfNeeded_DoesntInvokeEngineWhenNotTheViewController {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewControllerA = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = nil;
FlutterViewController* viewControllerB = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = viewControllerB;
[viewControllerA updateViewportMetricsIfNeeded];
flutter::ViewportMetrics viewportMetrics;
OCMVerify(never(), [mockEngine updateViewportMetrics:viewportMetrics]);
}
- (void)testUpdateViewportMetricsIfNeeded_DoesInvokeEngineWhenIsTheViewController {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = viewController;
flutter::ViewportMetrics viewportMetrics;
OCMExpect([mockEngine updateViewportMetrics:viewportMetrics]).ignoringNonObjectArgs();
[viewController updateViewportMetricsIfNeeded];
OCMVerifyAll(mockEngine);
}
- (void)testUpdateViewportMetricsIfNeeded_DoesNotInvokeEngineWhenShouldBeIgnoredDuringRotation {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
mockEngine.viewController = viewController;
id mockCoordinator = OCMProtocolMock(@protocol(UIViewControllerTransitionCoordinator));
OCMStub([mockCoordinator transitionDuration]).andReturn(0.5);
// Mimic the device rotation.
[viewController viewWillTransitionToSize:CGSizeZero withTransitionCoordinator:mockCoordinator];
// Should not trigger the engine call when during rotation.
[viewController updateViewportMetricsIfNeeded];
OCMVerify(never(), [mockEngine updateViewportMetrics:flutter::ViewportMetrics()]);
}
- (void)testViewWillTransitionToSize_DoesDelayEngineCallIfNonZeroDuration {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
mockEngine.viewController = viewController;
// Mimic the device rotation with non-zero transition duration.
NSTimeInterval transitionDuration = 0.5;
id mockCoordinator = OCMProtocolMock(@protocol(UIViewControllerTransitionCoordinator));
OCMStub([mockCoordinator transitionDuration]).andReturn(transitionDuration);
flutter::ViewportMetrics viewportMetrics;
OCMExpect([mockEngine updateViewportMetrics:viewportMetrics]).ignoringNonObjectArgs();
[viewController viewWillTransitionToSize:CGSizeZero withTransitionCoordinator:mockCoordinator];
// Should not immediately call the engine (this request should be ignored).
[viewController updateViewportMetricsIfNeeded];
OCMVerify(never(), [mockEngine updateViewportMetrics:flutter::ViewportMetrics()]);
// Should delay the engine call for half of the transition duration.
// Wait for additional transitionDuration to allow updateViewportMetrics calls if any.
XCTWaiterResult result = [XCTWaiter
waitForExpectations:@[ [self expectationWithDescription:@"Waiting for rotation duration"] ]
timeout:transitionDuration];
XCTAssertEqual(result, XCTWaiterResultTimedOut);
OCMVerifyAll(mockEngine);
}
- (void)testViewWillTransitionToSize_DoesNotDelayEngineCallIfZeroDuration {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
mockEngine.viewController = viewController;
// Mimic the device rotation with zero transition duration.
id mockCoordinator = OCMProtocolMock(@protocol(UIViewControllerTransitionCoordinator));
OCMStub([mockCoordinator transitionDuration]).andReturn(0);
flutter::ViewportMetrics viewportMetrics;
OCMExpect([mockEngine updateViewportMetrics:viewportMetrics]).ignoringNonObjectArgs();
// Should immediately trigger the engine call, without delay.
[viewController viewWillTransitionToSize:CGSizeZero withTransitionCoordinator:mockCoordinator];
[viewController updateViewportMetricsIfNeeded];
OCMVerifyAll(mockEngine);
}
- (void)testViewDidLoadDoesntInvokeEngineWhenNotTheViewController {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewControllerA = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = nil;
FlutterViewController* viewControllerB = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = viewControllerB;
UIView* view = viewControllerA.view;
XCTAssertNotNil(view);
OCMVerify(never(), [mockEngine attachView]);
}
- (void)testViewDidLoadDoesInvokeEngineWhenIsTheViewController {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
mockEngine.viewController = nil;
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
mockEngine.viewController = viewController;
UIView* view = viewController.view;
XCTAssertNotNil(view);
OCMVerify(times(1), [mockEngine attachView]);
}
- (void)testViewDidLoadDoesntInvokeEngineAttachViewWhenEngineNeedsLaunch {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
mockEngine.viewController = nil;
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
// sharedSetupWithProject sets the engine needs to be launched.
[viewController sharedSetupWithProject:nil initialRoute:nil];
mockEngine.viewController = viewController;
UIView* view = viewController.view;
XCTAssertNotNil(view);