1#![allow(deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/elements"#]
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 in-out property <int> touch1;
7 in-out property <int> touch2;
8 in-out property <int> touch3;
9
10 in-out property <string> pointer-event-test;
11
12 TouchArea {
13 x: 100phx;
14 y: 100phx;
15 width: 10phx;
16 height: 10phx;
17 clicked => { touch1+=1; }
18 mouse-cursor: move;
19 TouchArea {
20 y: 2phx;
21 height: 2phx;
22 x: 3phx;
23 width: 4phx;
24 clicked => { touch3+=1; }
25 mouse-cursor: default;
26 }
27 }
28 TouchArea {
29 x: 100phx;
30 y: 100phx;
31 width: 5phx;
32 height: 5phx;
33 mouse-cursor: pointer;
34 clicked => {
35 pointer-event-test += "click";
36 touch2+=1;
37 }
38 pointer-event(e) => {
39 if (e.kind == PointerEventKind.cancel) {
40 pointer-event-test += "cancel";
41 } else if (e.kind == PointerEventKind.up) {
42 pointer-event-test += "up";
43 } else if (e.kind == PointerEventKind.down) {
44 pointer-event-test += "down";
45 } else if (e.kind == PointerEventKind.move) {
46 pointer-event-test += "move";
47 } else {
48 pointer-event-test += "err";
49 }
50 if (e.button == PointerEventButton.right) {
51 pointer-event-test += "right";
52 } else if (e.button == PointerEventButton.left) {
53 pointer-event-test += "left";
54 } else if (e.button == PointerEventButton.middle) {
55 pointer-event-test += "middle";
56 } else if (e.button == PointerEventButton.other) {
57 pointer-event-test += "other";
58 } else {
59 pointer-event-test += "???";
60 }
61 if (e.modifiers.control) {
62 pointer-event-test += "(ctrl)";
63 }
64 if (e.modifiers.shift) {
65 pointer-event-test += "(shift)";
66 }
67 if (e.modifiers.meta) {
68 pointer-event-test += "(meta)";
69 }
70 if (e.modifiers.alt) {
71 pointer-event-test += "(alt)";
72 }
73 }
74 }
75}
76
77/*
78```cpp
79using slint::PointerEventButton;
80
81auto handle = TestCase::create();
82const TestCase &instance = *handle;
83
84// does not click on anything
85slint_testing::send_mouse_click(&instance, 5., 5.);
86assert_eq(instance.get_touch1(), 0);
87assert_eq(instance.get_touch2(), 0);
88assert_eq(instance.get_touch3(), 0);
89
90// click on second one
91slint_testing::send_mouse_click(&instance, 101., 101.);
92assert_eq(instance.get_touch1(), 0);
93assert_eq(instance.get_touch2(), 1);
94assert_eq(instance.get_touch3(), 0);
95
96// click on first one only
97slint_testing::send_mouse_click(&instance, 108., 108.);
98assert_eq(instance.get_touch1(), 1);
99assert_eq(instance.get_touch2(), 1);
100assert_eq(instance.get_touch3(), 0);
101
102// click on the third
103slint_testing::send_mouse_click(&instance, 106., 103.);
104assert_eq(instance.get_touch1(), 1);
105assert_eq(instance.get_touch2(), 1);
106assert_eq(instance.get_touch3(), 1);
107
108// The final moveother is added by the grab handler!
109assert_eq(instance.get_pointer_event_test(), "moveotherdownleftclickupleftmoveother");
110
111instance.set_pointer_event_test("");
112// issue #2918: press anywhere, release on a toucharea
113instance.window().dispatch_pointer_press_event(slint::LogicalPosition({70.0, 6.0}), PointerEventButton::Left);
114instance.window().dispatch_pointer_move_event(slint::LogicalPosition({ 102.0, 103.0 }));
115instance.window().dispatch_pointer_release_event(slint::LogicalPosition({101.0, 104.0}), PointerEventButton::Left);
116assert_eq(instance.get_pointer_event_test(), "moveotherupleft"); // no "clicked"
117assert_eq(instance.get_touch1(), 1);
118assert_eq(instance.get_touch2(), 1);
119assert_eq(instance.get_touch3(), 1);
120```
121
122```rust
123use slint::{platform::WindowEvent, platform::PointerEventButton, platform::Key, LogicalPosition};
124use slint::private_unstable_api::re_exports::MouseCursor;
125
126let instance = TestCase::new().unwrap();
127assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Default);
128
129// does not click on anything
130slint_testing::send_mouse_click(&instance, 5., 5.);
131assert_eq!(instance.get_touch1(), 0);
132assert_eq!(instance.get_touch2(), 0);
133assert_eq!(instance.get_touch3(), 0);
134assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Default);
135
136// click on second one
137slint_testing::send_mouse_click(&instance, 101., 101.);
138assert_eq!(instance.get_touch1(), 0);
139assert_eq!(instance.get_touch2(), 1);
140assert_eq!(instance.get_touch3(), 0);
141assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Pointer);
142
143// click on first one only
144slint_testing::send_mouse_click(&instance, 108., 108.);
145assert_eq!(instance.get_touch1(), 1);
146assert_eq!(instance.get_touch2(), 1);
147assert_eq!(instance.get_touch3(), 0);
148assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Move);
149
150// click on the third
151slint_testing::send_mouse_click(&instance, 106., 103.);
152assert_eq!(instance.get_touch1(), 1);
153assert_eq!(instance.get_touch2(), 1);
154assert_eq!(instance.get_touch3(), 1);
155assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Default);
156
157// The final moveother is added by the grab handler!
158assert_eq!(instance.get_pointer_event_test().as_str(), "moveotherdownleftclickupleftmoveother");
159
160instance.set_pointer_event_test("".into());
161// issue #2918: press anywhere, release on a toucharea
162instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(70.0, 6.0), button: PointerEventButton::Left });
163instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(102.0, 103.0) });
164instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(101.0, 104.0), button: PointerEventButton::Left });
165assert_eq!(instance.get_pointer_event_test().as_str(), "moveotherupleft"); // no "clicked"
166assert_eq!(instance.get_touch1(), 1);
167assert_eq!(instance.get_touch2(), 1);
168assert_eq!(instance.get_touch3(), 1);
169
170instance.set_pointer_event_test("".into());
171slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
172instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(101.0, 104.0), button: PointerEventButton::Left });
173slint_testing::send_keyboard_char(&instance, Key::Control.into(), false);
174slint_testing::send_keyboard_char(&instance, Key::Shift.into(), true);
175instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(101.0, 104.0), button: PointerEventButton::Left });
176assert_eq!(instance.get_pointer_event_test().as_str(), "downleft(ctrl)clickupleft(shift)moveother(shift)");
177```
178
179```js
180var instance = new slint.TestCase();
181// does not click on anything
182slintlib.private_api.send_mouse_click(instance, 5., 5.);
183assert.equal(instance.touch1, 0);
184assert.equal(instance.touch2, 0);
185assert.equal(instance.touch3, 0);
186
187// click on second one
188slintlib.private_api.send_mouse_click(instance, 101., 101.);
189assert.equal(instance.touch1, 0);
190assert.equal(instance.touch2, 1);
191assert.equal(instance.touch3, 0);
192
193// click on first one only
194slintlib.private_api.send_mouse_click(instance, 108., 108.);
195assert.equal(instance.touch1, 1);
196assert.equal(instance.touch2, 1);
197assert.equal(instance.touch3, 0);
198
199// click on the third
200slintlib.private_api.send_mouse_click(instance, 106., 103.);
201assert.equal(instance.touch1, 1);
202assert.equal(instance.touch2, 1);
203assert.equal(instance.touch3, 1);
204
205assert.equal(instance.pointer_event_test, "moveotherdownleftclickupleftmoveother");
206```
207*/
208}
209
210#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
211 use i_slint_backend_testing as slint_testing;
212 slint_testing::init();
213 use slint::{platform::WindowEvent, platform::PointerEventButton, platform::Key, LogicalPosition};
214 use slint::private_unstable_api::re_exports::MouseCursor;
215
216 let instance = TestCase::new().unwrap();
217 assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Default);
218
219 // does not click on anything
220 slint_testing::send_mouse_click(&instance, 5., 5.);
221 assert_eq!(instance.get_touch1(), 0);
222 assert_eq!(instance.get_touch2(), 0);
223 assert_eq!(instance.get_touch3(), 0);
224 assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Default);
225
226 // click on second one
227 slint_testing::send_mouse_click(&instance, 101., 101.);
228 assert_eq!(instance.get_touch1(), 0);
229 assert_eq!(instance.get_touch2(), 1);
230 assert_eq!(instance.get_touch3(), 0);
231 assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Pointer);
232
233 // click on first one only
234 slint_testing::send_mouse_click(&instance, 108., 108.);
235 assert_eq!(instance.get_touch1(), 1);
236 assert_eq!(instance.get_touch2(), 1);
237 assert_eq!(instance.get_touch3(), 0);
238 assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Move);
239
240 // click on the third
241 slint_testing::send_mouse_click(&instance, 106., 103.);
242 assert_eq!(instance.get_touch1(), 1);
243 assert_eq!(instance.get_touch2(), 1);
244 assert_eq!(instance.get_touch3(), 1);
245 assert_eq!(slint_testing::access_testing_window(instance.window(), |window| window.mouse_cursor.get()), MouseCursor::Default);
246
247 // The final moveother is added by the grab handler!
248 assert_eq!(instance.get_pointer_event_test().as_str(), "moveotherdownleftclickupleftmoveother");
249
250 instance.set_pointer_event_test("".into());
251 // issue #2918: press anywhere, release on a toucharea
252 instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(70.0, 6.0), button: PointerEventButton::Left });
253 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(102.0, 103.0) });
254 instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(101.0, 104.0), button: PointerEventButton::Left });
255 assert_eq!(instance.get_pointer_event_test().as_str(), "moveotherupleft"); // no "clicked"
256 assert_eq!(instance.get_touch1(), 1);
257 assert_eq!(instance.get_touch2(), 1);
258 assert_eq!(instance.get_touch3(), 1);
259
260 instance.set_pointer_event_test("".into());
261 slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
262 instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(101.0, 104.0), button: PointerEventButton::Left });
263 slint_testing::send_keyboard_char(&instance, Key::Control.into(), false);
264 slint_testing::send_keyboard_char(&instance, Key::Shift.into(), true);
265 instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(101.0, 104.0), button: PointerEventButton::Left });
266 assert_eq!(instance.get_pointer_event_test().as_str(), "downleft(ctrl)clickupleft(shift)moveother(shift)");
267 Ok(())
268}