-
Notifications
You must be signed in to change notification settings - Fork 432
feat(paywalls): web_view schema component, not yet registered (4/7) #7230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+285
−0
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8d2452a
feat(paywalls): web_view schema component, not yet registered (4/7)
cursoragent 521820f
Merge branch 'main' into webview/4-schema
ajpallares f85c4b1
Merge branch 'main' of https://github.com/RevenueCat/purchases-ios in…
ajpallares 65a782e
Wire WebViewComponentTests into UnitTests target, fix .fit compile br…
ajpallares 792a1e9
Drop redundant encode(to:) and type guard from WebViewComponent
ajpallares ae82b3b
Port Android WebViewComponent decode tests for parity
ajpallares 0cf48a1
Require id and protocol_version on WebViewComponent
ajpallares 9e016df
Merge branch 'main' of https://github.com/RevenueCat/purchases-ios in…
ajpallares f1e89cd
Trim WebViewComponent comments
ajpallares 5a57be9
Rely on synthesized Codable for WebViewComponent
ajpallares 80d4c45
Merge remote-tracking branch 'purchases-ios/main' into webview/4-schema
ajpallares b9db0e3
Drop superfluous nesting swiftlint disable
ajpallares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,72 @@ | ||
| // | ||
| // Copyright RevenueCat Inc. All Rights Reserved. | ||
| // | ||
| // Licensed under the MIT License (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://opensource.org/licenses/MIT | ||
| // | ||
| // PaywallWebViewComponent.swift | ||
| // | ||
| // swiftlint:disable missing_docs | ||
|
|
||
| import Foundation | ||
|
|
||
| @_spi(Internal) public extension PaywallComponent { | ||
|
|
||
| final class WebViewComponent: PaywallComponentBase { | ||
|
|
||
| /// Wire `type` value, kept as a `String` because `web_view` is not yet a `ComponentType` case. | ||
| let type: String | ||
| public let id: String | ||
| public let name: String? | ||
| public let visible: Bool? | ||
|
|
||
| public let protocolVersion: Int | ||
|
|
||
| /// The static HTTPS URL of the web bundle entry point. | ||
| public let url: String | ||
|
|
||
| public let size: Size | ||
|
|
||
| public init( | ||
| id: String, | ||
| name: String? = nil, | ||
| visible: Bool? = nil, | ||
| protocolVersion: Int, | ||
| url: String, | ||
| size: Size = .init(width: .fill, height: .fit(nil)) | ||
| ) { | ||
| self.type = "web_view" | ||
| self.id = id | ||
| self.name = name | ||
| self.visible = visible | ||
| self.protocolVersion = protocolVersion | ||
| self.url = url | ||
| self.size = size | ||
| } | ||
|
|
||
| public func hash(into hasher: inout Hasher) { | ||
| hasher.combine(type) | ||
| hasher.combine(id) | ||
| hasher.combine(name) | ||
| hasher.combine(visible) | ||
| hasher.combine(protocolVersion) | ||
| hasher.combine(url) | ||
| hasher.combine(size) | ||
| } | ||
|
|
||
| public static func == (lhs: WebViewComponent, rhs: WebViewComponent) -> Bool { | ||
| return lhs.type == rhs.type && | ||
| lhs.id == rhs.id && | ||
| lhs.name == rhs.name && | ||
| lhs.visible == rhs.visible && | ||
| lhs.protocolVersion == rhs.protocolVersion && | ||
| lhs.url == rhs.url && | ||
| lhs.size == rhs.size | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
205 changes: 205 additions & 0 deletions
205
Tests/RevenueCatUITests/PaywallsV2/WebViewComponentTests.swift
This file contains hidden or 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,205 @@ | ||
| // | ||
| // Copyright RevenueCat Inc. All Rights Reserved. | ||
| // | ||
|
|
||
| @_spi(Internal) @testable import RevenueCat | ||
| import XCTest | ||
|
|
||
| #if !os(tvOS) | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| final class WebViewComponentTests: TestCase { | ||
|
|
||
| func testDecodesMinimalJSONAndIgnoresUnknownKeys() throws { | ||
| let minimal = try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: Data(""" | ||
| { | ||
| "type": "web_view", | ||
| "id": "web", | ||
| "protocol_version": 1, | ||
| "url": "https://example.com", | ||
| "size": { "width": { "type": "fill" }, "height": { "type": "fit" } }, | ||
| "unknown": true | ||
| } | ||
| """.utf8)) | ||
|
|
||
| XCTAssertEqual(minimal.id, "web") | ||
| XCTAssertNil(minimal.name) | ||
| XCTAssertNil(minimal.visible) | ||
| XCTAssertEqual(minimal.protocolVersion, 1) | ||
| XCTAssertEqual(minimal.size.width, .fill) | ||
| XCTAssertEqual(minimal.size.height, .fit(nil)) | ||
| XCTAssertEqual(minimal.url, "https://example.com") | ||
| XCTAssertEqual(minimal.type, "web_view") | ||
| } | ||
|
|
||
| func testDecodesExplicitFields() throws { | ||
| let component = try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: Data(""" | ||
| { | ||
| "type": "web_view", | ||
| "id": "web", | ||
| "name": "Survey", | ||
| "visible": false, | ||
| "protocol_version": 2, | ||
| "url": "https://example.com/index.html", | ||
| "size": { "width": { "type": "fixed", "value": 320 }, "height": { "type": "fit" } } | ||
| } | ||
| """.utf8)) | ||
|
|
||
| XCTAssertEqual(component.id, "web") | ||
| XCTAssertEqual(component.name, "Survey") | ||
| XCTAssertEqual(component.visible, false) | ||
| XCTAssertEqual(component.protocolVersion, 2) | ||
| XCTAssertEqual(component.url, "https://example.com/index.html") | ||
| } | ||
|
|
||
| func testIgnoresCapabilitiesDeclaredByTheSchema() throws { | ||
| // Isolation from external sources is expected from the server-provided CSP, so any | ||
| // schema-declared capabilities are decoded-and-ignored rather than failing to parse. | ||
| let component = try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: Data(""" | ||
| { | ||
| "type": "web_view", | ||
| "id": "web", | ||
| "protocol_version": 1, | ||
| "url": "https://example.com/index.html", | ||
| "size": { "width": { "type": "fill" }, "height": { "type": "fit" } }, | ||
| "capabilities": { | ||
| "network_access": { "allowed_domains": ["api.segment.io"] }, | ||
| "camera": true, | ||
| "microphone": true, | ||
| "clipboard_write": true, | ||
| "clipboard_read": true, | ||
| "geolocation": true | ||
| } | ||
| } | ||
| """.utf8)) | ||
|
|
||
| XCTAssertEqual( | ||
| component, | ||
| PaywallComponent.WebViewComponent(id: "web", protocolVersion: 1, url: "https://example.com/index.html") | ||
| ) | ||
| } | ||
|
|
||
| func testDecodesTemplateURLVerbatim() throws { | ||
| // Template placeholders in the URL are resolved elsewhere; decoding must preserve them as-is. | ||
| let component = try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: Data(""" | ||
| { | ||
| "type": "web_view", | ||
| "id": "web", | ||
| "protocol_version": 1, | ||
| "url": "https://example.com/{{ custom.animal }}.html", | ||
| "size": { "width": { "type": "fill" }, "height": { "type": "fit" } } | ||
| } | ||
| """.utf8)) | ||
|
|
||
| XCTAssertEqual(component.url, "https://example.com/{{ custom.animal }}.html") | ||
| XCTAssertEqual( | ||
| component, | ||
| PaywallComponent.WebViewComponent( | ||
| id: "web", | ||
| protocolVersion: 1, | ||
| url: "https://example.com/{{ custom.animal }}.html" | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| func testEncodeDecodeRoundTripUsesSnakeCaseWireKeys() throws { | ||
| let component = PaywallComponent.WebViewComponent( | ||
| id: "web", | ||
| name: "Survey", | ||
| visible: true, | ||
| protocolVersion: 2, | ||
| url: "https://example.com/index.html" | ||
| ) | ||
|
|
||
| let data = try JSONEncoder.default.encode(component) | ||
| let json = try XCTUnwrap(String(data: data, encoding: .utf8)) | ||
| XCTAssertTrue(json.contains("\"protocol_version\"")) | ||
| XCTAssertTrue(json.contains("\"web_view\"")) | ||
|
|
||
| let decoded = try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: data) | ||
| XCTAssertEqual(decoded, component) | ||
| } | ||
|
|
||
| func testDecodingWithoutURLThrows() { | ||
| XCTAssertThrowsError( | ||
| try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: Data(""" | ||
| { | ||
| "type": "web_view", | ||
| "id": "web", | ||
| "protocol_version": 1, | ||
| "size": { "width": { "type": "fill" }, "height": { "type": "fit" } } | ||
| } | ||
| """.utf8)) | ||
| ) | ||
| } | ||
|
|
||
| func testDecodingWithoutIDThrows() { | ||
| XCTAssertThrowsError( | ||
| try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: Data(""" | ||
| { | ||
| "type": "web_view", | ||
| "protocol_version": 1, | ||
| "url": "https://example.com", | ||
| "size": { "width": { "type": "fill" }, "height": { "type": "fit" } } | ||
| } | ||
| """.utf8)) | ||
| ) | ||
| } | ||
|
|
||
| func testDecodingWithoutProtocolVersionThrows() { | ||
| XCTAssertThrowsError( | ||
| try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: Data(""" | ||
| { | ||
| "type": "web_view", | ||
| "id": "web", | ||
| "url": "https://example.com", | ||
| "size": { "width": { "type": "fill" }, "height": { "type": "fit" } } | ||
| } | ||
| """.utf8)) | ||
| ) | ||
| } | ||
|
|
||
| func testDecodingWithoutSizeThrows() { | ||
| XCTAssertThrowsError( | ||
| try JSONDecoder.default.decode(PaywallComponent.WebViewComponent.self, from: Data(""" | ||
| { "type": "web_view", "id": "web", "protocol_version": 1, "url": "https://example.com" } | ||
| """.utf8)) | ||
| ) | ||
| } | ||
|
|
||
| func testPaywallComponentTreatsWebViewAsUnknownAndUsesFallback() throws { | ||
| let decoded = try JSONDecoder.default.decode(PaywallComponent.self, from: Data(""" | ||
| { | ||
| "type": "web_view", | ||
| "url": "https://example.com", | ||
| "fallback": \(Self.fallbackStackJSON) | ||
| } | ||
| """.utf8)) | ||
|
|
||
| guard case .stack = decoded else { | ||
| return XCTFail("web_view should be unknown to PaywallComponent and fall back to the stack") | ||
| } | ||
| } | ||
|
|
||
| func testPaywallComponentWebViewWithoutFallbackThrows() { | ||
| XCTAssertThrowsError( | ||
| try JSONDecoder.default.decode(PaywallComponent.self, from: Data(""" | ||
| { "type": "web_view", "url": "https://example.com" } | ||
| """.utf8)) | ||
| ) | ||
| } | ||
|
|
||
| private static let fallbackStackJSON = """ | ||
| { | ||
| "type": "stack", | ||
| "dimension": { "type": "vertical", "alignment": "center", "distribution": "start" }, | ||
| "size": { "width": { "type": "fill" }, "height": { "type": "fill" } }, | ||
| "padding": { "top": 0, "bottom": 0, "leading": 0, "trailing": 0 }, | ||
| "margin": { "top": 0, "bottom": 0, "leading": 0, "trailing": 0 }, | ||
| "components": [] | ||
| } | ||
| """ | ||
|
|
||
| } | ||
|
|
||
| #endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
sizeis a required field always sent by the backend (see https://github.com/RevenueCat/khepri/blob/main/khepri/services/paywalls/domain/template_schemas/v2/components/web_view.py#L33), I've removed in 5a57be9 the custominit(from decoder)that was only used to assign a default value to the size property