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 width: 300px;
7 height: 300px;
8
9 in-out property <bool> popup-created;
10 in-out property <int> click-count;
11 in-out property <int> popup-selector: 0;
12 in-out property <int> popup-clicked;
13 out property <length> last-underneath-mouse-x: ta.mouse-x;
14 out property <length> last-underneath-mouse-y: ta.mouse-y;
15
16 callback do-close;
17 do-close => {
18 close-from-outside.close();
19 }
20
21 default-popup := PopupWindow {
22 x: 10px;
23 y: 10px;
24 width: parent.width - 20px;
25 height: parent.height - 20px;
26 Text {
27 text: "I'm a default";
28 }
29 init => {
30 root.popup-created = true;
31 }
32
33 TouchArea {
34 width: 7px; x: 0px;
35 clicked => {
36 popup-clicked += 1;
37 }
38 }
39 }
40
41 self-closing-popup := PopupWindow {
42 close-on-click: false;
43 x: 10px;
44 y: 10px;
45 width: parent.width - 20px;
46 height: parent.height - 20px;
47 Text {
48 text: "I'm a self-closing popup";
49 }
50 TouchArea {
51 clicked => {
52 self-closing-popup.close();
53 }
54 }
55 init => {
56 root.popup-created = true;
57 }
58 }
59
60 close-from-outside := PopupWindow {
61 close-on-click: false;
62 x: 10px;
63 y: 10px;
64 width: parent.width - 20px;
65 height: parent.height - 20px;
66 Text {
67 text: "I'm a popup that only close from outside";
68 }
69 TouchArea {
70 clicked => {
71 popup-clicked += 1000;
72 }
73 }
74 init => {
75 root.popup-created = true;
76 }
77 }
78
79 ta := TouchArea {
80 clicked => {
81 root.click-count = root.click-count + 1;
82 if (root.popup-selector == 0) {
83 root.popup-selector = 3;
84 default-popup.show();
85 } else if (root.popup-selector == 1) {
86 root.popup-selector = 3;
87 self-closing-popup.show();
88 } else if (root.popup-selector == 2) {
89 root.popup-selector = 3;
90 close-from-outside.show();
91 }
92 }
93 }
94}
95/*
96
97```rust
98use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
99
100let instance = TestCase::new().unwrap();
101
102assert_eq!(instance.get_click_count(), 0);
103assert_eq!(instance.get_popup_created(), false);
104
105// --------- Default popup
106instance.set_popup_selector(0);
107instance.set_popup_created(false);
108instance.set_click_count(0);
109slint_testing::send_mouse_click(&instance, 15., 15.);
110assert_eq!(instance.get_click_count(), 1);
111assert_eq!(instance.get_popup_created(), true);
112assert_eq!(instance.get_popup_clicked(), 0);
113
114// Click to close
115slint_testing::send_mouse_click(&instance, 15., 15.);
116assert_eq!(instance.get_click_count(), 1);
117// Subsequent click to verify that it was closed
118slint_testing::send_mouse_click(&instance, 15., 15.);
119assert_eq!(instance.get_click_count(), 2);
120assert_eq!(instance.get_popup_clicked(), 1);
121
122// --------- Default popup but verify closed on press when outside
123instance.set_popup_selector(0);
124instance.set_popup_created(false);
125instance.set_click_count(0);
126
127slint_testing::send_mouse_click(&instance, 15., 15.);
128assert_eq!(instance.get_last_underneath_mouse_x(), 15.);
129assert_eq!(instance.get_last_underneath_mouse_y(), 15.);
130assert_eq!(instance.get_click_count(), 1);
131assert_eq!(instance.get_popup_created(), true);
132assert_eq!(instance.get_popup_clicked(), 1);
133
134// mouse grabbed, underneath won't notice
135instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(1.0, 1.0) });
136assert_eq!(instance.get_last_underneath_mouse_x(), 15.);
137assert_eq!(instance.get_last_underneath_mouse_y(), 15.);
138
139// press should close
140instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(1.0, 1.0), button: PointerEventButton::Left });
141
142// if it was closed, the underneath should receive the move event
143instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(12.0, 12.0) });
144assert_eq!(instance.get_last_underneath_mouse_x(), 12.);
145assert_eq!(instance.get_last_underneath_mouse_y(), 12.);
146
147slint_testing::mock_elapsed_time(50);
148instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(12.0, 12.0), button: PointerEventButton::Left });
149
150assert_eq!(instance.get_click_count(), 1);
151
152// Subsequent click to verify that it was closed
153slint_testing::send_mouse_click(&instance, 15., 15.);
154assert_eq!(instance.get_click_count(), 2);
155assert_eq!(instance.get_popup_clicked(), 1);
156
157// --------- Popup with close-on-click: false
158instance.set_popup_selector(1);
159instance.set_popup_created(false);
160instance.set_click_count(0);
161
162slint_testing::send_mouse_click(&instance, 15., 15.);
163assert_eq!(instance.get_click_count(), 1);
164assert_eq!(instance.get_popup_created(), true);
165assert_eq!(instance.get_popup_clicked(), 1);
166
167// Click outside, nothing happens
168slint_testing::send_mouse_click(&instance, 1., 1.);
169assert_eq!(instance.get_click_count(), 1);
170// Click outside again, nothing happens
171slint_testing::send_mouse_click(&instance, 295., 295.);
172assert_eq!(instance.get_click_count(), 1);
173assert_eq!(instance.get_popup_clicked(), 1);
174
175// Click on the popup, it's registered and the custom TouchArea calls close()
176slint_testing::send_mouse_click(&instance, 15., 15.);
177assert_eq!(instance.get_click_count(), 1);
178
179// Subsequent click to verify that it was closed
180slint_testing::send_mouse_click(&instance, 15., 15.);
181assert_eq!(instance.get_click_count(), 2);
182assert_eq!(instance.get_popup_clicked(), 1);
183
184// --------- Popup with close-on-click: false closed externally
185instance.set_popup_selector(2);
186instance.set_popup_created(false);
187instance.set_click_count(0);
188slint_testing::send_mouse_click(&instance, 15., 15.);
189assert_eq!(instance.get_click_count(), 1);
190assert_eq!(instance.get_popup_created(), true);
191assert_eq!(instance.get_popup_clicked(), 1);
192
193// Click outside, nothing happens
194slint_testing::send_mouse_click(&instance, 1., 1.);
195assert_eq!(instance.get_click_count(), 1);
196assert_eq!(instance.get_popup_clicked(), 1);
197
198// Click on the popup, it's registered but nothing is done
199slint_testing::send_mouse_click(&instance, 15., 15.);
200assert_eq!(instance.get_click_count(), 1);
201assert_eq!(instance.get_popup_clicked(), 1001);
202// Click again to verify that it was _not_ closed
203slint_testing::send_mouse_click(&instance, 15., 15.);
204assert_eq!(instance.get_click_count(), 1);
205assert_eq!(instance.get_popup_clicked(), 2001);
206
207// Close manually and verify that subsequent click is passed through
208instance.invoke_do_close();
209slint_testing::send_mouse_click(&instance, 15., 15.);
210assert_eq!(instance.get_click_count(), 2);
211instance.invoke_do_close();
212slint_testing::send_mouse_click(&instance, 15., 15.);
213assert_eq!(instance.get_click_count(), 3);
214assert_eq!(instance.get_popup_clicked(), 2001);
215
216
217```
218
219```cpp
220auto handle = TestCase::create();
221const TestCase &instance = *handle;
222
223assert_eq(instance.get_click_count(), 0);
224assert_eq(instance.get_popup_created(), false);
225
226// --------- Default popup
227instance.set_popup_selector(0);
228instance.set_popup_created(false);
229instance.set_click_count(0);
230slint_testing::send_mouse_click(&instance, 15., 15.);
231assert_eq(instance.get_click_count(), 1);
232assert_eq(instance.get_popup_created(), true);
233assert_eq(instance.get_popup_clicked(), 0);
234
235// Click to close
236slint_testing::send_mouse_click(&instance, 15., 15.);
237assert_eq(instance.get_click_count(), 1);
238// Subsequent click to verify that it was closed
239slint_testing::send_mouse_click(&instance, 15., 15.);
240assert_eq(instance.get_click_count(), 2);
241assert_eq(instance.get_popup_clicked(), 1);
242
243// --------- Default popup but verify closed on press when outside
244instance.set_popup_selector(0);
245instance.set_popup_created(false);
246instance.set_click_count(0);
247
248slint_testing::send_mouse_click(&instance, 15., 15.);
249assert_eq(instance.get_last_underneath_mouse_x(), 15.);
250assert_eq(instance.get_last_underneath_mouse_y(), 15.);
251assert_eq(instance.get_click_count(), 1);
252assert_eq(instance.get_popup_created(), true);
253assert_eq(instance.get_popup_clicked(), 1);
254
255// mouse grabbed, underneath won't notice
256instance.window().dispatch_pointer_move_event(slint::LogicalPosition({1.0, 1.0}));
257assert_eq(instance.get_last_underneath_mouse_x(), 15.);
258assert_eq(instance.get_last_underneath_mouse_y(), 15.);
259
260// press should close
261instance.window().dispatch_pointer_press_event(slint::LogicalPosition({1.0, 1.0}), slint::PointerEventButton::Left);
262
263// if it was closed, the underneath should receive the move event
264instance.window().dispatch_pointer_move_event(slint::LogicalPosition({12.0, 12.0}));
265assert_eq(instance.get_last_underneath_mouse_x(), 12.);
266assert_eq(instance.get_last_underneath_mouse_y(), 12.);
267
268slint_testing::mock_elapsed_time(50);
269instance.window().dispatch_pointer_release_event(slint::LogicalPosition({12.0, 12.0}), slint::PointerEventButton::Left);
270
271assert_eq(instance.get_click_count(), 1);
272
273// Subsequent click to verify that it was closed
274slint_testing::send_mouse_click(&instance, 15., 15.);
275assert_eq(instance.get_click_count(), 2);
276assert_eq(instance.get_popup_clicked(), 1);
277
278// --------- Popup with close-on-click: false
279instance.set_popup_selector(1);
280instance.set_popup_created(false);
281instance.set_click_count(0);
282
283slint_testing::send_mouse_click(&instance, 15., 15.);
284assert_eq(instance.get_click_count(), 1);
285assert_eq(instance.get_popup_created(), true);
286assert_eq(instance.get_popup_clicked(), 1);
287
288// Click outside, nothing happens
289slint_testing::send_mouse_click(&instance, 1., 1.);
290assert_eq(instance.get_click_count(), 1);
291// Click outside again, nothing happens
292slint_testing::send_mouse_click(&instance, 295., 295.);
293assert_eq(instance.get_click_count(), 1);
294assert_eq(instance.get_popup_clicked(), 1);
295
296// Click on the popup, it's registered and the custom TouchArea calls close()
297slint_testing::send_mouse_click(&instance, 15., 15.);
298assert_eq(instance.get_click_count(), 1);
299
300// Subsequent click to verify that it was closed
301slint_testing::send_mouse_click(&instance, 15., 15.);
302assert_eq(instance.get_click_count(), 2);
303assert_eq(instance.get_popup_clicked(), 1);
304
305// --------- Popup with close-on-click: false closed externally
306instance.set_popup_selector(2);
307instance.set_popup_created(false);
308instance.set_click_count(0);
309slint_testing::send_mouse_click(&instance, 15., 15.);
310assert_eq(instance.get_click_count(), 1);
311assert_eq(instance.get_popup_created(), true);
312assert_eq(instance.get_popup_clicked(), 1);
313
314// Click outside, nothing happens
315slint_testing::send_mouse_click(&instance, 1., 1.);
316assert_eq(instance.get_click_count(), 1);
317assert_eq(instance.get_popup_clicked(), 1);
318
319// Click on the popup, it's registered but nothing is done
320slint_testing::send_mouse_click(&instance, 15., 15.);
321assert_eq(instance.get_click_count(), 1);
322assert_eq(instance.get_popup_clicked(), 1001);
323
324// Click again to verify that it was _not_ closed
325slint_testing::send_mouse_click(&instance, 15., 15.);
326assert_eq(instance.get_click_count(), 1);
327assert_eq(instance.get_popup_clicked(), 2001);
328
329// Close manually and verify that subsequent click is passed through
330instance.invoke_do_close();
331slint_testing::send_mouse_click(&instance, 15., 15.);
332assert_eq(instance.get_click_count(), 2);
333instance.invoke_do_close();
334slint_testing::send_mouse_click(&instance, 15., 15.);
335assert_eq(instance.get_click_count(), 3);
336assert_eq(instance.get_popup_clicked(), 2001);
337```
338
339```disable-because-nodejs-runs-with-qt-and-send-mouse-click-wont-send-to-popup-qwindow
340var instance = new slint.TestCase({});
341
342assert.equal(instance.click_count, 0);
343assert.equal(instance.popup_created, false);
344
345instance.popup_selector = 0;
346instance.popup_created = false;
347instance.click_count = 0;
348slintlib.private_api.send_mouse_click(instance, 15., 15.);
349assert.equal(instance.click_count, 1);
350assert.equal(instance.popup_created, true);
351slintlib.private_api.send_mouse_click(instance, 15., 15.);
352assert.equal(instance.click_count, 1);
353slintlib.private_api.send_mouse_click(instance, 15., 15.);
354assert.equal(instance.click_count, 2);
355
356instance.popup_selector = 1;
357instance.popup_created = false;
358instance.click_count = 0;
359slintlib.private_api.send_mouse_click(instance, 15., 15.);
360assert.equal(instance.click_count, 1);
361assert.equal(instance.popup_created, true);
362slintlib.private_api.send_mouse_click(instance, 1., 1.);
363assert.equal(instance.click_count, 1);
364slintlib.private_api.send_mouse_click(instance, 15., 15.);
365assert.equal(instance.click_count, 1);
366slintlib.private_api.send_mouse_click(instance, 15., 15.);
367assert.equal(instance.click_count, 2);
368
369instance.popup_selector = 2;
370instance.popup_created = false;
371instance.click_count = 0;
372slintlib.private_api.send_mouse_click(instance, 15., 15.);
373assert.equal(instance.click_count, 1);
374assert.equal(instance.popup_created, true);
375slintlib.private_api.send_mouse_click(instance, 15., 15.);
376assert.equal(instance.click_count, 1);
377slintlib.private_api.send_mouse_click(instance, 15., 15.);
378assert.equal(instance.click_count, 1);
379
380instance.do_close();
381slintlib.private_api.send_mouse_click(instance, 15., 15.);
382assert.equal(instance.click_count, 2);
383instance.do_close();
384slintlib.private_api.send_mouse_click(instance, 15., 15.);
385assert.equal(instance.click_count, 3);
386```
387
388*/
389}
390
391#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
392 use i_slint_backend_testing as slint_testing;
393 slint_testing::init();
394 use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
395
396 let instance = TestCase::new().unwrap();
397
398 assert_eq!(instance.get_click_count(), 0);
399 assert_eq!(instance.get_popup_created(), false);
400
401 // --------- Default popup
402 instance.set_popup_selector(0);
403 instance.set_popup_created(false);
404 instance.set_click_count(0);
405 slint_testing::send_mouse_click(&instance, 15., 15.);
406 assert_eq!(instance.get_click_count(), 1);
407 assert_eq!(instance.get_popup_created(), true);
408 assert_eq!(instance.get_popup_clicked(), 0);
409
410 // Click to close
411 slint_testing::send_mouse_click(&instance, 15., 15.);
412 assert_eq!(instance.get_click_count(), 1);
413 // Subsequent click to verify that it was closed
414 slint_testing::send_mouse_click(&instance, 15., 15.);
415 assert_eq!(instance.get_click_count(), 2);
416 assert_eq!(instance.get_popup_clicked(), 1);
417
418 // --------- Default popup but verify closed on press when outside
419 instance.set_popup_selector(0);
420 instance.set_popup_created(false);
421 instance.set_click_count(0);
422
423 slint_testing::send_mouse_click(&instance, 15., 15.);
424 assert_eq!(instance.get_last_underneath_mouse_x(), 15.);
425 assert_eq!(instance.get_last_underneath_mouse_y(), 15.);
426 assert_eq!(instance.get_click_count(), 1);
427 assert_eq!(instance.get_popup_created(), true);
428 assert_eq!(instance.get_popup_clicked(), 1);
429
430 // mouse grabbed, underneath won't notice
431 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(1.0, 1.0) });
432 assert_eq!(instance.get_last_underneath_mouse_x(), 15.);
433 assert_eq!(instance.get_last_underneath_mouse_y(), 15.);
434
435 // press should close
436 instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(1.0, 1.0), button: PointerEventButton::Left });
437
438 // if it was closed, the underneath should receive the move event
439 instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(12.0, 12.0) });
440 assert_eq!(instance.get_last_underneath_mouse_x(), 12.);
441 assert_eq!(instance.get_last_underneath_mouse_y(), 12.);
442
443 slint_testing::mock_elapsed_time(50);
444 instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(12.0, 12.0), button: PointerEventButton::Left });
445
446 assert_eq!(instance.get_click_count(), 1);
447
448 // Subsequent click to verify that it was closed
449 slint_testing::send_mouse_click(&instance, 15., 15.);
450 assert_eq!(instance.get_click_count(), 2);
451 assert_eq!(instance.get_popup_clicked(), 1);
452
453 // --------- Popup with close-on-click: false
454 instance.set_popup_selector(1);
455 instance.set_popup_created(false);
456 instance.set_click_count(0);
457
458 slint_testing::send_mouse_click(&instance, 15., 15.);
459 assert_eq!(instance.get_click_count(), 1);
460 assert_eq!(instance.get_popup_created(), true);
461 assert_eq!(instance.get_popup_clicked(), 1);
462
463 // Click outside, nothing happens
464 slint_testing::send_mouse_click(&instance, 1., 1.);
465 assert_eq!(instance.get_click_count(), 1);
466 // Click outside again, nothing happens
467 slint_testing::send_mouse_click(&instance, 295., 295.);
468 assert_eq!(instance.get_click_count(), 1);
469 assert_eq!(instance.get_popup_clicked(), 1);
470
471 // Click on the popup, it's registered and the custom TouchArea calls close()
472 slint_testing::send_mouse_click(&instance, 15., 15.);
473 assert_eq!(instance.get_click_count(), 1);
474
475 // Subsequent click to verify that it was closed
476 slint_testing::send_mouse_click(&instance, 15., 15.);
477 assert_eq!(instance.get_click_count(), 2);
478 assert_eq!(instance.get_popup_clicked(), 1);
479
480 // --------- Popup with close-on-click: false closed externally
481 instance.set_popup_selector(2);
482 instance.set_popup_created(false);
483 instance.set_click_count(0);
484 slint_testing::send_mouse_click(&instance, 15., 15.);
485 assert_eq!(instance.get_click_count(), 1);
486 assert_eq!(instance.get_popup_created(), true);
487 assert_eq!(instance.get_popup_clicked(), 1);
488
489 // Click outside, nothing happens
490 slint_testing::send_mouse_click(&instance, 1., 1.);
491 assert_eq!(instance.get_click_count(), 1);
492 assert_eq!(instance.get_popup_clicked(), 1);
493
494 // Click on the popup, it's registered but nothing is done
495 slint_testing::send_mouse_click(&instance, 15., 15.);
496 assert_eq!(instance.get_click_count(), 1);
497 assert_eq!(instance.get_popup_clicked(), 1001);
498 // Click again to verify that it was _not_ closed
499 slint_testing::send_mouse_click(&instance, 15., 15.);
500 assert_eq!(instance.get_click_count(), 1);
501 assert_eq!(instance.get_popup_clicked(), 2001);
502
503 // Close manually and verify that subsequent click is passed through
504 instance.invoke_do_close();
505 slint_testing::send_mouse_click(&instance, 15., 15.);
506 assert_eq!(instance.get_click_count(), 2);
507 instance.invoke_do_close();
508 slint_testing::send_mouse_click(&instance, 15., 15.);
509 assert_eq!(instance.get_click_count(), 3);
510 assert_eq!(instance.get_popup_clicked(), 2001);
511
512
513 Ok(())
514}