| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | TestCase := TextInput { |
| 5 | width: 100phx; |
| 6 | height: 100phx; |
| 7 | single-line: false; |
| 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 BACK_CODE: char = '\u{0008}'; // backspace \b |
| 19 | |
| 20 | fn ctrl_key(instance: &TestCase, key: &str, shift: bool) { |
| 21 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), true); |
| 22 | if shift { |
| 23 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true); |
| 24 | } |
| 25 | slint_testing::send_keyboard_string_sequence(instance, key); |
| 26 | |
| 27 | // release |
| 28 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), false); |
| 29 | if shift { |
| 30 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn undo(instance: &TestCase) { |
| 35 | ctrl_key(instance, "z", false); |
| 36 | } |
| 37 | |
| 38 | fn redo(instance: &TestCase) { |
| 39 | ctrl_key(instance, "z", true); |
| 40 | ctrl_key(instance, "y", false); // for windows |
| 41 | } |
| 42 | |
| 43 | let instance = TestCase::new().unwrap(); |
| 44 | assert!(instance.get_input_focused()); |
| 45 | assert_eq!(instance.get_test_text(), ""); |
| 46 | |
| 47 | slint_testing::send_keyboard_string_sequence(&instance, "First line\nSecond line"); |
| 48 | assert_eq!(instance.get_test_text(), "First line\nSecond line"); |
| 49 | |
| 50 | // undo |
| 51 | undo(&instance); |
| 52 | assert_eq!(instance.get_test_text(), "First line\n"); |
| 53 | undo(&instance); |
| 54 | assert_eq!(instance.get_test_text(), "First line"); |
| 55 | undo(&instance); |
| 56 | assert_eq!(instance.get_test_text(), ""); |
| 57 | |
| 58 | // redo |
| 59 | redo(&instance); |
| 60 | assert_eq!(instance.get_test_text(), "First line"); |
| 61 | redo(&instance); |
| 62 | assert_eq!(instance.get_test_text(), "First line\n"); |
| 63 | redo(&instance); |
| 64 | assert_eq!(instance.get_test_text(), "First line\nSecond line"); |
| 65 | |
| 66 | // CASE: select all -> remove -> undo -> should restore original text + selection |
| 67 | ctrl_key(&instance, "a", /*shift=*/false); |
| 68 | |
| 69 | assert!(instance.get_has_selection()); |
| 70 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
| 71 | assert_eq!(instance.get_test_text(), "" ); // empty |
| 72 | assert!(!instance.get_has_selection()); // no selection |
| 73 | undo(&instance); |
| 74 | assert_eq!(instance.get_test_text(), "First line\nSecond line" ); // restored |
| 75 | assert!(instance.get_has_selection()); // selection should be there |
| 76 | ``` |
| 77 | */ |
| 78 | |