Skip to content

Commit

Permalink
Add dummy WebView to window for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaresiak committed Nov 14, 2024
1 parent 6cc733a commit 9efe782
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Core/DataStoreWarmup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public class DataStoreWarmup {

}

private class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {
public class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {

var finished: PassthroughSubject? = PassthroughSubject<Void, Never>()

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
return .allow
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if let finished {
finished.send()
self.finished = nil
Expand All @@ -59,7 +59,7 @@ private class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {
}
}

func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
public func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
Pixel.fire(pixel: .webKitDidTerminateDuringWarmup)

if let finished {
Expand All @@ -71,7 +71,7 @@ private class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {
}

var cancellable: AnyCancellable?
func waitForLoad() async {
public func waitForLoad() async {
await withCheckedContinuation { continuation in
cancellable = finished?.sink { _ in
continuation.resume()
Expand All @@ -80,10 +80,16 @@ private class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {
}

@MainActor
func loadInBackgroundWebView(url: URL) async {
public func prepareWebView() -> WKWebView {
let config = WKWebViewConfiguration.persistent()
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = self
return webView
}

@MainActor
public func loadInBackgroundWebView(url: URL) async {
let webView = prepareWebView()
let request = URLRequest(url: url)
webView.load(request)
await waitForLoad()
Expand Down
12 changes: 12 additions & 0 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,19 @@ import os.log
_ = DefaultUserAgentManager.shared
Database.shared.loadStore { _, _ in }
_ = BookmarksDatabaseSetup().loadStoreAndMigrate(bookmarksDatabase: bookmarksDatabase)

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIStoryboard.init(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()

let blockingDelegate = BlockingNavigationDelegate()
let webView = blockingDelegate.prepareWebView()
window?.rootViewController?.view.addSubview(webView)
window?.rootViewController?.view.backgroundColor = .red
webView.frame = CGRect(x: 10, y: 10, width: 300, height: 300)

let request = URLRequest(url: URL(string: "about:blank")!)
webView.load(request)

return true
}

Expand Down

0 comments on commit 9efe782

Please sign in to comment.