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

Commit ba8828f

Browse files
committed
use mainScreenIfViewLoaded
1 parent 09e4dc1 commit ba8828f

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
@@ -1318,12 +1318,12 @@ - (void)viewSafeAreaInsetsDidChange {
13181318

13191319
// Set _viewportMetrics physical size.
13201320
- (void)setViewportMetricsSize {
1321-
// TODO(hellohuanlin): Use [self mainScreenIfViewLoaded] instead of [UIScreen mainScreen].
1322-
// This requires adding the view to window during unit tests, which calls multiple engine calls
1323-
// that is hard to mock since they take/return structs. An alternative approach is to partial mock
1324-
// the FlutterViewController to make view controller life cycle methods no-op, and insert
1325-
// this mock into the responder chain.
1326-
CGFloat scale = [UIScreen mainScreen].scale;
1321+
UIScreen* mainScreen = [self mainScreenIfViewLoaded];
1322+
if (!mainScreen) {
1323+
return;
1324+
}
1325+
1326+
CGFloat scale = mainScreen.scale;
13271327
_viewportMetrics.physical_width = self.view.bounds.size.width * scale;
13281328
_viewportMetrics.physical_height = self.view.bounds.size.height * scale;
13291329
}
@@ -1332,7 +1332,12 @@ - (void)setViewportMetricsSize {
13321332
//
13331333
// Viewport paddings represent the iOS safe area insets.
13341334
- (void)setViewportMetricsPaddings {
1335-
CGFloat scale = [UIScreen mainScreen].scale;
1335+
UIScreen* mainScreen = [self mainScreenIfViewLoaded];
1336+
if (!mainScreen) {
1337+
return;
1338+
}
1339+
1340+
CGFloat scale = mainScreen.scale;
13361341
_viewportMetrics.physical_padding_top = self.view.safeAreaInsets.top * scale;
13371342
_viewportMetrics.physical_padding_left = self.view.safeAreaInsets.left * scale;
13381343
_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
@@ -869,6 +869,8 @@ - (void)testUpdateViewportMetricsIfNeeded_DoesNotInvokeEngineWhenShouldBeIgnored
869869
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
870870
nibName:nil
871871
bundle:nil];
872+
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
873+
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
872874
mockEngine.viewController = viewController;
873875

874876
id mockCoordinator = OCMProtocolMock(@protocol(UIViewControllerTransitionCoordinator));
@@ -888,6 +890,8 @@ - (void)testViewWillTransitionToSize_DoesDelayEngineCallIfNonZeroDuration {
888890
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
889891
nibName:nil
890892
bundle:nil];
893+
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
894+
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
891895
mockEngine.viewController = viewController;
892896

893897
// Mimic the device rotation with non-zero transition duration.
@@ -919,6 +923,8 @@ - (void)testViewWillTransitionToSize_DoesNotDelayEngineCallIfZeroDuration {
919923
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
920924
nibName:nil
921925
bundle:nil];
926+
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
927+
OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen);
922928
mockEngine.viewController = viewController;
923929

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

0 commit comments

Comments
 (0)