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 | |
5 | TestCase := 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 | |
18 | const UP_CODE: char = '\u{F700}'; |
19 | const DOWN_CODE: char = '\u{F701}'; |
20 | const LEFT_CODE: char = '\u{F702}'; |
21 | const RIGHT_CODE: char = '\u{F703}'; |
22 | const DEL_CODE: char = '\u{007f}'; |
23 | const BACK_CODE: char = '\u{0008}'; // backspace \b |
24 | |
25 | fn 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 | |
35 | fn 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 | |
47 | let instance = TestCase::new().unwrap(); |
48 | slint_testing::send_mouse_click(&instance, 50., 50.); |
49 | assert!(instance.get_input_focused()); |
50 | assert_eq!(instance.get_test_text(), ""); |
51 | slint_testing::send_keyboard_string_sequence(&instance, "Test"); |
52 | assert_eq!(instance.get_test_text(), "Test"); |
53 | assert!(!instance.get_has_selection()); |
54 | |
55 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true); |
56 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
57 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false); |
58 | assert!(instance.get_has_selection()); |
59 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
60 | assert!(!instance.get_has_selection()); |
61 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
62 | |
63 | assert_eq!(instance.get_test_text(), "Te"); |
64 | |
65 | slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string()); |
66 | slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string()); |
67 | slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string()); |
68 | slint_testing::send_keyboard_string_sequence(&instance, &RIGHT_CODE.to_string()); |
69 | assert_eq!(instance.get_test_cursor_pos(), 2); |
70 | |
71 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
72 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
73 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
74 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
75 | assert_eq!(instance.get_test_cursor_pos(), 0); |
76 | |
77 | send_move_mod_shift_modifier(&instance, true); |
78 | slint_testing::send_keyboard_string_sequence(&instance, &DOWN_CODE.to_string()); |
79 | send_move_mod_shift_modifier(&instance, false); |
80 | assert!(instance.get_has_selection()); |
81 | assert_eq!(instance.get_test_cursor_pos(), 2); |
82 | assert_eq!(instance.get_test_anchor_pos(), 0); |
83 | |
84 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
85 | assert!(!instance.get_has_selection()); |
86 | |
87 | send_move_mod_shift_modifier(&instance, true); |
88 | slint_testing::send_keyboard_string_sequence(&instance, &UP_CODE.to_string()); |
89 | send_move_mod_shift_modifier(&instance, false); |
90 | assert!(instance.get_has_selection()); |
91 | assert_eq!(instance.get_test_cursor_pos(), 0); |
92 | assert_eq!(instance.get_test_anchor_pos(), 1); |
93 | |
94 | // Select all and start over |
95 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), true); |
96 | slint_testing::send_keyboard_string_sequence(&instance, &"a"); |
97 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), false); |
98 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
99 | assert!(!instance.get_has_selection()); |
100 | assert_eq!(instance.get_test_text(), ""); |
101 | |
102 | slint_testing::send_keyboard_string_sequence(&instance, "First Word Third Word Fifth"); |
103 | assert_eq!(instance.get_test_text(), "First Word Third Word Fifth"); |
104 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
105 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
106 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
107 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
108 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
109 | assert_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" |
113 | send_move_mod_modifier(&instance, true); |
114 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
115 | send_move_mod_modifier(&instance, false); |
116 | assert_eq!(instance.get_test_text(), "First Word Third Fifth"); |
117 | |
118 | // Once more :-) |
119 | send_move_mod_modifier(&instance, true); |
120 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
121 | send_move_mod_modifier(&instance, false); |
122 | assert_eq!(instance.get_test_text(), "First Word Fifth"); |
123 | |
124 | // Move cursor between the "d" of "Word" and the trailing space |
125 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
126 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
127 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
128 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
129 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
130 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
131 | |
132 | // Delete word forwards |
133 | send_move_mod_modifier(&instance, true); |
134 | slint_testing::send_keyboard_string_sequence(&instance, &DEL_CODE.to_string()); |
135 | send_move_mod_modifier(&instance, false); |
136 | assert_eq!(instance.get_test_text(), "First Fifth"); |
137 | |
138 | // Select all and start over |
139 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), true); |
140 | slint_testing::send_keyboard_string_sequence(&instance, &"a"); |
141 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Control.into(), false); |
142 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
143 | assert!(!instance.get_has_selection()); |
144 | assert_eq!(instance.get_test_text(), ""); |
145 | |
146 | slint_testing::send_keyboard_string_sequence(&instance, "First Second"); |
147 | assert_eq!(instance.get_test_text(), "First Second"); |
148 | |
149 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
150 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
151 | |
152 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true); |
153 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
154 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
155 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false); |
156 | assert!(instance.get_has_selection()); |
157 | |
158 | // When there's an existing selection, always just delete that |
159 | send_move_mod_modifier(&instance, true); |
160 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
161 | send_move_mod_modifier(&instance, false); |
162 | assert_eq!(instance.get_test_text(), "First Send"); |
163 | |
164 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
165 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
166 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
167 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
168 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
169 | |
170 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true); |
171 | slint_testing::send_keyboard_string_sequence(&instance, &LEFT_CODE.to_string()); |
172 | slint_testing::send_keyboard_char(&instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false); |
173 | assert!(instance.get_has_selection()); |
174 | |
175 | // When there's an existing selection, always just delete that |
176 | send_move_mod_modifier(&instance, true); |
177 | slint_testing::send_keyboard_string_sequence(&instance, &DEL_CODE.to_string()); |
178 | send_move_mod_modifier(&instance, false); |
179 | assert_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 | } |