Skip to content

Commit 5e4042d

Browse files
committed
Add basic AppKitBackend layout test, re-enable tests in CI
1 parent 71ed927 commit 5e4042d

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

Package.resolved

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"originHash" : "221fb04e97a64f8c2af1115148d6ced51af05f9f0f177402133694a73d9d2b94",
2+
"originHash" : "55a84247053982279804c20f6120f043f327ebac8c19e5ce749978c828726b2e",
33
"pins" : [
44
{
55
"identity" : "jpeg",

Package.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let package = Package(
6767
.library(name: "AppKitBackend", type: libraryType, targets: ["AppKitBackend"]),
6868
.library(name: "GtkBackend", type: libraryType, targets: ["GtkBackend"]),
6969
.library(name: "Gtk3Backend", type: libraryType, targets: ["Gtk3Backend"]),
70-
.library(name: "WinUIBackend", targets: ["WinUIBackend"]),
70+
.library(name: "WinUIBackend", type: libraryType, targets: ["WinUIBackend"]),
7171
.library(name: "DefaultBackend", type: libraryType, targets: ["DefaultBackend"]),
7272
.library(name: "UIKitBackend", type: libraryType, targets: ["UIKitBackend"]),
7373
.library(name: "Gtk", type: libraryType, targets: ["Gtk"]),
@@ -141,7 +141,10 @@ let package = Package(
141141
),
142142
.testTarget(
143143
name: "SwiftCrossUITests",
144-
dependencies: ["SwiftCrossUI"]
144+
dependencies: [
145+
"SwiftCrossUI",
146+
.target(name: "AppKitBackend", condition: .when(platforms: [.macOS])),
147+
]
145148
),
146149
.target(
147150
name: "DefaultBackend",

Tests/SwiftCrossUITests/SwiftCrossUITests.swift

+77
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@ import XCTest
22

33
@testable import SwiftCrossUI
44

5+
#if canImport(AppKitBackend)
6+
@testable import AppKitBackend
7+
#endif
8+
9+
struct CounterView: View {
10+
@State var count = 0
11+
12+
var body: some View {
13+
VStack {
14+
Button("Decrease") { count -= 1 }
15+
Text("Count: 1")
16+
Button("Increase") { count += 1 }
17+
}.padding()
18+
}
19+
}
20+
21+
struct XCTError: LocalizedError {
22+
var message: String
23+
24+
var errorDescription: String? {
25+
message
26+
}
27+
}
28+
529
final class SwiftCrossUITests: XCTestCase {
630
func testCodableNavigationPath() throws {
731
var path = NavigationPath()
@@ -39,4 +63,57 @@ final class SwiftCrossUITests: XCTestCase {
3963

4064
return original == decoded
4165
}
66+
67+
#if canImport(AppKitBackend)
68+
func testBasicLayout() throws {
69+
let backend = AppKitBackend()
70+
let window = backend.createWindow(withDefaultSize: SIMD2(200, 200))
71+
let environment = EnvironmentValues(backend: backend)
72+
.with(\.window, window)
73+
let viewGraph = ViewGraph(
74+
for: CounterView(),
75+
backend: backend,
76+
environment: environment
77+
)
78+
backend.setChild(ofWindow: window, to: viewGraph.rootNode.widget.into())
79+
80+
let result = viewGraph.update(
81+
proposedSize: SIMD2(200, 200),
82+
environment: environment,
83+
dryRun: false
84+
)
85+
let view: AppKitBackend.Widget = viewGraph.rootNode.widget.into()
86+
backend.setSize(of: view, to: result.size.size)
87+
backend.setSize(ofWindow: window, to: result.size.size)
88+
89+
XCTAssertEqual(
90+
result.size,
91+
ViewSize(fixedSize: SIMD2(94, 95)),
92+
"View update result mismatch"
93+
)
94+
95+
XCTAssert(
96+
result.preferences.onOpenURL == nil,
97+
"onOpenURL not nil"
98+
)
99+
100+
}
101+
102+
static func snapshotView(_ view: NSView) throws -> Data {
103+
view.wantsLayer = true
104+
view.layer?.backgroundColor = CGColor.white
105+
106+
guard let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) else {
107+
throw XCTError(message: "Failed to create bitmap backing")
108+
}
109+
110+
view.cacheDisplay(in: view.bounds, to: bitmap)
111+
112+
guard let data = bitmap.tiffRepresentation else {
113+
throw XCTError(message: "Failed to create tiff representation")
114+
}
115+
116+
return data
117+
}
118+
#endif
42119
}

0 commit comments

Comments
 (0)