Skip to content

Commit dad86b7

Browse files
Tieme van VeenTieme van Veen
Tieme van Veen
authored and
Tieme van Veen
committed
Fix pointfreeco#440 - Replace WKWebView navigation delegate by observing wkWebView.isLoading
1 parent ab5a962 commit dad86b7

File tree

6 files changed

+26
-146
lines changed

6 files changed

+26
-146
lines changed

SnapshotTesting.xcodeproj/project.pbxproj

-8
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@
148148
BC1C30E837022BC96B8707B7 /* testUpdateSeveralSnapshotsSwapingLines1.1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AF3E4E28A1292D02E080C7 /* testUpdateSeveralSnapshotsSwapingLines1.1.swift */; };
149149
BC673F41050B566E6553814F /* testUpdateSnapshotWithLongerExtendedDelimiter1.1.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E18803C6CA9CC80FF55033 /* testUpdateSnapshotWithLongerExtendedDelimiter1.1.swift */; };
150150
BD42403C6DDE4A5B453DFA9F /* SnapshotTestingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA789F513A8B1E8EF5BF81E2 /* SnapshotTestingTests.swift */; };
151-
BDB6003F26028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDB6003E26028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift */; };
152-
BDB6004026028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDB6003E26028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift */; };
153-
BDB6004126028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDB6003E26028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift */; };
154151
BF239214620668C79B314F6F /* testUpdateSnapshotWithExtendedDelimiter2.1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CAC8CA2C073A9D03BC22880 /* testUpdateSnapshotWithExtendedDelimiter2.1.swift */; };
155152
BF4FD7664E50DA3FC34E91CF /* testUpdateSnapshotWithExtendedDelimiter1.1.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D7D0D81B7E35C6284A3E65 /* testUpdateSnapshotWithExtendedDelimiter1.1.swift */; };
156153
C0F19173230428932EDD1A78 /* testUpdateSeveralSnapshotsSwapingLines2.1.swift in Sources */ = {isa = PBXBuildFile; fileRef = D77BE996AE7868E9B58D6C74 /* testUpdateSeveralSnapshotsSwapingLines2.1.swift */; };
@@ -321,7 +318,6 @@
321318
ADACB597E6A3CAC5BFEEFB05 /* testCreateSnapshotWithShorterExtendedDelimiter2.1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = testCreateSnapshotWithShorterExtendedDelimiter2.1.swift; sourceTree = "<group>"; };
322319
B843712AB555B46BA6E4EC2A /* CaseIterable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CaseIterable.swift; sourceTree = "<group>"; };
323320
B8F1FEF91276E0D2BEFD9BC6 /* testCreateSnapshotEscapedNewlineLastLine.1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = testCreateSnapshotEscapedNewlineLastLine.1.swift; sourceTree = "<group>"; };
324-
BDB6003E26028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForwardingWKNavigationDelegate.swift; sourceTree = "<group>"; };
325321
C13AB875AB58FF3BCAC1AA66 /* testCreateSnapshotWithExtendedDelimiterSingleLine1.1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = testCreateSnapshotWithExtendedDelimiterSingleLine1.1.swift; sourceTree = "<group>"; };
326322
CB124D18325606D35B7027A6 /* NSViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSViewController.swift; sourceTree = "<group>"; };
327323
D1FE81FC04984B93B0C27FA6 /* testUpdateSnapshot.1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = testUpdateSnapshot.1.swift; sourceTree = "<group>"; };
@@ -498,7 +494,6 @@
498494
D49092873A7A5D6DA623E352 /* String+SpecialCharacters.swift */,
499495
0F6B5B3AB021FA3CD7519733 /* View.swift */,
500496
4BE8DE5E8A102E1012CD0C95 /* XCTAttachment.swift */,
501-
BDB6003E26028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift */,
502497
);
503498
path = Common;
504499
sourceTree = "<group>";
@@ -735,7 +730,6 @@
735730
42EEB5E183FA787232DCF756 /* UIBezierPath.swift in Sources */,
736731
2D9BA837D802B7347244178B /* UIImage.swift in Sources */,
737732
3CB93CF15AA49A2CB461D45E /* UIView.swift in Sources */,
738-
BDB6004026028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift in Sources */,
739733
1ABB68DCE39CE794B07296A7 /* UIViewController.swift in Sources */,
740734
30413A9E862B77B868A57B65 /* URLRequest.swift in Sources */,
741735
E66A02C861A316EDB4377C2E /* View.swift in Sources */,
@@ -776,7 +770,6 @@
776770
4CF683A5FA0EDF2C6ABC8B10 /* UIBezierPath.swift in Sources */,
777771
0C80A6C3FD6C62FDAC4123B3 /* UIImage.swift in Sources */,
778772
6630A021629D13CD012A62FD /* UIView.swift in Sources */,
779-
BDB6003F26028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift in Sources */,
780773
65ED4CF70D09F51DAD4C692A /* UIViewController.swift in Sources */,
781774
7B9F0DFC589C66887A37F0FB /* URLRequest.swift in Sources */,
782775
9727BFCBEE9C51BEFB754140 /* View.swift in Sources */,
@@ -889,7 +882,6 @@
889882
5E7F4795AFED0D8D4E1D75A6 /* UIBezierPath.swift in Sources */,
890883
B7C6F1A74C6D9DF70ACC52DB /* UIImage.swift in Sources */,
891884
436C10BC8313288D84733DC5 /* UIView.swift in Sources */,
892-
BDB6004126028DCC00DECEE8 /* ForwardingWKNavigationDelegate.swift in Sources */,
893885
969985906E267A3F203613F1 /* UIViewController.swift in Sources */,
894886
573488A1374FE9CD280A0BEE /* URLRequest.swift in Sources */,
895887
393181237190376E8A9D300E /* View.swift in Sources */,

Sources/SnapshotTesting/Common/ForwardingWKNavigationDelegate.swift

-134
This file was deleted.

Sources/SnapshotTesting/Common/View.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@ extension View {
604604
#if os(iOS) || os(macOS)
605605
if let wkWebView = self as? WKWebView {
606606
return Async<Image> { callback in
607-
let delegate = ForwardingWKNavigationDelegate(originalDelegate: wkWebView.navigationDelegate)
608607
let work = {
609608
if #available(iOS 11.0, macOS 10.13, *) {
610609
inWindow {
@@ -613,7 +612,6 @@ extension View {
613612
return
614613
}
615614
wkWebView.takeSnapshot(with: nil) { image, _ in
616-
_ = delegate
617615
callback(image!)
618616
}
619617
}
@@ -627,8 +625,14 @@ extension View {
627625
}
628626

629627
if wkWebView.isLoading {
630-
delegate.didFinish = work
631-
wkWebView.navigationDelegate = delegate
628+
var subscription: NSKeyValueObservation?
629+
subscription = wkWebView.observe(\.isLoading, options: [.initial, .new]) { (webview, change) in
630+
subscription?.invalidate()
631+
subscription = nil
632+
if change.newValue == false {
633+
work()
634+
}
635+
}
632636
} else {
633637
work()
634638
}

Tests/SnapshotTestingTests/SnapshotTestingTests.swift

+18
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,24 @@ final class SnapshotTestingTests: XCTestCase {
10501050
_ = manipulatingWKWebViewNavigationDelegate
10511051
}
10521052

1053+
#if os(iOS) || os(macOS)
1054+
func testWebViewWithRealUrl() throws {
1055+
let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate()
1056+
let webView = WKWebView()
1057+
webView.navigationDelegate = manipulatingWKWebViewNavigationDelegate
1058+
1059+
webView.load(URLRequest(url: URL(string: "https://www.pointfree.co")!))
1060+
if !ProcessInfo.processInfo.environment.keys.contains("GITHUB_WORKFLOW") {
1061+
assertSnapshot(
1062+
matching: webView,
1063+
as: .image(size: .init(width: 800, height: 600)),
1064+
named: platform
1065+
)
1066+
}
1067+
_ = manipulatingWKWebViewNavigationDelegate
1068+
}
1069+
#endif
1070+
10531071
final class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {
10541072
func webView(
10551073
_ webView: WKWebView,
Loading

0 commit comments

Comments
 (0)