|
| 1 | +import XCTest |
| 2 | + |
| 3 | +// One-time warm-up of the private XCTest synthesized-input pipeline. |
| 4 | +// |
| 5 | +// XCTest attaches its HID digitizer to the system lazily, on the first synthesized event a runner |
| 6 | +// process posts. On a warm host that attach is immediate; on a cold or loaded simulator it can lag |
| 7 | +// several seconds behind the synthesizeWithError call that triggered it. When the first synthesized |
| 8 | +// gesture of a process is a *timed* one (a drag with an activation hold, a paced pan), the touch-down |
| 9 | +// then lands seconds into a window whose later samples were scheduled relative to the intended |
| 10 | +// touch-down, so the gesture the app reconstructs is malformed even though synthesizeWithError |
| 11 | +// reported success. Once the digitizer is attached, every later synthesized gesture in the process — |
| 12 | +// including after a target relaunch — lands on schedule. |
| 13 | +// |
| 14 | +// Absorbing that first-attach latency with a throwaway synthesized contact, before the first real |
| 15 | +// gesture runs, keeps the real gesture's timing intact. This is not tolerance widening: the gesture's |
| 16 | +// own timings are unchanged; the warm-up only moves the unavoidable one-time attach cost off the |
| 17 | +// first user gesture. It is a no-op on a warm host (the attach it forces has already happened). |
| 18 | + |
| 19 | +extension RunnerTests { |
| 20 | + /// Whether the synthesized-input pipeline still needs its one-time warm-up. Pure so the |
| 21 | + /// once-per-process contract is testable without a live pipeline. |
| 22 | + func shouldWarmSynthesizedInput(alreadyWarmed: Bool) -> Bool { |
| 23 | + !alreadyWarmed |
| 24 | + } |
| 25 | + |
| 26 | + /// The throwaway warm-up contact point: the top-center of the touch reference frame, in the |
| 27 | + /// status-bar band. The digitizer attaches on the first synthesis regardless of where the contact |
| 28 | + /// lands, so the point is chosen to be inert — the status-bar band does not forward touches to app |
| 29 | + /// content — rather than meaningful. Returns nil when the frame is unusable. |
| 30 | + func synthesizedInputWarmupPoint(referenceFrame: CGRect) -> CGPoint? { |
| 31 | + guard referenceFrame.width > 0, referenceFrame.height > 0 else { return nil } |
| 32 | + return CGPoint(x: referenceFrame.midX, y: referenceFrame.minY + 1) |
| 33 | + } |
| 34 | + |
| 35 | + /// Best-effort: force the HID digitizer attach once per runner process, before the first real |
| 36 | + /// synthesized gesture. Never fails the caller — a warm-up that cannot resolve a frame or that the |
| 37 | + /// synthesizer refuses leaves the flag set (a real gesture would hit the same condition) so the |
| 38 | + /// warm-up is not retried on every gesture. |
| 39 | + func ensureSynthesizedInputWarmed(app: XCUIApplication) { |
| 40 | +#if os(iOS) |
| 41 | + guard shouldWarmSynthesizedInput(alreadyWarmed: didWarmSynthesizedInput) else { return } |
| 42 | + didWarmSynthesizedInput = true |
| 43 | + let frame = resolvedTouchReferenceFrame(app: app, appFrame: app.frame) |
| 44 | + guard let point = synthesizedInputWarmupPoint(referenceFrame: frame) else { |
| 45 | + NSLog("AGENT_DEVICE_RUNNER_SYNTHESIZED_INPUT_WARMUP outcome=skipped reason=no_reference_frame") |
| 46 | + return |
| 47 | + } |
| 48 | + let startedAt = ProcessInfo.processInfo.systemUptime |
| 49 | + let outcome = synthesizedTapAt(app: app, x: Double(point.x), y: Double(point.y)) |
| 50 | + let elapsedMs = (ProcessInfo.processInfo.systemUptime - startedAt) * 1000 |
| 51 | + switch outcome { |
| 52 | + case .performed: |
| 53 | + NSLog("AGENT_DEVICE_RUNNER_SYNTHESIZED_INPUT_WARMUP outcome=performed elapsedMs=%.0f", elapsedMs) |
| 54 | + case .unsupported(let message, _): |
| 55 | + NSLog( |
| 56 | + "AGENT_DEVICE_RUNNER_SYNTHESIZED_INPUT_WARMUP outcome=unsupported elapsedMs=%.0f detail=%@", |
| 57 | + elapsedMs, |
| 58 | + message |
| 59 | + ) |
| 60 | + } |
| 61 | +#endif |
| 62 | + } |
| 63 | +} |
0 commit comments