Skip to content

Commit 1fbe15b

Browse files
committed
fix(ios): read sole-display size from XCTest screen metrics
1 parent 00d3643 commit 1fbe15b

4 files changed

Lines changed: 98 additions & 48 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ extension RunnerTests {
623623
@MainActor
624624
private func soleDisplayPortraitSize() -> CGSize? {
625625
if let cached = mainOwned.soleDisplayPortraitSize { return cached }
626-
let screen = RunnerSystemApplicationScreenSize()
626+
let screen = RunnerSoleScreenSize()
627627
guard screen.width > 0, screen.height > 0 else { return nil }
628628
let size = CGSize(width: min(screen.width, screen.height), height: max(screen.width, screen.height))
629629
mainOwned.soleDisplayPortraitSize = size

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerXCTestEventBridge.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,11 @@ FOUNDATION_EXPORT NSString * _Nullable RunnerResolveSoleScreen(
7171
NSUInteger *displayID
7272
);
7373

74-
// The device screen's size in points, read off the AXRuntime system application, which always
75-
// fills the screen. It does not depend on the runner process's own `UIScreen`, which the generated
76-
// runner app, having no launch screen, can read in compatibility mode (768x1024 on an iPad). Any
77-
// failed read answers `CGSizeZero`.
74+
// The sole XCTest display's pixel bounds divided by its scale, in points. Neither the runner's
75+
// compatibility-mode `UIScreen` nor the system application's possibly empty AX frame supplies
76+
// this measurement. Any failed read or a screen count other than one answers `CGSizeZero`.
7877
#if TARGET_OS_IOS
79-
FOUNDATION_EXPORT CGSize RunnerSystemApplicationScreenSize(void);
78+
FOUNDATION_EXPORT CGSize RunnerSoleScreenSize(void);
8079
#endif
8180

8281
// What the full-screen proof concluded. The decision is a function of these values, so a test

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerXCTestEventBridge.m‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -251,28 +251,35 @@ RunnerFullScreenEvidence RunnerFullScreenEvidenceFromForeground(
251251
return cached;
252252
}
253253

254-
CGSize RunnerSystemApplicationScreenSize(void) {
255-
Class<AgentDeviceForegroundElement> element = RunnerForegroundElementClass();
256-
BOOL available = element != Nil && [element respondsToSelector:@selector(systemApplication)];
257-
__block CGRect screen = CGRectNull;
258-
NSString *exception = available ? [RunnerObjCExceptionCatcher catchException:^{
259-
id<AgentDeviceForegroundElement> systemApp = [element systemApplication];
260-
if (systemApp == nil || ![systemApp respondsToSelector:@selector(frame)]) return;
261-
screen = [systemApp frame];
262-
}] : nil;
263-
if (!available || exception != nil || !AgentDeviceForegroundFrameIsMeasurable(screen)) {
254+
CGSize RunnerSoleScreenSize(void) {
255+
__block NSUInteger screenCount = 0;
256+
__block CGRect bounds = CGRectNull;
257+
__block CGFloat scale = 0;
258+
NSString *exception = [RunnerObjCExceptionCatcher catchException:^{
259+
NSArray<XCUIScreen *> *screens = XCUIScreen.screens;
260+
screenCount = screens.count;
261+
if (screenCount != 1) return;
262+
XCUIScreen *screen = screens.firstObject;
263+
NSValue *boundsValue = [screen valueForKey:@"bounds"];
264+
[boundsValue getValue:&bounds size:sizeof(bounds)];
265+
NSNumber *scaleValue = [screen valueForKey:@"scale"];
266+
if ([scaleValue isKindOfClass:NSNumber.class]) scale = scaleValue.doubleValue;
267+
}];
268+
if (exception != nil || screenCount != 1 ||
269+
!AgentDeviceForegroundFrameIsMeasurable(bounds) || !isfinite(scale) || scale <= 0) {
264270
NSLog(
265-
@"AGENT_DEVICE_RUNNER_APP_SCREEN_UNRESOLVED stage=system-app available=%@ frame=(%g,%g %gx%g) raised=%@",
266-
available ? @"yes" : @"no",
267-
screen.origin.x,
268-
screen.origin.y,
269-
screen.size.width,
270-
screen.size.height,
271+
@"AGENT_DEVICE_RUNNER_APP_SCREEN_UNRESOLVED stage=sole-screen-size screens=%lu bounds=(%g,%g %gx%g) scale=%g raised=%@",
272+
(unsigned long)screenCount,
273+
bounds.origin.x,
274+
bounds.origin.y,
275+
bounds.size.width,
276+
bounds.size.height,
277+
scale,
271278
exception != nil ? @"yes" : @"no"
272279
);
273280
return CGSizeZero;
274281
}
275-
return screen.size;
282+
return CGSizeMake(bounds.size.width / scale, bounds.size.height / scale);
276283
}
277284

278285
// One contained probe: it checks each selector before sending it and reads the target pid and both
@@ -347,7 +354,7 @@ static RunnerFullScreenEvidence RunnerProbeApplicationFillsScreen(id application
347354
}
348355
// An iPhone keeps every app full-screen, so its idiom answers the rule and admission never pays
349356
// for the fills-screen probe. Off the phone idiom the route opens only on that proof. The display
350-
// size is a separate AXRuntime read (~0.6–0.9 ms) the caller makes after admission, the first time
357+
// size is a separate XCTest screen read the caller makes after admission, the first time
351358
// a command needs the box; a command keeps a successful read, and the next gesture retries a failed
352359
// one.
353360
if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone &&

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

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private final class RunnerCompatibilityModeScreenStub: NSObject {
3535
@objc var nativeScale: CGFloat { 2 }
3636
}
3737

38-
/// Answers like an AXRuntime whose system application cannot be read, so the display size is unknown.
38+
/// Physical iOS can expose no usable accessibility frame for the system application.
3939
private final class RunnerUnreadableSystemApplicationStub: NSObject {
4040
@objc class func systemApplication() -> AnyObject? { nil }
4141
}
@@ -44,8 +44,7 @@ extension RunnerTests {
4444
/// Installs `RunnerUnreadableSystemApplicationStub` on AXRuntime's `AXElement` until the returned
4545
/// closure restores it.
4646
private func simulateUnreadableSystemApplication() throws -> () -> Void {
47-
// The bridge loads AXRuntime on its first read.
48-
_ = RunnerSystemApplicationScreenSize()
47+
_ = dlopen("/System/Library/PrivateFrameworks/AXRuntime.framework/AXRuntime", RTLD_NOW)
4948
let element: AnyClass = try XCTUnwrap(NSClassFromString("AXElement"))
5049
let selector = NSSelectorFromString("systemApplication")
5150
let target = try XCTUnwrap(class_getClassMethod(element, selector))
@@ -54,28 +53,49 @@ extension RunnerTests {
5453
return { method_setImplementation(target, original) }
5554
}
5655

57-
/// Turns the device to landscape until the host app's interface follows, so a synthesized turn
58-
/// needs its display box; the returned closure turns it back to portrait.
56+
private func simulateUnreadableDisplay() throws -> () -> Void {
57+
let selector = #selector(NSObject.value(forKey:))
58+
let inherited = try XCTUnwrap(class_getInstanceMethod(XCUIScreen.self, selector))
59+
class_addMethod(XCUIScreen.self, selector, method_getImplementation(inherited), method_getTypeEncoding(inherited))
60+
let target = try XCTUnwrap(class_getInstanceMethod(XCUIScreen.self, selector))
61+
let original = method_getImplementation(target)
62+
typealias ReadValue = @convention(c) (AnyObject, Selector, NSString) -> AnyObject?
63+
let readValue = unsafeBitCast(original, to: ReadValue.self)
64+
let read: @convention(block) (AnyObject, NSString) -> AnyObject? = { screen, key in
65+
if key == "scale" { return NSNumber(value: 0) }
66+
return readValue(screen, selector, key)
67+
}
68+
let replacement = imp_implementationWithBlock(read)
69+
method_setImplementation(target, replacement)
70+
return {
71+
method_setImplementation(target, original)
72+
imp_removeBlock(replacement)
73+
}
74+
}
75+
76+
/// Sets the requested orientation and waits for the host app's interface to follow.
5977
@MainActor
60-
private func rotateHostAppToLandscape() throws -> () -> Void {
61-
XCUIDevice.shared.orientation = .landscapeLeft
78+
private func rotateHostApp(landscape: Bool) throws -> () -> Void {
79+
XCUIDevice.shared.orientation = landscape ? .landscapeLeft : .portrait
6280
let restore = { XCUIDevice.shared.orientation = .portrait }
63-
let landscape = [RunnerInterfaceOrientation.landscapeLeft, RunnerInterfaceOrientation.landscapeRight]
81+
let expected = landscape
82+
? [RunnerInterfaceOrientation.landscapeLeft, RunnerInterfaceOrientation.landscapeRight]
83+
: [RunnerInterfaceOrientation.portrait]
6484
let deadline = Date().addingTimeInterval(5)
65-
while !landscape.contains(Int(RunnerSynthesizedGesture.interfaceOrientation(forApplication: app))),
85+
while !expected.contains(Int(RunnerSynthesizedGesture.interfaceOrientation(forApplication: app))),
6686
Date() < deadline
6787
{
6888
RunLoop.current.run(until: Date().addingTimeInterval(0.1))
6989
}
70-
let turned = landscape.contains(Int(RunnerSynthesizedGesture.interfaceOrientation(forApplication: app)))
90+
let turned = expected.contains(Int(RunnerSynthesizedGesture.interfaceOrientation(forApplication: app)))
7191
if !turned { restore() }
72-
_ = try XCTUnwrap(turned ? true : nil, "the host app did not turn to landscape")
92+
_ = try XCTUnwrap(turned ? true : nil, "the host app did not follow the requested orientation")
7393
return restore
7494
}
7595

76-
/// The state the AX-failure tap tests start from: the host app launched (turned to landscape when
96+
/// The state the unreadable-display tap tests start from: the host app launched (turned to landscape when
7797
/// asked), a tap context on its resolved window (measured at `windowFrame` when given) or, when
78-
/// the window query is rejected, an unserializable tree and no context, and AXRuntime's display
98+
/// the window query is rejected, an unserializable tree and no context, and XCTest's display
7999
/// read failing with no kept size. The returned cleanup undoes every step.
80100
@MainActor
81101
private func launchWithUnreadableDisplay(
@@ -92,7 +112,7 @@ extension RunnerTests {
92112
self.invalidateCachedTarget(reason: "unit_test_cleanup")
93113
self.app.terminate()
94114
}
95-
if landscape { cleanups.append(try rotateHostAppToLandscape()) }
115+
cleanups.append(try rotateHostApp(landscape: landscape))
96116
var context: SynthesizedCoordinateContext?
97117
if !windowQueryRejected {
98118
let resolved = try XCTUnwrap(resolveRunnerWindowElement(app: app))
@@ -104,9 +124,9 @@ extension RunnerTests {
104124
)
105125
)
106126
}
107-
cleanups.append(try simulateUnreadableSystemApplication())
127+
cleanups.append(try simulateUnreadableDisplay())
108128
if windowQueryRejected { cleanups.append(try simulateUnserializableAppTree()) }
109-
// An earlier gesture may have kept a successful read; this one must reach AXRuntime.
129+
// An earlier gesture may have kept a successful read; this one must reach XCTest.
110130
mainOwned.soleDisplayPortraitSize = nil
111131
return (context, cleanup)
112132
} catch {
@@ -289,6 +309,31 @@ extension RunnerTests {
289309
XCTAssertEqual(displayID, 0)
290310
}
291311

312+
/// Physical iOS can report no system application's AX frame while its display is measurable.
313+
@MainActor
314+
func testSoleDisplaySizeDoesNotRequireASystemApplicationAccessibilityFrame() throws {
315+
try XCTSkipUnless(XCUIScreen.screens.count == 1, "the rotation basis requires one display")
316+
app.launch()
317+
defer {
318+
invalidateCachedTarget(reason: "unit_test_cleanup")
319+
app.terminate()
320+
}
321+
try XCTSkipUnless(soleScreenRouteIsOpen(for: app), "the host app must fill the display")
322+
let frame = app.frame
323+
let restoreUIKit = try simulateCompatibilityModeScreen()
324+
defer { restoreUIKit() }
325+
let restoreAXRuntime = try simulateUnreadableSystemApplication()
326+
defer { restoreAXRuntime() }
327+
mainOwned.soleDisplayPortraitSize = nil
328+
let placedWindow = CGRect(x: 224, y: 207, width: 386, height: 682)
329+
let measured = try XCTUnwrap(synthesizedRotationFrame(
330+
app: app, fallback: placedWindow, interfaceOrientation: RunnerInterfaceOrientation.landscapeRight
331+
))
332+
XCTAssertEqual(measured.origin, .zero)
333+
XCTAssertEqual(measured.width, max(frame.width, frame.height), accuracy: 0.5)
334+
XCTAssertEqual(measured.height, min(frame.width, frame.height), accuracy: 0.5)
335+
}
336+
292337
/// #3899: a synthesized turn swaps the display's side lengths, never a placed window's smaller box.
293338
/// #4091: the display is the one the launched host app fills, not the runner process's own
294339
/// `UIScreen`, which the generated runner app (no launch screen) reads in compatibility mode.
@@ -310,8 +355,7 @@ extension RunnerTests {
310355
height: max(appFrame.width, appFrame.height)
311356
)
312357
XCTAssertNotEqual(screen, CGSize(width: 768, height: 1024), "the host app must not fill a 768x1024 screen")
313-
// Two AX readers of one screen; the fills-screen proof ties them within 0.5 pt. Portrait and an
314-
// unknown orientation skip the read; landscape and upside-down read the box, so they are checked.
358+
// Portrait and an unknown orientation skip the read; landscape and upside-down read the box.
315359
let expectations = [
316360
(RunnerInterfaceOrientation.portraitUpsideDown, screen),
317361
(RunnerInterfaceOrientation.landscapeRight, CGSize(width: screen.height, height: screen.width)),
@@ -324,8 +368,8 @@ extension RunnerTests {
324368
}
325369
}
326370

327-
/// The display size costs one AXRuntime read, so a command keeps its first successful answer and
328-
/// later gestures take it even while AXRuntime would fail. The next command reads again, because
371+
/// A command keeps its first successful display size and later gestures take it even while the
372+
/// display read would fail. The next command reads again, because
329373
/// display scaling can change the size, and a failed read is not kept.
330374
@MainActor
331375
func testSoleDisplaySizeIsReadOncePerCommandAndAFailedReadIsNotKept() throws {
@@ -338,15 +382,15 @@ extension RunnerTests {
338382

339383
let nextCommand = try runnerCommandFixture(#"{"command":"uptime","commandId":"next-command"}"#)
340384
// Nothing between the install and the restore throws, so the restore always runs.
341-
let restoreAXRuntime = try simulateUnreadableSystemApplication()
385+
let restoreDisplay = try simulateUnreadableDisplay()
342386
XCTAssertEqual(synthesizedRotationFrame(app: app, fallback: placedWindow, interfaceOrientation: landscape), measured)
343387
_ = try? executeOnMainPrepared(command: nextCommand, activeApp: app)
344388
XCTAssertNotEqual(
345389
synthesizedRotationFrame(app: app, fallback: placedWindow, interfaceOrientation: landscape),
346390
measured,
347391
"the next command must read the size again rather than reuse the kept one"
348392
)
349-
restoreAXRuntime()
393+
restoreDisplay()
350394
XCTAssertEqual(synthesizedRotationFrame(app: app, fallback: placedWindow, interfaceOrientation: landscape), measured)
351395
}
352396

@@ -379,7 +423,7 @@ extension RunnerTests {
379423
defer { cleanup() }
380424
try XCTSkipUnless(
381425
soleScreenRouteIsOpen(for: app),
382-
"without AXRuntime an iPad cannot admit the sole-screen route; the landscape test covers refusal"
426+
"the sole-screen rule must admit the app; the landscape test covers refusal"
383427
)
384428
XCTAssertEqual(
385429
Int(RunnerSynthesizedGesture.interfaceOrientation(forApplication: app)),
@@ -418,7 +462,7 @@ extension RunnerTests {
418462
defer { cleanup() }
419463
try XCTSkipUnless(
420464
soleScreenRouteIsOpen(for: app),
421-
"without AXRuntime an iPad cannot prove the app fills the screen; the refusal test covers it"
465+
"the app must provably fill the screen; the refusal test covers it"
422466
)
423467

424468
let orientation = Int(RunnerSynthesizedGesture.interfaceOrientation(forApplication: app))

0 commit comments

Comments
 (0)