Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class NavigationDelegateProxyAPITests: XCTestCase {
let registrar = TestProxyApiRegistrar()
let instance = NavigationDelegateImpl(api: api, registrar: registrar)
let webView = WKWebView(frame: .zero)
let navigationResponse = TestNavigationResponse()
let navigationResponse = TestNavigationResponse.instance

var result: WKNavigationResponsePolicy?
let callbackExpectation = expectation(description: "Wait for callback.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NavigationResponseProxyAPITests: XCTestCase {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiWKNavigationResponse(registrar)

let instance = WKNavigationResponse()
let instance = TestNavigationResponse.instance
let value = try? api.pigeonDelegate.response(pigeonApi: api, pigeonInstance: instance)

XCTAssertEqual(value, instance.response)
Expand All @@ -22,16 +22,24 @@ class NavigationResponseProxyAPITests: XCTestCase {
let registrar = TestProxyApiRegistrar()
let api = registrar.apiDelegate.pigeonApiWKNavigationResponse(registrar)

let instance = TestNavigationResponse()
let instance = TestNavigationResponse.instance
let value = try? api.pigeonDelegate.isForMainFrame(pigeonApi: api, pigeonInstance: instance)

XCTAssertEqual(value, instance.isForMainFrame)
}
}

class TestNavigationResponse: WKNavigationResponse {
// Provides a static instance to prevent a crash when a WKNavigationResponse is deallocated
// See https://github.com/flutter/flutter/issues/173326
static let instance = TestNavigationResponse()

Comment on lines +35 to +36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fully enforce the singleton pattern and prevent accidental creation of new TestNavigationResponse instances elsewhere in the tests, it's a good practice to make the initializer private. This will prevent reintroducing the deallocation crash in the future.

  static let instance = TestNavigationResponse()

  private override init() {
    super.init()
  }

let testResponse = URLResponse()

private override init() {
super.init()
}

override var isForMainFrame: Bool {
return true
}
Expand Down