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 | |
5 | export 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 |
98 | use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition}; |
99 | |
100 | let instance = TestCase::new().unwrap(); |
101 | |
102 | assert_eq!(instance.get_click_count(), 0); |
103 | assert_eq!(instance.get_popup_created(), false); |
104 | |
105 | // --------- Default popup |
106 | instance.set_popup_selector(0); |
107 | instance.set_popup_created(false); |
108 | instance.set_click_count(0); |
109 | slint_testing::send_mouse_click(&instance, 15., 15.); |
110 | assert_eq!(instance.get_click_count(), 1); |
111 | assert_eq!(instance.get_popup_created(), true); |
112 | assert_eq!(instance.get_popup_clicked(), 0); |
113 | |
114 | // Click to close |
115 | slint_testing::send_mouse_click(&instance, 15., 15.); |
116 | assert_eq!(instance.get_click_count(), 1); |
117 | // Subsequent click to verify that it was closed |
118 | slint_testing::send_mouse_click(&instance, 15., 15.); |
119 | assert_eq!(instance.get_click_count(), 2); |
120 | assert_eq!(instance.get_popup_clicked(), 1); |
121 | |
122 | // --------- Default popup but verify closed on press when outside |
123 | instance.set_popup_selector(0); |
124 | instance.set_popup_created(false); |
125 | instance.set_click_count(0); |
126 | |
127 | slint_testing::send_mouse_click(&instance, 15., 15.); |
128 | assert_eq!(instance.get_last_underneath_mouse_x(), 15.); |
129 | assert_eq!(instance.get_last_underneath_mouse_y(), 15.); |
130 | assert_eq!(instance.get_click_count(), 1); |
131 | assert_eq!(instance.get_popup_created(), true); |
132 | assert_eq!(instance.get_popup_clicked(), 1); |
133 | |
134 | // mouse grabbed, underneath won't notice |
135 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(1.0, 1.0) }); |
136 | assert_eq!(instance.get_last_underneath_mouse_x(), 15.); |
137 | assert_eq!(instance.get_last_underneath_mouse_y(), 15.); |
138 | |
139 | // press should close |
140 | instance.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 |
143 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(12.0, 12.0) }); |
144 | assert_eq!(instance.get_last_underneath_mouse_x(), 12.); |
145 | assert_eq!(instance.get_last_underneath_mouse_y(), 12.); |
146 | |
147 | slint_testing::mock_elapsed_time(50); |
148 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(12.0, 12.0), button: PointerEventButton::Left }); |
149 | |
150 | assert_eq!(instance.get_click_count(), 1); |
151 | |
152 | // Subsequent click to verify that it was closed |
153 | slint_testing::send_mouse_click(&instance, 15., 15.); |
154 | assert_eq!(instance.get_click_count(), 2); |
155 | assert_eq!(instance.get_popup_clicked(), 1); |
156 | |
157 | // --------- Popup with close-on-click: false |
158 | instance.set_popup_selector(1); |
159 | instance.set_popup_created(false); |
160 | instance.set_click_count(0); |
161 | |
162 | slint_testing::send_mouse_click(&instance, 15., 15.); |
163 | assert_eq!(instance.get_click_count(), 1); |
164 | assert_eq!(instance.get_popup_created(), true); |
165 | assert_eq!(instance.get_popup_clicked(), 1); |
166 | |
167 | // Click outside, nothing happens |
168 | slint_testing::send_mouse_click(&instance, 1., 1.); |
169 | assert_eq!(instance.get_click_count(), 1); |
170 | // Click outside again, nothing happens |
171 | slint_testing::send_mouse_click(&instance, 295., 295.); |
172 | assert_eq!(instance.get_click_count(), 1); |
173 | assert_eq!(instance.get_popup_clicked(), 1); |
174 | |
175 | // Click on the popup, it's registered and the custom TouchArea calls close() |
176 | slint_testing::send_mouse_click(&instance, 15., 15.); |
177 | assert_eq!(instance.get_click_count(), 1); |
178 | |
179 | // Subsequent click to verify that it was closed |
180 | slint_testing::send_mouse_click(&instance, 15., 15.); |
181 | assert_eq!(instance.get_click_count(), 2); |
182 | assert_eq!(instance.get_popup_clicked(), 1); |
183 | |
184 | // --------- Popup with close-on-click: false closed externally |
185 | instance.set_popup_selector(2); |
186 | instance.set_popup_created(false); |
187 | instance.set_click_count(0); |
188 | slint_testing::send_mouse_click(&instance, 15., 15.); |
189 | assert_eq!(instance.get_click_count(), 1); |
190 | assert_eq!(instance.get_popup_created(), true); |
191 | assert_eq!(instance.get_popup_clicked(), 1); |
192 | |
193 | // Click outside, nothing happens |
194 | slint_testing::send_mouse_click(&instance, 1., 1.); |
195 | assert_eq!(instance.get_click_count(), 1); |
196 | assert_eq!(instance.get_popup_clicked(), 1); |
197 | |
198 | // Click on the popup, it's registered but nothing is done |
199 | slint_testing::send_mouse_click(&instance, 15., 15.); |
200 | assert_eq!(instance.get_click_count(), 1); |
201 | assert_eq!(instance.get_popup_clicked(), 1001); |
202 | // Click again to verify that it was _not_ closed |
203 | slint_testing::send_mouse_click(&instance, 15., 15.); |
204 | assert_eq!(instance.get_click_count(), 1); |
205 | assert_eq!(instance.get_popup_clicked(), 2001); |
206 | |
207 | // Close manually and verify that subsequent click is passed through |
208 | instance.invoke_do_close(); |
209 | slint_testing::send_mouse_click(&instance, 15., 15.); |
210 | assert_eq!(instance.get_click_count(), 2); |
211 | instance.invoke_do_close(); |
212 | slint_testing::send_mouse_click(&instance, 15., 15.); |
213 | assert_eq!(instance.get_click_count(), 3); |
214 | assert_eq!(instance.get_popup_clicked(), 2001); |
215 | |
216 | |
217 | ``` |
218 | |
219 | ```cpp |
220 | auto handle = TestCase::create(); |
221 | const TestCase &instance = *handle; |
222 | |
223 | assert_eq(instance.get_click_count(), 0); |
224 | assert_eq(instance.get_popup_created(), false); |
225 | |
226 | // --------- Default popup |
227 | instance.set_popup_selector(0); |
228 | instance.set_popup_created(false); |
229 | instance.set_click_count(0); |
230 | slint_testing::send_mouse_click(&instance, 15., 15.); |
231 | assert_eq(instance.get_click_count(), 1); |
232 | assert_eq(instance.get_popup_created(), true); |
233 | assert_eq(instance.get_popup_clicked(), 0); |
234 | |
235 | // Click to close |
236 | slint_testing::send_mouse_click(&instance, 15., 15.); |
237 | assert_eq(instance.get_click_count(), 1); |
238 | // Subsequent click to verify that it was closed |
239 | slint_testing::send_mouse_click(&instance, 15., 15.); |
240 | assert_eq(instance.get_click_count(), 2); |
241 | assert_eq(instance.get_popup_clicked(), 1); |
242 | |
243 | // --------- Default popup but verify closed on press when outside |
244 | instance.set_popup_selector(0); |
245 | instance.set_popup_created(false); |
246 | instance.set_click_count(0); |
247 | |
248 | slint_testing::send_mouse_click(&instance, 15., 15.); |
249 | assert_eq(instance.get_last_underneath_mouse_x(), 15.); |
250 | assert_eq(instance.get_last_underneath_mouse_y(), 15.); |
251 | assert_eq(instance.get_click_count(), 1); |
252 | assert_eq(instance.get_popup_created(), true); |
253 | assert_eq(instance.get_popup_clicked(), 1); |
254 | |
255 | // mouse grabbed, underneath won't notice |
256 | instance.window().dispatch_pointer_move_event(slint::LogicalPosition({1.0, 1.0})); |
257 | assert_eq(instance.get_last_underneath_mouse_x(), 15.); |
258 | assert_eq(instance.get_last_underneath_mouse_y(), 15.); |
259 | |
260 | // press should close |
261 | instance.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 |
264 | instance.window().dispatch_pointer_move_event(slint::LogicalPosition({12.0, 12.0})); |
265 | assert_eq(instance.get_last_underneath_mouse_x(), 12.); |
266 | assert_eq(instance.get_last_underneath_mouse_y(), 12.); |
267 | |
268 | slint_testing::mock_elapsed_time(50); |
269 | instance.window().dispatch_pointer_release_event(slint::LogicalPosition({12.0, 12.0}), slint::PointerEventButton::Left); |
270 | |
271 | assert_eq(instance.get_click_count(), 1); |
272 | |
273 | // Subsequent click to verify that it was closed |
274 | slint_testing::send_mouse_click(&instance, 15., 15.); |
275 | assert_eq(instance.get_click_count(), 2); |
276 | assert_eq(instance.get_popup_clicked(), 1); |
277 | |
278 | // --------- Popup with close-on-click: false |
279 | instance.set_popup_selector(1); |
280 | instance.set_popup_created(false); |
281 | instance.set_click_count(0); |
282 | |
283 | slint_testing::send_mouse_click(&instance, 15., 15.); |
284 | assert_eq(instance.get_click_count(), 1); |
285 | assert_eq(instance.get_popup_created(), true); |
286 | assert_eq(instance.get_popup_clicked(), 1); |
287 | |
288 | // Click outside, nothing happens |
289 | slint_testing::send_mouse_click(&instance, 1., 1.); |
290 | assert_eq(instance.get_click_count(), 1); |
291 | // Click outside again, nothing happens |
292 | slint_testing::send_mouse_click(&instance, 295., 295.); |
293 | assert_eq(instance.get_click_count(), 1); |
294 | assert_eq(instance.get_popup_clicked(), 1); |
295 | |
296 | // Click on the popup, it's registered and the custom TouchArea calls close() |
297 | slint_testing::send_mouse_click(&instance, 15., 15.); |
298 | assert_eq(instance.get_click_count(), 1); |
299 | |
300 | // Subsequent click to verify that it was closed |
301 | slint_testing::send_mouse_click(&instance, 15., 15.); |
302 | assert_eq(instance.get_click_count(), 2); |
303 | assert_eq(instance.get_popup_clicked(), 1); |
304 | |
305 | // --------- Popup with close-on-click: false closed externally |
306 | instance.set_popup_selector(2); |
307 | instance.set_popup_created(false); |
308 | instance.set_click_count(0); |
309 | slint_testing::send_mouse_click(&instance, 15., 15.); |
310 | assert_eq(instance.get_click_count(), 1); |
311 | assert_eq(instance.get_popup_created(), true); |
312 | assert_eq(instance.get_popup_clicked(), 1); |
313 | |
314 | // Click outside, nothing happens |
315 | slint_testing::send_mouse_click(&instance, 1., 1.); |
316 | assert_eq(instance.get_click_count(), 1); |
317 | assert_eq(instance.get_popup_clicked(), 1); |
318 | |
319 | // Click on the popup, it's registered but nothing is done |
320 | slint_testing::send_mouse_click(&instance, 15., 15.); |
321 | assert_eq(instance.get_click_count(), 1); |
322 | assert_eq(instance.get_popup_clicked(), 1001); |
323 | |
324 | // Click again to verify that it was _not_ closed |
325 | slint_testing::send_mouse_click(&instance, 15., 15.); |
326 | assert_eq(instance.get_click_count(), 1); |
327 | assert_eq(instance.get_popup_clicked(), 2001); |
328 | |
329 | // Close manually and verify that subsequent click is passed through |
330 | instance.invoke_do_close(); |
331 | slint_testing::send_mouse_click(&instance, 15., 15.); |
332 | assert_eq(instance.get_click_count(), 2); |
333 | instance.invoke_do_close(); |
334 | slint_testing::send_mouse_click(&instance, 15., 15.); |
335 | assert_eq(instance.get_click_count(), 3); |
336 | assert_eq(instance.get_popup_clicked(), 2001); |
337 | ``` |
338 | |
339 | ```disable-because-nodejs-runs-with-qt-and-send-mouse-click-wont-send-to-popup-qwindow |
340 | var instance = new slint.TestCase({}); |
341 | |
342 | assert.equal(instance.click_count, 0); |
343 | assert.equal(instance.popup_created, false); |
344 | |
345 | instance.popup_selector = 0; |
346 | instance.popup_created = false; |
347 | instance.click_count = 0; |
348 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
349 | assert.equal(instance.click_count, 1); |
350 | assert.equal(instance.popup_created, true); |
351 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
352 | assert.equal(instance.click_count, 1); |
353 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
354 | assert.equal(instance.click_count, 2); |
355 | |
356 | instance.popup_selector = 1; |
357 | instance.popup_created = false; |
358 | instance.click_count = 0; |
359 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
360 | assert.equal(instance.click_count, 1); |
361 | assert.equal(instance.popup_created, true); |
362 | slintlib.private_api.send_mouse_click(instance, 1., 1.); |
363 | assert.equal(instance.click_count, 1); |
364 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
365 | assert.equal(instance.click_count, 1); |
366 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
367 | assert.equal(instance.click_count, 2); |
368 | |
369 | instance.popup_selector = 2; |
370 | instance.popup_created = false; |
371 | instance.click_count = 0; |
372 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
373 | assert.equal(instance.click_count, 1); |
374 | assert.equal(instance.popup_created, true); |
375 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
376 | assert.equal(instance.click_count, 1); |
377 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
378 | assert.equal(instance.click_count, 1); |
379 | |
380 | instance.do_close(); |
381 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
382 | assert.equal(instance.click_count, 2); |
383 | instance.do_close(); |
384 | slintlib.private_api.send_mouse_click(instance, 15., 15.); |
385 | assert.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 | } |