@@ -3,10 +3,10 @@ import XCTest
33// Swift port of buildScrollGesturePlan from packages/contracts/src/scroll-gesture.ts.
44//
55// This is a deliberate two-place invariant: the daemon keeps the TS implementation (for Android,
6- // recording, and reported-pixels), and the runner places the gesture with this Swift copy. The
7- // parity test vectors at the bottom of this file mirror
8- // packages/contracts/src/scroll-gesture.test.ts —
9- // if you change the math in either language, update the other and both vector sets .
6+ // recording, and reported-pixels), and the runner places the gesture with this Swift copy. Both
7+ // ports are asserted against the same table, contracts/fixtures/scroll-gesture.json (gated
8+ // XCTest at the bottom of this file, vitest twin packages/contracts/src/scroll-gesture.test.ts) —
9+ // if you change the math in either language, update the other and the table .
1010//
1111// All inputs here are positive (reference dims, travel, center), so Swift's `.rounded()`
1212// (half away from zero) matches JS `Math.round` (half up) on every value computed below.
@@ -20,8 +20,9 @@ struct RunnerScrollGesturePlan {
2020}
2121
2222private let runnerDefaultScrollAmount = 0.6
23- // Mirrors DEFAULT_EDGE_PADDING_FRACTION: scroll gestures stay out of the outer 10% of each axis so a
24- // saturated scroll never touches down inside the status bar / Dynamic Island band (#1781 A1).
23+ // Both constants are pinned by contracts/fixtures/scroll-gesture.json (`constants`). Scroll gestures
24+ // stay out of the outer 10% of each axis so a saturated scroll never touches down inside the status
25+ // bar / Dynamic Island band (#1781 A1).
2526private let runnerDefaultEdgePaddingFraction = 0.1
2627
2728func runnerScrollGesturePlan (
@@ -66,133 +67,88 @@ func runnerScrollGesturePlan(
6667}
6768
6869#if AGENT_DEVICE_RUNNER_UNIT_TESTS
69- extension RunnerTests {
70- // Cross-language parity vectors mirroring packages/contracts/src/scroll-gesture.test.ts. Keep these
71- // in sync with the vitest vectors so the two buildScrollGesturePlan implementations cannot drift.
72-
73- func testRunnerScrollGesturePlanMapsRelativeAmount ( ) throws {
74- let plan = try XCTUnwrap (
75- runnerScrollGesturePlan (
76- direction : " down " ,
77- amount : 0.5 ,
78- pixels : nil ,
79- referenceWidth : 400 ,
80- referenceHeight : 800
81- )
82- )
83- XCTAssertEqual ( plan . x1 , 200 )
84- XCTAssertEqual ( plan . y1 , 600 )
85- XCTAssertEqual ( plan . x2 , 200 )
86- XCTAssertEqual ( plan . y2 , 200 )
87- XCTAssertEqual ( plan . travelPixels , 400 )
70+ private struct ScrollGestureFixture : Decodable {
71+ struct Constants : Decodable {
72+ let defaultScrollAmount : Double
73+ let defaultEdgePaddingFraction : Double
8874 }
89-
90- func testRunnerScrollGesturePlanPixelsDown ( ) throws {
91- // 300x600, down, pixels 120 -> (150,360)->(150,240), travel 120.
92- let plan = try XCTUnwrap (
93- runnerScrollGesturePlan (
94- direction : " down " ,
95- amount : nil ,
96- pixels : 120 ,
97- referenceWidth : 300 ,
98- referenceHeight : 600
99- )
100- )
101- XCTAssertEqual ( plan . x1 , 150 )
102- XCTAssertEqual ( plan . y1 , 360 )
103- XCTAssertEqual ( plan . x2 , 150 )
104- XCTAssertEqual ( plan . y2 , 240 )
105- XCTAssertEqual ( plan . travelPixels , 120 )
75+ struct Expected : Decodable {
76+ let x1 : Double
77+ let y1 : Double
78+ let x2 : Double
79+ let y2 : Double
80+ let pixels : Double
10681 }
107-
108- func testRunnerScrollGesturePlanClampsAmountAboveOne ( ) throws {
109- // 400x800, down, amount 2 -> requested 1600 clamps to the safe band (640): (200,720)->(200,80).
110- let plan = try XCTUnwrap (
111- runnerScrollGesturePlan (
112- direction : " down " ,
113- amount : 2 ,
114- pixels : nil ,
115- referenceWidth : 400 ,
116- referenceHeight : 800
117- )
118- )
119- XCTAssertEqual ( plan . x1 , 200 )
120- XCTAssertEqual ( plan . y1 , 720 )
121- XCTAssertEqual ( plan . x2 , 200 )
122- XCTAssertEqual ( plan . y2 , 80 )
123- XCTAssertEqual ( plan . travelPixels , 640 )
82+ struct Case : Decodable {
83+ let name : String
84+ let direction : String
85+ let amount : Double ?
86+ let pixels : Double ?
87+ let referenceWidth : Double
88+ let referenceHeight : Double
89+ let expected : Expected
12490 }
12591
126- func testRunnerScrollGesturePlanClampsExplicitPixelsVertically ( ) throws {
127- // 400x800, down, pixels 1000 clamps travel to the safe band (640): (200,720)->(200,80).
128- let plan = try XCTUnwrap (
129- runnerScrollGesturePlan (
130- direction : " down " ,
131- amount : nil ,
132- pixels : 1000 ,
133- referenceWidth : 400 ,
134- referenceHeight : 800
135- )
136- )
137- XCTAssertEqual ( plan . x1 , 200 )
138- XCTAssertEqual ( plan . y1 , 720 )
139- XCTAssertEqual ( plan . x2 , 200 )
140- XCTAssertEqual ( plan . y2 , 80 )
141- XCTAssertEqual ( plan . travelPixels , 640 )
92+ let constants : Constants
93+ let cases : [ Case ]
94+ }
95+
96+ extension RunnerTests {
97+ // Cross-language parity table: every case in contracts/fixtures/scroll-gesture.json must agree
98+ // with the vitest twin (packages/contracts/src/scroll-gesture.test.ts). Add vectors there,
99+ // never fork the math.
100+ private func loadScrollGestureFixture ( ) throws -> ScrollGestureFixture {
101+ let fixtureURL = URL ( fileURLWithPath : #filePath )
102+ . deletingLastPathComponent ( ) // AgentDeviceRunnerUITests
103+ . deletingLastPathComponent ( ) // AgentDeviceRunner
104+ . deletingLastPathComponent ( ) // runner
105+ . deletingLastPathComponent ( ) // apple
106+ . deletingLastPathComponent ( ) // repo root
107+ . appendingPathComponent ( " contracts " )
108+ . appendingPathComponent ( " fixtures " )
109+ . appendingPathComponent ( " scroll-gesture.json " )
110+ return try JSONDecoder ( ) . decode ( ScrollGestureFixture . self , from : Data ( contentsOf : fixtureURL ) )
142111 }
143112
144- func testRunnerScrollGesturePlanFloorsTinyFrames ( ) throws {
145- // 2x2, down, pixels 10 engages every max(1, ...) floor and the .5 rounding cases the two
146- // ports must agree on (halfTravel 0.5 -> 1, center 1 from 2/2): (1,2)->(1,0), travel 1.
147- let plan = try XCTUnwrap (
148- runnerScrollGesturePlan (
149- direction : " down " ,
150- amount : nil ,
151- pixels : 10 ,
152- referenceWidth : 2 ,
153- referenceHeight : 2
113+ func testRunnerScrollGesturePlanMatchesParityTable ( ) throws {
114+ let fixture = try loadScrollGestureFixture ( )
115+ XCTAssertFalse ( fixture . cases . isEmpty , " parity table must not be empty " )
116+ for testCase in fixture . cases {
117+ let plan = try XCTUnwrap (
118+ runnerScrollGesturePlan (
119+ direction : testCase . direction ,
120+ amount : testCase . amount ,
121+ pixels : testCase . pixels ,
122+ referenceWidth : testCase . referenceWidth ,
123+ referenceHeight : testCase . referenceHeight
124+ ) ,
125+ testCase . name
154126 )
155- )
156- XCTAssertEqual ( plan . x1 , 1 )
157- XCTAssertEqual ( plan . y1 , 2 )
158- XCTAssertEqual ( plan . x2 , 1 )
159- XCTAssertEqual ( plan . y2 , 0 )
160- XCTAssertEqual ( plan . travelPixels , 1 )
127+ XCTAssertEqual ( plan . x1 , testCase . expected . x1 , testCase . name )
128+ XCTAssertEqual ( plan . y1 , testCase . expected . y1 , testCase . name )
129+ XCTAssertEqual ( plan . x2 , testCase . expected . x2 , testCase . name )
130+ XCTAssertEqual ( plan . y2 , testCase . expected . y2 , testCase . name )
131+ XCTAssertEqual ( plan . travelPixels , testCase . expected . pixels , testCase . name )
132+ }
161133 }
162134
163- func testRunnerScrollGesturePlanClampsToSafeBand ( ) throws {
164- // 300x600, right, pixels 500 clamps travel to the safe band (240).
165- let plan = try XCTUnwrap (
135+ // The planner constants are private on both sides; the table pins them behaviourally on a
136+ // 1000px axis where every rounding step is exact.
137+ func testRunnerScrollGesturePlanUsesParityTableConstants ( ) throws {
138+ let constants = try loadScrollGestureFixture ( ) . constants
139+ let defaulted = try XCTUnwrap (
166140 runnerScrollGesturePlan (
167- direction : " right " ,
168- amount : nil ,
169- pixels : 500 ,
170- referenceWidth : 300 ,
171- referenceHeight : 600
141+ direction : " down " , amount : nil , pixels : nil , referenceWidth : 1000 , referenceHeight : 1000
172142 )
173143 )
174- XCTAssertEqual ( plan . x1 , 270 )
175- XCTAssertEqual ( plan . x2 , 30 )
176- XCTAssertEqual ( plan . y1 , 300 )
177- XCTAssertEqual ( plan . y2 , 300 )
178- XCTAssertEqual ( plan . travelPixels , 240 )
179- }
180-
181- func testRunnerScrollGesturePlanKeepsSaturatedScrollUpOutOfStatusBar ( ) throws {
182- // 1080x2400, up, amount 3 (#1781 A1): the touch-down at y=240 clears a Pixel 7's 136px cutout
183- // status bar; the 5% band used to start at y=120 and pulled the notification shade down.
184- let plan = try XCTUnwrap (
144+ XCTAssertEqual ( defaulted . travelPixels , 1000 * constants . defaultScrollAmount )
145+ let saturated = try XCTUnwrap (
185146 runnerScrollGesturePlan (
186- direction : " up " ,
187- amount : 3 ,
188- pixels : nil ,
189- referenceWidth : 1080 ,
190- referenceHeight : 2400
147+ direction : " down " , amount : 10 , pixels : nil , referenceWidth : 1000 , referenceHeight : 1000
191148 )
192149 )
193- XCTAssertEqual ( plan . y1 , 240 )
194- XCTAssertEqual ( plan . y2 , 2160 )
195- XCTAssertGreaterThan ( plan . y1 , 136 )
150+ XCTAssertEqual (
151+ saturated . travelPixels , 1000 - 2 * 1000 * constants . defaultEdgePaddingFraction )
196152 }
197153
198154 func testRunnerScrollGesturePlanRejectsUnknownDirection ( ) {
0 commit comments