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 f3a422371d..f78ca85f79 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 @@ -1035,10 +1035,15 @@ static void SendFakeTouchEvent(UIScreen* screen, if (_viewportMetrics.physical_width) { [self surfaceUpdated:YES]; } - [self goToApplicationLifecycle:@"AppLifecycleState.resumed"]; + [self performSelector:@selector(goToApplicationLifecycle:) + withObject:@"AppLifecycleState.resumed" + afterDelay:0.0f]; } - (void)appOrSceneWillResignActive { + [NSObject cancelPreviousPerformRequestsWithTarget:self + selector:@selector(goToApplicationLifecycle:) + object:@"AppLifecycleState.resumed"]; [self goToApplicationLifecycle:@"AppLifecycleState.inactive"]; } 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 d642b2c4ce..e348978f51 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 @@ -1858,8 +1858,15 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; #endif XCTAssertFalse(flutterViewController.isKeyboardInOrTransitioningFromBackground); OCMVerify([mockVC surfaceUpdated:YES]); - OCMVerify([mockVC goToApplicationLifecycle:@"AppLifecycleState.resumed"]); - [flutterViewController deregisterNotifications]; + XCTestExpectation* timeoutApplicationLifeCycle = + [self expectationWithDescription:@"timeoutApplicationLifeCycle"]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), + dispatch_get_main_queue(), ^{ + [timeoutApplicationLifeCycle fulfill]; + OCMVerify([mockVC goToApplicationLifecycle:@"AppLifecycleState.resumed"]); + [flutterViewController deregisterNotifications]; + }); + [self waitForExpectationsWithTimeout:5.0 handler:nil]; } - (void)testLifeCycleNotificationWillResignActive { @@ -1975,4 +1982,36 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; [flutterViewController deregisterNotifications]; } +- (void)testLifeCycleNotificationCancelledInvalidResumed { + FlutterEngine* engine = [[FlutterEngine alloc] init]; + [engine runWithEntrypoint:nil]; + FlutterViewController* flutterViewController = + [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil]; + NSNotification* applicationDidBecomeActiveNotification = + [NSNotification notificationWithName:UIApplicationDidBecomeActiveNotification + object:nil + userInfo:nil]; + NSNotification* applicationWillResignActiveNotification = + [NSNotification notificationWithName:UIApplicationWillResignActiveNotification + object:nil + userInfo:nil]; + id mockVC = OCMPartialMock(flutterViewController); + [[NSNotificationCenter defaultCenter] postNotification:applicationDidBecomeActiveNotification]; + [[NSNotificationCenter defaultCenter] postNotification:applicationWillResignActiveNotification]; +#if APPLICATION_EXTENSION_API_ONLY +#else + OCMVerify([mockVC goToApplicationLifecycle:@"AppLifecycleState.inactive"]); +#endif + + XCTestExpectation* timeoutApplicationLifeCycle = + [self expectationWithDescription:@"timeoutApplicationLifeCycle"]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), + dispatch_get_main_queue(), ^{ + OCMReject([mockVC goToApplicationLifecycle:@"AppLifecycleState.resumed"]); + [timeoutApplicationLifeCycle fulfill]; + [flutterViewController deregisterNotifications]; + }); + [self waitForExpectationsWithTimeout:5.0 handler:nil]; +} + @end