Read-only mirror of github.com/ondeinference/xcrs-controlkit.
Mirrored from https://github.com/ondeinference/xcrs-controlkit.git
Synced Nov 24, 19:30 UTC
feature/runner-io-ui-hinge-methods
| 1 | import Foundation |
| 2 | |
| 3 | /// The Apple platform represented by a ControlKit runner. |
| 4 | public enum XCRSControlKitPlatform: String, Codable, CaseIterable, Sendable { |
| 5 | case iOS |
| 6 | case iPadOS |
| 7 | case tvOS |
| 8 | case watchOS |
| 9 | case visionOS |
| 10 | case macOS |
| 11 | } |
| 12 | |
| 13 | /// An operation supported by a ControlKit runner. |
| 14 | public enum XCRSControlKitCapability: String, Codable, CaseIterable, Sendable { |
| 15 | case accessibility |
| 16 | case screenshot |
| 17 | case appLifecycle |
| 18 | case touch |
| 19 | case textInput |
| 20 | case swipe |
| 21 | case orientation |
| 22 | case remoteButtons |
| 23 | case keyboard |
| 24 | case pointer |
| 25 | case spatialInteraction |
| 26 | case pairedDevice |
| 27 | /// Reports a hinge angle and fold state. Advertised at runtime, only by foldable devices. |
| 28 | case hinge |
| 29 | } |
| 30 | |
| 31 | /// A simulator or device controlled by a runner. |
| 32 | public struct XCRSControlKitTarget: Codable, Equatable, Sendable { |
| 33 | // MARK: - Public |
| 34 | |
| 35 | public let platform: XCRSControlKitPlatform |
| 36 | public let identifier: String |
| 37 | public let name: String? |
| 38 | |
| 39 | public init( |
| 40 | platform: XCRSControlKitPlatform, |
| 41 | identifier: String, |
| 42 | name: String? = nil |
| 43 | ) { |
| 44 | self.platform = platform |
| 45 | self.identifier = identifier |
| 46 | self.name = name |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// The capabilities advertised by a runner. |
| 51 | public struct XCRSControlKitCapabilities: Codable, Equatable, Sendable { |
| 52 | // MARK: - Public |
| 53 | |
| 54 | public let platform: XCRSControlKitPlatform |
| 55 | public let values: Set<XCRSControlKitCapability> |
| 56 | |
| 57 | public init( |
| 58 | platform: XCRSControlKitPlatform, |
| 59 | values: Set<XCRSControlKitCapability> |
| 60 | ) { |
| 61 | self.platform = platform |
| 62 | self.values = values |
| 63 | } |
| 64 | |
| 65 | public func supports(_ capability: XCRSControlKitCapability) -> Bool { |
| 66 | values.contains(capability) |
| 67 | } |
| 68 | |
| 69 | public static func standard( |
| 70 | for platform: XCRSControlKitPlatform |
| 71 | ) -> Self { |
| 72 | let values: Set<XCRSControlKitCapability> |
| 73 | |
| 74 | switch platform { |
| 75 | case .iOS, .iPadOS: |
| 76 | values = [ |
| 77 | .accessibility, |
| 78 | .screenshot, |
| 79 | .appLifecycle, |
| 80 | .touch, |
| 81 | .textInput, |
| 82 | .swipe, |
| 83 | .orientation |
| 84 | ] |
| 85 | case .tvOS: |
| 86 | values = [ |
| 87 | .accessibility, |
| 88 | .screenshot, |
| 89 | .appLifecycle, |
| 90 | .remoteButtons |
| 91 | ] |
| 92 | case .watchOS: |
| 93 | values = [ |
| 94 | .accessibility, |
| 95 | .screenshot, |
| 96 | .appLifecycle, |
| 97 | .pairedDevice |
| 98 | ] |
| 99 | case .visionOS: |
| 100 | values = [ |
| 101 | .accessibility, |
| 102 | .screenshot, |
| 103 | .appLifecycle, |
| 104 | .spatialInteraction |
| 105 | ] |
| 106 | case .macOS: |
| 107 | values = [ |
| 108 | .accessibility, |
| 109 | .screenshot, |
| 110 | .appLifecycle, |
| 111 | .keyboard, |
| 112 | .pointer |
| 113 | ] |
| 114 | } |
| 115 | |
| 116 | return Self(platform: platform, values: values) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// A screen-space rectangle reported by the accessibility hierarchy. |
| 121 | public struct XCRSControlKitRect: Codable, Equatable, Sendable { |
| 122 | // MARK: - Public |
| 123 | |
| 124 | public let x: Double |
| 125 | public let y: Double |
| 126 | public let width: Double |
| 127 | public let height: Double |
| 128 | |
| 129 | public init(x: Double, y: Double, width: Double, height: Double) { |
| 130 | self.x = x |
| 131 | self.y = y |
| 132 | self.width = width |
| 133 | self.height = height |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /// A transport-stable accessibility element returned by `device.dump.ui`. |
| 138 | public struct XCRSControlKitAccessibilityNode: Codable, Equatable, Sendable { |
| 139 | // MARK: - Public |
| 140 | |
| 141 | public let type: String |
| 142 | public let label: String? |
| 143 | public let name: String? |
| 144 | public let value: String? |
| 145 | public let placeholderValue: String? |
| 146 | public let rawIdentifier: String? |
| 147 | public let rect: XCRSControlKitRect |
| 148 | public let depth: Int? |
| 149 | public let enabled: Bool? |
| 150 | public let selected: Bool? |
| 151 | public let focused: Bool? |
| 152 | public let hittable: Bool? |
| 153 | public let children: [XCRSControlKitAccessibilityNode] |
| 154 | |
| 155 | public init( |
| 156 | type: String, |
| 157 | label: String? = nil, |
| 158 | name: String? = nil, |
| 159 | value: String? = nil, |
| 160 | placeholderValue: String? = nil, |
| 161 | rawIdentifier: String? = nil, |
| 162 | rect: XCRSControlKitRect, |
| 163 | depth: Int? = nil, |
| 164 | enabled: Bool? = nil, |
| 165 | selected: Bool? = nil, |
| 166 | focused: Bool? = nil, |
| 167 | hittable: Bool? = nil, |
| 168 | children: [XCRSControlKitAccessibilityNode] = [] |
| 169 | ) { |
| 170 | self.type = type |
| 171 | self.label = label |
| 172 | self.name = name |
| 173 | self.value = value |
| 174 | self.placeholderValue = placeholderValue |
| 175 | self.rawIdentifier = rawIdentifier |
| 176 | self.rect = rect |
| 177 | self.depth = depth |
| 178 | self.enabled = enabled |
| 179 | self.selected = selected |
| 180 | self.focused = focused |
| 181 | self.hittable = hittable |
| 182 | self.children = children |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /// Converts XCTest's single-snapshot hierarchy into ControlKit nodes. |
| 187 | public enum XCRSControlKitAccessibilityParser { |
| 188 | // MARK: - Public |
| 189 | |
| 190 | public static func parse( |
| 191 | debugDescription: String |
| 192 | ) throws -> XCRSControlKitAccessibilityNode { |
| 193 | let lines = debugDescription.components(separatedBy: .newlines) |
| 194 | guard |
| 195 | let subtreeStart = lines.firstIndex(of: "Element subtree:"), |
| 196 | let subtreeEnd = lines[subtreeStart...].firstIndex(of: "Path to element:") |
| 197 | else { |
| 198 | throw XCRSControlKitError.invalidRequest( |
| 199 | "XCTest did not return an accessibility subtree" |
| 200 | ) |
| 201 | } |
| 202 | |
| 203 | let nodes = lines[lines.index(after: subtreeStart)..<subtreeEnd] |
| 204 | .compactMap(parseLine) |
| 205 | guard !nodes.isEmpty else { |
| 206 | throw XCRSControlKitError.invalidRequest( |
| 207 | "XCTest returned an empty accessibility subtree" |
| 208 | ) |
| 209 | } |
| 210 | |
| 211 | var index = 0 |
| 212 | let root = tree(from: nodes, at: &index, depth: 0) |
| 213 | guard index == nodes.endIndex else { |
| 214 | throw XCRSControlKitError.invalidRequest( |
| 215 | "XCTest returned multiple root accessibility elements" |
| 216 | ) |
| 217 | } |
| 218 | return root |
| 219 | } |
| 220 | |
| 221 | // MARK: - Private |
| 222 | |
| 223 | private struct ParsedNode { |
| 224 | let node: XCRSControlKitAccessibilityNode |
| 225 | let indentation: Int |
| 226 | } |
| 227 | |
| 228 | private static func parseLine( |
| 229 | _ line: String |
| 230 | ) -> ParsedNode? { |
| 231 | let indentation = line.prefix(while: { $0 == " " }).count |
| 232 | var content = line.trimmingCharacters(in: .whitespaces) |
| 233 | if content.first == "→" { |
| 234 | content.removeFirst() |
| 235 | } |
| 236 | guard let typeEnd = content.firstIndex(of: ",") else { |
| 237 | return nil |
| 238 | } |
| 239 | |
| 240 | let type = String(content[..<typeEnd]) |
| 241 | let identifier = quotedAttribute("identifier", in: content) |
| 242 | let rect = rectangle(in: content) ?? XCRSControlKitRect( |
| 243 | x: 0, |
| 244 | y: 0, |
| 245 | width: 0, |
| 246 | height: 0 |
| 247 | ) |
| 248 | return ParsedNode( |
| 249 | node: XCRSControlKitAccessibilityNode( |
| 250 | type: type, |
| 251 | label: quotedAttribute("label", in: content), |
| 252 | name: identifier, |
| 253 | value: quotedAttribute("value", in: content), |
| 254 | placeholderValue: quotedAttribute("placeholderValue", in: content), |
| 255 | rawIdentifier: identifier, |
| 256 | rect: rect, |
| 257 | enabled: !hasStandaloneAttribute("Disabled", in: content), |
| 258 | selected: hasStandaloneAttribute("Selected", in: content), |
| 259 | focused: hasStandaloneAttribute("Focused", in: content) |
| 260 | ), |
| 261 | indentation: indentation |
| 262 | ) |
| 263 | } |
| 264 | |
| 265 | private static func tree( |
| 266 | from nodes: [ParsedNode], |
| 267 | at index: inout Int, |
| 268 | depth: Int |
| 269 | ) -> XCRSControlKitAccessibilityNode { |
| 270 | let parsedNode = nodes[index] |
| 271 | index += 1 |
| 272 | |
| 273 | var children: [XCRSControlKitAccessibilityNode] = [] |
| 274 | while index < nodes.count, nodes[index].indentation > parsedNode.indentation { |
| 275 | children.append(tree(from: nodes, at: &index, depth: depth + 1)) |
| 276 | } |
| 277 | |
| 278 | let node = parsedNode.node |
| 279 | return XCRSControlKitAccessibilityNode( |
| 280 | type: node.type, |
| 281 | label: node.label, |
| 282 | name: node.name, |
| 283 | value: node.value, |
| 284 | placeholderValue: node.placeholderValue, |
| 285 | rawIdentifier: node.rawIdentifier, |
| 286 | rect: node.rect, |
| 287 | depth: depth, |
| 288 | enabled: node.enabled, |
| 289 | selected: node.selected, |
| 290 | focused: node.focused, |
| 291 | hittable: node.hittable, |
| 292 | children: children |
| 293 | ) |
| 294 | } |
| 295 | |
| 296 | private static func rectangle(in line: String) -> XCRSControlKitRect? { |
| 297 | guard |
| 298 | let rectangleStart = line.range(of: "{{")?.upperBound, |
| 299 | let rectangleEnd = line[rectangleStart...].range(of: "}}")?.lowerBound |
| 300 | else { |
| 301 | return nil |
| 302 | } |
| 303 | |
| 304 | let components = line[rectangleStart..<rectangleEnd] |
| 305 | .split(whereSeparator: { character in |
| 306 | character == "{" || character == "}" || character == "," |
| 307 | }) |
| 308 | .map { $0.trimmingCharacters(in: .whitespaces) } |
| 309 | .filter { !$0.isEmpty } |
| 310 | guard |
| 311 | components.count == 4, |
| 312 | let x = Double(components[0]), |
| 313 | let y = Double(components[1]), |
| 314 | let width = Double(components[2]), |
| 315 | let height = Double(components[3]) |
| 316 | else { |
| 317 | return nil |
| 318 | } |
| 319 | return XCRSControlKitRect( |
| 320 | x: x, |
| 321 | y: y, |
| 322 | width: width, |
| 323 | height: height |
| 324 | ) |
| 325 | } |
| 326 | |
| 327 | private static func quotedAttribute( |
| 328 | _ name: String, |
| 329 | in line: String |
| 330 | ) -> String? { |
| 331 | let prefix = "\(name): '" |
| 332 | guard let valueStart = line.range(of: prefix)?.upperBound else { |
| 333 | return nil |
| 334 | } |
| 335 | |
| 336 | var index = valueStart |
| 337 | while index < line.endIndex { |
| 338 | let character = line[index] |
| 339 | if character == "'", isClosingQuote(at: index, in: line) { |
| 340 | let value = String(line[valueStart..<index]) |
| 341 | return value.isEmpty ? nil : value |
| 342 | } |
| 343 | index = line.index(after: index) |
| 344 | } |
| 345 | |
| 346 | return nil |
| 347 | } |
| 348 | |
| 349 | private static func hasStandaloneAttribute( |
| 350 | _ attribute: String, |
| 351 | in line: String |
| 352 | ) -> Bool { |
| 353 | var segmentStart = line.startIndex |
| 354 | var index = line.startIndex |
| 355 | var quoted = false |
| 356 | |
| 357 | while index < line.endIndex { |
| 358 | let character = line[index] |
| 359 | if character == "'" { |
| 360 | if quoted { |
| 361 | if isClosingQuote(at: index, in: line) { |
| 362 | quoted = false |
| 363 | } |
| 364 | } else { |
| 365 | quoted = true |
| 366 | } |
| 367 | } else if character == ",", !quoted { |
| 368 | if line[segmentStart..<index].trimmingCharacters(in: .whitespaces) == attribute { |
| 369 | return true |
| 370 | } |
| 371 | segmentStart = line.index(after: index) |
| 372 | } |
| 373 | index = line.index(after: index) |
| 374 | } |
| 375 | |
| 376 | return line[segmentStart...].trimmingCharacters(in: .whitespaces) == attribute |
| 377 | } |
| 378 | |
| 379 | private static func isClosingQuote( |
| 380 | at quoteIndex: String.Index, |
| 381 | in line: String |
| 382 | ) -> Bool { |
| 383 | var backslashCount = 0 |
| 384 | var index = quoteIndex |
| 385 | while index > line.startIndex { |
| 386 | let previousIndex = line.index(before: index) |
| 387 | guard line[previousIndex] == "\\" else { |
| 388 | break |
| 389 | } |
| 390 | backslashCount += 1 |
| 391 | index = previousIndex |
| 392 | } |
| 393 | guard backslashCount.isMultiple(of: 2) else { |
| 394 | return false |
| 395 | } |
| 396 | |
| 397 | index = line.index(after: quoteIndex) |
| 398 | while index < line.endIndex, line[index].isWhitespace { |
| 399 | index = line.index(after: index) |
| 400 | } |
| 401 | return index == line.endIndex || line[index] == "," || line[index] == "}" |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /// A transport-neutral command sent to a runner. |
| 406 | public struct XCRSControlKitRequest: Codable, Equatable, Sendable { |
| 407 | // MARK: - Public |
| 408 | |
| 409 | public let id: String |
| 410 | public let method: String |
| 411 | public let parameters: [String: String] |
| 412 | |
| 413 | public init( |
| 414 | id: String = UUID().uuidString, |
| 415 | method: String, |
| 416 | parameters: [String: String] = [:] |
| 417 | ) { |
| 418 | self.id = id |
| 419 | self.method = method |
| 420 | self.parameters = parameters |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /// Errors shared by package consumers and platform runners. |
| 425 | public enum XCRSControlKitError: Error, Equatable, Sendable { |
| 426 | case unsupportedPlatform(XCRSControlKitPlatform) |
| 427 | case unsupportedCapability(XCRSControlKitCapability) |
| 428 | case invalidRequest(String) |
| 429 | case transport(String) |
| 430 | } |