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
5export component TestCase {
6 width: 100phx;
7 height: 100phx;
8
9 ti := TextInput {}
10
11 in-out property<string> test_text <=> ti.text;
12
13 callback do_select();
14 do_select => {
15 ti.select_all();
16 }
17
18 callback do_copy();
19 do_copy => {
20 ti.copy();
21 }
22
23 callback do_cut();
24 do_cut => {
25 ti.cut();
26 }
27
28 callback do_paste();
29 do_paste => {
30 ti.paste();
31 }
32
33 callback do_deselect();
34 do_deselect => {
35 ti.clear-selection();
36 }
37
38 callback set_selection_offsets(int, int);
39 set_selection_offsets(start, end) => {
40 ti.set-selection-offsets(start, end);
41 }
42
43 out property<bool> has_selection: ti.cursor_position_byte_offset != ti.anchor_position_byte_offset;
44 out property<int> test_cursor_pos: ti.cursor_position_byte_offset;
45 out property<int> test_anchor_pos: ti.anchor_position_byte_offset;
46}
47
48/*
49```rust
50let instance = TestCase::new().unwrap();
51instance.set_test_text("Hello World".into());
52assert!(!instance.get_has_selection());
53instance.invoke_do_select();
54assert!(instance.get_has_selection());
55
56instance.invoke_do_copy();
57assert!(instance.get_has_selection());
58assert_eq!(instance.get_test_text(), "Hello World");
59instance.invoke_do_cut();
60assert!(!instance.get_has_selection());
61assert_eq!(instance.get_test_text(), "");
62
63instance.invoke_do_paste();
64assert_eq!(instance.get_test_text(), "Hello World");
65instance.invoke_do_select();
66assert!(instance.get_has_selection());
67instance.invoke_do_deselect();
68assert!(!instance.get_has_selection());
69
70instance.set_test_text("Hello".into());
71instance.invoke_set_selection_offsets(-3, 10);
72assert_eq!(instance.get_test_anchor_pos(), 0);
73assert_eq!(instance.get_test_cursor_pos(), 5);
74
75instance.set_test_text("🥳😎".into());
76instance.invoke_set_selection_offsets(0, 3);
77assert_eq!(instance.get_test_anchor_pos(), 0);
78assert_eq!(instance.get_test_cursor_pos(), 4);
79
80```
81*/
82
83/*
84```cpp
85auto handle = TestCase::create();
86const TestCase &instance = *handle;
87instance.set_test_text("Hello World");
88assert(!instance.get_has_selection());
89instance.invoke_do_select();
90assert(instance.get_has_selection());
91
92instance.invoke_do_copy();
93assert(instance.get_has_selection());
94assert_eq(instance.get_test_text(), "Hello World");
95instance.invoke_do_cut();
96assert(!instance.get_has_selection());
97assert_eq(instance.get_test_text(), "");
98
99instance.invoke_do_paste();
100assert_eq(instance.get_test_text(), "Hello World");
101instance.invoke_do_select();
102assert(instance.get_has_selection());
103instance.invoke_do_deselect();
104assert(!instance.get_has_selection());
105
106instance.set_test_text("Hello");
107instance.invoke_set_selection_offsets(-3, 10);
108assert_eq(instance.get_test_anchor_pos(), 0);
109assert_eq(instance.get_test_cursor_pos(), 5);
110
111instance.set_test_text("🥳😎");
112instance.invoke_set_selection_offsets(0, 3);
113assert_eq(instance.get_test_anchor_pos(), 0);
114assert_eq(instance.get_test_cursor_pos(), 4);
115
116```
117*/
118
119/*
120```js
121var instance = new slint.TestCase({});
122instance.test_text = "Hello World";
123assert(!instance.has_selection);
124instance.do_select();
125assert(instance.has_selection);
126
127instance.do_copy();
128assert(instance.has_selection);
129assert.equal(instance.test_text, "Hello World");
130instance.do_cut();
131assert(!instance.has_selection);
132assert.equal(instance.test_text, "");
133
134instance.do_paste();
135assert.equal(instance.test_text, "Hello World");
136instance.do_select();
137assert(instance.has_selection);
138instance.do_deselect();
139assert(!instance.has_selection);
140
141instance.test_text = "Hello";
142instance.set_selection_offsets(-3, 10);
143assert.equal(instance.test_anchor_pos, 0);
144assert.equal(instance.test_cursor_pos, 5);
145
146instance.test_text = "🥳😎";
147instance.set_selection_offsets(0, 3);
148assert.equal(instance.test_anchor_pos, 0);
149assert.equal(instance.test_cursor_pos, 4);
150
151```
152*/
153}
154
155#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
156 use i_slint_backend_testing as slint_testing;
157 slint_testing::init();
158 let instance = TestCase::new().unwrap();
159 instance.set_test_text("Hello World".into());
160 assert!(!instance.get_has_selection());
161 instance.invoke_do_select();
162 assert!(instance.get_has_selection());
163
164 instance.invoke_do_copy();
165 assert!(instance.get_has_selection());
166 assert_eq!(instance.get_test_text(), "Hello World");
167 instance.invoke_do_cut();
168 assert!(!instance.get_has_selection());
169 assert_eq!(instance.get_test_text(), "");
170
171 instance.invoke_do_paste();
172 assert_eq!(instance.get_test_text(), "Hello World");
173 instance.invoke_do_select();
174 assert!(instance.get_has_selection());
175 instance.invoke_do_deselect();
176 assert!(!instance.get_has_selection());
177
178 instance.set_test_text("Hello".into());
179 instance.invoke_set_selection_offsets(-3, 10);
180 assert_eq!(instance.get_test_anchor_pos(), 0);
181 assert_eq!(instance.get_test_cursor_pos(), 5);
182
183 instance.set_test_text("🥳😎".into());
184 instance.invoke_set_selection_offsets(0, 3);
185 assert_eq!(instance.get_test_anchor_pos(), 0);
186 assert_eq!(instance.get_test_cursor_pos(), 4);
187
188 Ok(())
189}