Skip to content

Commit 16b39d2

Browse files
committed
test(ios): cover capture-refusal mapping and model the system-surface fallback as data
Drive ScreenRecorder.start with an injected failing capture and exercise the in-app back fallback's refusal->code mapping so the changed lines are tested behavior. Replace the ScreenCaptureObservation mode flag with SystemSurfaceFallback { never, onUnresolvedWindow }, so the back fallback is handed no system-surface resolver instead of building one it never calls.
1 parent 7b5b504 commit 16b39d2

4 files changed

Lines changed: 179 additions & 26 deletions

File tree

‎.github/workflows/ios.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ jobs:
154154
-xctestrun "$XCTESTRUN_PATH" \
155155
-destination "platform=iOS Simulator,id=${{ steps.ios-simulator.outputs.simulator-udid }}" \
156156
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testSinglePointerFlingFallsBackToXCTestCoordinateDragWhenPrivateSynthesisFails \
157+
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testRecordStartThrowsTheCaptureRefusalItReceived \
157158
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertDispatchResolvesItsOwnModalWithoutCoordinateTapRoutingProbe \
158159
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAlertResolutionCannotBypassRequestedDeadline \
159160
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testTypeWithoutResolvedInputReturnsTypedFailureBeforeDispatchingText \

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerAppScreenCapture.swift‎

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,46 @@ enum RunnerAppScreenCaptureFailure: String, Error {
5454
}
5555
}
5656

57+
/// What a window-following capture does when the target app resolves no window. The two callers want
58+
/// two honest answers, and the difference is carried as data so neither call site passes a resolver it
59+
/// does not use:
60+
/// - `.onUnresolvedWindow` (a screenshot): with no session window the home screen really does own a
61+
/// window, so the supplied resolver names a display a window actually occupies.
62+
/// - `.never` (the in-app `back` fallback): sampling SpringBoard's home screen twice would read as
63+
/// "unchanged" and launder a wrong-process sample into a false "no back control", so the fallback
64+
/// reports the app's own refusal and is handed no system-surface resolver to reach for (#2728).
65+
enum SystemSurfaceFallback {
66+
case never
67+
case onUnresolvedWindow(() -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure>)
68+
}
69+
70+
extension RunnerTests {
71+
/// The target rule behind every window-following capture, kept apart from the queries so the rule
72+
/// itself is testable. A resolved window that refused to name its display is reported as-is; only
73+
/// `.onUnresolvedWindow` moves an unresolved *window* on to the system surface, and `.never` has no
74+
/// system surface to move on to (#2728).
75+
func selectScreenCapture(
76+
resolving: () -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure>,
77+
fallback: SystemSurfaceFallback
78+
) -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> {
79+
let outcome = resolving()
80+
guard case .onUnresolvedWindow(let systemSurface) = fallback else {
81+
return outcome
82+
}
83+
if case .failure(.unresolvedWindow) = outcome {
84+
return systemSurface()
85+
}
86+
return outcome
87+
}
88+
89+
func selectObservedScreenCapture(
90+
resolving: () -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure>,
91+
fallingBack: @escaping () -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure>
92+
) -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> {
93+
selectScreenCapture(resolving: resolving, fallback: .onUnresolvedWindow(fallingBack))
94+
}
95+
}
96+
5797
#if canImport(UIKit) && os(iOS)
5898
extension RunnerTests {
5999
/// Captures the display hosting `app` instead of `XCUIScreen.main`.
@@ -99,23 +139,10 @@ extension RunnerTests {
99139
> {
100140
selectObservedScreenCapture(
101141
resolving: { captureResolvedAppScreen(app: app) },
102-
fallingBack: { captureResolvedAppScreen(app: springboard) }
142+
fallingBack: { self.captureResolvedAppScreen(app: self.springboard) }
103143
)
104144
}
105145

106-
/// The target rule, kept apart from the two queries so the rule itself is testable: an unresolved
107-
/// window asks the system surface, and nothing else does.
108-
func selectObservedScreenCapture(
109-
resolving: () -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure>,
110-
fallingBack: () -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure>
111-
) -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> {
112-
let outcome = resolving()
113-
if case .failure(.unresolvedWindow) = outcome {
114-
return fallingBack()
115-
}
116-
return outcome
117-
}
118-
119146
private static func resolveCapturedAppScreen(
120147
app: XCUIApplication
121148
) -> Result<CapturedAppScreen, RunnerAppScreenCaptureFailure> {

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Navigation.swift‎

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,38 @@ extension RunnerTests {
177177

178178
private func captureNavigationFallbackVisualState(app: XCUIApplication) -> NavigationVisualSample {
179179
#if os(iOS)
180-
// A visual check is optional evidence, and the check is about THIS app's screen after the tap.
181-
// It uses the app's own resolved screen, never the system-surface fallback: sampling SpringBoard's
182-
// home screen twice would read as "unchanged" for an app that resolved no window, laundering a
183-
// wrong-process sample into the same false "no back control" verdict a `nil` sample produces (#2728).
184-
switch captureResolvedAppScreen(app: app) {
180+
// A visual check is optional evidence, and the check is about THIS app's screen after the tap. It
181+
// observes the app's own resolved screen with a `.never` system-surface fallback: sampling
182+
// SpringBoard's home screen twice would read as "unchanged" for an app that resolved no window,
183+
// laundering a wrong-process sample into the same false "no back control" verdict a `nil` sample
184+
// produces (#2728). The fallback is handed no system-surface resolver to reach for.
185+
let outcome = selectScreenCapture(
186+
resolving: { captureResolvedAppScreen(app: app) },
187+
fallback: .never
188+
)
189+
return Self.navigationVisualSample(from: outcome, encoding: { runnerPngData(for: $0.image) })
190+
#else
191+
return NavigationVisualSample(data: nil)
192+
#endif
193+
}
194+
195+
/// Turns a capture answer into a navigation sample: the encoded frame when the display answered,
196+
/// and the typed reason it refused otherwise. A resolved display whose image would not encode is
197+
/// named as the capture failure it is, not as an unnamed no-sample that would default to "no
198+
/// display resolved" (#2728). Kept apart from the query so the mapping itself is testable.
199+
static func navigationVisualSample(
200+
from outcome: Result<CapturedAppScreen, RunnerAppScreenCaptureFailure>,
201+
encoding: (CapturedAppScreen) -> Data?
202+
) -> NavigationVisualSample {
203+
switch outcome {
185204
case .success(let captured):
186-
guard let png = runnerPngData(for: captured.image) else {
187-
// The display resolved but the image would not encode: name it as the capture failure it is,
188-
// not as an unnamed no-sample that would default to "no display resolved" (#2728).
205+
guard let png = encoding(captured) else {
189206
let refusal = RunnerAppScreenCaptureFailure.unrenderableImage
190-
return NavigationVisualSample(data: nil, refusalCode: refusal.rawValue, refusalHint: refusal.hint)
207+
return NavigationVisualSample(
208+
data: nil,
209+
refusalCode: refusal.rawValue,
210+
refusalHint: refusal.hint
211+
)
191212
}
192213
return NavigationVisualSample(data: png)
193214
case .failure(let failure):
@@ -197,9 +218,6 @@ extension RunnerTests {
197218
refusalHint: failure.hint
198219
)
199220
}
200-
#else
201-
return NavigationVisualSample(data: nil)
202-
#endif
203221
}
204222

205223
private func verifyNavigationFallbackOutcome(
@@ -217,6 +235,16 @@ extension RunnerTests {
217235
after.data?.count ?? -1,
218236
observation.logToken
219237
)
238+
return Self.inAppBackOutcome(observation: observation, before: before, after: after)
239+
}
240+
241+
/// What an observation of the fallback's before/after samples concludes. Kept apart from the sleep
242+
/// and the capture so the three-way decision is testable without a running app.
243+
static func inAppBackOutcome(
244+
observation: NavigationVisualObservation,
245+
before: NavigationVisualSample,
246+
after: NavigationVisualSample
247+
) -> InAppBackOutcome {
220248
switch observation {
221249
case .changed:
222250
return .performed
@@ -363,5 +391,66 @@ extension RunnerTests {
363391
XCTAssertEqual(unnamed.code, "APP_SCREEN_UNRESOLVED")
364392
XCTAssertTrue(unnamed.message.contains("unknown outcome"))
365393
}
394+
395+
func testVerifyNavigationFallbackOutcomeReportsUnresolvedWindowWithoutSystemSurface() {
396+
// The in-app `back` fallback ran its tap but the app resolved no window. The conclusion must name
397+
// that refusal as an unknown outcome, and the fallback observes the app only: it selects through a
398+
// `.never` system-surface fallback, so it is handed no SpringBoard resolver to sample. Asking the
399+
// home screen twice would read as "unchanged" and launder a wrong-process capture into "no back
400+
// control exists" (#2728). Driving the real selection + mapping + decision here is what turns the
401+
// changed lines into tested behavior.
402+
let outcome = selectScreenCapture(
403+
resolving: { .failure(.unresolvedWindow) },
404+
fallback: .never
405+
)
406+
407+
let sample = Self.navigationVisualSample(from: outcome, encoding: { _ in Data([1, 2, 3]) })
408+
XCTAssertNil(sample.data)
409+
XCTAssertEqual(sample.refusalCode, "APP_SCREEN_WINDOW_UNRESOLVED")
410+
411+
let observation = Self.navigationVisualObservation(before: sample.data, after: sample.data)
412+
XCTAssertEqual(observation, .unobserved)
413+
414+
switch Self.inAppBackOutcome(observation: observation, before: sample, after: sample) {
415+
case .unverified(let payload):
416+
XCTAssertEqual(payload.code, "APP_SCREEN_WINDOW_UNRESOLVED")
417+
case .performed, .unavailable:
418+
XCTFail("a refused capture must report an unknown outcome, not 'no back control'")
419+
}
420+
}
421+
422+
func testNavigationVisualSampleDistinguishesEncodedFrameFromRefusal() {
423+
// The same capture entry point yields three different samples, and only the refusal ones may carry
424+
// a code: an encoded frame is evidence, a resolved-but-unencodable image and a refusal are not
425+
// (#2728). Reverting the mapping to a plain no-sample loses the reason a host keys on.
426+
let captured = CapturedAppScreen(
427+
image: RunnerImage(),
428+
displayID: 3,
429+
pixelWidth: 12,
430+
pixelHeight: 24,
431+
pixelsPerPoint: 3
432+
)
433+
434+
let encoded = Self.navigationVisualSample(
435+
from: .success(captured),
436+
encoding: { _ in Data([7, 7]) }
437+
)
438+
XCTAssertEqual(encoded.data, Data([7, 7]))
439+
XCTAssertNil(encoded.refusalCode)
440+
441+
let unencodable = Self.navigationVisualSample(
442+
from: .success(captured),
443+
encoding: { _ in nil }
444+
)
445+
XCTAssertNil(unencodable.data)
446+
XCTAssertEqual(unencodable.refusalCode, "APP_SCREEN_CAPTURE_UNRENDERABLE")
447+
448+
let refused = Self.navigationVisualSample(
449+
from: .failure(.unresolvedScreen),
450+
encoding: { _ in Data([7, 7]) }
451+
)
452+
XCTAssertNil(refused.data)
453+
XCTAssertEqual(refused.refusalCode, "APP_SCREEN_UNRESOLVED")
454+
}
366455
#endif
367456
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+RecordingTests.swift‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,39 @@ extension RunnerTests {
5959
}
6060
#endif
6161
}
62+
63+
#if AGENT_DEVICE_RUNNER_UNIT_TESTS && os(iOS)
64+
extension RunnerTests {
65+
func testRecordStartThrowsTheCaptureRefusalItReceived() {
66+
// The bootstrap frame is required, so `record start` must surface the exact refusal its capture
67+
// saw rather than the generic "failed to capture initial frame" every refusal used to collapse
68+
// into. Driving `start` with an always-refusing capture is what proves the bootstrap forwards
69+
// its last refusal instead of dropping it — the pure mapping tests cannot catch that wiring.
70+
let outputPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(
71+
"record-refusal-\(UUID().uuidString).mp4"
72+
)
73+
let recorder = ScreenRecorder(outputPath: outputPath, fps: 30)
74+
75+
var captureCalls = 0
76+
XCTAssertThrowsError(
77+
try recorder.start(capture: {
78+
captureCalls += 1
79+
return .failure(.unresolvedWindow)
80+
})
81+
) { error in
82+
XCTAssertEqual(
83+
(error as? RunnerAppScreenCaptureFailure)?.rawValue,
84+
"APP_SCREEN_WINDOW_UNRESOLVED",
85+
"the thrown bootstrap error is the refusal the capture returned"
86+
)
87+
}
88+
XCTAssertGreaterThan(captureCalls, 0, "the bootstrap must poll the injected capture")
89+
90+
// That thrown refusal reaches the host as its own code, not the generic record error.
91+
let payload = RunnerTests.recordingStartErrorPayload(
92+
for: RunnerAppScreenCaptureFailure.unresolvedWindow
93+
)
94+
XCTAssertEqual(payload.code, "APP_SCREEN_WINDOW_UNRESOLVED")
95+
}
96+
}
97+
#endif

0 commit comments

Comments
 (0)