-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[webview_flutter_wkwebview] Tear down ProxyAPIRegistrar in applicationWillTerminate
#11567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
edf39b1
46de295
e291901
44ccc3a
da61175
b595f99
5dfba8d
c933821
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,85 @@ | ||||||||||||
| // Copyright 2013 The Flutter Authors | ||||||||||||
| // Use of this source code is governed by a BSD-style license that can be | ||||||||||||
| // found in the LICENSE file. | ||||||||||||
|
|
||||||||||||
| import XCTest | ||||||||||||
|
|
||||||||||||
| @testable import webview_flutter_wkwebview | ||||||||||||
|
|
||||||||||||
| #if os(iOS) | ||||||||||||
| import Flutter | ||||||||||||
| import UIKit | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| class WebViewFlutterPluginTests: XCTestCase { | ||||||||||||
| #if os(iOS) | ||||||||||||
| func testInstanceManagerIsDeallocatedInApplicationWillTerminate() { | ||||||||||||
| let plugin = WebViewFlutterPlugin(binaryMessenger: TestBinaryMessenger()) | ||||||||||||
| plugin.proxyApiRegistrar!.setUp() | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||
|
|
||||||||||||
| let view = UIView() | ||||||||||||
| _ = plugin.proxyApiRegistrar!.instanceManager.addHostCreatedInstance(view) | ||||||||||||
|
|
||||||||||||
| // Attaches an associated object to the InstanceManager to listen for when it is deallocated. | ||||||||||||
| var finalizer: TestFinalizer? = TestFinalizer() | ||||||||||||
|
|
||||||||||||
| let key = malloc(1)! | ||||||||||||
| defer { | ||||||||||||
| free(key) | ||||||||||||
| } | ||||||||||||
| objc_setAssociatedObject( | ||||||||||||
| plugin.proxyApiRegistrar!.instanceManager, key, finalizer, .OBJC_ASSOCIATION_RETAIN) | ||||||||||||
| let expectation = self.expectation(description: "Wait for InstanceManager to be deallocated.") | ||||||||||||
| TestFinalizer.onDeinit = { | ||||||||||||
| expectation.fulfill() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Ensure method is from `FlutterApplicationLifeCycleDelegate`. | ||||||||||||
| (plugin as FlutterApplicationLifeCycleDelegate).applicationWillTerminate!( | ||||||||||||
| UIApplication.shared) | ||||||||||||
| XCTAssertNil(plugin.proxyApiRegistrar) | ||||||||||||
|
|
||||||||||||
| finalizer = nil | ||||||||||||
| waitForExpectations(timeout: 5.0) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| func testInstanceManagerIsDeallocatedInSceneDidDisconnect() { | ||||||||||||
| let plugin = WebViewFlutterPlugin(binaryMessenger: TestBinaryMessenger()) | ||||||||||||
| plugin.proxyApiRegistrar!.setUp() | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||
|
|
||||||||||||
| let view = UIView() | ||||||||||||
| _ = plugin.proxyApiRegistrar!.instanceManager.addHostCreatedInstance(view) | ||||||||||||
|
|
||||||||||||
| // Attaches an associated object to the InstanceManager to listen for when it is deallocated. | ||||||||||||
| var finalizer: TestFinalizer? = TestFinalizer() | ||||||||||||
|
|
||||||||||||
| let key = malloc(1)! | ||||||||||||
| defer { | ||||||||||||
| free(key) | ||||||||||||
| } | ||||||||||||
| objc_setAssociatedObject( | ||||||||||||
| plugin.proxyApiRegistrar!.instanceManager, key, finalizer, .OBJC_ASSOCIATION_RETAIN) | ||||||||||||
| let expectation = self.expectation(description: "Wait for InstanceManager to be deallocated.") | ||||||||||||
| TestFinalizer.onDeinit = { | ||||||||||||
| expectation.fulfill() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| let scene = UIApplication.shared.connectedScenes.first! | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a forced unwrap on
Suggested change
|
||||||||||||
|
|
||||||||||||
| // Ensure method is from `FlutterSceneLifeCycleDelegate`. | ||||||||||||
| (plugin as FlutterSceneLifeCycleDelegate).sceneDidDisconnect?(scene) | ||||||||||||
| XCTAssertNil(plugin.proxyApiRegistrar) | ||||||||||||
|
|
||||||||||||
| finalizer = nil | ||||||||||||
| waitForExpectations(timeout: 5.0) | ||||||||||||
| } | ||||||||||||
| #endif | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| class TestFinalizer { | ||||||||||||
| static var onDeinit: (() -> Void)? | ||||||||||||
|
|
||||||||||||
| deinit { | ||||||||||||
| Self.onDeinit?() | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,13 +28,37 @@ public class WebViewFlutterPlugin: NSObject, FlutterPlugin { | |
| let plugin = WebViewFlutterPlugin(binaryMessenger: binaryMessenger) | ||
|
|
||
| let viewFactory = FlutterViewFactory(instanceManager: plugin.proxyApiRegistrar!.instanceManager) | ||
|
|
||
| #if os(iOS) | ||
| registrar.addApplicationDelegate(plugin) | ||
| registrar.addSceneDelegate(plugin) | ||
| #endif | ||
|
|
||
| registrar.register(viewFactory, withId: "plugins.flutter.io/webview") | ||
| registrar.publish(plugin) | ||
| } | ||
|
|
||
| public func detachFromEngine(for registrar: FlutterPluginRegistrar) { | ||
| proxyApiRegistrar!.ignoreCallsToDart = true | ||
| proxyApiRegistrar!.tearDown() | ||
| tearDownProxyAPIRegistrar() | ||
| } | ||
|
|
||
| private func tearDownProxyAPIRegistrar() { | ||
| proxyApiRegistrar?.ignoreCallsToDart = true | ||
| proxyApiRegistrar?.tearDown() | ||
| try? proxyApiRegistrar?.instanceManager.removeAllObjects() | ||
| proxyApiRegistrar = nil | ||
| } | ||
| } | ||
|
|
||
| #if os(iOS) | ||
| extension WebViewFlutterPlugin: FlutterApplicationLifeCycleDelegate, FlutterSceneLifeCycleDelegate | ||
| { | ||
| public func applicationWillTerminate(_ application: UIApplication) { | ||
| tearDownProxyAPIRegistrar() | ||
| } | ||
|
|
||
| public func sceneDidDisconnect(_ scene: UIScene) { | ||
| tearDownProxyAPIRegistrar() | ||
| } | ||
|
Comment on lines
+60
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tearing down the |
||
| } | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,8 @@ | |
| 8F1488FE2D2DE27000191744 /* HTTPCookieProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F1488C82D2DE27000191744 /* HTTPCookieProxyAPITests.swift */; }; | ||
| 8F1488FF2D2DE27000191744 /* NavigationActionProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F1488CB2D2DE27000191744 /* NavigationActionProxyAPITests.swift */; }; | ||
| 8F1489012D2DE91C00191744 /* AuthenticationChallengeResponseProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F1489002D2DE91C00191744 /* AuthenticationChallengeResponseProxyAPITests.swift */; }; | ||
| 8F63D06B2F8812E400EC5076 /* WebViewFlutterPluginTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F63D06A2F8812E400EC5076 /* WebViewFlutterPluginTests.swift */; }; | ||
| 8F63D06C2F8812E400EC5076 /* PlatformViewImplTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F63D0692F8812E400EC5076 /* PlatformViewImplTests.swift */; }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 8FEC64852DA2C6DC00C48569 /* GetTrustResultResponseProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FEC64812DA2C6DC00C48569 /* GetTrustResultResponseProxyAPITests.swift */; }; | ||
| 8FEC64862DA2C6DC00C48569 /* WebpagePreferencesProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FEC64842DA2C6DC00C48569 /* WebpagePreferencesProxyAPITests.swift */; }; | ||
| 8FEC64872DA2C6DC00C48569 /* SecCertificateProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FEC64822DA2C6DC00C48569 /* SecCertificateProxyAPITests.swift */; }; | ||
|
|
@@ -128,6 +130,8 @@ | |
| 8F1488E02D2DE27000191744 /* WebViewConfigurationProxyAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WebViewConfigurationProxyAPITests.swift; path = ../../darwin/Tests/WebViewConfigurationProxyAPITests.swift; sourceTree = SOURCE_ROOT; }; | ||
| 8F1488E12D2DE27000191744 /* WebViewProxyAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WebViewProxyAPITests.swift; path = ../../darwin/Tests/WebViewProxyAPITests.swift; sourceTree = SOURCE_ROOT; }; | ||
| 8F1489002D2DE91C00191744 /* AuthenticationChallengeResponseProxyAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AuthenticationChallengeResponseProxyAPITests.swift; path = ../../darwin/Tests/AuthenticationChallengeResponseProxyAPITests.swift; sourceTree = SOURCE_ROOT; }; | ||
| 8F63D0692F8812E400EC5076 /* PlatformViewImplTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = PlatformViewImplTests.swift; path = ../../darwin/Tests/PlatformViewImplTests.swift; sourceTree = SOURCE_ROOT; }; | ||
| 8F63D06A2F8812E400EC5076 /* WebViewFlutterPluginTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WebViewFlutterPluginTests.swift; path = ../../darwin/Tests/WebViewFlutterPluginTests.swift; sourceTree = SOURCE_ROOT; }; | ||
| 8FEC64812DA2C6DC00C48569 /* GetTrustResultResponseProxyAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = GetTrustResultResponseProxyAPITests.swift; path = ../../darwin/Tests/GetTrustResultResponseProxyAPITests.swift; sourceTree = SOURCE_ROOT; }; | ||
| 8FEC64822DA2C6DC00C48569 /* SecCertificateProxyAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SecCertificateProxyAPITests.swift; path = ../../darwin/Tests/SecCertificateProxyAPITests.swift; sourceTree = SOURCE_ROOT; }; | ||
| 8FEC64832DA2C6DC00C48569 /* SecTrustProxyAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SecTrustProxyAPITests.swift; path = ../../darwin/Tests/SecTrustProxyAPITests.swift; sourceTree = SOURCE_ROOT; }; | ||
|
|
@@ -174,6 +178,8 @@ | |
| 68BDCAEA23C3F7CB00D9C032 /* RunnerTests */ = { | ||
| isa = PBXGroup; | ||
| children = ( | ||
| 8F63D0692F8812E400EC5076 /* PlatformViewImplTests.swift */, | ||
| 8F63D06A2F8812E400EC5076 /* WebViewFlutterPluginTests.swift */, | ||
| 8F0E23512EEB5D6B002AB342 /* ColorProxyAPITests.swift */, | ||
| 8F0EDFD22E1F4967001938E6 /* ProxyAPIRegistrarTests.swift */, | ||
| 8FEC64812DA2C6DC00C48569 /* GetTrustResultResponseProxyAPITests.swift */, | ||
|
|
@@ -498,6 +504,8 @@ | |
| 8F1488F42D2DE27000191744 /* URLCredentialProxyAPITests.swift in Sources */, | ||
| 8F1488F52D2DE27000191744 /* URLAuthenticationChallengeProxyAPITests.swift in Sources */, | ||
| 8F1488F62D2DE27000191744 /* NavigationDelegateProxyAPITests.swift in Sources */, | ||
| 8F63D06B2F8812E400EC5076 /* WebViewFlutterPluginTests.swift in Sources */, | ||
| 8F63D06C2F8812E400EC5076 /* PlatformViewImplTests.swift in Sources */, | ||
| 8F1488F72D2DE27000191744 /* UIDelegateProxyAPITests.swift in Sources */, | ||
| 8F1488F82D2DE27000191744 /* ScrollViewDelegateProxyAPITests.swift in Sources */, | ||
| 8F1488FA2D2DE27000191744 /* FWFWebViewFlutterWKWebViewExternalAPITests.swift in Sources */, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed that this is being added to the iOS project but not the macOS project, even though it sounds generic. Someone will almost certainly add tests here in the future thinking they will run on macOS, so the file should be added to both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and added all the tests files not added on macOS.