[iOS] Don't call App lifecycle resumed when device is locked (flutter/engine#44947)

Fixed https://github.com/flutter/flutter/issues/130055.
We can cancel the `resumed` life cycle notification if we received `inactive` notification in the same runloop.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Wu Zhong
2023-08-29 14:59:05 +08:00
committed by GitHub
parent 1dd53f28b5
commit 5c3efc3707
2 changed files with 47 additions and 3 deletions

View File

@@ -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"];
}

View File

@@ -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