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 | single-line: false; |
9 | property<string> test_text: self.text; |
10 | property<int> test_cursor_pos: self.cursor_position_byte_offset; |
11 | property<int> test_anchor_pos: self.anchor_position_byte_offset; |
12 | property<bool> has_selection: self.test_cursor_pos != self.test_anchor_pos; |
13 | property<bool> input_focused: self.has_focus; |
14 | } |
15 | |
16 | /* |
17 | ```rust |
18 | |
19 | const BACK_CODE: char = '\u{0008}'; // backspace \b |
20 | |
21 | fn ctrl_key(instance: &TestCase, key: &str, shift: bool) { |
22 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), true); |
23 | if shift { |
24 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true); |
25 | } |
26 | slint_testing::send_keyboard_string_sequence(instance, key); |
27 | |
28 | // release |
29 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), false); |
30 | if shift { |
31 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false); |
32 | } |
33 | } |
34 | |
35 | fn undo(instance: &TestCase) { |
36 | ctrl_key(instance, "z", false); |
37 | } |
38 | |
39 | fn redo(instance: &TestCase) { |
40 | ctrl_key(instance, "z", true); |
41 | ctrl_key(instance, "y", false); // for windows |
42 | } |
43 | |
44 | let instance = TestCase::new().unwrap(); |
45 | assert!(instance.get_input_focused()); |
46 | assert_eq!(instance.get_test_text(), ""); |
47 | |
48 | slint_testing::send_keyboard_string_sequence(&instance, "First line\nSecond line"); |
49 | assert_eq!(instance.get_test_text(), "First line\nSecond line"); |
50 | |
51 | // undo |
52 | undo(&instance); |
53 | assert_eq!(instance.get_test_text(), "First line\n"); |
54 | undo(&instance); |
55 | assert_eq!(instance.get_test_text(), "First line"); |
56 | undo(&instance); |
57 | assert_eq!(instance.get_test_text(), ""); |
58 | |
59 | // redo |
60 | redo(&instance); |
61 | assert_eq!(instance.get_test_text(), "First line"); |
62 | redo(&instance); |
63 | assert_eq!(instance.get_test_text(), "First line\n"); |
64 | redo(&instance); |
65 | assert_eq!(instance.get_test_text(), "First line\nSecond line"); |
66 | |
67 | // CASE: select all -> remove -> undo -> should restore original text + selection |
68 | ctrl_key(&instance, "a", /*shift=*/false); |
69 | |
70 | assert!(instance.get_has_selection()); |
71 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
72 | assert_eq!(instance.get_test_text(), ""); // empty |
73 | assert!(!instance.get_has_selection()); // no selection |
74 | undo(&instance); |
75 | assert_eq!(instance.get_test_text(), "First line\nSecond line"); // restored |
76 | assert!(instance.get_has_selection()); // selection should be there |
77 | ``` |
78 | */ |
79 | } |
80 | |
81 | #[test ] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { |
82 | use i_slint_backend_testing as slint_testing; |
83 | slint_testing::init(); |
84 | |
85 | const BACK_CODE: char = ' \u{0008}' ; // backspace \b |
86 | |
87 | fn ctrl_key(instance: &TestCase, key: &str, shift: bool) { |
88 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), true); |
89 | if shift { |
90 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Shift.into(), true); |
91 | } |
92 | slint_testing::send_keyboard_string_sequence(instance, key); |
93 | |
94 | // release |
95 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Control.into(), false); |
96 | if shift { |
97 | slint_testing::send_keyboard_char(instance, slint::private_unstable_api::re_exports::Key::Shift.into(), false); |
98 | } |
99 | } |
100 | |
101 | fn undo(instance: &TestCase) { |
102 | ctrl_key(instance, "z" , false); |
103 | } |
104 | |
105 | fn redo(instance: &TestCase) { |
106 | ctrl_key(instance, "z" , true); |
107 | ctrl_key(instance, "y" , false); // for windows |
108 | } |
109 | |
110 | let instance = TestCase::new().unwrap(); |
111 | assert!(instance.get_input_focused()); |
112 | assert_eq!(instance.get_test_text(), "" ); |
113 | |
114 | slint_testing::send_keyboard_string_sequence(&instance, "First line \nSecond line" ); |
115 | assert_eq!(instance.get_test_text(), "First line \nSecond line" ); |
116 | |
117 | // undo |
118 | undo(&instance); |
119 | assert_eq!(instance.get_test_text(), "First line \n" ); |
120 | undo(&instance); |
121 | assert_eq!(instance.get_test_text(), "First line" ); |
122 | undo(&instance); |
123 | assert_eq!(instance.get_test_text(), "" ); |
124 | |
125 | // redo |
126 | redo(&instance); |
127 | assert_eq!(instance.get_test_text(), "First line" ); |
128 | redo(&instance); |
129 | assert_eq!(instance.get_test_text(), "First line \n" ); |
130 | redo(&instance); |
131 | assert_eq!(instance.get_test_text(), "First line \nSecond line" ); |
132 | |
133 | // CASE: select all -> remove -> undo -> should restore original text + selection |
134 | ctrl_key(&instance, "a" , /*shift=*/false); |
135 | |
136 | assert!(instance.get_has_selection()); |
137 | slint_testing::send_keyboard_string_sequence(&instance, &BACK_CODE.to_string()); |
138 | assert_eq!(instance.get_test_text(), "" ); // empty |
139 | assert!(!instance.get_has_selection()); // no selection |
140 | undo(&instance); |
141 | assert_eq!(instance.get_test_text(), "First line \nSecond line" ); // restored |
142 | assert!(instance.get_has_selection()); // selection should be there |
143 | Ok(()) |
144 | } |