@@ -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+
529final 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