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
5TestCase := Window {
6
7
8 width: 500phx;
9 height: 500phx;
10 no-frame: false;
11
12 f := Flickable {
13 x: 10phx;
14 y: 10phx;
15 width: parent.width - 20phx;
16 height: parent.height - 20phx;
17 viewport_width: 2100phx;
18 viewport_height: 2100phx;
19
20 flicked => {
21 root.flicked += viewport_x/1phx * 100000 + viewport_y/1phx;
22 }
23
24 inner_ta := TouchArea {
25 x: 150phx;
26 y: 150phx;
27 width: 50phx;
28 height: 50phx;
29 Rectangle {
30 background: parent.pressed ? blue : parent.has_hover ? green : red;
31 }
32 clicked => {
33 root.clicked = mouse_x/1phx * 100000 + mouse_y/1phx;
34 }
35 }
36
37 }
38
39 property<length> offset_x: -f.viewport_x;
40 property<length> offset_y: -f.viewport_y;
41 property<bool> inner_ta_pressed: inner_ta.pressed;
42 property<bool> inner_ta_has_hover: inner_ta.has_hover;
43 property<int> clicked;
44 property <int> flicked;
45}
46
47/*
48
49```rust
50// Test that basic scrolling works, and that releasing the mouse animates
51use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
52let instance = TestCase::new().unwrap();
53instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(300.0, 100.0) });
54slint_testing::mock_elapsed_time(5000);
55instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(300.0, 100.0), button: PointerEventButton::Left });
56assert_eq!(instance.get_offset_x(), 0.);
57assert_eq!(instance.get_offset_y(), 0.);
58instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(200.0, 50.0) });
59assert_eq!(instance.get_offset_x(), 100.);
60assert_eq!(instance.get_offset_y(), 50.);
61slint_testing::mock_elapsed_time(200);
62assert_eq!(instance.get_offset_x(), 100.);
63assert_eq!(instance.get_offset_y(), 50.);
64instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 50.0) });
65assert_eq!(instance.get_offset_x(), 200.);
66assert_eq!(instance.get_offset_y(), 50.);
67instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 50.0), button: PointerEventButton::Left });
68// Start of the animation, the position is still unchanged
69assert_eq!(instance.get_offset_x(), 200.);
70assert_eq!(instance.get_offset_y(), 50.);
71slint_testing::mock_elapsed_time(50);
72// middle of the animation
73assert!(instance.get_offset_x() > 210.);
74assert!(instance.get_offset_y() > 60.);
75assert!(instance.get_offset_x() < 290.);
76assert!(instance.get_offset_y() < 70.);
77
78slint_testing::mock_elapsed_time(200);
79// end of the animation
80assert_eq!(instance.get_offset_x(), 450.);
81assert_eq!(instance.get_offset_y(), 112.5);
82slint_testing::mock_elapsed_time(50);
83assert_eq!(instance.get_offset_x(), 450.);
84assert_eq!(instance.get_offset_y(), 112.5);
85
86assert!(!instance.get_inner_ta_pressed());
87assert!(!instance.get_inner_ta_has_hover());
88assert_eq!(instance.get_clicked(), 0);
89```
90
91```rust
92// Test interaction with inner mouse area
93use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
94let instance = TestCase::new().unwrap();
95instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(175.0, 175.0) });
96assert!(!instance.get_inner_ta_pressed());
97assert!(instance.get_inner_ta_has_hover());
98assert_eq!(instance.get_clicked(), 0);
99slint_testing::mock_elapsed_time(5000);
100assert!(!instance.get_inner_ta_pressed());
101assert!(instance.get_inner_ta_has_hover());
102assert_eq!(instance.get_clicked(), 0);
103
104// Start a press
105instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left });
106//assert!(!instance.get_inner_ta_pressed());
107assert!(instance.get_inner_ta_has_hover());
108assert_eq!(instance.get_clicked(), 0);
109// Release almost immediately
110instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(178.0, 173.0), button: PointerEventButton::Left });
111assert!(!instance.get_inner_ta_pressed());
112assert!(instance.get_inner_ta_has_hover());
113assert_eq!(instance.get_clicked(), 18_00013);
114assert_eq!(instance.get_offset_x(), 0.);
115assert_eq!(instance.get_offset_y(), 0.);
116
117instance.set_clicked(-1);
118
119slint_testing::mock_elapsed_time(1000);
120
121// - Press, move a triny, then release quickly: should click
122// Start a press
123instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(165.0, 175.0), button: PointerEventButton::Left });
124assert!(!instance.get_inner_ta_pressed());
125assert!(instance.get_inner_ta_has_hover());
126assert_eq!(instance.get_clicked(), -1);
127// make a small move
128instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(166.0, 174.0) });
129assert!(!instance.get_inner_ta_pressed());
130assert!(instance.get_inner_ta_has_hover());
131slint_testing::mock_elapsed_time(30);
132instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(167.0, 175.0) });
133assert!(!instance.get_inner_ta_pressed());
134// and release
135instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(165.0, 174.0), button: PointerEventButton::Left });
136assert!(!instance.get_inner_ta_pressed());
137assert!(instance.get_inner_ta_has_hover());
138assert_eq!(instance.get_clicked(), 5_00014);
139assert_eq!(instance.get_offset_x(), 0.);
140assert_eq!(instance.get_offset_y(), 0.);
141
142instance.set_clicked(-1);
143
144// Start a press
145instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left });
146assert!(!instance.get_inner_ta_pressed());
147assert!(instance.get_inner_ta_has_hover());
148assert_eq!(instance.get_clicked(), -1);
149assert_eq!(instance.get_offset_x(), 0.);
150assert_eq!(instance.get_offset_y(), 0.);
151
152//wait a delay
153slint_testing::mock_elapsed_time(300);
154assert!(instance.get_inner_ta_pressed()); // only now we are pressed
155assert!(instance.get_inner_ta_has_hover());
156assert_eq!(instance.get_clicked(), -1);
157assert_eq!(instance.get_offset_x(), 0.);
158assert_eq!(instance.get_offset_y(), 0.);
159
160instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 120.0) });
161assert!(!instance.get_inner_ta_pressed()); // We should no longer be pressed
162// no hover when the flickable is flicking
163assert!(!instance.get_inner_ta_has_hover());
164assert_eq!(instance.get_clicked(), -1); // not clicked
165assert_eq!(instance.get_offset_x(), 75.);
166assert_eq!(instance.get_offset_y(), 55.);
167
168slint_testing::mock_elapsed_time(10000);
169
170instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 120.0), button: PointerEventButton::Left });
171assert!(!instance.get_inner_ta_pressed());
172assert!(instance.get_inner_ta_has_hover());
173assert_eq!(instance.get_clicked(), -1); // not clicked
174assert_eq!(instance.get_offset_x(), 75.);
175assert_eq!(instance.get_offset_y(), 55.);
176
177instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(200.0, 50.0) });
178slint_testing::mock_elapsed_time(1000);
179
180assert!(!instance.get_inner_ta_pressed());
181assert!(!instance.get_inner_ta_has_hover());
182assert_eq!(instance.get_clicked(), -1);
183assert!((instance.get_offset_x() - 75.).abs() < 5.); // only small animation on release
184assert!((instance.get_offset_y() - 55.).abs() < 5.);
185
186```
187
188```rust
189// Test wheel events
190use slint::{LogicalPosition, platform::{WindowEvent, Key} };
191let instance = TestCase::new().unwrap();
192instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(175.0, 175.0), delta_x: -30.0, delta_y: -50.0 });
193assert_eq!(instance.get_offset_x(), 30.);
194assert_eq!(instance.get_offset_y(), 50.);
195
196// When shift is pressed, it invert the direction
197// (this test don't work on macos because on macos the backend does the inversion, not the core lib)
198if !cfg!(target_os = "macos") {
199 slint_testing::send_keyboard_char(&instance, Key::Shift.into(), true);
200 instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(175.0, 175.0), delta_x: 15.0, delta_y: -60.0 });
201 slint_testing::send_keyboard_char(&instance, Key::Shift.into(), false);
202 assert_eq!(instance.get_offset_x(), 30. + 60.);
203 assert_eq!(instance.get_offset_y(), 50. - 15.);
204}
205```
206
207```rust
208// Test flicked-Callback behaviour
209use slint::{LogicalPosition, platform::{WindowEvent, PointerEventButton} };
210let instance = TestCase::new().unwrap();
211assert_eq!(instance.get_flicked(), 0);
212
213// test scrolling behaviour
214instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(175.0, 175.0), delta_x: -30.0, delta_y: -50.0 });
215dbg!(instance.get_flicked());
216assert_eq!(instance.get_flicked(), -3000050); //flicked got called after scrolling
217instance.set_flicked(0);
218
219// test dragging bevaviour
220instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left });
221slint_testing::mock_elapsed_time(300);
222assert_eq!(instance.get_flicked(), 0); //flicked didn't get called by just pressing
223instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 120.0) });
224slint_testing::mock_elapsed_time(10000);
225assert_eq!(instance.get_flicked(), -10500105); //flicked got called during drag
226instance.set_flicked(0);
227instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 120.0), button: PointerEventButton::Left });
228assert_eq!(instance.get_flicked(), -10500105); //flicked got called after drag
229instance.set_flicked(0);
230
231```
232*/
233}
234
235#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
236 use i_slint_backend_testing as slint_testing;
237 slint_testing::init();
238 // Test that basic scrolling works, and that releasing the mouse animates
239 use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
240 let instance = TestCase::new().unwrap();
241 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(300.0, 100.0) });
242 slint_testing::mock_elapsed_time(5000);
243 instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(300.0, 100.0), button: PointerEventButton::Left });
244 assert_eq!(instance.get_offset_x(), 0.);
245 assert_eq!(instance.get_offset_y(), 0.);
246 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(200.0, 50.0) });
247 assert_eq!(instance.get_offset_x(), 100.);
248 assert_eq!(instance.get_offset_y(), 50.);
249 slint_testing::mock_elapsed_time(200);
250 assert_eq!(instance.get_offset_x(), 100.);
251 assert_eq!(instance.get_offset_y(), 50.);
252 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 50.0) });
253 assert_eq!(instance.get_offset_x(), 200.);
254 assert_eq!(instance.get_offset_y(), 50.);
255 instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 50.0), button: PointerEventButton::Left });
256 // Start of the animation, the position is still unchanged
257 assert_eq!(instance.get_offset_x(), 200.);
258 assert_eq!(instance.get_offset_y(), 50.);
259 slint_testing::mock_elapsed_time(50);
260 // middle of the animation
261 assert!(instance.get_offset_x() > 210.);
262 assert!(instance.get_offset_y() > 60.);
263 assert!(instance.get_offset_x() < 290.);
264 assert!(instance.get_offset_y() < 70.);
265
266 slint_testing::mock_elapsed_time(200);
267 // end of the animation
268 assert_eq!(instance.get_offset_x(), 450.);
269 assert_eq!(instance.get_offset_y(), 112.5);
270 slint_testing::mock_elapsed_time(50);
271 assert_eq!(instance.get_offset_x(), 450.);
272 assert_eq!(instance.get_offset_y(), 112.5);
273
274 assert!(!instance.get_inner_ta_pressed());
275 assert!(!instance.get_inner_ta_has_hover());
276 assert_eq!(instance.get_clicked(), 0);
277 Ok(())
278}
279#[test] fn t_1() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
280 use i_slint_backend_testing as slint_testing;
281 slint_testing::init();
282 // Test interaction with inner mouse area
283 use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
284 let instance = TestCase::new().unwrap();
285 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(175.0, 175.0) });
286 assert!(!instance.get_inner_ta_pressed());
287 assert!(instance.get_inner_ta_has_hover());
288 assert_eq!(instance.get_clicked(), 0);
289 slint_testing::mock_elapsed_time(5000);
290 assert!(!instance.get_inner_ta_pressed());
291 assert!(instance.get_inner_ta_has_hover());
292 assert_eq!(instance.get_clicked(), 0);
293
294 // Start a press
295 instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left });
296 //assert!(!instance.get_inner_ta_pressed());
297 assert!(instance.get_inner_ta_has_hover());
298 assert_eq!(instance.get_clicked(), 0);
299 // Release almost immediately
300 instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(178.0, 173.0), button: PointerEventButton::Left });
301 assert!(!instance.get_inner_ta_pressed());
302 assert!(instance.get_inner_ta_has_hover());
303 assert_eq!(instance.get_clicked(), 18_00013);
304 assert_eq!(instance.get_offset_x(), 0.);
305 assert_eq!(instance.get_offset_y(), 0.);
306
307 instance.set_clicked(-1);
308
309 slint_testing::mock_elapsed_time(1000);
310
311 // - Press, move a triny, then release quickly: should click
312 // Start a press
313 instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(165.0, 175.0), button: PointerEventButton::Left });
314 assert!(!instance.get_inner_ta_pressed());
315 assert!(instance.get_inner_ta_has_hover());
316 assert_eq!(instance.get_clicked(), -1);
317 // make a small move
318 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(166.0, 174.0) });
319 assert!(!instance.get_inner_ta_pressed());
320 assert!(instance.get_inner_ta_has_hover());
321 slint_testing::mock_elapsed_time(30);
322 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(167.0, 175.0) });
323 assert!(!instance.get_inner_ta_pressed());
324 // and release
325 instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(165.0, 174.0), button: PointerEventButton::Left });
326 assert!(!instance.get_inner_ta_pressed());
327 assert!(instance.get_inner_ta_has_hover());
328 assert_eq!(instance.get_clicked(), 5_00014);
329 assert_eq!(instance.get_offset_x(), 0.);
330 assert_eq!(instance.get_offset_y(), 0.);
331
332 instance.set_clicked(-1);
333
334 // Start a press
335 instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left });
336 assert!(!instance.get_inner_ta_pressed());
337 assert!(instance.get_inner_ta_has_hover());
338 assert_eq!(instance.get_clicked(), -1);
339 assert_eq!(instance.get_offset_x(), 0.);
340 assert_eq!(instance.get_offset_y(), 0.);
341
342 //wait a delay
343 slint_testing::mock_elapsed_time(300);
344 assert!(instance.get_inner_ta_pressed()); // only now we are pressed
345 assert!(instance.get_inner_ta_has_hover());
346 assert_eq!(instance.get_clicked(), -1);
347 assert_eq!(instance.get_offset_x(), 0.);
348 assert_eq!(instance.get_offset_y(), 0.);
349
350 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 120.0) });
351 assert!(!instance.get_inner_ta_pressed()); // We should no longer be pressed
352 // no hover when the flickable is flicking
353 assert!(!instance.get_inner_ta_has_hover());
354 assert_eq!(instance.get_clicked(), -1); // not clicked
355 assert_eq!(instance.get_offset_x(), 75.);
356 assert_eq!(instance.get_offset_y(), 55.);
357
358 slint_testing::mock_elapsed_time(10000);
359
360 instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 120.0), button: PointerEventButton::Left });
361 assert!(!instance.get_inner_ta_pressed());
362 assert!(instance.get_inner_ta_has_hover());
363 assert_eq!(instance.get_clicked(), -1); // not clicked
364 assert_eq!(instance.get_offset_x(), 75.);
365 assert_eq!(instance.get_offset_y(), 55.);
366
367 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(200.0, 50.0) });
368 slint_testing::mock_elapsed_time(1000);
369
370 assert!(!instance.get_inner_ta_pressed());
371 assert!(!instance.get_inner_ta_has_hover());
372 assert_eq!(instance.get_clicked(), -1);
373 assert!((instance.get_offset_x() - 75.).abs() < 5.); // only small animation on release
374 assert!((instance.get_offset_y() - 55.).abs() < 5.);
375
376 Ok(())
377}
378#[test] fn t_2() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
379 use i_slint_backend_testing as slint_testing;
380 slint_testing::init();
381 // Test wheel events
382 use slint::{LogicalPosition, platform::{WindowEvent, Key} };
383 let instance = TestCase::new().unwrap();
384 instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(x:175.0, y:175.0), delta_x: -30.0, delta_y: -50.0 });
385 assert_eq!(instance.get_offset_x(), 30.);
386 assert_eq!(instance.get_offset_y(), 50.);
387
388 // When shift is pressed, it invert the direction
389 // (this test don't work on macos because on macos the backend does the inversion, not the core lib)
390 if !cfg!(target_os = "macos") {
391 slint_testing::send_keyboard_char(&instance, string:Key::Shift.into(), pressed:true);
392 instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(x:175.0, y:175.0), delta_x: 15.0, delta_y: -60.0 });
393 slint_testing::send_keyboard_char(&instance, string:Key::Shift.into(), pressed:false);
394 assert_eq!(instance.get_offset_x(), 30. + 60.);
395 assert_eq!(instance.get_offset_y(), 50. - 15.);
396 }
397 Ok(())
398}
399#[test] fn t_3() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
400 use i_slint_backend_testing as slint_testing;
401 slint_testing::init();
402 // Test flicked-Callback behaviour
403 use slint::{LogicalPosition, platform::{WindowEvent, PointerEventButton} };
404 let instance = TestCase::new().unwrap();
405 assert_eq!(instance.get_flicked(), 0);
406
407 // test scrolling behaviour
408 instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(175.0, 175.0), delta_x: -30.0, delta_y: -50.0 });
409 dbg!(instance.get_flicked());
410 assert_eq!(instance.get_flicked(), -3000050); //flicked got called after scrolling
411 instance.set_flicked(0);
412
413 // test dragging bevaviour
414 instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left });
415 slint_testing::mock_elapsed_time(300);
416 assert_eq!(instance.get_flicked(), 0); //flicked didn't get called by just pressing
417 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 120.0) });
418 slint_testing::mock_elapsed_time(10000);
419 assert_eq!(instance.get_flicked(), -10500105); //flicked got called during drag
420 instance.set_flicked(0);
421 instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 120.0), button: PointerEventButton::Left });
422 assert_eq!(instance.get_flicked(), -10500105); //flicked got called after drag
423 instance.set_flicked(0);
424
425 Ok(())
426}