diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index da09c2d8ac..5666f336af 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -680,35 +680,39 @@ static void sendFakeTouchEvent(FlutterEngine* engine, - (void)viewWillAppear:(BOOL)animated { TRACE_EVENT0("flutter", "viewWillAppear"); + if ([_engine.get() viewController] == self) { + // Send platform settings to Flutter, e.g., platform brightness. + [self onUserSettingsChanged:nil]; - // Send platform settings to Flutter, e.g., platform brightness. - [self onUserSettingsChanged:nil]; - - // Only recreate surface on subsequent appearances when viewport metrics are known. - // First time surface creation is done on viewDidLayoutSubviews. - if (_viewportMetrics.physical_width) { - [self surfaceUpdated:YES]; + // Only recreate surface on subsequent appearances when viewport metrics are known. + // First time surface creation is done on viewDidLayoutSubviews. + if (_viewportMetrics.physical_width) { + [self surfaceUpdated:YES]; + } + [[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.inactive"]; + [[_engine.get() restorationPlugin] markRestorationComplete]; } - [[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.inactive"]; - [[_engine.get() restorationPlugin] markRestorationComplete]; [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { TRACE_EVENT0("flutter", "viewDidAppear"); - [self onUserSettingsChanged:nil]; - [self onAccessibilityStatusChanged:nil]; - if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) { - [[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.resumed"]; + if ([_engine.get() viewController] == self) { + [self onUserSettingsChanged:nil]; + [self onAccessibilityStatusChanged:nil]; + if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) { + [[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.resumed"]; + } } [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { TRACE_EVENT0("flutter", "viewWillDisappear"); - [[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.inactive"]; - + if ([_engine.get() viewController] == self) { + [[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.inactive"]; + } [super viewWillDisappear:animated]; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index ecc522e292..0ee9487cba 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -121,6 +121,7 @@ typedef enum UIAccessibilityContrast : NSInteger { nextAction:(void (^)())next API_AVAILABLE(ios(13.4)); - (void)scrollEvent:(UIPanGestureRecognizer*)recognizer; - (void)updateViewportMetrics; +- (void)onUserSettingsChanged:(NSNotification*)notification; @end @interface FlutterViewControllerTest : XCTestCase @@ -183,6 +184,91 @@ typedef enum UIAccessibilityContrast : NSInteger { 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)testUpdateViewportMetricsDoesntInvokeEngineWhenNotTheViewController { FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]); [mockEngine createShell:@"" libraryURI:@"" initialRoute:nil]; @@ -277,9 +363,10 @@ typedef enum UIAccessibilityContrast : NSInteger { - (void)testItReportsPlatformBrightnessWhenViewWillAppear { // Setup test. id settingsChannel = OCMClassMock([FlutterBasicMessageChannel class]); - OCMStub([self.mockEngine settingsChannel]).andReturn(settingsChannel); - - FlutterViewController* vc = [[FlutterViewController alloc] initWithEngine:self.mockEngine + FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]); + [mockEngine createShell:@"" libraryURI:@"" initialRoute:nil]; + OCMStub([mockEngine settingsChannel]).andReturn(settingsChannel); + FlutterViewController* vc = [[FlutterViewController alloc] initWithEngine:mockEngine nibName:nil bundle:nil]; @@ -376,12 +463,13 @@ typedef enum UIAccessibilityContrast : NSInteger { } else { return; } + FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]); + [mockEngine createShell:@"" libraryURI:@"" initialRoute:nil]; // Setup test. id settingsChannel = OCMClassMock([FlutterBasicMessageChannel class]); - OCMStub([self.mockEngine settingsChannel]).andReturn(settingsChannel); - - FlutterViewController* vc = [[FlutterViewController alloc] initWithEngine:self.mockEngine + OCMStub([mockEngine settingsChannel]).andReturn(settingsChannel); + FlutterViewController* vc = [[FlutterViewController alloc] initWithEngine:mockEngine nibName:nil bundle:nil];