Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ extension RunnerTests {
context: hostContext,
deadline: deadline
)
} catch let refusal as RunnerGestureRefusedWithoutFallback {
outcome = .unsupported(message: refusal.message, hint: refusal.hint)
return
} catch {
activationError = error
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ struct RunnerGestureOutcomeUnknown: Error {
let hint: String?
}

/// A synthesized gesture that sent nothing and that no XCTest gesture may replace: on a simulator a
/// tap whose touchscreen HID contact cannot be sent. Thrown rather than returned so that no fallback
/// policy answers it; `performSynthesizedGesture` turns it into an `UNSUPPORTED_OPERATION` refusal.
struct RunnerGestureRefusedWithoutFallback: Error {
let message: String
let hint: String?
}

private struct RunnerUnsupportedOperationError: LocalizedError {
let message: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension RunnerTests {
guard let portraitSize = simulatorTouchDisplayPortraitSize(), let samples = planned.pointerSamples.first else {
return PreparedTouch(notSent: Self.unmeasuredSoleDisplayOutcome(gesture: "edge swipe"))
}
let hint = "Retry after the app is foregrounded on a simulator display with a touchscreen."
let hint = Self.simulatorHIDRefusalHint
var failure: NSString?
guard let touch = RunnerSimulatorTouch.prepare(
application: app,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,19 @@ extension RunnerTests {
) throws -> SynthesizedGestureAttempt {
var outcome: RunnerInteractionOutcome?
let timing: (gestureStartUptimeMs: Double, gestureEndUptimeMs: Double)
let gestureStartUptimeMs = currentUptimeMs()
do {
timing = try measureGesture { outcome = try synthesize() }
} catch let unknown as RunnerGestureOutcomeUnknown {
// A gesture that may have run is never repeated through a fallback.
logSynthesizedGesturePolicyDecision(kind: kind, context: context, fallbackAttempted: false)
throw unknown
} catch let refusal as RunnerGestureRefusedWithoutFallback {
logSynthesizedGesturePolicyDecision(kind: kind, context: context, fallbackAttempted: false)
return .refused(
timing: (gestureStartUptimeMs, currentUptimeMs()),
refusal: .unsupported(message: refusal.message, hint: refusal.hint)
)
}
guard let outcome else {
logSynthesizedGesturePolicyDecision(kind: kind, context: context, fallbackAttempted: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,8 @@ extension RunnerTests {
orientation: orientation
)
#if targetEnvironment(simulator)
switch try simulatorHIDTap(app: app, point: point, resolvedWindow: resolvedWindow, deadline: deadline) {
case .performed:
return .performed
case .deadlineExceeded:
return nil
case .unavailable:
break
}
#endif
return try simulatorHIDTap(app: app, point: point, resolvedWindow: resolvedWindow, deadline: deadline)
#else
var message: NSString?
let status = RunnerSynthesizedGesture.synthesizeTap(
withApplication: app,
Expand All @@ -139,6 +132,7 @@ extension RunnerTests {
hint: "Inspect the current app before deciding whether to act again."
)
}
#endif
#elseif os(tvOS)
return .unsupported(
message: "coordinate tap is not supported on tvOS; move focus with swipe or scroll, then select the focused element",
Expand Down Expand Up @@ -610,36 +604,30 @@ extension RunnerTests {
}

#if targetEnvironment(simulator)
/// How a simulator HID tap ended. `.unavailable` sent nothing, so the XCTest synthesis may run.
enum SimulatorHIDTapResult {
case performed
case deadlineExceeded
case unavailable
}

/// Taps `point` (native portrait points) with one touchscreen HID contact from inside the
/// simulator. XCTest event synthesis spends about 280 ms per tap waiting on its own round trip;
/// this contact returns once the finger lifts. A contact that went down and then failed may have
/// reached the app, so it throws `RunnerGestureOutcomeUnknown` and nothing repeats it.
/// simulator, its only tap route. XCTest event synthesis spends about 280 ms per tap waiting on its
/// own round trip; this contact returns once the finger lifts. A contact that cannot be prepared,
/// or that never went down, sent nothing: it throws `RunnerGestureRefusedWithoutFallback`, because
/// that is a broken simulator runtime, which no XCTest gesture may hide. A contact that went down
/// and then failed may have reached the app, so it throws `RunnerGestureOutcomeUnknown` and nothing
/// repeats it. Nil: the deadline passed before the contact.
@MainActor
private func simulatorHIDTap(
app: XCUIApplication,
point: CGPoint,
resolvedWindow: XCUIElement?,
deadline: Date?
) throws -> SimulatorHIDTapResult {
) throws -> RunnerInteractionOutcome? {
let startedAt = Date()
var failure: NSString?
guard let screenSize = simulatorTouchDisplayPortraitSize(),
let touch = RunnerSimulatorTouch.prepare(application: app, resolvedWindow: resolvedWindow, failure: &failure)
else {
NSLog(
"AGENT_DEVICE_RUNNER_SIMULATOR_HID_TAP outcome=unavailable reason=%@",
(failure as String?) ?? "display size unreadable"
)
return .unavailable
let reason = (failure as String?) ?? "the display size could not be read"
NSLog("AGENT_DEVICE_RUNNER_SIMULATOR_HID_TAP outcome=unavailable reason=%@", reason)
throw Self.simulatorHIDTapRefusal(reason: reason)
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}
if let deadline, deadline.timeIntervalSinceNow <= 0 { return .deadlineExceeded }
if let deadline, deadline.timeIntervalSinceNow <= 0 { return nil }
let preparedAt = Date()
var touchDown: ObjCBool = false
let message = touch.dispatchTap(at: point, screenSize: screenSize, touchDown: &touchDown)
Expand All @@ -650,12 +638,24 @@ extension RunnerTests {
Date().timeIntervalSince(preparedAt) * 1000
)
guard let message else { return .performed }
guard touchDown.boolValue else { return .unavailable }
guard touchDown.boolValue else { throw Self.simulatorHIDTapRefusal(reason: message) }
throw RunnerGestureOutcomeUnknown(
message: message,
hint: "Check the screen with snapshot before you retry; the tap may already have run."
)
}

/// The hint of every simulator HID refusal. A contact that cannot be prepared or never went down
/// means a broken simulator runtime or host, so foregrounding the app does not help.
static let simulatorHIDRefusalHint =
"Restart the simulator. If the failure persists, rebuild the runner."

static func simulatorHIDTapRefusal(reason: String) -> RunnerGestureRefusedWithoutFallback {
RunnerGestureRefusedWithoutFallback(
message: "simulator tap could not send a touchscreen HID contact: \(reason)",
hint: simulatorHIDRefusalHint
)
}
#endif

/// Where a synthesized point gesture lands: its native point and the window that routes it, or the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,29 @@ private struct RecordedSurfaceDispatch {
let displayID: UInt
let start: CGPoint
let end: CGPoint?
let orientation: Int
/// Nil for a simulator HID tap, whose point is already in native portrait points.
let orientation: Int?
}

#if targetEnvironment(simulator)
/// Stands in for a prepared `RunnerSimulatorTouch`: records its tap as the probe's dispatch.
@MainActor
private final class LitPanelSimulatorTouch: NSObject {
let window: AnyObject?

init(window: AnyObject?) {
self.window = window
}

@objc(dispatchTapAtPoint:screenSize:touchDown:)
func dispatchTap(at point: CGPoint, screenSize: CGSize, touchDown: UnsafeMutablePointer<ObjCBool>) -> NSString? {
touchDown.pointee = true
LitPanelBookingProbe.record(window: window, orientation: nil, start: point)
return nil
}
}
#endif

@MainActor
private final class LitPanelBookingProbe: NSObject {
static var originalValueForKey: IMP?
Expand Down Expand Up @@ -135,12 +155,13 @@ private final class LitPanelBookingProbe: NSObject {
return unsafeBitCast(SurfaceSelectorQuery(elements), to: XCUIElementQuery.self)
}

@objc(synthesizeTapWithApplication:resolvedWindow:interfaceOrientation:point:deadline:errorMessage:)
class func synthesizeTap(_ app: AnyObject, resolvedWindow: AnyObject?, interfaceOrientation: Int, point: CGPoint,
deadline: NSDate?, errorMessage: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Int {
record(window: resolvedWindow, orientation: interfaceOrientation, start: point)
return 0
#if targetEnvironment(simulator)
@objc(prepareWithApplication:resolvedWindow:failure:)
class func prepareSimulatorTouch(_ app: AnyObject, resolvedWindow: AnyObject?,
failure: AutoreleasingUnsafeMutablePointer<NSString?>) -> AnyObject? {
LitPanelSimulatorTouch(window: resolvedWindow)
}
#endif

@objc(synthesizeDragWithApplication:resolvedWindow:interfaceOrientation:drag:dispatched:)
class func synthesizeDrag(_ app: AnyObject, resolvedWindow: AnyObject?, interfaceOrientation: Int,
Expand All @@ -166,7 +187,7 @@ private final class LitPanelBookingProbe: NSObject {
return nil
}

private static func record(window: AnyObject?, orientation: Int, start: CGPoint, end: CGPoint? = nil) {
static func record(window: AnyObject?, orientation: Int?, start: CGPoint, end: CGPoint? = nil) {
var display: UInt = 0
if let window { _ = RunnerResolveWindowDisplayID(window, &display) }
dispatches.append(RecordedSurfaceDispatch(window: window, displayID: display, start: start, end: end, orientation: orientation))
Expand Down Expand Up @@ -287,7 +308,6 @@ extension RunnerTests {
let dispatch = try XCTUnwrap(LitPanelBookingProbe.dispatches.first)
XCTAssertEqual(dispatch.displayID, 3)
XCTAssertEqual(dispatch.start, CGPoint(x: 200, y: 150))
XCTAssertEqual(dispatch.orientation, RunnerInterfaceOrientation.portrait)
}
}
}
Expand Down Expand Up @@ -393,7 +413,6 @@ extension RunnerTests {
XCTAssertEqual(LitPanelBookingProbe.dispatches.count, 1)
let dispatch = try XCTUnwrap(LitPanelBookingProbe.dispatches.first)
XCTAssertEqual(dispatch.displayID, 1)
XCTAssertEqual(dispatch.orientation, RunnerInterfaceOrientation.landscapeRight)
XCTAssertEqual(dispatch.start, CGPoint(x: 624, y: 300))
XCTAssertTrue(dispatch.window === LitPanelBookingProbe.window)
XCTAssertEqual((dispatch.window as? XCUIElement)?.frame, CGRect(x: 224, y: 207, width: 386, height: 682))
Expand Down Expand Up @@ -765,10 +784,14 @@ extension RunnerTests {
(#selector(getter: XCUIElement.frame), #selector(getter: LitPanelBookingProbe.frame)),
(#selector(XCUIElement.descendants(matching:)), #selector(LitPanelBookingProbe.descendants(matching:))),
].map { (class_getInstanceMethod(windowClass, $0.0), class_getInstanceMethod(LitPanelBookingProbe.self, $0.1)) }
let synthesisSwap = (
class_getClassMethod(RunnerSynthesizedGesture.self, NSSelectorFromString("synthesizeTapWithApplication:resolvedWindow:interfaceOrientation:point:deadline:errorMessage:")),
class_getClassMethod(LitPanelBookingProbe.self, #selector(LitPanelBookingProbe.synthesizeTap(_:resolvedWindow:interfaceOrientation:point:deadline:errorMessage:)))
)
#if targetEnvironment(simulator)
let tapSwaps: [(Method?, Method?)] = [(
class_getClassMethod(RunnerSimulatorTouch.self, NSSelectorFromString("prepareWithApplication:resolvedWindow:failure:")),
class_getClassMethod(LitPanelBookingProbe.self, #selector(LitPanelBookingProbe.prepareSimulatorTouch(_:resolvedWindow:failure:)))
)]
#else
let tapSwaps: [(Method?, Method?)] = []
Comment thread
thymikee marked this conversation as resolved.
Resolved
#endif
let dragStub = class_getClassMethod(LitPanelBookingProbe.self, #selector(LitPanelBookingProbe.synthesizeDrag(_:resolvedWindow:interfaceOrientation:drag:dispatched:)))
let composedSwaps: [(Method?, Method?)] = [
(class_getInstanceMethod(try XCTUnwrap(object_getClass(app)), NSSelectorFromString("keyboards")),
Expand Down Expand Up @@ -806,7 +829,7 @@ extension RunnerTests {
litPanelState.panel = nil
litPanelState.surface = nil
}
for (method, stub) in swaps + (composed ? attributeSwaps + [synthesisSwap] + composedSwaps : []) {
for (method, stub) in swaps + (composed ? attributeSwaps + tapSwaps + composedSwaps : []) {
let method = try XCTUnwrap(method)
let stub = try XCTUnwrap(stub)
originals.append((method, method_setImplementation(method, method_getImplementation(stub))))
Expand Down
Loading
Loading