Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 07713a9

Browse files
committed
refactor to make it easier to switch to a test screen instead of UIScreen.mainScreen
1 parent 5b4edef commit 07713a9

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,14 @@ - (void)tearDown {
174174
self.messageSent = nil;
175175
}
176176

177+
- (id)testScreen {
178+
return UIScreen.mainScreen;
179+
}
180+
177181
- (id)setupMockMainScreenAndView:(FlutterViewController*)viewControllerMock
178182
viewFrame:(CGRect)viewFrame
179183
convertedFrame:(CGRect)convertedFrame {
180-
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(UIScreen.mainScreen);
184+
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn([self testScreen]);
181185
id mockView = OCMClassMock([UIView class]);
182186
OCMStub([mockView frame]).andReturn(viewFrame);
183187
OCMStub([mockView convertRect:viewFrame toCoordinateSpace:[OCMArg any]])
@@ -222,7 +226,8 @@ - (void)testSetupKeyboardSpringAnimationIfNeeded {
222226
nibName:nil
223227
bundle:nil];
224228
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
225-
CGRect viewFrame = UIScreen.mainScreen.bounds;
229+
UIScreen* screen = [self testScreen];
230+
CGRect viewFrame = screen.bounds;
226231
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
227232

228233
// Null check.
@@ -261,12 +266,13 @@ - (void)testKeyboardAnimationIsShowingAndCompounding {
261266
nibName:nil
262267
bundle:nil];
263268
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
264-
CGRect viewFrame = UIScreen.mainScreen.bounds;
269+
UIScreen* screen = [self testScreen];
270+
CGRect viewFrame = screen.bounds;
265271
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
266272

267273
BOOL isLocal = YES;
268-
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
269-
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.height;
274+
CGFloat screenHeight = screen.bounds.size.height;
275+
CGFloat screenWidth = screen.bounds.size.height;
270276

271277
// Start show keyboard animation.
272278
CGRect initialShowKeyboardBeginFrame = CGRectMake(0, screenHeight, screenWidth, 250);
@@ -347,11 +353,12 @@ - (void)testShouldIgnoreKeyboardNotification {
347353
nibName:nil
348354
bundle:nil];
349355
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
350-
CGRect viewFrame = UIScreen.mainScreen.bounds;
356+
UIScreen* screen = [self testScreen];
357+
CGRect viewFrame = screen.bounds;
351358
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
352359

353-
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
354-
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
360+
CGFloat screenWidth = screen.bounds.size.width;
361+
CGFloat screenHeight = screen.bounds.size.height;
355362
CGRect emptyKeyboard = CGRectZero;
356363
CGRect zeroHeightKeyboard = CGRectMake(0, 0, screenWidth, 0);
357364
CGRect validKeyboardEndFrame = CGRectMake(0, screenHeight - 320, screenWidth, 320);
@@ -487,11 +494,12 @@ - (void)testCalculateKeyboardAttachMode {
487494
bundle:nil];
488495

489496
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
490-
CGRect viewFrame = UIScreen.mainScreen.bounds;
497+
UIScreen* screen = [self testScreen];
498+
CGRect viewFrame = screen.bounds;
491499
[self setupMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame];
492500

493-
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
494-
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
501+
CGFloat screenWidth = screen.bounds.size.width;
502+
CGFloat screenHeight = screen.bounds.size.height;
495503

496504
// hide notification
497505
CGRect keyboardFrame = CGRectZero;
@@ -612,9 +620,10 @@ - (void)testCalculateMultitaskingAdjustment {
612620
bundle:nil];
613621
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
614622

615-
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
616-
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
617-
CGRect screenRect = UIScreen.mainScreen.bounds;
623+
UIScreen* screen = [self testScreen];
624+
CGFloat screenWidth = screen.bounds.size.width;
625+
CGFloat screenHeight = screen.bounds.size.height;
626+
CGRect screenRect = screen.bounds;
618627
CGRect viewOrigFrame = CGRectMake(0, 0, 320, screenHeight - 40);
619628
CGRect convertedViewFrame = CGRectMake(20, 20, 320, screenHeight - 40);
620629
CGRect keyboardFrame = CGRectMake(20, screenHeight - 320, screenWidth, 300);
@@ -639,10 +648,11 @@ - (void)testCalculateKeyboardInset {
639648
nibName:nil
640649
bundle:nil];
641650
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
642-
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(UIScreen.mainScreen);
651+
UIScreen* screen = [self testScreen];
652+
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen);
643653

644-
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
645-
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
654+
CGFloat screenWidth = screen.bounds.size.width;
655+
CGFloat screenHeight = screen.bounds.size.height;
646656
CGRect viewOrigFrame = CGRectMake(0, 0, 320, screenHeight - 40);
647657
CGRect convertedViewFrame = CGRectMake(20, 20, 320, screenHeight - 40);
648658
CGRect keyboardFrame = CGRectMake(20, screenHeight - 320, screenWidth, 300);
@@ -653,7 +663,7 @@ - (void)testCalculateKeyboardInset {
653663

654664
CGFloat inset = [viewControllerMock calculateKeyboardInset:keyboardFrame
655665
keyboardMode:FlutterKeyboardModeDocked];
656-
XCTAssertTrue(inset == 300 * UIScreen.mainScreen.scale);
666+
XCTAssertTrue(inset == 300 * screen.scale);
657667
}
658668

659669
- (void)testHandleKeyboardNotification {
@@ -663,10 +673,11 @@ - (void)testHandleKeyboardNotification {
663673
nibName:nil
664674
bundle:nil];
665675
// keyboard is empty
666-
CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width;
667-
CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
676+
UIScreen* screen = [self testScreen];
677+
CGFloat screenWidth = screen.bounds.size.width;
678+
CGFloat screenHeight = screen.bounds.size.height;
668679
CGRect keyboardFrame = CGRectMake(0, screenHeight - 320, screenWidth, 320);
669-
CGRect viewFrame = UIScreen.mainScreen.bounds;
680+
CGRect viewFrame = screen.bounds;
670681
BOOL isLocal = YES;
671682
NSNotification* notification =
672683
[NSNotification notificationWithName:UIKeyboardWillShowNotification
@@ -685,7 +696,7 @@ - (void)testHandleKeyboardNotification {
685696
});
686697

687698
[viewControllerMock handleKeyboardNotification:notification];
688-
XCTAssertTrue(viewControllerMock.targetViewInsetBottom == 320 * UIScreen.mainScreen.scale);
699+
XCTAssertTrue(viewControllerMock.targetViewInsetBottom == 320 * screen.scale);
689700
OCMVerify([viewControllerMock startKeyBoardAnimation:0.25]);
690701
[self waitForExpectationsWithTimeout:5.0 handler:nil];
691702
}
@@ -896,7 +907,8 @@ - (void)testUpdateViewportMetricsIfNeeded_DoesNotInvokeEngineWhenShouldBeIgnored
896907
nibName:nil
897908
bundle:nil];
898909
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
899-
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(UIScreen.mainScreen);
910+
UIScreen* screen = [self testScreen];
911+
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen);
900912
mockEngine.viewController = viewController;
901913

902914
id mockCoordinator = OCMProtocolMock(@protocol(UIViewControllerTransitionCoordinator));
@@ -917,7 +929,8 @@ - (void)testViewWillTransitionToSize_DoesDelayEngineCallIfNonZeroDuration {
917929
nibName:nil
918930
bundle:nil];
919931
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
920-
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(UIScreen.mainScreen);
932+
UIScreen* screen = [self testScreen];
933+
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen);
921934
mockEngine.viewController = viewController;
922935

923936
// Mimic the device rotation with non-zero transition duration.
@@ -950,7 +963,8 @@ - (void)testViewWillTransitionToSize_DoesNotDelayEngineCallIfZeroDuration {
950963
nibName:nil
951964
bundle:nil];
952965
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
953-
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(UIScreen.mainScreen);
966+
UIScreen* screen = [self testScreen];
967+
OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen);
954968
mockEngine.viewController = viewController;
955969

956970
// Mimic the device rotation with zero transition duration.

0 commit comments

Comments
 (0)