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 | #if os(macOS) |
| 2 | import CoreGraphics |
| 3 | import Foundation |
| 4 | import ImageIO |
| 5 | import XCTest |
| 6 | |
| 7 | @testable import XCRSHostInspect |
| 8 | |
| 9 | /// The fixture is an 8x8 PNG whose top-left 4x4 quadrant is red and whose |
| 10 | /// remaining pixels are blue, which is what pins the coordinate conventions: |
| 11 | /// rows count down from the top, columns count right from the left. |
| 12 | final class HostInspectCommandTests: XCTestCase { |
| 13 | private var outputDirectory: URL { |
| 14 | FileManager.default.temporaryDirectory |
| 15 | .appendingPathComponent("xcrs-host-inspect-tests", isDirectory: true) |
| 16 | } |
| 17 | |
| 18 | override func setUpWithError() throws { |
| 19 | try super.setUpWithError() |
| 20 | try FileManager.default.createDirectory( |
| 21 | at: outputDirectory, |
| 22 | withIntermediateDirectories: true |
| 23 | ) |
| 24 | } |
| 25 | |
| 26 | func testColumnProbeReadsFromTheTop() throws { |
| 27 | let probe = try decode( |
| 28 | HostProbeResult.self, |
| 29 | from: try invoke(["probe", "--image", try fixture().path, "--column", "1"]) |
| 30 | ) |
| 31 | |
| 32 | XCTAssertEqual(probe.axis, .column) |
| 33 | XCTAssertEqual(probe.runs.map(\.color), ["#f80000", "#0000f8"]) |
| 34 | XCTAssertEqual(probe.runs.map(\.start), [0, 4]) |
| 35 | XCTAssertEqual(probe.runs.map(\.end), [4, 8]) |
| 36 | XCTAssertEqual(probe.runs.map(\.width), [4, 4]) |
| 37 | } |
| 38 | |
| 39 | func testRowProbeReadsFromTheLeft() throws { |
| 40 | let probe = try decode( |
| 41 | HostProbeResult.self, |
| 42 | from: try invoke(["probe", "--image", try fixture().path, "--row", "1"]) |
| 43 | ) |
| 44 | |
| 45 | XCTAssertEqual(probe.axis, .row) |
| 46 | XCTAssertEqual(probe.runs.map(\.color), ["#f80000", "#0000f8"]) |
| 47 | XCTAssertEqual(probe.runs.map(\.start), [0, 4]) |
| 48 | } |
| 49 | |
| 50 | func testProbeHonoursFromAndTo() throws { |
| 51 | let probe = try decode( |
| 52 | HostProbeResult.self, |
| 53 | from: try invoke( |
| 54 | ["probe", "--image", try fixture().path, "--column", "1", "--from", "4", "--to", "8"] |
| 55 | ) |
| 56 | ) |
| 57 | |
| 58 | XCTAssertEqual(probe.from, 4) |
| 59 | XCTAssertEqual(probe.to, 8) |
| 60 | XCTAssertEqual(probe.runs.map(\.color), ["#0000f8"]) |
| 61 | XCTAssertEqual(probe.runs.map(\.start), [4]) |
| 62 | XCTAssertEqual(probe.runs.map(\.end), [8]) |
| 63 | } |
| 64 | |
| 65 | func testCropKeepsTheTopLeftOrigin() throws { |
| 66 | let source = try fixture().path |
| 67 | |
| 68 | let topLeft = try temporaryFile(named: "crop-top-left.png") |
| 69 | let result = try decode( |
| 70 | HostCropResult.self, |
| 71 | from: try invoke( |
| 72 | ["crop", "--image", source, "--rect", "0,0,4,4", "--out", topLeft.path] |
| 73 | ) |
| 74 | ) |
| 75 | XCTAssertEqual(result.pixelWidth, 4) |
| 76 | XCTAssertEqual(result.pixelHeight, 4) |
| 77 | XCTAssertEqual(try Set(pixelColors(of: topLeft)), ["#ff0000"]) |
| 78 | |
| 79 | let bottomRight = try temporaryFile(named: "crop-bottom-right.png") |
| 80 | _ = try invoke( |
| 81 | ["crop", "--image", source, "--rect", "4,4,4,4", "--out", bottomRight.path] |
| 82 | ) |
| 83 | XCTAssertEqual(try Set(pixelColors(of: bottomRight)), ["#0000ff"]) |
| 84 | } |
| 85 | |
| 86 | func testScaleConvertsUnitsToPixels() throws { |
| 87 | // At --scale 4 the 8x8 fixture is 2x2 units, so the quadrant boundary |
| 88 | // falls on the unit boundary at 1. |
| 89 | let probe = try decode( |
| 90 | HostProbeResult.self, |
| 91 | from: try invoke( |
| 92 | ["probe", "--image", try fixture().path, "--column", "0.5", "--scale", "4"] |
| 93 | ) |
| 94 | ) |
| 95 | |
| 96 | XCTAssertEqual(probe.image.pixelWidth, 8) |
| 97 | XCTAssertEqual(probe.image.width, 2) |
| 98 | XCTAssertEqual(probe.image.height, 2) |
| 99 | XCTAssertEqual(probe.position, 0.5) |
| 100 | XCTAssertEqual(probe.runs.map(\.start), [0, 1]) |
| 101 | XCTAssertEqual(probe.runs.map(\.end), [1, 2]) |
| 102 | XCTAssertEqual(probe.runs.map(\.color), ["#f80000", "#0000f8"]) |
| 103 | } |
| 104 | |
| 105 | func testScaleRejectsZero() { |
| 106 | let result = HostInspectCommand.invoke( |
| 107 | arguments: ["probe", "--image", "x.png", "--column", "1", "--scale", "0"] |
| 108 | ) |
| 109 | XCTAssertEqual(result.exitCode, 2) |
| 110 | XCTAssertTrue(result.standardError.contains("--scale")) |
| 111 | } |
| 112 | |
| 113 | func testProbeNeedsAnAxis() { |
| 114 | let result = HostInspectCommand.invoke(arguments: ["probe", "--image", "x.png"]) |
| 115 | XCTAssertEqual(result.exitCode, 2) |
| 116 | XCTAssertEqual(result.standardOutput, "") |
| 117 | XCTAssertTrue(result.standardError.contains("--row or --column")) |
| 118 | XCTAssertTrue(result.standardError.contains("xcrs-host-inspect"), "expected the usage text") |
| 119 | } |
| 120 | |
| 121 | func testProbeRejectsBothAxes() { |
| 122 | let result = HostInspectCommand.invoke( |
| 123 | arguments: ["probe", "--image", "x.png", "--row", "1", "--column", "1"] |
| 124 | ) |
| 125 | XCTAssertEqual(result.exitCode, 2) |
| 126 | XCTAssertTrue(result.standardError.contains("either --row or --column")) |
| 127 | } |
| 128 | |
| 129 | func testMissingImageIsAUsageError() { |
| 130 | let result = HostInspectCommand.invoke(arguments: ["ocr"]) |
| 131 | XCTAssertEqual(result.exitCode, 2) |
| 132 | XCTAssertTrue(result.standardError.contains("--image is required")) |
| 133 | } |
| 134 | |
| 135 | func testBadRectIsAUsageError() { |
| 136 | let result = HostInspectCommand.invoke( |
| 137 | arguments: ["crop", "--image", "x.png", "--rect", "1,2,3", "--out", "o.png"] |
| 138 | ) |
| 139 | XCTAssertEqual(result.exitCode, 2) |
| 140 | XCTAssertTrue(result.standardError.contains("--rect expects x,y,w,h")) |
| 141 | } |
| 142 | |
| 143 | func testUnknownOptionIsAUsageError() { |
| 144 | let result = HostInspectCommand.invoke(arguments: ["windows", "--nope"]) |
| 145 | XCTAssertEqual(result.exitCode, 2) |
| 146 | XCTAssertTrue(result.standardError.contains("unknown option --nope")) |
| 147 | } |
| 148 | |
| 149 | func testDevicePostureNeedsDeviceAndPosture() { |
| 150 | let missingDevice = HostInspectCommand.invoke(arguments: ["devicehub-posture", "--posture", "open"]) |
| 151 | XCTAssertEqual(missingDevice.exitCode, 2) |
| 152 | XCTAssertTrue(missingDevice.standardError.contains("--device is required")) |
| 153 | |
| 154 | let missingPosture = HostInspectCommand.invoke(arguments: ["devicehub-posture", "--device", "iPhone Duo"]) |
| 155 | XCTAssertEqual(missingPosture.exitCode, 2) |
| 156 | XCTAssertTrue(missingPosture.standardError.contains("--posture is required")) |
| 157 | } |
| 158 | |
| 159 | func testUnknownDevicePostureIsAUsageError() { |
| 160 | let result = HostInspectCommand.invoke( |
| 161 | arguments: ["devicehub-posture", "--device", "iPhone Duo", "--posture", "bent"] |
| 162 | ) |
| 163 | XCTAssertEqual(result.exitCode, 2) |
| 164 | XCTAssertTrue(result.standardError.contains("--posture must be one of closed, open, partiallyOpen")) |
| 165 | } |
| 166 | |
| 167 | func testUnknownCommandIsAUsageError() { |
| 168 | let result = HostInspectCommand.invoke(arguments: ["snapshot"]) |
| 169 | XCTAssertEqual(result.exitCode, 2) |
| 170 | XCTAssertTrue(result.standardError.contains("unknown command snapshot")) |
| 171 | } |
| 172 | |
| 173 | func testHelpPrintsUsage() { |
| 174 | let result = HostInspectCommand.invoke(arguments: ["help"]) |
| 175 | XCTAssertEqual(result.exitCode, 0) |
| 176 | XCTAssertTrue(result.standardOutput.contains("xcrs-host-inspect")) |
| 177 | XCTAssertTrue(result.standardError.isEmpty) |
| 178 | } |
| 179 | |
| 180 | func testUnreadableImageFails() { |
| 181 | let result = HostInspectCommand.invoke( |
| 182 | arguments: [ |
| 183 | "crop", "--image", "/nonexistent/missing.png", "--rect", "0,0,1,1", "--out", |
| 184 | temporaryFile(named: "unused.png").path, |
| 185 | ] |
| 186 | ) |
| 187 | XCTAssertEqual(result.exitCode, 1) |
| 188 | XCTAssertTrue(result.standardError.contains("cannot open an image")) |
| 189 | } |
| 190 | |
| 191 | func testCropOutsideTheImageFails() throws { |
| 192 | let result = HostInspectCommand.invoke( |
| 193 | arguments: [ |
| 194 | "crop", "--image", try fixture().path, "--rect", "6,6,4,4", "--out", |
| 195 | temporaryFile(named: "outside.png").path, |
| 196 | ] |
| 197 | ) |
| 198 | XCTAssertEqual(result.exitCode, 1) |
| 199 | XCTAssertTrue(result.standardError.contains("outside the 8x8 px image")) |
| 200 | } |
| 201 | |
| 202 | func testWindowsReturnsAWindowList() throws { |
| 203 | let list = try decode( |
| 204 | HostWindowList.self, |
| 205 | from: try invoke(["windows"]) |
| 206 | ) |
| 207 | XCTAssertTrue( |
| 208 | list.windows.allSatisfy { $0.bounds.width >= 0 && $0.bounds.height >= 0 }, |
| 209 | "window bounds should never be negative" |
| 210 | ) |
| 211 | |
| 212 | let filtered = try decode( |
| 213 | HostWindowList.self, |
| 214 | from: try invoke(["windows", "--owner", "no-such-app-2a3f"]) |
| 215 | ) |
| 216 | XCTAssertTrue(filtered.windows.isEmpty, "the owner filter should exclude everything") |
| 217 | } |
| 218 | |
| 219 | func testQuantizeCollapsesLowOrderBits() { |
| 220 | XCTAssertEqual( |
| 221 | HostInspect.quantize(red: 0xFF, green: 0x00, blue: 0x00), |
| 222 | HostInspect.quantize(red: 0xF8, green: 0x07, blue: 0x07) |
| 223 | ) |
| 224 | XCTAssertEqual( |
| 225 | HostInspect.hexColor(HostInspect.quantize(red: 0xFF, green: 0x00, blue: 0x00)), |
| 226 | "#f80000" |
| 227 | ) |
| 228 | } |
| 229 | |
| 230 | func testGroupRunsCollapsesNeighbours() { |
| 231 | let runs = HostInspect.groupRuns([1, 1, 2, 2, 2, 1]) |
| 232 | XCTAssertEqual(runs.map { $0.start }, [0, 2, 5]) |
| 233 | XCTAssertEqual(runs.map { $0.end }, [2, 5, 6]) |
| 234 | XCTAssertEqual(runs.map { $0.key }, [1, 2, 1]) |
| 235 | } |
| 236 | |
| 237 | func testInlineOptionValuesAreAccepted() throws { |
| 238 | let probe = try decode( |
| 239 | HostProbeResult.self, |
| 240 | from: try invoke(["probe", "--image=\(try fixture().path)", "--column=1"]) |
| 241 | ) |
| 242 | XCTAssertEqual(probe.runs.count, 2) |
| 243 | } |
| 244 | |
| 245 | private func invoke(_ arguments: [String]) throws -> String { |
| 246 | let result = HostInspectCommand.invoke(arguments: arguments) |
| 247 | XCTAssertEqual(result.exitCode, 0, result.standardError) |
| 248 | return result.standardOutput |
| 249 | } |
| 250 | |
| 251 | private func decode<T: Decodable>(_ type: T.Type, from json: String) throws -> T { |
| 252 | try JSONDecoder().decode(type, from: Data(json.utf8)) |
| 253 | } |
| 254 | |
| 255 | private func fixture() throws -> URL { |
| 256 | try XCTUnwrap( |
| 257 | Bundle.module.url( |
| 258 | forResource: "top-left-red.png", |
| 259 | withExtension: nil, |
| 260 | subdirectory: "Fixtures" |
| 261 | ), |
| 262 | "the top-left-red.png fixture is missing from the test bundle" |
| 263 | ) |
| 264 | } |
| 265 | |
| 266 | private func temporaryFile(named name: String) -> URL { |
| 267 | outputDirectory.appendingPathComponent(name) |
| 268 | } |
| 269 | |
| 270 | /// Reads every pixel of a PNG as raw hex, without quantisation, so the |
| 271 | /// assertions above only fail for a real orientation or colour mistake. |
| 272 | private func pixelColors(of url: URL) throws -> [String] { |
| 273 | let source = try XCTUnwrap(CGImageSourceCreateWithURL(url as CFURL, nil)) |
| 274 | let image = try XCTUnwrap(CGImageSourceCreateImageAtIndex(source, 0, nil)) |
| 275 | var buffer = [UInt8](repeating: 0, count: image.width * image.height * 4) |
| 276 | buffer.withUnsafeMutableBytes { raw in |
| 277 | guard |
| 278 | let context = CGContext( |
| 279 | data: raw.baseAddress, |
| 280 | width: image.width, |
| 281 | height: image.height, |
| 282 | bitsPerComponent: 8, |
| 283 | bytesPerRow: image.width * 4, |
| 284 | space: CGColorSpaceCreateDeviceRGB(), |
| 285 | bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue |
| 286 | ) |
| 287 | else { |
| 288 | return |
| 289 | } |
| 290 | context.draw( |
| 291 | image, |
| 292 | in: CGRect(x: 0, y: 0, width: image.width, height: image.height) |
| 293 | ) |
| 294 | } |
| 295 | return stride(from: 0, to: buffer.count, by: 4).map { index in |
| 296 | String( |
| 297 | format: "#%02x%02x%02x", |
| 298 | buffer[index], |
| 299 | buffer[index + 1], |
| 300 | buffer[index + 2] |
| 301 | ) |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | #endif |