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

Commit f08fc61

Browse files
committed
use mainScreenIfViewLoaded
1 parent e2c811a commit f08fc61

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,12 +1370,12 @@ - (void)viewSafeAreaInsetsDidChange {
13701370

13711371
// Set _viewportMetrics physical size.
13721372
- (void)setViewportMetricsSize {
1373-
// TODO(hellohuanlin): Use [self mainScreenIfViewLoaded] instead of [UIScreen mainScreen].
1374-
// This requires adding the view to window during unit tests, which calls multiple engine calls
1375-
// that is hard to mock since they take/return structs. An alternative approach is to partial mock
1376-
// the FlutterViewController to make view controller life cycle methods no-op, and insert
1377-
// this mock into the responder chain.
1378-
CGFloat scale = [UIScreen mainScreen].scale;
1373+
UIScreen* mainScreen = [self mainScreenIfViewLoaded];
1374+
if (!mainScreen) {
1375+
return;
1376+
}
1377+
1378+
CGFloat scale = mainScreen.scale;
13791379
_viewportMetrics.physical_width = self.view.bounds.size.width * scale;
13801380
_viewportMetrics.physical_height = self.view.bounds.size.height * scale;
13811381
}
@@ -1384,7 +1384,12 @@ - (void)setViewportMetricsSize {
13841384
//
13851385
// Viewport paddings represent the iOS safe area insets.
13861386
- (void)setViewportMetricsPaddings {
1387-
CGFloat scale = [UIScreen mainScreen].scale;
1387+
UIScreen* mainScreen = [self mainScreenIfViewLoaded];
1388+
if (!mainScreen) {
1389+
return;
1390+
}
1391+
1392+
CGFloat scale = mainScreen.scale;
13881393
_viewportMetrics.physical_padding_top = self.view.safeAreaInsets.top * scale;
13891394
_viewportMetrics.physical_padding_left = self.view.safeAreaInsets.left * scale;
13901395
_viewportMetrics.physical_padding_right = self.view.safeAreaInsets.right * scale;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,8 @@ - (void)testUpdateViewportMetricsIfNeeded_DoesNotInvokeEngineWhenShouldBeIgnored
871871
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
872872
nibName:nil
873873
bundle:nil];
874+
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
875+
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
874876
mockEngine.viewController = viewController;
875877

876878
id mockCoordinator = OCMProtocolMock(@protocol(UIViewControllerTransitionCoordinator));
@@ -890,6 +892,8 @@ - (void)testViewWillTransitionToSize_DoesDelayEngineCallIfNonZeroDuration {
890892
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
891893
nibName:nil
892894
bundle:nil];
895+
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
896+
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
893897
mockEngine.viewController = viewController;
894898

895899
// Mimic the device rotation with non-zero transition duration.
@@ -921,6 +925,8 @@ - (void)testViewWillTransitionToSize_DoesNotDelayEngineCallIfZeroDuration {
921925
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
922926
nibName:nil
923927
bundle:nil];
928+
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
929+
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
924930
mockEngine.viewController = viewController;
925931

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

0 commit comments

Comments
 (0)