Skip to content

Commit

Permalink
Refactor FXIOS-8106 [v124] WKUIDelegate engine tests fix (mozilla-mob…
Browse files Browse the repository at this point in the history
  • Loading branch information
lmarceau authored Jan 25, 2024
1 parent 0797c67 commit efa6554
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import WebKit
@testable import WebEngine

class MockWKEngineWebView: UIView, WKEngineWebView {
var uiDelegate: WKUIDelegate?
var navigationDelegate: WKNavigationDelegate?
var engineConfiguration: WKEngineConfiguration
var interactionState: Any?
var scrollView = UIScrollView()
var url: URL?
var navigationDelegate: WKNavigationDelegate?
var allowsBackForwardNavigationGestures = true
var allowsLinkPreview = true
var isInspectable = true
Expand Down
34 changes: 32 additions & 2 deletions BrowserKit/Tests/WebEngineTests/WKEngineSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class WKEngineSessionTests: XCTestCase {
subject?.load(url: url)

XCTAssertEqual(webViewProvider.webView.loadCalled, 0)
prepareForTearDown(subject!)
}

func testLoadURLGivenNotAURLThenDoesntLoad() {
Expand All @@ -49,6 +50,7 @@ final class WKEngineSessionTests: XCTestCase {

// TODO: FXIOS-7981 Check scheme before loading
XCTAssertEqual(webViewProvider.webView.loadCalled, 1)
prepareForTearDown(subject!)
}

func testLoadURLGivenNormalURLThenLoad() {
Expand All @@ -59,6 +61,7 @@ final class WKEngineSessionTests: XCTestCase {

XCTAssertEqual(webViewProvider.webView.loadCalled, 1)
XCTAssertEqual(webViewProvider.webView.url?.absoluteString, url)
prepareForTearDown(subject!)
}

func testLoadURLGivenReaderModeURLThenLoad() {
Expand All @@ -70,6 +73,7 @@ final class WKEngineSessionTests: XCTestCase {
XCTAssertEqual(webViewProvider.webView.loadCalled, 1)
XCTAssertEqual(webViewProvider.webView.url?.absoluteString,
"http://localhost:0/reader-mode/page?url=http%3A%2F%2Fexample%2Ecom")
prepareForTearDown(subject!)
}

func testLoadURLGivenFileURLThenLoadFileURL() {
Expand All @@ -82,6 +86,7 @@ final class WKEngineSessionTests: XCTestCase {
XCTAssertEqual(webViewProvider.webView.loadFileURLCalled, 1)
XCTAssertEqual(webViewProvider.webView.url?.absoluteString, "file://path/to/abc/dirA/A.html")
XCTAssertEqual(webViewProvider.webView.loadFileReadAccessURL?.absoluteString, "file://path/to/abc/dirA/")
prepareForTearDown(subject!)
}

// MARK: Stop URL
Expand All @@ -92,6 +97,7 @@ final class WKEngineSessionTests: XCTestCase {
subject?.stopLoading()

XCTAssertEqual(webViewProvider.webView.stopLoadingCalled, 1)
prepareForTearDown(subject!)
}

// MARK: Go back
Expand All @@ -102,6 +108,7 @@ final class WKEngineSessionTests: XCTestCase {
subject?.goBack()

XCTAssertEqual(webViewProvider.webView.goBackCalled, 1)
prepareForTearDown(subject!)
}

// MARK: Go forward
Expand All @@ -112,6 +119,7 @@ final class WKEngineSessionTests: XCTestCase {
subject?.goForward()

XCTAssertEqual(webViewProvider.webView.goForwardCalled, 1)
prepareForTearDown(subject!)
}

// MARK: Reload
Expand All @@ -122,6 +130,7 @@ final class WKEngineSessionTests: XCTestCase {
subject?.reload()

XCTAssertEqual(webViewProvider.webView.reloadFromOriginCalled, 1)
prepareForTearDown(subject!)
}

func testReloadWhenErrorPageThenReplaceLocation() {
Expand All @@ -135,6 +144,7 @@ final class WKEngineSessionTests: XCTestCase {
XCTAssertEqual(webViewProvider.webView.reloadFromOriginCalled, 0)
XCTAssertEqual(webViewProvider.webView.replaceLocationCalled, 1)
XCTAssertEqual(webViewProvider.webView.url?.absoluteString, errorPageURL)
prepareForTearDown(subject!)
}

// MARK: Restore
Expand All @@ -147,6 +157,7 @@ final class WKEngineSessionTests: XCTestCase {

XCTAssertEqual(webViewProvider.webView.interactionState as! Data, restoredState)
XCTAssertEqual(webViewProvider.webView.loadCalled, 0)
prepareForTearDown(subject!)
}

func testRestoreWhenHasLastRequestThenLoadISCalled() {
Expand All @@ -158,13 +169,15 @@ final class WKEngineSessionTests: XCTestCase {

XCTAssertEqual(webViewProvider.webView.interactionState as! Data, restoredState)
XCTAssertEqual(webViewProvider.webView.loadCalled, 2, "Load calls it once, then restore calls it again")
prepareForTearDown(subject!)
}

// MARK: Observers

func testAddObserversWhenCreatedSubjectThenObserversAreAdded() {
_ = createSubject()
let subject = createSubject()
XCTAssertEqual(webViewProvider.webView.addObserverCalled, 7, "There are 7 KVO Constants")
prepareForTearDown(subject!)
}

func testRemoveObserversWhenCloseIsCalledThenObserversAreRemoved() {
Expand All @@ -173,6 +186,7 @@ final class WKEngineSessionTests: XCTestCase {
subject?.close()

XCTAssertEqual(webViewProvider.webView.removeObserverCalled, 7, "There are 7 KVO Constants")
prepareForTearDown(subject!)
}

func testCanGoBackGivenWebviewStateThenCallsNavigationStateChanged() {
Expand All @@ -189,6 +203,7 @@ final class WKEngineSessionTests: XCTestCase {
XCTAssertEqual(engineSessionDelegate.onNavigationStateChangeCalled, 1)
XCTAssertTrue(engineSessionDelegate.savedCanGoBack!)
XCTAssertFalse(engineSessionDelegate.savedCanGoForward!)
prepareForTearDown(subject!)
}

func testCanGoForwardGivenWebviewStateThenCallsNavigationStateChanged() {
Expand All @@ -205,6 +220,7 @@ final class WKEngineSessionTests: XCTestCase {
XCTAssertEqual(engineSessionDelegate.onNavigationStateChangeCalled, 1)
XCTAssertFalse(engineSessionDelegate.savedCanGoBack!)
XCTAssertTrue(engineSessionDelegate.savedCanGoForward!)
prepareForTearDown(subject!)
}

func testEstimatedProgressGivenWebviewStateThenCallsOnProgress() {
Expand All @@ -219,6 +235,7 @@ final class WKEngineSessionTests: XCTestCase {

XCTAssertEqual(engineSessionDelegate.onProgressCalled, 1)
XCTAssertEqual(engineSessionDelegate.savedProgressValue, 70)
prepareForTearDown(subject!)
}

func testLoadingGivenNoChangeThenDoesNotCallOnLoadingStateChange() {
Expand All @@ -231,6 +248,7 @@ final class WKEngineSessionTests: XCTestCase {
context: nil)

XCTAssertEqual(engineSessionDelegate.onLoadingStateChangeCalled, 0)
prepareForTearDown(subject!)
}

func testLoadingGivenOldKeyThenDoesNotCallOnLoadingStateChange() {
Expand All @@ -243,6 +261,7 @@ final class WKEngineSessionTests: XCTestCase {
context: nil)

XCTAssertEqual(engineSessionDelegate.onLoadingStateChangeCalled, 0)
prepareForTearDown(subject!)
}

func testLoadingGivenNewKeyThenCallsOnLoadingStateChange() {
Expand All @@ -256,13 +275,15 @@ final class WKEngineSessionTests: XCTestCase {

XCTAssertEqual(engineSessionDelegate.onLoadingStateChangeCalled, 1)
XCTAssertTrue(engineSessionDelegate.savedLoading!)
prepareForTearDown(subject!)
}

// MARK: User script manager

func testUserScriptWhenSubjectCreatedThenInjectionIntoWebviewCalled() {
_ = createSubject()
let subject = createSubject()
XCTAssertEqual(userScriptManager.injectUserScriptsIntoWebViewCalled, 1)
prepareForTearDown(subject!)
}

// MARK: Content script manager
Expand All @@ -285,7 +306,16 @@ final class WKEngineSessionTests: XCTestCase {
contentScriptManager: contentScriptManager) else {
return nil
}

trackForMemoryLeaks(subject, file: file, line: line)

return subject
}

// Adding a special teardown since as part of tracking memory leaks, we can't use an instance variable on the
// test suite. A normal instance `tearDown` is called after a `addTeardownBlock`. To ensure we still can
// `trackForMemoryLeaks` we need to always close any engine session that is opened, otherwise leaks happens.
func prepareForTearDown(_ subject: WKEngineSession) {
subject.close()
}
}

0 comments on commit efa6554

Please sign in to comment.