forked from pointfreeco/swift-snapshot-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
143 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
Tests/SnapshotTestingTests/Snapshotting/WKWebViewTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import Foundation | ||
import XCTest | ||
|
||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
#if canImport(WebKit) | ||
import WebKit | ||
#endif | ||
|
||
#if canImport(UIKit) | ||
import UIKit.UIView | ||
#endif | ||
|
||
@testable import SnapshotTesting | ||
|
||
final class WKWebViewTests: XCTestCase { | ||
override func setUp() { | ||
super.setUp() | ||
diffTool = "ksdiff" | ||
// isRecording = true | ||
} | ||
|
||
override func tearDown() { | ||
isRecording = false | ||
super.tearDown() | ||
} | ||
|
||
#if os(iOS) || os(macOS) | ||
func testWebView() throws { | ||
let webView = WKWebView() | ||
webView.load(.init(url: .htmlFixture)) | ||
assertSnapshot( | ||
of: webView, | ||
as: .image(precision: 0.99, perceptualPrecision: 0.99, size: .init(width: 800, height: 600)), | ||
named: platform | ||
) | ||
} | ||
|
||
func testWebViewWithManipulatingNavigationDelegate() throws { | ||
final class ManipulatingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate { | ||
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | ||
webView.evaluateJavaScript("document.body.children[0].classList.remove(\"hero\")") // Change layout | ||
} | ||
} | ||
let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate() | ||
let webView = WKWebView() | ||
webView.navigationDelegate = manipulatingWKWebViewNavigationDelegate | ||
webView.load(.init(url: .htmlFixture)) | ||
assertSnapshot( | ||
of: webView, | ||
as: .image(precision: 0.99, perceptualPrecision: 0.99, size: .init(width: 800, height: 600)), | ||
named: platform | ||
) | ||
_ = manipulatingWKWebViewNavigationDelegate | ||
} | ||
|
||
func testWebViewWithCancellingNavigationDelegate() throws { | ||
final class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate { | ||
func webView( | ||
_ webView: WKWebView, | ||
decidePolicyFor navigationAction: WKNavigationAction, | ||
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void | ||
) { | ||
decisionHandler(.cancel) | ||
} | ||
} | ||
let cancellingWKWebViewNavigationDelegate = CancellingWKWebViewNavigationDelegate() | ||
let webView = WKWebView() | ||
webView.navigationDelegate = cancellingWKWebViewNavigationDelegate | ||
webView.load(.init(url: .htmlFixture)) | ||
assertSnapshot( | ||
of: webView, | ||
as: .image(size: .init(width: 800, height: 600)), | ||
named: platform | ||
) | ||
_ = cancellingWKWebViewNavigationDelegate | ||
} | ||
#endif | ||
|
||
#if os(iOS) | ||
func testEmbeddedWebView() throws { | ||
let label = UILabel() | ||
label.text = "Hello, Blob!" | ||
|
||
let webView = WKWebView() | ||
webView.load(.init(url: .htmlFixture)) | ||
webView.isHidden = true | ||
|
||
let stackView = UIStackView(arrangedSubviews: [label, webView]) | ||
stackView.axis = .vertical | ||
|
||
assertSnapshot( | ||
of: stackView, | ||
as: .image(precision: 0.99, perceptualPrecision: 0.99, size: .init(width: 800, height: 600)), | ||
named: platform | ||
) | ||
} | ||
#endif | ||
} | ||
|
||
private extension URL { | ||
static var htmlFixture: URL { | ||
URL(fileURLWithPath: String(#file), isDirectory: false) | ||
.deletingLastPathComponent() | ||
.deletingLastPathComponent() | ||
.appendingPathComponent("__Fixtures__/pointfree.html") | ||
} | ||
} |
Binary file renamed
BIN
+77.8 KB
...tTestingTests/testEmbeddedWebView.ios.png → ...KWebViewTests/testEmbeddedWebView.ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+123 KB
...hotTestingTests/Snapshotting/__Snapshots__/WKWebViewTests/testWebView.macos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file renamed
BIN
+78.1 KB
...wWithCancellingNavigationDelegate.ios.png → ...wWithCancellingNavigationDelegate.ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added
BIN
+123 KB
...pshots__/WKWebViewTests/testWebViewWithManipulatingNavigationDelegate.macos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed
BIN
-217 KB
Tests/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testWebView.ios.png
Binary file not shown.
Binary file removed
BIN
-123 KB
...s/SnapshotTestingTests/__Snapshots__/SnapshotTestingTests/testWebView.macos.png
Binary file not shown.
Binary file removed
BIN
-205 KB
...ts__/SnapshotTestingTests/testWebViewWithManipulatingNavigationDelegate.ios.png
Binary file not shown.
Binary file removed
BIN
-123 KB
...__/SnapshotTestingTests/testWebViewWithManipulatingNavigationDelegate.macos.png
Binary file not shown.