@@ -82,5 +82,144 @@ extension RunnerTests {
8282 XCTAssertEqual ( RunnerKeyboardReturnKey . kind ( of : label ) , expected [ label ] , label )
8383 }
8484 }
85+
86+ /// What a `keyboard status` read builds from each observation. The visible keyboard's return
87+ /// control decides both fields: its wording names the kind through the same rule `keyboard enter`
88+ /// probes with, and it is kept as the accessibility tree carries it apart from surrounding
89+ /// whitespace, so a key this read cannot name is still readable.
90+ func testKeyboardStatusReportsTheObservedReturnControl ( ) {
91+ let status = RunnerKeyboardStatus . observed ( visible : true , returnControlText : " Search " )
92+ XCTAssertEqual ( status , RunnerKeyboardStatus ( visible : true , returnKey : " search " , returnKeyLabel : " Search " ) )
93+ }
94+
95+ /// The kinds `kind(of:)` names but the press list never probes are reported by the read, because
96+ /// the read looks at the keyboard's own keys rather than looking a control up by English name.
97+ func testKeyboardStatusReportsAKindThePressProbeListOmits ( ) {
98+ for ( label , kind ) in [ ( " Route " , " route " ) , ( " Continue " , " continue " ) ] {
99+ XCTAssertEqual (
100+ RunnerKeyboardStatus . observed ( visible : true , returnControlText : label ) ,
101+ RunnerKeyboardStatus ( visible : true , returnKey : kind , returnKeyLabel : label ) ,
102+ label
103+ )
104+ XCTAssertFalse (
105+ RunnerKeyboardReturnKey . probeLabels . contains ( label ) ,
106+ " \( label ) must stay out of the press probe list, which the read does not use "
107+ )
108+ }
109+ }
110+
111+ /// A hidden keyboard reports no return key at all. That is a different fact from one it could not
112+ /// name, so both fields stay out rather than answering `unknown` for a key nobody can press. The
113+ /// keyboard's keys are ignored: nothing is being submitted to.
114+ func testKeyboardStatusReportsNoReturnKeyWhileTheKeyboardIsHidden ( ) {
115+ XCTAssertEqual (
116+ RunnerKeyboardStatus . observed ( visible : false , returnControlText : " Search " ) ,
117+ RunnerKeyboardStatus ( visible : false , returnKey : nil , returnKeyLabel : nil )
118+ )
119+ XCTAssertEqual (
120+ RunnerKeyboardStatus
121+ . observed ( visible : false , keyboardKeys : [ RunnerKeyboardStatus . ObservedKeyboardKey ( text : " Done " , frame : CGRect ( x : 0 , y : 0 , width : 40 , height : 40 ) ) ] ) ,
122+ RunnerKeyboardStatus ( visible : false , returnKey : nil , returnKeyLabel : nil )
123+ )
124+ }
125+
126+ /// The English keyboard, as the accessibility tree carries it: the search field's return key reads
127+ /// `Search` in the bottom-trailing slot. The picker names the kind from its wording.
128+ func testKeyboardStatusPicksTheReturnKeyFromTheKeyboardKeysByWording ( ) {
129+ let keys = observedKeyboardKeysFixture ( [
130+ ( " Q " , 5 , 959 , 39 ) , ( " W " , 44 , 959 , 40 ) , ( " shift " , 5 , 1067 , 51 ) , ( " Delete " , 348 , 1067 , 52 ) ,
131+ ( " Numbers " , 5 , 1121 , 49 ) , ( " Emoji " , 54 , 1121 , 49 ) , ( " Space " , 103 , 1121 , 197 ) ,
132+ ( " Search " , 301 , 1121 , 99 ) , ( " Padding-Right " , 0 , 952 , 0 ) ,
133+ ] )
134+ XCTAssertEqual (
135+ RunnerKeyboardStatus . observed ( visible : true , keyboardKeys : keys ) ,
136+ RunnerKeyboardStatus ( visible : true , returnKey : " search " , returnKeyLabel : " Search " )
137+ )
138+ }
139+
140+ /// The whole point of reading the keyboard's own keys: a fully localized keyboard names no kind, so
141+ /// the return key is the bottom-trailing control and its wording is reported even though the kind
142+ /// is `unknown`. These are the frames this read measured on a Polish iOS keyboard, where `szukaj`
143+ /// sits beside a wide space bar in the last row and the letter, shift and delete keys are higher or
144+ /// further left. The headless test IME, which exposes keys with no wording at all, is the same
145+ /// shape with nothing to report.
146+ func testKeyboardStatusReportsALocalizedReturnKeyWithItsOwnWording ( ) {
147+ let keys = observedKeyboardKeysFixture ( [
148+ ( " Padding-Left " , 0 , 952 , 0 ) , ( " Q " , 5 , 959 , 39 ) , ( " P " , 360 , 959 , 40 ) , ( " shift " , 5 , 1067 , 51 ) ,
149+ ( " M " , 301 , 1067 , 40 ) , ( " usuń " , 348 , 1067 , 52 ) , ( " cyfry " , 5 , 1121 , 49 ) , ( " Emoji " , 54 , 1121 , 49 ) ,
150+ ( " " , 103 , 1121 , 197 ) , ( " szukaj " , 301 , 1121 , 99 ) ,
151+ ] )
152+ XCTAssertEqual (
153+ RunnerKeyboardStatus . observed ( visible : true , keyboardKeys : keys ) ,
154+ RunnerKeyboardStatus ( visible : true , returnKey : " unknown " , returnKeyLabel : " szukaj " )
155+ )
156+ }
157+
158+ /// The picker takes a control whose wording names a kind even when another control sits further
159+ /// down-right, so a keyboard that localizes some keys and not others is still named by wording.
160+ /// Padding elements, which carry no usable frame, never win on position.
161+ func testKeyboardStatusPrefersNamedWordingOverKeyboardGeometry ( ) {
162+ let keys = observedKeyboardKeysFixture ( [
163+ ( " Done " , 5 , 959 , 39 ) , ( " ż " , 301 , 1121 , 99 ) , ( " Padding-Right " , 0 , 952 , 0 ) ,
164+ ] )
165+ XCTAssertEqual (
166+ RunnerKeyboardStatus . observed ( visible : true , keyboardKeys : keys ) ,
167+ RunnerKeyboardStatus ( visible : true , returnKey : " done " , returnKeyLabel : " Done " )
168+ )
169+ }
170+
171+ /// Only a visible keyboard that exposes no control worth reporting answers `unknown` with no label:
172+ /// an empty layout, a keyboard whose keys all carry no wording (a headless test IME), and one whose
173+ /// only wording sits on a zero-frame padding element.
174+ func testKeyboardStatusReportsUnknownForAVisibleKeyboardWithNoReadableKey ( ) {
175+ let expected = RunnerKeyboardStatus ( visible : true , returnKey : " unknown " , returnKeyLabel : nil )
176+ XCTAssertEqual ( RunnerKeyboardStatus . observed ( visible : true , keyboardKeys : [ ] ) , expected )
177+ XCTAssertEqual (
178+ RunnerKeyboardStatus . observed (
179+ visible : true ,
180+ keyboardKeys : observedKeyboardKeysFixture ( [ ( " " , 301 , 1121 , 99 ) ] )
181+ ) ,
182+ expected
183+ )
184+ XCTAssertEqual (
185+ RunnerKeyboardStatus . observed (
186+ visible : true ,
187+ keyboardKeys : observedKeyboardKeysFixture ( [ ( " szukaj " , 0 , 952 , 0 ) ] )
188+ ) ,
189+ expected
190+ )
191+ }
192+
193+ /// The press path is unchanged by what the read can see: it still looks its own labels up in its
194+ /// own order, and the read never consults that list.
195+ func testKeyboardEnterProbeListStillOwnsItsOrder ( ) {
196+ XCTAssertEqual (
197+ RunnerKeyboardReturnKey . probeLabels ,
198+ [ " return " , " Return " , " Enter " , " Go " , " Search " , " Next " , " Done " , " Send " , " Join " ]
199+ )
200+ }
201+
202+ /// A key whose wording the rule does not recognize keeps its wording, which is the whole point of
203+ /// reporting the label: a localized or a custom key stays readable where the kind is `unknown`.
204+ func testKeyboardStatusKeepsAnUnrecognizedLabelVerbatim ( ) {
205+ for label in [ " Send Message " , " 搜索 " , " 完了 " ] {
206+ let status = RunnerKeyboardStatus . observed ( visible : true , returnControlText : label )
207+ XCTAssertEqual ( status . returnKey , " unknown " , label )
208+ XCTAssertEqual ( status . returnKeyLabel , label )
209+ }
210+ }
211+
212+ /// Builds a keyboard's observed controls from the frames this read measured; a zero width is an
213+ /// element with no usable frame, such as the padding entries a keyboard exposes.
214+ private func observedKeyboardKeysFixture (
215+ _ entries : [ ( text : String , x : CGFloat , y : CGFloat , width : CGFloat ) ]
216+ ) -> [ RunnerKeyboardStatus . ObservedKeyboardKey ] {
217+ entries . map { entry in
218+ RunnerKeyboardStatus . ObservedKeyboardKey (
219+ text : entry . text ,
220+ frame : CGRect ( x : entry . x , y : entry . y , width : entry . width , height : entry . width > 0 ? 54 : 0 )
221+ )
222+ }
223+ }
85224}
86225#endif
0 commit comments