@ondeinference / xcrs-controlkit Mirror · read-only
Fork 0

Read-only mirror of github.com/ondeinference/xcrs-controlkit.

Mirrored from https://github.com/ondeinference/xcrs-controlkit.git

Synced Nov 24, 19:30 UTC

1 import XCTest
2 @testable import XCRSControlKit
3
4 final class XCRSControlKitTests: XCTestCase {
5 func testStandardCapabilitiesArePlatformSpecific() {
6 let iOSCapabilities = XCRSControlKitCapabilities.standard(for: .iOS)
7 let tvOSCapabilities = XCRSControlKitCapabilities.standard(for: .tvOS)
8
9 XCTAssertTrue(iOSCapabilities.supports(.touch))
10 XCTAssertFalse(iOSCapabilities.supports(.remoteButtons))
11 XCTAssertTrue(tvOSCapabilities.supports(.remoteButtons))
12 XCTAssertFalse(tvOSCapabilities.supports(.touch))
13 }
14
15 func testCapabilitiesAreCodable() throws {
16 let capabilities = XCRSControlKitCapabilities.standard(for: .macOS)
17 let encoded = try JSONEncoder().encode(capabilities)
18 let decoded = try JSONDecoder().decode(
19 XCRSControlKitCapabilities.self,
20 from: encoded
21 )
22
23 XCTAssertEqual(decoded, capabilities)
24 }
25
26 func testRequestHasStableTransportShape() throws {
27 let request = XCRSControlKitRequest(
28 id: "request-1",
29 method: "device.io.button",
30 parameters: ["button": "select"]
31 )
32
33 let object = try JSONSerialization.jsonObject(
34 with: JSONEncoder().encode(request)
35 ) as? [String: Any]
36
37 XCTAssertEqual(object?["id"] as? String, "request-1")
38 XCTAssertEqual(object?["method"] as? String, "device.io.button")
39 XCTAssertEqual(
40 (object?["parameters"] as? [String: String])?["button"],
41 "select"
42 )
43 }
44
45 func testAccessibilityNodeHasStableTransportShape() throws {
46 let node = XCRSControlKitAccessibilityNode(
47 type: "Button",
48 label: "Play",
49 name: "play-button",
50 rawIdentifier: "play-button",
51 rect: XCRSControlKitRect(x: 100, y: 200, width: 80, height: 40),
52 enabled: true,
53 selected: false,
54 focused: false,
55 hittable: true
56 )
57
58 let object = try JSONSerialization.jsonObject(
59 with: JSONEncoder().encode(node)
60 ) as? [String: Any]
61 let rect = object?["rect"] as? [String: Any]
62
63 XCTAssertEqual(object?["type"] as? String, "Button")
64 XCTAssertEqual(object?["label"] as? String, "Play")
65 XCTAssertEqual(object?["rawIdentifier"] as? String, "play-button")
66 XCTAssertEqual(rect?["x"] as? Double, 100)
67 XCTAssertEqual(rect?["width"] as? Double, 80)
68 XCTAssertEqual((object?["children"] as? [[String: Any]])?.count, 0)
69 }
70
71 func testAccessibilityParserReadsSingleXCTestSnapshot() throws {
72 let hierarchy = """
73 Attributes: Application, 0x1, pid: 42, label: 'Sample App'
74 Element subtree:
75 →Application, 0x1, pid: 42, label: 'Sample App'
76 Window (Main), 0x2, {{0.0, 0.0}, {1920.0, 1080.0}}
77 Button, 0x3, {{576.0, 64.0}, {63.0, 32.0}}, identifier: 'Start', label: 'Hem', Focused
78 Path to element:
79 →Application, 0x1, pid: 42, label: 'Sample App'
80 """
81
82 let root = try XCRSControlKitAccessibilityParser.parse(
83 debugDescription: hierarchy
84 )
85
86 XCTAssertEqual(root.type, "Application")
87 XCTAssertEqual(root.label, "Sample App")
88 XCTAssertEqual(root.children.count, 1)
89 let window = try XCTUnwrap(root.children.first)
90 XCTAssertEqual(window.type, "Window (Main)")
91 let button = try XCTUnwrap(window.children.first)
92 XCTAssertEqual(button.type, "Button")
93 XCTAssertEqual(button.label, "Hem")
94 XCTAssertEqual(button.rawIdentifier, "Start")
95 XCTAssertEqual(button.rect.x, 576)
96 XCTAssertEqual(root.depth, 0)
97 XCTAssertEqual(window.depth, 1)
98 XCTAssertEqual(button.depth, 2)
99 XCTAssertEqual(button.selected, false)
100 XCTAssertEqual(button.focused, true)
101 }
102
103 func testAccessibilityParserReadsQuotedAttributesAndStates() throws {
104 let hierarchy = """
105 Element subtree:
106 →Application, 0x1, label: 'Disabled, Selected, Focused'
107 Button, 0x2, label: 'O'Brien', Disabled, Selected, Focused
108 Path to element:
109 """
110
111 let root = try XCRSControlKitAccessibilityParser.parse(
112 debugDescription: hierarchy
113 )
114
115 XCTAssertEqual(root.label, "Disabled, Selected, Focused")
116 let button = try XCTUnwrap(root.children.first)
117 XCTAssertEqual(button.label, "O'Brien")
118 XCTAssertEqual(button.enabled, false)
119 XCTAssertEqual(button.selected, true)
120 XCTAssertEqual(button.focused, true)
121 XCTAssertEqual(root.enabled, true)
122 }
123
124 func testAccessibilityParserKeepsSiblingsAtTheSameIndentation() throws {
125 let hierarchy = """
126 Element subtree:
127 →Application, 0x1
128 Button, 0x2, label: 'First'
129 Button, 0x3, label: 'Second'
130 Path to element:
131 """
132
133 let root = try XCRSControlKitAccessibilityParser.parse(
134 debugDescription: hierarchy
135 )
136
137 XCTAssertEqual(root.children.map(\.label), ["First", "Second"])
138 XCTAssertTrue(root.children.allSatisfy(\.children.isEmpty))
139 }
140
141 func testAccessibilityParserReadsEscapedAndBraceTerminatedAttributes() throws {
142 let hierarchy = """
143 Element subtree:
144 →Application, 0x1, label: 'Sample'}
145 Button, 0x2, label: 'It\\'s ready', Selected
146 Path to element:
147 """
148
149 let root = try XCRSControlKitAccessibilityParser.parse(
150 debugDescription: hierarchy
151 )
152
153 XCTAssertEqual(root.label, "Sample")
154 let button = try XCTUnwrap(root.children.first)
155 XCTAssertEqual(button.label, "It\\'s ready")
156 XCTAssertEqual(button.selected, true)
157 }
158
159 /// Lines as XCTest prints them for a WebKit (Tauri) window on macOS: controls
160 /// carry `title`, static text an unquoted trailing `value`.
161 func testAccessibilityParserReadsMacOSTitlesAndUnquotedValues() throws {
162 let hierarchy = """
163 Element subtree:
164 →Application, 0x1, pid: 52989, title: 'SplitFire AI', Disabled
165 Window (Main), 0x2, {{356.0, 107.0}, {800.0, 600.0}}, title: 'SplitFire AI', Disabled
166 StaticText, 0x3, {{440.0, 270.0}, {139.0, 17.0}}, value: No file selected
167 Button, 0x4, {{950.0, 256.0}, {122.0, 45.0}}, title: 'CHOOSE FILE'
168 Button, 0x5, {{420.0, 618.0}, {672.0, 61.0}}, title: 'SEPARATE', Disabled
169 StaticText, 0x6, {{1.0, 2.0}, {3.0, 4.0}}, value: 01 — SOURCE AUDIO, Disabled
170 Button, 0x7, {{1.0, 2.0}, {3.0, 4.0}}, identifier: 'close', label: 'Close', title: 'Ignored'
171 Path to element:
172 """
173
174 let root = try XCRSControlKitAccessibilityParser.parse(
175 debugDescription: hierarchy
176 )
177
178 XCTAssertEqual(root.label, "SplitFire AI")
179 let window = try XCTUnwrap(root.children.first)
180 XCTAssertEqual(window.label, "SplitFire AI")
181 XCTAssertEqual(window.enabled, false)
182 let children = window.children
183 XCTAssertEqual(children.count, 5)
184 XCTAssertEqual(children[0].value, "No file selected")
185 XCTAssertNil(children[0].label)
186 XCTAssertEqual(children[1].label, "CHOOSE FILE")
187 XCTAssertEqual(children[1].rect, XCRSControlKitRect(x: 950, y: 256, width: 122, height: 45))
188 XCTAssertEqual(children[2].label, "SEPARATE")
189 XCTAssertEqual(children[2].enabled, false)
190 XCTAssertEqual(children[3].value, "01 — SOURCE AUDIO")
191 XCTAssertEqual(children[3].enabled, false)
192 XCTAssertEqual(children[4].label, "Close", "an explicit label wins over title")
193 }
194
195 func testAccessibilityParserIgnoresValueSeparatorInsideQuotedTitle() throws {
196 let hierarchy = """
197 Element subtree:
198 →Application, 0x1, pid: 1
199 Window (Main), 0x2, {{0.0, 0.0}, {10.0, 10.0}}
200 StaticText, 0x3, {{0.0, 0.0}, {1.0, 1.0}}, title: 'Total, value: n/a'
201 StaticText, 0x4, {{0.0, 0.0}, {1.0, 1.0}}, title: 'Volume', value: 50%
202 Path to element:
203 """
204
205 let root = try XCRSControlKitAccessibilityParser.parse(
206 debugDescription: hierarchy
207 )
208 let window = try XCTUnwrap(root.children.first)
209 let children = window.children
210 XCTAssertEqual(children[0].label, "Total, value: n/a")
211 XCTAssertNil(children[0].value, "a `, value: ` inside a quoted title is not a value attribute")
212 XCTAssertEqual(children[1].label, "Volume")
213 XCTAssertEqual(children[1].value, "50%", "a real unquoted value outside quotes is still read")
214 }
215
216 func testAccessibilityParserRejectsMultipleRootElements() {
217 let hierarchy = """
218 Element subtree:
219 →Application, 0x1
220 →Window, 0x2
221 Path to element:
222 """
223
224 XCTAssertThrowsError(
225 try XCRSControlKitAccessibilityParser.parse(
226 debugDescription: hierarchy
227 )
228 ) { error in
229 XCTAssertEqual(
230 error as? XCRSControlKitError,
231 .invalidRequest("XCTest returned multiple root accessibility elements")
232 )
233 }
234 }
235 }