Skip to content

Commit

Permalink
Merge pull request #25 from cybozu/add-load-html-string
Browse files Browse the repository at this point in the history
Add loadHTMLString() method to WebViewProxy.
  • Loading branch information
Kyome22 authored Oct 17, 2024
2 parents 0b60108 + 9fb06e4 commit 10a6ca0
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
183D25982B566967001FDBDB /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 183D25972B566967001FDBDB /* Preview Assets.xcassets */; };
183D25A12B5669ED001FDBDB /* WebUI in Frameworks */ = {isa = PBXBuildFile; productRef = 183D25A02B5669ED001FDBDB /* WebUI */; };
183D25A32B566AE8001FDBDB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 183D25A22B566AE8001FDBDB /* AppDelegate.swift */; };
1852BC8F2CBFB72600133987 /* sample.html in Resources */ = {isa = PBXBuildFile; fileRef = 1852BC8E2CBFB72600133987 /* sample.html */; };
1899E0F72BC7AE8E00BDCFD4 /* ExamplesUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1899E0F62BC7AE8E00BDCFD4 /* ExamplesUITests.swift */; };
18C686122B6B3DB600D45A40 /* ContentViewState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18C686112B6B3DB600D45A40 /* ContentViewState.swift */; };
/* End PBXBuildFile section */
Expand All @@ -37,6 +38,7 @@
183D259E2B56699B001FDBDB /* WebUI */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = WebUI; path = ..; sourceTree = "<group>"; };
183D25A22B566AE8001FDBDB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
183D25A42B566BDB001FDBDB /* Examples.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = Examples.xctestplan; sourceTree = "<group>"; };
1852BC8E2CBFB72600133987 /* sample.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = sample.html; sourceTree = "<group>"; };
1899E0F42BC7AE8E00BDCFD4 /* ExamplesUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ExamplesUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
1899E0F62BC7AE8E00BDCFD4 /* ExamplesUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExamplesUITests.swift; sourceTree = "<group>"; };
18C686112B6B3DB600D45A40 /* ContentViewState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentViewState.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -92,6 +94,7 @@
183D25932B566967001FDBDB /* Assets.xcassets */,
183D25952B566967001FDBDB /* Examples.entitlements */,
183D25962B566967001FDBDB /* Preview Content */,
1852BC8E2CBFB72600133987 /* sample.html */,
);
path = Examples;
sourceTree = "<group>";
Expand Down Expand Up @@ -205,6 +208,7 @@
files = (
183D25982B566967001FDBDB /* Preview Assets.xcassets in Resources */,
183D25942B566967001FDBDB /* Assets.xcassets in Resources */,
1852BC8F2CBFB72600133987 /* sample.html in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 6 additions & 0 deletions Examples/Examples/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ struct ContentView: View {
Label("Clear", systemImage: "clear")
.labelStyle(.iconOnly)
}
Button {
proxy.loadHTMLString(viewState.htmlString, baseURL: viewState.htmlURL)
} label: {
Label("Load HTML String", systemImage: "doc")
.labelStyle(.iconOnly)
}
}
.padding(.vertical, 8)

Expand Down
3 changes: 3 additions & 0 deletions Examples/Examples/ContentViewState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ final class ContentViewState: NSObject, ObservableObject {

let configuration = WKWebViewConfiguration()
let request = URLRequest(url: URL(string: "https://cybozu.github.io/webview-debugger")!)
let htmlURL = Bundle.main.url(forResource: "sample", withExtension: "html")!
let htmlString: String

override init() {
htmlString = try! String(contentsOf: htmlURL, encoding: .utf8)
super.init()
setCookie(name: "SampleKey", value: "SampleValue")
}
Expand Down
15 changes: 15 additions & 0 deletions Examples/Examples/sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div>
<h1>Sample</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mattis augue dui, ut vulputate diam pretium id.</p>
</div>
</body>
</html>
5 changes: 5 additions & 0 deletions Examples/ExamplesUITests/ExamplesUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ final class ExamplesUITests: XCTestCase {
XCTAssertTrue(app.webViews.staticTexts["0"].waitForExistence(timeout: 3))
}

XCTContext.runActivity(named: "WebViewProxy.loadHTMLString()") { _ in
app.buttons["Load HTML String"].tap()
XCTAssertTrue(app.webViews.staticTexts["Sample"].waitForExistence(timeout: 15))
}

XCTContext.runActivity(named: "WebViewProxy.clearAll()") { _ in
app.buttons["Clear"].tap()
XCTAssertFalse(app.buttons["Go Back"].isEnabled)
Expand Down
8 changes: 8 additions & 0 deletions Sources/WebUI/WebViewProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public final class WebViewProxy: ObservableObject {
webView?.wrappedValue.load(request)
}

/// Loads the contents of the specified HTML string and navigates to it.
/// - Parameters:
/// - string: The string to use as the contents of the webpage.
/// - baseURL: The base URL to use when the system resolves relative URLs within the HTML string.
public func loadHTMLString(_ string: String, baseURL: URL?) {
webView?.wrappedValue.loadHTMLString(string, baseURL: baseURL)
}

/// Reloads the current webpage.
public func reload() {
webView?.wrappedValue.reload()
Expand Down
8 changes: 8 additions & 0 deletions Tests/WebUITests/Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import WebKit

final class EnhancedWKWebViewMock: EnhancedWKWebView {
private(set) var loadedRequest: URLRequest?
private(set) var loadedHTMLString: String?
private(set) var loadedBaseURL: URL?
private(set) var reloadCalled = false
private(set) var goBackCalled = false
private(set) var goForwardCalled = false
Expand All @@ -13,6 +15,12 @@ final class EnhancedWKWebViewMock: EnhancedWKWebView {
return nil
}

override func loadHTMLString(_ string: String, baseURL: URL?) -> WKNavigation? {
loadedHTMLString = string
loadedBaseURL = baseURL
return nil
}

override func reload() -> WKNavigation? {
reloadCalled = true
return nil
Expand Down
12 changes: 12 additions & 0 deletions Tests/WebUITests/WebViewProxyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ final class WebViewProxyTests: XCTestCase {
XCTAssertEqual((webViewMock.wrappedValue as! EnhancedWKWebViewMock).loadedRequest, request)
}

@MainActor
func test_load_html_string() {
let sut = WebViewProxy()
let webViewMock = Remakeable {
EnhancedWKWebViewMock() as EnhancedWKWebView
}
sut.setUp(webViewMock)
sut.loadHTMLString("<dummy/>", baseURL: URL(string: "/dummy")!)
XCTAssertEqual((webViewMock.wrappedValue as! EnhancedWKWebViewMock).loadedHTMLString, "<dummy/>")
XCTAssertEqual((webViewMock.wrappedValue as! EnhancedWKWebViewMock).loadedBaseURL, URL(string: "/dummy")!)
}

@MainActor
func test_reload() {
let sut = WebViewProxy()
Expand Down

0 comments on commit 10a6ca0

Please sign in to comment.