1#![allow(deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/text"#]
2// Copyright © SixtyFPS GmbH <info@slint.dev>
3// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
4
5TestCase := TextInput {
6 width: 100phx;
7 height: 100phx;
8 property<string> test_text: self.text;
9 property<int> test_cursor_pos: self.cursor_position_byte_offset;
10 property<int> test_anchor_pos: self.anchor_position_byte_offset;
11 property<bool> has_selection: self.test_cursor_pos != self.test_anchor_pos;
12 property<bool> input_focused: self.has_focus;
13}
14
15/*
16```rust
17
18const UP_CODE: char = '\u{F700}';
19const DOWN_CODE: char = '\u{F701}';
20const LEFT_CODE: char = '\u{F702}';
21const RIGHT_CODE: char = '\u{F703}';
22const DEL_CODE: char = '\u{007f}';
23const BACK_CODE: char = '\u{0008}'; // backspace \b
24
25fn send_move_mod_modifier(instance: &TestCase, pressed: bool) {
26 if cfg!(not(target_os = "macos")) {
27 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), pressed);
28 }
29
30 if cfg!(target_os = "macos") {
31 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Alt.into(), pressed);
32 }
33}
34
35fn send_move_mod_shift_modifier(instance: &TestCase, pressed: bool) {
36 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Shift.into(), pressed);
37
38 if cfg!(not(target_os = "macos")) {
39 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), pressed);
40 }
41
42 if cfg!(target_os = "macos") {
43 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Alt.into(), pressed);
44 }
45}
46
47let instance = TestCase::new().unwrap();
48slint_testing::send_mouse_click(&instance, 50., 50.);
49assert!(instance.get_input_focused());
50assert_eq!(instance.get_test_text(), "");
51slint_testing::send_keyboard_string_sequence(&instance, "Test");
52assert_eq!(instance.get_test_text(), "Test");
53assert!(!instance.get_has_selection());
54
55slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true);
56slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
57slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false);
58assert!(instance.get_has_selection());
59slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
60assert!(!instance.get_has_selection());
61slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
62
63assert_eq!(instance.get_test_text(), "Te");
64
65slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string());
66slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string());
67slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string());
68slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string());
69assert_eq!(instance.get_test_cursor_pos(), 2);
70
71slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
72slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
73slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
74slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
75assert_eq!(instance.get_test_cursor_pos(), 0);
76
77send_move_mod_shift_modifier(&instance, true);
78slint_testing::send_keyboard_string_sequence(&instance, &DOWN_CODE.to_string());
79send_move_mod_shift_modifier(&instance, false);
80assert!(instance.get_has_selection());
81assert_eq!(instance.get_test_cursor_pos(), 2);
82assert_eq!(instance.get_test_anchor_pos(), 0);
83
84slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
85assert!(!instance.get_has_selection());
86
87send_move_mod_shift_modifier(&instance, true);
88slint_testing::send_keyboard_string_sequence(&instance, &UP_CODE.to_string());
89send_move_mod_shift_modifier(&instance, false);
90assert!(instance.get_has_selection());
91assert_eq!(instance.get_test_cursor_pos(), 0);
92assert_eq!(instance.get_test_anchor_pos(), 1);
93
94// Select all and start over
95slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), true);
96slint_testing::send_keyboard_string_sequence(&instance, &"a");
97slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), false);
98slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
99assert!(!instance.get_has_selection());
100assert_eq!(instance.get_test_text(), "");
101
102slint_testing::send_keyboard_string_sequence(&instance, "First Word Third Word Fifth");
103assert_eq!(instance.get_test_text(), "First Word Third Word Fifth");
104slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
105slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
106slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
107slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
108slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
109assert_eq!(instance.get_test_cursor_pos(), 22);
110
111// Delete word backwards when the cursor is between the 'F' of Fifth and the leading space.
112// -> Delete "Word"
113send_move_mod_modifier(&instance, true);
114slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
115send_move_mod_modifier(&instance, false);
116assert_eq!(instance.get_test_text(), "First Word Third Fifth");
117
118// Once more :-)
119send_move_mod_modifier(&instance, true);
120slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
121send_move_mod_modifier(&instance, false);
122assert_eq!(instance.get_test_text(), "First Word Fifth");
123
124// Move cursor between the "d" of "Word" and the trailing space
125slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
126slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
127slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
128slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
129slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
130slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
131
132// Delete word forwards
133send_move_mod_modifier(&instance, true);
134slint_testing::send_keyboard_string_sequence(&instance, &DEL_CODE.to_string());
135send_move_mod_modifier(&instance, false);
136assert_eq!(instance.get_test_text(), "First Fifth");
137
138// Select all and start over
139slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), true);
140slint_testing::send_keyboard_string_sequence(&instance, &"a");
141slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), false);
142slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
143assert!(!instance.get_has_selection());
144assert_eq!(instance.get_test_text(), "");
145
146slint_testing::send_keyboard_string_sequence(&instance, "First Second");
147assert_eq!(instance.get_test_text(), "First Second");
148
149slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
150slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
151
152slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true);
153slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
154slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
155slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false);
156assert!(instance.get_has_selection());
157
158// When there's an existing selection, always just delete that
159send_move_mod_modifier(&instance, true);
160slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
161send_move_mod_modifier(&instance, false);
162assert_eq!(instance.get_test_text(), "First Send");
163
164slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
165slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
166slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
167slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
168slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
169
170slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true);
171slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
172slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false);
173assert!(instance.get_has_selection());
174
175// When there's an existing selection, always just delete that
176send_move_mod_modifier(&instance, true);
177slint_testing::send_keyboard_string_sequence(&instance, &DEL_CODE.to_string());
178send_move_mod_modifier(&instance, false);
179assert_eq!(instance.get_test_text(), "Fist Send");
180```
181*/
182}
183
184#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
185 use i_slint_backend_testing as slint_testing;
186 slint_testing::init();
187
188 const UP_CODE: char = '\u{F700}';
189 const DOWN_CODE: char = '\u{F701}';
190 const LEFT_CODE: char = '\u{F702}';
191 const RIGHT_CODE: char = '\u{F703}';
192 const DEL_CODE: char = '\u{007f}';
193 const BACK_CODE: char = '\u{0008}'; // backspace \b
194
195 fn send_move_mod_modifier(instance: &TestCase, pressed: bool) {
196 if cfg!(not(target_os = "macos")) {
197 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), pressed);
198 }
199
200 if cfg!(target_os = "macos") {
201 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Alt.into(), pressed);
202 }
203 }
204
205 fn send_move_mod_shift_modifier(instance: &TestCase, pressed: bool) {
206 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Shift.into(), pressed);
207
208 if cfg!(not(target_os = "macos")) {
209 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), pressed);
210 }
211
212 if cfg!(target_os = "macos") {
213 slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Alt.into(), pressed);
214 }
215 }
216
217 let instance = TestCase::new().unwrap();
218 slint_testing::send_mouse_click(&instance, 50., 50.);
219 assert!(instance.get_input_focused());
220 assert_eq!(instance.get_test_text(), "");
221 slint_testing::send_keyboard_string_sequence(&instance, "Test");
222 assert_eq!(instance.get_test_text(), "Test");
223 assert!(!instance.get_has_selection());
224
225 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true);
226 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
227 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false);
228 assert!(instance.get_has_selection());
229 slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
230 assert!(!instance.get_has_selection());
231 slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
232
233 assert_eq!(instance.get_test_text(), "Te");
234
235 slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string());
236 slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string());
237 slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string());
238 slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string());
239 assert_eq!(instance.get_test_cursor_pos(), 2);
240
241 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
242 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
243 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
244 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
245 assert_eq!(instance.get_test_cursor_pos(), 0);
246
247 send_move_mod_shift_modifier(&instance, true);
248 slint_testing::send_keyboard_string_sequence(&instance, &DOWN_CODE.to_string());
249 send_move_mod_shift_modifier(&instance, false);
250 assert!(instance.get_has_selection());
251 assert_eq!(instance.get_test_cursor_pos(), 2);
252 assert_eq!(instance.get_test_anchor_pos(), 0);
253
254 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
255 assert!(!instance.get_has_selection());
256
257 send_move_mod_shift_modifier(&instance, true);
258 slint_testing::send_keyboard_string_sequence(&instance, &UP_CODE.to_string());
259 send_move_mod_shift_modifier(&instance, false);
260 assert!(instance.get_has_selection());
261 assert_eq!(instance.get_test_cursor_pos(), 0);
262 assert_eq!(instance.get_test_anchor_pos(), 1);
263
264 // Select all and start over
265 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), true);
266 slint_testing::send_keyboard_string_sequence(&instance, &"a");
267 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), false);
268 slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
269 assert!(!instance.get_has_selection());
270 assert_eq!(instance.get_test_text(), "");
271
272 slint_testing::send_keyboard_string_sequence(&instance, "First Word Third Word Fifth");
273 assert_eq!(instance.get_test_text(), "First Word Third Word Fifth");
274 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
275 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
276 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
277 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
278 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
279 assert_eq!(instance.get_test_cursor_pos(), 22);
280
281 // Delete word backwards when the cursor is between the 'F' of Fifth and the leading space.
282 // -> Delete "Word"
283 send_move_mod_modifier(&instance, true);
284 slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
285 send_move_mod_modifier(&instance, false);
286 assert_eq!(instance.get_test_text(), "First Word Third Fifth");
287
288 // Once more :-)
289 send_move_mod_modifier(&instance, true);
290 slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
291 send_move_mod_modifier(&instance, false);
292 assert_eq!(instance.get_test_text(), "First Word Fifth");
293
294 // Move cursor between the "d" of "Word" and the trailing space
295 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
296 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
297 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
298 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
299 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
300 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
301
302 // Delete word forwards
303 send_move_mod_modifier(&instance, true);
304 slint_testing::send_keyboard_string_sequence(&instance, &DEL_CODE.to_string());
305 send_move_mod_modifier(&instance, false);
306 assert_eq!(instance.get_test_text(), "First Fifth");
307
308 // Select all and start over
309 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), true);
310 slint_testing::send_keyboard_string_sequence(&instance, &"a");
311 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), false);
312 slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
313 assert!(!instance.get_has_selection());
314 assert_eq!(instance.get_test_text(), "");
315
316 slint_testing::send_keyboard_string_sequence(&instance, "First Second");
317 assert_eq!(instance.get_test_text(), "First Second");
318
319 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
320 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
321
322 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true);
323 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
324 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
325 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false);
326 assert!(instance.get_has_selection());
327
328 // When there's an existing selection, always just delete that
329 send_move_mod_modifier(&instance, true);
330 slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string());
331 send_move_mod_modifier(&instance, false);
332 assert_eq!(instance.get_test_text(), "First Send");
333
334 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
335 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
336 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
337 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
338 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
339
340 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true);
341 slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string());
342 slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false);
343 assert!(instance.get_has_selection());
344
345 // When there's an existing selection, always just delete that
346 send_move_mod_modifier(&instance, true);
347 slint_testing::send_keyboard_string_sequence(&instance, &DEL_CODE.to_string());
348 send_move_mod_modifier(&instance, false);
349 assert_eq!(instance.get_test_text(), "Fist Send");
350 Ok(())
351}