Skip to content

Commit

Permalink
[iOS] Destroy the engine prior to application termination. (#29295)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongwuzw authored Nov 23, 2021
1 parent fc150a7 commit 09ce0bd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ - (void)setupNotificationCenterObservers {
name:UIApplicationWillResignActiveNotification
object:nil];

[center addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];

[center addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
Expand Down Expand Up @@ -816,6 +821,11 @@ - (void)applicationWillResignActive:(NSNotification*)notification {
[self goToApplicationLifecycle:@"AppLifecycleState.inactive"];
}

- (void)applicationWillTerminate:(NSNotification*)notification {
[self goToApplicationLifecycle:@"AppLifecycleState.detached"];
[self.engine destroyContext];
}

- (void)applicationDidEnterBackground:(NSNotification*)notification {
TRACE_EVENT0("flutter", "applicationDidEnterBackground");
[self surfaceUpdated:NO];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ - (void)handlePressEvent:(FlutterUIPressProxy*)press
- (void)scrollEvent:(UIPanGestureRecognizer*)recognizer;
- (void)updateViewportMetrics;
- (void)onUserSettingsChanged:(NSNotification*)notification;
- (void)applicationWillTerminate:(NSNotification*)notification;
- (void)goToApplicationLifecycle:(nonnull NSString*)state;
- (void)keyboardWillChangeFrame:(NSNotification*)notification;
- (void)startKeyBoardAnimation:(NSTimeInterval)duration;
- (void)ensureViewportMetricsIsCorrect;
Expand Down Expand Up @@ -214,6 +216,20 @@ - (void)testViewDidDisappearDoesntPauseEngineWhenNotTheViewController {
OCMReject([viewControllerMock surfaceUpdated:[OCMArg any]]);
}

- (void)testAppWillTerminateViewDidDestroyTheEngine {
FlutterEngine* mockEngine = OCMPartialMock([[FlutterEngine alloc] init]);
[mockEngine createShell:@"" libraryURI:@"" initialRoute:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:mockEngine
nibName:nil
bundle:nil];
id viewControllerMock = OCMPartialMock(viewController);
OCMStub([viewControllerMock goToApplicationLifecycle:@"AppLifecycleState.detached"]);
OCMStub([mockEngine destroyContext]);
[viewController applicationWillTerminate:nil];
OCMVerify([viewControllerMock goToApplicationLifecycle:@"AppLifecycleState.detached"]);
OCMVerify([mockEngine destroyContext]);
}

- (void)testViewDidDisappearDoesPauseEngineWhenIsTheViewController {
id lifecycleChannel = OCMClassMock([FlutterBasicMessageChannel class]);
FlutterEnginePartialMock* mockEngine = [[FlutterEnginePartialMock alloc] init];
Expand Down

0 comments on commit 09ce0bd

Please sign in to comment.