@@ -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 .
3939private 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