Debian Codegraph @

tests/scene/test_viewport.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_viewport.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "scene/2d/node_2d.h"
34 #include "scene/gui/control.h"
35 #include "scene/gui/subviewport_container.h"
36 #include "scene/main/canvas_layer.h"
37 #include "scene/main/window.h"
38
39 #ifndef PHYSICS_2D_DISABLED
40 #include "scene/2d/physics/area_2d.h"
41 #include "scene/2d/physics/collision_shape_2d.h"
42 #include "scene/resources/2d/rectangle_shape_2d.h"
43 #include "servers/physics_2d/physics_server_2d_dummy.h"
44 #endif // PHYSICS_2D_DISABLED
45
46 #include "tests/test_macros.h"
47
48 namespace TestViewport {
49
50 class NotificationControlViewport : public Control {
51 GDCLASS(NotificationControlViewport, Control);
52
53 protected:
54 void _notification(int p_what) {
55 switch (p_what) {
56 case NOTIFICATION_MOUSE_ENTER: {
57 if (mouse_over) {
58 invalid_order = true;
59 }
60 mouse_over = true;
61 } break;
62
63 case NOTIFICATION_MOUSE_EXIT: {
64 if (!mouse_over) {
65 invalid_order = true;
66 }
67 mouse_over = false;
68 } break;
69
70 case NOTIFICATION_MOUSE_ENTER_SELF: {
71 if (mouse_over_self) {
72 invalid_order = true;
73 }
74 mouse_over_self = true;
75 } break;
76
77 case NOTIFICATION_MOUSE_EXIT_SELF: {
78 if (!mouse_over_self) {
79 invalid_order = true;
80 }
81 mouse_over_self = false;
82 } break;
83 }
84 }
85
86 public:
87 bool mouse_over = false;
88 bool mouse_over_self = false;
89 bool invalid_order = false;
90 };
91
92 // `NotificationControlViewport`-derived class that additionally
93 // - allows start Dragging
94 // - stores mouse information of last event
95 class DragStart : public NotificationControlViewport {
96 GDCLASS(DragStart, NotificationControlViewport);
97
98 public:
99 MouseButton last_mouse_button;
100 Point2i last_mouse_move_position;
101 StringName drag_data_name = SNAME("Drag Data");
102
103 virtual Variant get_drag_data(const Point2 &p_point) override {
104 return drag_data_name;
105 }
106
107 virtual void gui_input(const Ref<InputEvent> &p_event) override {
108 Ref<InputEventMouseButton> mb = p_event;
109 if (mb.is_valid()) {
110 last_mouse_button = mb->get_button_index();
111 return;
112 }
113
114 Ref<InputEventMouseMotion> mm = p_event;
115 if (mm.is_valid()) {
116 last_mouse_move_position = mm->get_position();
117 return;
118 }
119 }
120 };
121
122 // `NotificationControlViewport`-derived class that acts as a Drag and Drop target.
123 class DragTarget : public NotificationControlViewport {
124 GDCLASS(DragTarget, NotificationControlViewport);
125
126 protected:
127 void _notification(int p_what) {
128 switch (p_what) {
129 case NOTIFICATION_DRAG_BEGIN: {
130 during_drag = true;
131 } break;
132
133 case NOTIFICATION_DRAG_END: {
134 during_drag = false;
135 } break;
136 }
137 }
138
139 public:
140 Variant drag_data;
141 bool valid_drop = false;
142 bool during_drag = false;
143 virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override {
144 StringName string_data = p_data;
145 // Verify drag data is compatible.
146 if (string_data != SNAME("Drag Data")) {
147 return false;
148 }
149 // Only the left half is droppable area.
150 if (p_point.x * 2 > get_size().x) {
151 return false;
152 }
153 return true;
154 }
155
156 virtual void drop_data(const Point2 &p_point, const Variant &p_data) override {
157 drag_data = p_data;
158 valid_drop = true;
159 }
160 };
161
162 TEST_CASE("[SceneTree][Viewport] Controls and InputEvent handling") {
163 DragStart *node_a = memnew(DragStart);
164 NotificationControlViewport *node_b = memnew(NotificationControlViewport);
165 Node2D *node_c = memnew(Node2D);
166 DragTarget *node_d = memnew(DragTarget);
167 NotificationControlViewport *node_e = memnew(NotificationControlViewport);
168 Node *node_f = memnew(Node);
169 NotificationControlViewport *node_g = memnew(NotificationControlViewport);
170 NotificationControlViewport *node_h = memnew(NotificationControlViewport);
171 NotificationControlViewport *node_i = memnew(NotificationControlViewport);
172 NotificationControlViewport *node_j = memnew(NotificationControlViewport);
173
174 node_a->set_name(SNAME("NodeA"));
175 node_b->set_name(SNAME("NodeB"));
176 node_c->set_name(SNAME("NodeC"));
177 node_d->set_name(SNAME("NodeD"));
178 node_e->set_name(SNAME("NodeE"));
179 node_f->set_name(SNAME("NodeF"));
180 node_g->set_name(SNAME("NodeG"));
181 node_h->set_name(SNAME("NodeH"));
182 node_i->set_name(SNAME("NodeI"));
183 node_j->set_name(SNAME("NodeJ"));
184
185 node_a->set_position(Point2i(0, 0));
186 node_b->set_position(Point2i(10, 10));
187 node_c->set_position(Point2i(0, 0));
188 node_d->set_position(Point2i(10, 10));
189 node_e->set_position(Point2i(10, 100));
190 node_g->set_position(Point2i(10, 100));
191 node_h->set_position(Point2i(10, 120));
192 node_i->set_position(Point2i(2, 0));
193 node_j->set_position(Point2i(2, 0));
194 node_a->set_size(Point2i(30, 30));
195 node_b->set_size(Point2i(30, 30));
196 node_d->set_size(Point2i(30, 30));
197 node_e->set_size(Point2i(10, 10));
198 node_g->set_size(Point2i(10, 10));
199 node_h->set_size(Point2i(10, 10));
200 node_i->set_size(Point2i(10, 10));
201 node_j->set_size(Point2i(10, 10));
202 node_a->set_focus_mode(Control::FOCUS_CLICK);
203 node_b->set_focus_mode(Control::FOCUS_CLICK);
204 node_d->set_focus_mode(Control::FOCUS_CLICK);
205 node_e->set_focus_mode(Control::FOCUS_CLICK);
206 node_g->set_focus_mode(Control::FOCUS_CLICK);
207 node_h->set_focus_mode(Control::FOCUS_CLICK);
208 node_i->set_focus_mode(Control::FOCUS_CLICK);
209 node_j->set_focus_mode(Control::FOCUS_CLICK);
210 Window *root = SceneTree::get_singleton()->get_root();
211 DisplayServerMock *DS = (DisplayServerMock *)(DisplayServer::get_singleton());
212
213 // Scene tree:
214 // - root
215 // - a (Control)
216 // - b (Control)
217 // - c (Node2D)
218 // - d (Control)
219 // - e (Control)
220 // - f (Node)
221 // - g (Control)
222 // - h (Control)
223 // - i (Control)
224 // - j (Control)
225 root->add_child(node_a);
226 root->add_child(node_b);
227 node_b->add_child(node_c);
228 node_c->add_child(node_d);
229 root->add_child(node_e);
230 node_e->add_child(node_f);
231 node_f->add_child(node_g);
232 root->add_child(node_h);
233 node_h->add_child(node_i);
234 node_i->add_child(node_j);
235
236 Point2i on_a = Point2i(5, 5);
237 Point2i on_b = Point2i(15, 15);
238 Point2i on_d = Point2i(25, 25);
239 Point2i on_e = Point2i(15, 105);
240 Point2i on_g = Point2i(15, 105);
241 Point2i on_i = Point2i(13, 125);
242 Point2i on_j = Point2i(15, 125);
243 Point2i on_background = Point2i(500, 500);
244 Point2i on_outside = Point2i(-1, -1);
245
246 // Unit tests for Viewport::gui_find_control and Viewport::_gui_find_control_at_pos
247 SUBCASE("[VIEWPORT][GuiFindControl] Finding Controls at a Viewport-position") {
248 // FIXME: It is extremely difficult to create a situation where the Control has a zero determinant.
249 // Leaving that if-branch untested.
250
251 SUBCASE("[VIEWPORT][GuiFindControl] Basic position tests") {
252 CHECK(root->gui_find_control(on_a) == node_a);
253 CHECK(root->gui_find_control(on_b) == node_b);
254 CHECK(root->gui_find_control(on_d) == node_d);
255 CHECK(root->gui_find_control(on_e) == node_g); // Node F makes G a Root Control at the same position as E
256 CHECK(root->gui_find_control(on_g) == node_g);
257 CHECK_FALSE(root->gui_find_control(on_background));
258 }
259
260 SUBCASE("[VIEWPORT][GuiFindControl] Invisible nodes are not considered as results.") {
261 // Non-Root Control
262 node_d->hide();
263 CHECK(root->gui_find_control(on_d) == node_b);
264 // Root Control
265 node_b->hide();
266 CHECK(root->gui_find_control(on_b) == node_a);
267 }
268
269 SUBCASE("[VIEWPORT][GuiFindControl] Root Control with CanvasItem as parent is affected by parent's transform.") {
270 node_b->remove_child(node_c);
271 node_c->set_position(Point2i(50, 50));
272 root->add_child(node_c);
273 CHECK(root->gui_find_control(Point2i(65, 65)) == node_d);
274 }
275
276 SUBCASE("[VIEWPORT][GuiFindControl] Control Contents Clipping clips accessible position of children.") {
277 CHECK_FALSE(node_b->is_clipping_contents());
278 CHECK(root->gui_find_control(on_d + Point2i(20, 20)) == node_d);
279 node_b->set_clip_contents(true);
280 CHECK(root->gui_find_control(on_d) == node_d);
281 CHECK_FALSE(root->gui_find_control(on_d + Point2i(20, 20)));
282 }
283
284 SUBCASE("[VIEWPORT][GuiFindControl] Top Level Control as descendant of CanvasItem isn't affected by parent's transform.") {
285 CHECK(root->gui_find_control(on_d + Point2i(20, 20)) == node_d);
286 node_d->set_as_top_level(true);
287 CHECK_FALSE(root->gui_find_control(on_d + Point2i(20, 20)));
288 CHECK(root->gui_find_control(on_b) == node_d);
289 }
290 }
291
292 SUBCASE("[Viewport][GuiInputEvent] nullptr as argument doesn't lead to a crash.") {
293 ERR_PRINT_OFF;
294 root->push_input(Ref<InputEvent>());
295 ERR_PRINT_ON;
296 }
297
298 // Unit tests for Viewport::_gui_input_event (Mouse Buttons)
299 SUBCASE("[Viewport][GuiInputEvent] Mouse Button Down/Up.") {
300 SUBCASE("[Viewport][GuiInputEvent] Mouse Button Control Focus Change.") {
301 SUBCASE("[Viewport][GuiInputEvent] Grab Focus while no Control has focus.") {
302 CHECK_FALSE(root->gui_get_focus_owner());
303
304 // Click on A
305 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
306 CHECK(node_a->has_focus());
307 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
308 }
309
310 SUBCASE("[Viewport][GuiInputEvent] Grab Focus from other Control.") {
311 node_a->grab_focus();
312 CHECK(node_a->has_focus());
313
314 // Click on D
315 SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
316 CHECK(node_d->has_focus());
317 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
318 }
319
320 SUBCASE("[Viewport][GuiInputEvent] Non-CanvasItem breaks Transform hierarchy.") {
321 CHECK_FALSE(root->gui_get_focus_owner());
322
323 // Click on G absolute coordinates
324 SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(15, 105), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
325 CHECK(node_g->has_focus());
326 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(Point2i(15, 105), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
327 }
328
329 SUBCASE("[Viewport][GuiInputEvent] No Focus change when clicking in background.") {
330 CHECK_FALSE(root->gui_get_focus_owner());
331
332 SEND_GUI_MOUSE_BUTTON_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
333 CHECK_FALSE(root->gui_get_focus_owner());
334 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
335
336 node_a->grab_focus();
337 CHECK(node_a->has_focus());
338
339 SEND_GUI_MOUSE_BUTTON_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
340 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
341 CHECK(node_a->has_focus());
342 }
343
344 SUBCASE("[Viewport][GuiInputEvent] Mouse Button No Focus Steal while other Mouse Button is pressed.") {
345 CHECK_FALSE(root->gui_get_focus_owner());
346
347 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
348 CHECK(node_a->has_focus());
349
350 SEND_GUI_MOUSE_BUTTON_EVENT(on_b, MouseButton::RIGHT, MouseButtonMask::LEFT | MouseButtonMask::RIGHT, Key::NONE);
351 CHECK(node_a->has_focus());
352
353 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_b, MouseButton::RIGHT, MouseButtonMask::LEFT, Key::NONE);
354 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
355 CHECK(node_a->has_focus());
356 }
357
358 SUBCASE("[Viewport][GuiInputEvent] Allow Focus Steal with LMB while other Mouse Button is held down and was initially pressed without being over a Control.") {
359 // TODO: Not sure, if this is intended behavior, but this is an edge case.
360 CHECK_FALSE(root->gui_get_focus_owner());
361
362 SEND_GUI_MOUSE_BUTTON_EVENT(on_background, MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
363 CHECK_FALSE(root->gui_get_focus_owner());
364
365 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT | MouseButtonMask::RIGHT, Key::NONE);
366 CHECK(node_a->has_focus());
367 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::RIGHT, Key::NONE);
368 CHECK(node_a->has_focus());
369
370 SEND_GUI_MOUSE_BUTTON_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::LEFT | MouseButtonMask::RIGHT, Key::NONE);
371 CHECK(node_b->has_focus());
372
373 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::RIGHT, Key::NONE);
374 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::RIGHT, MouseButtonMask::NONE, Key::NONE);
375 CHECK(node_b->has_focus());
376 }
377
378 SUBCASE("[Viewport][GuiInputEvent] Ignore Focus from Mouse Buttons when mouse-filter is set to ignore.") {
379 node_d->grab_focus();
380 node_d->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
381 CHECK(node_d->has_focus());
382
383 // Click on overlapping area B&D.
384 SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
385 CHECK(node_b->has_focus());
386 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
387 }
388
389 SUBCASE("[Viewport][GuiInputEvent] RMB doesn't grab focus.") {
390 node_a->grab_focus();
391 CHECK(node_a->has_focus());
392
393 SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
394 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::RIGHT, MouseButtonMask::NONE, Key::NONE);
395 CHECK(node_a->has_focus());
396 }
397
398 SUBCASE("[Viewport][GuiInputEvent] LMB on unfocusable Control doesn't grab focus.") {
399 CHECK_FALSE(node_g->has_focus());
400 node_g->set_focus_mode(Control::FOCUS_NONE);
401
402 SEND_GUI_MOUSE_BUTTON_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
403 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
404 CHECK_FALSE(node_g->has_focus());
405
406 // Now verify the opposite with FOCUS_CLICK
407 node_g->set_focus_mode(Control::FOCUS_CLICK);
408 SEND_GUI_MOUSE_BUTTON_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
409 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
410 CHECK(node_g->has_focus());
411 node_g->set_focus_mode(Control::FOCUS_CLICK);
412 }
413
414 SUBCASE("[Viewport][GuiInputEvent] Signal 'gui_focus_changed' is only emitted if a previously unfocused Control grabs focus.") {
415 SIGNAL_WATCH(root, SNAME("gui_focus_changed"));
416 Array signal_args = { { node_a } };
417
418 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
419 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
420 SIGNAL_CHECK(SNAME("gui_focus_changed"), signal_args);
421
422 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
423 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
424 CHECK(node_a->has_focus());
425 SIGNAL_CHECK_FALSE(SNAME("gui_focus_changed"));
426
427 SIGNAL_UNWATCH(root, SNAME("gui_focus_changed"));
428 }
429
430 SUBCASE("[Viewport][GuiInputEvent] Focus Propagation to parent items.") {
431 SUBCASE("[Viewport][GuiInputEvent] Unfocusable Control with MOUSE_FILTER_PASS propagates focus to parent CanvasItem.") {
432 node_d->set_focus_mode(Control::FOCUS_NONE);
433 node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
434
435 SEND_GUI_MOUSE_BUTTON_EVENT(on_d + Point2i(20, 20), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
436 CHECK(node_b->has_focus());
437 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d + Point2i(20, 20), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
438
439 // Verify break condition for Root Control.
440 node_a->set_focus_mode(Control::FOCUS_NONE);
441 node_a->set_mouse_filter(Control::MOUSE_FILTER_PASS);
442
443 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
444 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
445 CHECK(node_b->has_focus());
446 }
447
448 SUBCASE("[Viewport][GuiInputEvent] Top Level CanvasItem stops focus propagation.") {
449 node_d->set_focus_mode(Control::FOCUS_NONE);
450 node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
451 node_c->set_as_top_level(true);
452
453 SEND_GUI_MOUSE_BUTTON_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
454 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
455 CHECK_FALSE(root->gui_get_focus_owner());
456
457 node_d->set_focus_mode(Control::FOCUS_CLICK);
458 SEND_GUI_MOUSE_BUTTON_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
459 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
460 CHECK(node_d->has_focus());
461 }
462 }
463 }
464
465 SUBCASE("[Viewport][GuiInputEvent] Process-Mode affects, if GUI Mouse Button Events are processed.") {
466 node_a->last_mouse_button = MouseButton::NONE;
467 node_a->set_process_mode(Node::PROCESS_MODE_DISABLED);
468 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
469 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
470 CHECK(node_a->last_mouse_button == MouseButton::NONE);
471
472 // Now verify that with allowed processing the event is processed.
473 node_a->set_process_mode(Node::PROCESS_MODE_ALWAYS);
474 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
475 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
476 CHECK(node_a->last_mouse_button == MouseButton::LEFT);
477 }
478 }
479
480 // Unit tests for Viewport::_gui_input_event (Mouse Motion)
481 SUBCASE("[Viewport][GuiInputEvent] Mouse Motion") {
482 // FIXME: Tooltips are not yet tested. They likely require an internal clock.
483
484 SUBCASE("[Viewport][GuiInputEvent] Mouse Motion changes the Control that it is over.") {
485 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
486 CHECK_FALSE(node_a->mouse_over);
487 CHECK_FALSE(node_a->mouse_over_self);
488
489 // Move over Control.
490 SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::NONE, Key::NONE);
491 CHECK(node_a->mouse_over);
492 CHECK(node_a->mouse_over_self);
493
494 // No change.
495 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(1, 1), MouseButtonMask::NONE, Key::NONE);
496 CHECK(node_a->mouse_over);
497 CHECK(node_a->mouse_over_self);
498
499 // Move over other Control.
500 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
501 CHECK_FALSE(node_a->mouse_over);
502 CHECK_FALSE(node_a->mouse_over_self);
503 CHECK(node_d->mouse_over);
504 CHECK(node_d->mouse_over_self);
505
506 // Move to background.
507 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
508 CHECK_FALSE(node_d->mouse_over);
509 CHECK_FALSE(node_d->mouse_over_self);
510
511 CHECK_FALSE(node_a->invalid_order);
512 CHECK_FALSE(node_d->invalid_order);
513 }
514
515 SUBCASE("[Viewport][GuiInputEvent] Mouse behavior recursive disables mouse motion events.") {
516 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
517
518 // Enabled when parent is set to inherit.
519 node_h->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_INHERITED);
520 node_i->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_INHERITED);
521 SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
522 CHECK(node_i->mouse_over);
523 CHECK(node_i->mouse_over_self);
524
525 // Enabled when parent is set to enabled.
526 node_h->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_ENABLED);
527 node_i->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_INHERITED);
528 SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
529 CHECK(node_i->mouse_over);
530 CHECK(node_i->mouse_over_self);
531
532 // Disabled when parent is set to disabled.
533 node_h->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_DISABLED);
534 node_i->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_INHERITED);
535 SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
536 CHECK_FALSE(node_i->mouse_over);
537 CHECK_FALSE(node_i->mouse_over_self);
538
539 // Enabled when set to enabled and parent is set to disabled.
540 node_h->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_DISABLED);
541 node_i->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_ENABLED);
542 SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
543 CHECK(node_i->mouse_over);
544 CHECK(node_i->mouse_over_self);
545
546 // Disabled when it is set to disabled.
547 node_h->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_ENABLED);
548 node_i->set_mouse_behavior_recursive(Control::MOUSE_BEHAVIOR_DISABLED);
549 SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
550 CHECK_FALSE(node_i->mouse_over);
551 CHECK_FALSE(node_i->mouse_over_self);
552 }
553
554 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation.") {
555 node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
556 node_g->set_mouse_filter(Control::MOUSE_FILTER_PASS);
557
558 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
559 CHECK_FALSE(node_b->mouse_over);
560 CHECK_FALSE(node_b->mouse_over_self);
561 CHECK_FALSE(node_d->mouse_over);
562 CHECK_FALSE(node_d->mouse_over_self);
563
564 // Move to Control node_d. node_b receives mouse over since it is only separated by a CanvasItem.
565 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
566 CHECK(node_b->mouse_over);
567 CHECK_FALSE(node_b->mouse_over_self);
568 CHECK(node_d->mouse_over);
569 CHECK(node_d->mouse_over_self);
570
571 // Move to background.
572 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
573 CHECK_FALSE(node_b->mouse_over);
574 CHECK_FALSE(node_b->mouse_over_self);
575 CHECK_FALSE(node_d->mouse_over);
576 CHECK_FALSE(node_d->mouse_over_self);
577
578 CHECK_FALSE(node_e->mouse_over);
579 CHECK_FALSE(node_e->mouse_over_self);
580 CHECK_FALSE(node_g->mouse_over);
581 CHECK_FALSE(node_g->mouse_over_self);
582
583 // Move to Control node_g. node_g receives mouse over but node_e does not since it is separated by a non-CanvasItem.
584 SEND_GUI_MOUSE_MOTION_EVENT(on_g, MouseButtonMask::NONE, Key::NONE);
585 CHECK_FALSE(node_e->mouse_over);
586 CHECK_FALSE(node_e->mouse_over_self);
587 CHECK(node_g->mouse_over);
588 CHECK(node_g->mouse_over_self);
589
590 // Move to background.
591 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
592 CHECK_FALSE(node_e->mouse_over);
593 CHECK_FALSE(node_e->mouse_over_self);
594 CHECK_FALSE(node_g->mouse_over);
595 CHECK_FALSE(node_g->mouse_over_self);
596
597 CHECK_FALSE(node_b->invalid_order);
598 CHECK_FALSE(node_d->invalid_order);
599 CHECK_FALSE(node_e->invalid_order);
600 CHECK_FALSE(node_g->invalid_order);
601
602 node_d->set_mouse_filter(Control::MOUSE_FILTER_STOP);
603 node_g->set_mouse_filter(Control::MOUSE_FILTER_STOP);
604 }
605
606 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation when moving into child.") {
607 SIGNAL_WATCH(node_i, SceneStringName(mouse_entered));
608 SIGNAL_WATCH(node_i, SceneStringName(mouse_exited));
609 Array signal_args = { {} };
610
611 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
612
613 // Move to background.
614 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
615 CHECK_FALSE(node_i->mouse_over);
616 CHECK_FALSE(node_i->mouse_over_self);
617 CHECK_FALSE(node_j->mouse_over);
618 CHECK_FALSE(node_j->mouse_over_self);
619
620 // Move to Control node_i.
621 SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
622 CHECK(node_i->mouse_over);
623 CHECK(node_i->mouse_over_self);
624 CHECK_FALSE(node_j->mouse_over);
625 CHECK_FALSE(node_j->mouse_over_self);
626 SIGNAL_CHECK(SceneStringName(mouse_entered), signal_args);
627 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
628
629 // Move to child Control node_j. node_i should not receive any new Mouse Enter signals.
630 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
631 CHECK(node_i->mouse_over);
632 CHECK_FALSE(node_i->mouse_over_self);
633 CHECK(node_j->mouse_over);
634 CHECK(node_j->mouse_over_self);
635 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
636 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
637
638 // Move to parent Control node_i. node_i should not receive any new Mouse Enter signals.
639 SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
640 CHECK(node_i->mouse_over);
641 CHECK(node_i->mouse_over_self);
642 CHECK_FALSE(node_j->mouse_over);
643 CHECK_FALSE(node_j->mouse_over_self);
644 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
645 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
646
647 // Move to background.
648 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
649 CHECK_FALSE(node_i->mouse_over);
650 CHECK_FALSE(node_i->mouse_over_self);
651 CHECK_FALSE(node_j->mouse_over);
652 CHECK_FALSE(node_j->mouse_over_self);
653 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
654 SIGNAL_CHECK(SceneStringName(mouse_exited), signal_args);
655
656 CHECK_FALSE(node_i->invalid_order);
657 CHECK_FALSE(node_j->invalid_order);
658
659 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
660
661 SIGNAL_UNWATCH(node_i, SceneStringName(mouse_entered));
662 SIGNAL_UNWATCH(node_i, SceneStringName(mouse_exited));
663 }
664
665 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation with top level.") {
666 node_c->set_as_top_level(true);
667 node_i->set_as_top_level(true);
668 node_c->set_position(node_b->get_global_position());
669 node_i->set_position(node_h->get_global_position());
670 node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
671 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
672 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
673
674 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
675 CHECK_FALSE(node_b->mouse_over);
676 CHECK_FALSE(node_b->mouse_over_self);
677 CHECK_FALSE(node_d->mouse_over);
678 CHECK_FALSE(node_d->mouse_over_self);
679
680 // Move to Control node_d. node_b does not receive mouse over since node_c is top level.
681 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
682 CHECK_FALSE(node_b->mouse_over);
683 CHECK_FALSE(node_b->mouse_over_self);
684 CHECK(node_d->mouse_over);
685 CHECK(node_d->mouse_over_self);
686
687 // Move to background.
688 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
689 CHECK_FALSE(node_b->mouse_over);
690 CHECK_FALSE(node_b->mouse_over_self);
691 CHECK_FALSE(node_d->mouse_over);
692 CHECK_FALSE(node_d->mouse_over_self);
693
694 CHECK_FALSE(node_g->mouse_over);
695 CHECK_FALSE(node_g->mouse_over_self);
696 CHECK_FALSE(node_h->mouse_over);
697 CHECK_FALSE(node_h->mouse_over_self);
698 CHECK_FALSE(node_i->mouse_over);
699 CHECK_FALSE(node_i->mouse_over_self);
700
701 // Move to Control node_j. node_h does not receive mouse over since node_i is top level.
702 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
703 CHECK_FALSE(node_h->mouse_over);
704 CHECK_FALSE(node_h->mouse_over_self);
705 CHECK(node_i->mouse_over);
706 CHECK_FALSE(node_i->mouse_over_self);
707 CHECK(node_j->mouse_over);
708 CHECK(node_j->mouse_over_self);
709
710 // Move to background.
711 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
712 CHECK_FALSE(node_h->mouse_over);
713 CHECK_FALSE(node_h->mouse_over_self);
714 CHECK_FALSE(node_i->mouse_over);
715 CHECK_FALSE(node_i->mouse_over_self);
716 CHECK_FALSE(node_j->mouse_over);
717 CHECK_FALSE(node_j->mouse_over_self);
718
719 CHECK_FALSE(node_b->invalid_order);
720 CHECK_FALSE(node_d->invalid_order);
721 CHECK_FALSE(node_e->invalid_order);
722 CHECK_FALSE(node_h->invalid_order);
723 CHECK_FALSE(node_i->invalid_order);
724 CHECK_FALSE(node_j->invalid_order);
725
726 node_c->set_as_top_level(false);
727 node_i->set_as_top_level(false);
728 node_c->set_position(Point2i(0, 0));
729 node_i->set_position(Point2i(0, 0));
730 node_d->set_mouse_filter(Control::MOUSE_FILTER_STOP);
731 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
732 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
733 }
734
735 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation with mouse filter stop.") {
736 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
737 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
738
739 // Move to background.
740 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
741 CHECK_FALSE(node_h->mouse_over);
742 CHECK_FALSE(node_h->mouse_over_self);
743 CHECK_FALSE(node_i->mouse_over);
744 CHECK_FALSE(node_i->mouse_over_self);
745 CHECK_FALSE(node_j->mouse_over);
746 CHECK_FALSE(node_j->mouse_over_self);
747
748 // Move to Control node_j. node_h does not receive mouse over since node_i is MOUSE_FILTER_STOP.
749 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
750 CHECK_FALSE(node_h->mouse_over);
751 CHECK_FALSE(node_h->mouse_over_self);
752 CHECK(node_i->mouse_over);
753 CHECK_FALSE(node_i->mouse_over_self);
754 CHECK(node_j->mouse_over);
755 CHECK(node_j->mouse_over_self);
756
757 // Move to background.
758 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
759 CHECK_FALSE(node_h->mouse_over);
760 CHECK_FALSE(node_h->mouse_over_self);
761 CHECK_FALSE(node_i->mouse_over);
762 CHECK_FALSE(node_i->mouse_over_self);
763 CHECK_FALSE(node_j->mouse_over);
764 CHECK_FALSE(node_j->mouse_over_self);
765
766 CHECK_FALSE(node_h->invalid_order);
767 CHECK_FALSE(node_i->invalid_order);
768 CHECK_FALSE(node_j->invalid_order);
769
770 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
771 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
772 }
773
774 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation with mouse filter ignore.") {
775 node_i->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
776 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
777
778 // Move to background.
779 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
780 CHECK_FALSE(node_h->mouse_over);
781 CHECK_FALSE(node_h->mouse_over_self);
782 CHECK_FALSE(node_i->mouse_over);
783 CHECK_FALSE(node_i->mouse_over_self);
784 CHECK_FALSE(node_j->mouse_over);
785 CHECK_FALSE(node_j->mouse_over_self);
786
787 // Move to Control node_j. node_i does not receive mouse over since node_i is MOUSE_FILTER_IGNORE.
788 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
789 CHECK(node_h->mouse_over);
790 CHECK_FALSE(node_h->mouse_over_self);
791 CHECK_FALSE(node_i->mouse_over);
792 CHECK_FALSE(node_i->mouse_over_self);
793 CHECK(node_j->mouse_over);
794 CHECK(node_j->mouse_over_self);
795
796 // Move to background.
797 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
798 CHECK_FALSE(node_h->mouse_over);
799 CHECK_FALSE(node_h->mouse_over_self);
800 CHECK_FALSE(node_i->mouse_over);
801 CHECK_FALSE(node_i->mouse_over_self);
802 CHECK_FALSE(node_j->mouse_over);
803 CHECK_FALSE(node_j->mouse_over_self);
804
805 CHECK_FALSE(node_h->invalid_order);
806 CHECK_FALSE(node_i->invalid_order);
807 CHECK_FALSE(node_j->invalid_order);
808
809 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
810 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
811 }
812
813 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when changing top level.") {
814 SIGNAL_WATCH(node_i, SceneStringName(mouse_entered));
815 SIGNAL_WATCH(node_i, SceneStringName(mouse_exited));
816 Array signal_args = { {} };
817
818 node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
819 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
820 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
821
822 // Move to Control node_d.
823 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
824 CHECK(node_b->mouse_over);
825 CHECK_FALSE(node_b->mouse_over_self);
826 CHECK(node_d->mouse_over);
827 CHECK(node_d->mouse_over_self);
828
829 // Change node_c to be top level. node_b should receive Mouse Exit.
830 node_c->set_as_top_level(true);
831 CHECK_FALSE(node_b->mouse_over);
832 CHECK_FALSE(node_b->mouse_over_self);
833 CHECK(node_d->mouse_over);
834 CHECK(node_d->mouse_over_self);
835
836 // Change node_c to be not top level. node_b should receive Mouse Enter.
837 node_c->set_as_top_level(false);
838 CHECK(node_b->mouse_over);
839 CHECK_FALSE(node_b->mouse_over_self);
840 CHECK(node_d->mouse_over);
841 CHECK(node_d->mouse_over_self);
842
843 // Move to Control node_j.
844 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
845 CHECK(node_h->mouse_over);
846 CHECK_FALSE(node_h->mouse_over_self);
847 CHECK(node_i->mouse_over);
848 CHECK_FALSE(node_i->mouse_over_self);
849 CHECK(node_j->mouse_over);
850 CHECK(node_j->mouse_over_self);
851 SIGNAL_CHECK(SceneStringName(mouse_entered), signal_args);
852 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
853
854 // Change node_i to top level. node_h should receive Mouse Exit. node_i should not receive any new signals.
855 node_i->set_as_top_level(true);
856 CHECK_FALSE(node_h->mouse_over);
857 CHECK_FALSE(node_h->mouse_over_self);
858 CHECK(node_i->mouse_over);
859 CHECK_FALSE(node_i->mouse_over_self);
860 CHECK(node_j->mouse_over);
861 CHECK(node_j->mouse_over_self);
862 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
863 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
864
865 // Change node_i to not top level. node_h should receive Mouse Enter. node_i should not receive any new signals.
866 node_i->set_as_top_level(false);
867 CHECK(node_h->mouse_over);
868 CHECK_FALSE(node_h->mouse_over_self);
869 CHECK(node_i->mouse_over);
870 CHECK_FALSE(node_i->mouse_over_self);
871 CHECK(node_j->mouse_over);
872 CHECK(node_j->mouse_over_self);
873 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
874 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
875
876 CHECK_FALSE(node_b->invalid_order);
877 CHECK_FALSE(node_d->invalid_order);
878 CHECK_FALSE(node_e->invalid_order);
879 CHECK_FALSE(node_h->invalid_order);
880 CHECK_FALSE(node_i->invalid_order);
881 CHECK_FALSE(node_j->invalid_order);
882
883 node_d->set_mouse_filter(Control::MOUSE_FILTER_STOP);
884 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
885 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
886
887 SIGNAL_UNWATCH(node_i, SceneStringName(mouse_entered));
888 SIGNAL_UNWATCH(node_i, SceneStringName(mouse_exited));
889 }
890
891 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when changing the mouse filter to stop.") {
892 SIGNAL_WATCH(node_i, SceneStringName(mouse_entered));
893 SIGNAL_WATCH(node_i, SceneStringName(mouse_exited));
894 Array signal_args = { {} };
895
896 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
897 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
898
899 // Move to Control node_j.
900 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
901 CHECK(node_h->mouse_over);
902 CHECK_FALSE(node_h->mouse_over_self);
903 CHECK(node_i->mouse_over);
904 CHECK_FALSE(node_i->mouse_over_self);
905 CHECK(node_j->mouse_over);
906 CHECK(node_j->mouse_over_self);
907 SIGNAL_CHECK(SceneStringName(mouse_entered), signal_args);
908 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
909
910 // Change node_i to MOUSE_FILTER_STOP. node_h should receive Mouse Exit. node_i should not receive any new signals.
911 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
912 CHECK_FALSE(node_h->mouse_over);
913 CHECK_FALSE(node_h->mouse_over_self);
914 CHECK(node_i->mouse_over);
915 CHECK_FALSE(node_i->mouse_over_self);
916 CHECK(node_j->mouse_over);
917 CHECK(node_j->mouse_over_self);
918 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
919 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
920
921 // Change node_i to MOUSE_FILTER_PASS. node_h should receive Mouse Enter. node_i should not receive any new signals.
922 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
923 CHECK(node_h->mouse_over);
924 CHECK_FALSE(node_h->mouse_over_self);
925 CHECK(node_i->mouse_over);
926 CHECK_FALSE(node_i->mouse_over_self);
927 CHECK(node_j->mouse_over);
928 CHECK(node_j->mouse_over_self);
929 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
930 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
931
932 CHECK_FALSE(node_h->invalid_order);
933 CHECK_FALSE(node_i->invalid_order);
934 CHECK_FALSE(node_j->invalid_order);
935
936 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
937 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
938
939 SIGNAL_UNWATCH(node_i, SceneStringName(mouse_entered));
940 SIGNAL_UNWATCH(node_i, SceneStringName(mouse_exited));
941 }
942
943 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when changing the mouse filter to ignore.") {
944 SIGNAL_WATCH(node_i, SceneStringName(mouse_entered));
945 SIGNAL_WATCH(node_i, SceneStringName(mouse_exited));
946 Array signal_args = { {} };
947
948 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
949 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
950
951 // Move to Control node_j.
952 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
953 CHECK(node_h->mouse_over);
954 CHECK_FALSE(node_h->mouse_over_self);
955 CHECK(node_i->mouse_over);
956 CHECK_FALSE(node_i->mouse_over_self);
957 CHECK(node_j->mouse_over);
958 CHECK(node_j->mouse_over_self);
959 SIGNAL_CHECK(SceneStringName(mouse_entered), signal_args);
960 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
961
962 // Change node_i to MOUSE_FILTER_IGNORE. node_i should receive Mouse Exit.
963 node_i->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
964 CHECK(node_h->mouse_over);
965 CHECK_FALSE(node_h->mouse_over_self);
966 CHECK_FALSE(node_i->mouse_over);
967 CHECK_FALSE(node_i->mouse_over_self);
968 CHECK(node_j->mouse_over);
969 CHECK(node_j->mouse_over_self);
970 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
971 SIGNAL_CHECK(SceneStringName(mouse_exited), signal_args);
972
973 // Change node_i to MOUSE_FILTER_PASS. node_i should receive Mouse Enter.
974 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
975 CHECK(node_h->mouse_over);
976 CHECK_FALSE(node_h->mouse_over_self);
977 CHECK(node_i->mouse_over);
978 CHECK_FALSE(node_i->mouse_over_self);
979 CHECK(node_j->mouse_over);
980 CHECK(node_j->mouse_over_self);
981 SIGNAL_CHECK(SceneStringName(mouse_entered), signal_args);
982 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
983
984 // Change node_j to MOUSE_FILTER_IGNORE. After updating the mouse motion, node_i should now have mouse_over_self.
985 node_j->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
986 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
987 CHECK(node_h->mouse_over);
988 CHECK_FALSE(node_h->mouse_over_self);
989 CHECK(node_i->mouse_over);
990 CHECK(node_i->mouse_over_self);
991 CHECK_FALSE(node_j->mouse_over);
992 CHECK_FALSE(node_j->mouse_over_self);
993 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
994 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
995
996 // Change node_j to MOUSE_FILTER_PASS. After updating the mouse motion, node_j should now have mouse_over_self.
997 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
998 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
999 CHECK(node_h->mouse_over);
1000 CHECK_FALSE(node_h->mouse_over_self);
1001 CHECK(node_i->mouse_over);
1002 CHECK_FALSE(node_i->mouse_over_self);
1003 CHECK(node_j->mouse_over);
1004 CHECK(node_j->mouse_over_self);
1005 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
1006 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1007
1008 CHECK_FALSE(node_h->invalid_order);
1009 CHECK_FALSE(node_i->invalid_order);
1010 CHECK_FALSE(node_j->invalid_order);
1011
1012 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1013 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1014
1015 SIGNAL_UNWATCH(node_i, SceneStringName(mouse_entered));
1016 SIGNAL_UNWATCH(node_i, SceneStringName(mouse_exited));
1017 }
1018
1019 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when removing the hovered Control.") {
1020 SIGNAL_WATCH(node_h, SceneStringName(mouse_entered));
1021 SIGNAL_WATCH(node_h, SceneStringName(mouse_exited));
1022 Array signal_args = { {} };
1023
1024 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1025 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1026
1027 // Move to Control node_j.
1028 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
1029 CHECK(node_h->mouse_over);
1030 CHECK_FALSE(node_h->mouse_over_self);
1031 CHECK(node_i->mouse_over);
1032 CHECK_FALSE(node_i->mouse_over_self);
1033 CHECK(node_j->mouse_over);
1034 CHECK(node_j->mouse_over_self);
1035 SIGNAL_CHECK(SceneStringName(mouse_entered), signal_args);
1036 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1037
1038 // Remove node_i from the tree. node_i and node_j should receive Mouse Exit. node_h should not receive any new signals.
1039 node_h->remove_child(node_i);
1040 CHECK(node_h->mouse_over);
1041 CHECK_FALSE(node_h->mouse_over_self);
1042 CHECK_FALSE(node_i->mouse_over);
1043 CHECK_FALSE(node_i->mouse_over_self);
1044 CHECK_FALSE(node_j->mouse_over);
1045 CHECK_FALSE(node_j->mouse_over_self);
1046 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
1047 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1048
1049 // Add node_i to the tree and update the mouse. node_i and node_j should receive Mouse Enter. node_h should not receive any new signals.
1050 node_h->add_child(node_i);
1051 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
1052 CHECK(node_h->mouse_over);
1053 CHECK_FALSE(node_h->mouse_over_self);
1054 CHECK(node_i->mouse_over);
1055 CHECK_FALSE(node_i->mouse_over_self);
1056 CHECK(node_j->mouse_over);
1057 CHECK(node_j->mouse_over_self);
1058 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
1059 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1060
1061 CHECK_FALSE(node_h->invalid_order);
1062 CHECK_FALSE(node_i->invalid_order);
1063 CHECK_FALSE(node_j->invalid_order);
1064
1065 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1066 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1067
1068 SIGNAL_UNWATCH(node_h, SceneStringName(mouse_entered));
1069 SIGNAL_UNWATCH(node_h, SceneStringName(mouse_exited));
1070 }
1071
1072 SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when hiding the hovered Control.") {
1073 SIGNAL_WATCH(node_h, SceneStringName(mouse_entered));
1074 SIGNAL_WATCH(node_h, SceneStringName(mouse_exited));
1075 Array signal_args = { {} };
1076
1077 node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1078 node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1079
1080 // Move to Control node_j.
1081 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
1082 CHECK(node_h->mouse_over);
1083 CHECK_FALSE(node_h->mouse_over_self);
1084 CHECK(node_i->mouse_over);
1085 CHECK_FALSE(node_i->mouse_over_self);
1086 CHECK(node_j->mouse_over);
1087 CHECK(node_j->mouse_over_self);
1088 SIGNAL_CHECK(SceneStringName(mouse_entered), signal_args);
1089 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1090
1091 // Hide node_i. node_i and node_j should receive Mouse Exit. node_h should not receive any new signals.
1092 node_i->hide();
1093 CHECK(node_h->mouse_over);
1094 CHECK_FALSE(node_h->mouse_over_self);
1095 CHECK_FALSE(node_i->mouse_over);
1096 CHECK_FALSE(node_i->mouse_over_self);
1097 CHECK_FALSE(node_j->mouse_over);
1098 CHECK_FALSE(node_j->mouse_over_self);
1099 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
1100 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1101
1102 // Show node_i and update the mouse. node_i and node_j should receive Mouse Enter. node_h should not receive any new signals.
1103 node_i->show();
1104 SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
1105 CHECK(node_h->mouse_over);
1106 CHECK_FALSE(node_h->mouse_over_self);
1107 CHECK(node_i->mouse_over);
1108 CHECK_FALSE(node_i->mouse_over_self);
1109 CHECK(node_j->mouse_over);
1110 CHECK(node_j->mouse_over_self);
1111 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
1112 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1113
1114 CHECK_FALSE(node_h->invalid_order);
1115 CHECK_FALSE(node_i->invalid_order);
1116 CHECK_FALSE(node_j->invalid_order);
1117
1118 node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1119 node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1120
1121 SIGNAL_UNWATCH(node_h, SceneStringName(mouse_entered));
1122 SIGNAL_UNWATCH(node_h, SceneStringName(mouse_exited));
1123 }
1124
1125 SUBCASE("[Viewport][GuiInputEvent] Window Mouse Enter/Exit signals.") {
1126 SIGNAL_WATCH(root, SceneStringName(mouse_entered));
1127 SIGNAL_WATCH(root, SceneStringName(mouse_exited));
1128 Array signal_args = { {} };
1129
1130 SEND_GUI_MOUSE_MOTION_EVENT(on_outside, MouseButtonMask::NONE, Key::NONE);
1131 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
1132 SIGNAL_CHECK(SceneStringName(mouse_exited), signal_args);
1133
1134 SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::NONE, Key::NONE);
1135 SIGNAL_CHECK(SceneStringName(mouse_entered), signal_args);
1136 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1137
1138 SIGNAL_UNWATCH(root, SceneStringName(mouse_entered));
1139 SIGNAL_UNWATCH(root, SceneStringName(mouse_exited));
1140 }
1141
1142 SUBCASE("[Viewport][GuiInputEvent] Process-Mode affects, if GUI Mouse Motion Events are processed.") {
1143 node_a->last_mouse_move_position = on_outside;
1144 node_a->set_process_mode(Node::PROCESS_MODE_DISABLED);
1145 SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::NONE, Key::NONE);
1146 CHECK(node_a->last_mouse_move_position == on_outside);
1147
1148 // Now verify that with allowed processing the event is processed.
1149 node_a->set_process_mode(Node::PROCESS_MODE_ALWAYS);
1150 SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::NONE, Key::NONE);
1151 CHECK(node_a->last_mouse_move_position == on_a);
1152 }
1153 }
1154
1155 // Unit tests for Viewport::_gui_input_event (Drag and Drop)
1156 SUBCASE("[Viewport][GuiInputEvent] Drag and Drop") {
1157 // FIXME: Drag-Preview will likely change. Tests for this part would have to be rewritten anyway.
1158 // See https://github.com/godotengine/godot/pull/67531#issuecomment-1385353430 for details.
1159 // Note: Testing Drag and Drop with non-embedded windows would require DisplayServerMock additions.
1160 int min_grab_movement = 11;
1161 SUBCASE("[Viewport][GuiInputEvent][DnD] Drag from one Control to another in the same viewport.") {
1162 SUBCASE("[Viewport][GuiInputEvent][DnD] Perform successful Drag and Drop on a different Control.") {
1163 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1164 CHECK_FALSE(root->gui_is_dragging());
1165
1166 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
1167 CHECK(root->gui_is_dragging());
1168
1169 // Move above a Control, that is a Drop target and allows dropping at this point.
1170 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::LEFT, Key::NONE);
1171 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_CAN_DROP);
1172
1173 CHECK(root->gui_is_dragging());
1174 CHECK_FALSE(root->gui_is_drag_successful());
1175 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1176 CHECK_FALSE(root->gui_is_dragging());
1177 CHECK(root->gui_is_drag_successful());
1178 CHECK((StringName)node_d->drag_data == SNAME("Drag Data"));
1179 }
1180
1181 SUBCASE("[Viewport][GuiInputEvent][DnD] Perform unsuccessful drop on Control.") {
1182 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1183 CHECK_FALSE(root->gui_is_dragging());
1184
1185 // Move, but don't trigger DnD yet.
1186 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(0, min_grab_movement - 1), MouseButtonMask::LEFT, Key::NONE);
1187 CHECK_FALSE(root->gui_is_dragging());
1188
1189 // Move and trigger DnD.
1190 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
1191 CHECK(root->gui_is_dragging());
1192
1193 // Move above a Control, that is not a Drop target.
1194 SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::LEFT, Key::NONE);
1195 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_FORBIDDEN);
1196
1197 // Move above a Control, that is a Drop target, but has disallowed this point.
1198 SEND_GUI_MOUSE_MOTION_EVENT(on_d + Point2i(20, 0), MouseButtonMask::LEFT, Key::NONE);
1199 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_FORBIDDEN);
1200 CHECK(root->gui_is_dragging());
1201
1202 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d + Point2i(20, 0), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1203 CHECK_FALSE(root->gui_is_dragging());
1204 CHECK_FALSE(root->gui_is_drag_successful());
1205 }
1206
1207 SUBCASE("[Viewport][GuiInputEvent][DnD] Perform unsuccessful drop on No-Control.") {
1208 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1209 CHECK_FALSE(root->gui_is_dragging());
1210
1211 // Move, but don't trigger DnD yet.
1212 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement - 1, 0), MouseButtonMask::LEFT, Key::NONE);
1213 CHECK_FALSE(root->gui_is_dragging());
1214
1215 // Move and trigger DnD.
1216 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
1217 CHECK(root->gui_is_dragging());
1218
1219 // Move away from Controls.
1220 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::LEFT, Key::NONE);
1221 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_ARROW);
1222
1223 CHECK(root->gui_is_dragging());
1224 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1225 CHECK_FALSE(root->gui_is_dragging());
1226 CHECK_FALSE(root->gui_is_drag_successful());
1227 }
1228
1229 SUBCASE("[Viewport][GuiInputEvent][DnD] Perform unsuccessful drop outside of window.") {
1230 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1231 CHECK_FALSE(root->gui_is_dragging());
1232
1233 // Move and trigger DnD.
1234 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
1235 CHECK(root->gui_is_dragging());
1236
1237 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::LEFT, Key::NONE);
1238 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_CAN_DROP);
1239
1240 // Move outside of window.
1241 SEND_GUI_MOUSE_MOTION_EVENT(on_outside, MouseButtonMask::LEFT, Key::NONE);
1242 CHECK(root->gui_is_dragging());
1243
1244 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_outside, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1245 CHECK_FALSE(root->gui_is_dragging());
1246 CHECK_FALSE(root->gui_is_drag_successful());
1247 }
1248
1249 SUBCASE("[Viewport][GuiInputEvent][DnD] Drag and Drop doesn't work with other Mouse Buttons than LMB.") {
1250 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::MIDDLE, MouseButtonMask::MIDDLE, Key::NONE);
1251 CHECK_FALSE(root->gui_is_dragging());
1252
1253 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::MIDDLE, Key::NONE);
1254 CHECK_FALSE(root->gui_is_dragging());
1255 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::MIDDLE, MouseButtonMask::NONE, Key::NONE);
1256 }
1257
1258 SUBCASE("[Viewport][GuiInputEvent][DnD] Drag and Drop parent propagation.") {
1259 Node2D *node_aa = memnew(Node2D);
1260 Control *node_aaa = memnew(Control);
1261 Node2D *node_dd = memnew(Node2D);
1262 Control *node_ddd = memnew(Control);
1263 node_aaa->set_size(Size2i(10, 10));
1264 node_aaa->set_position(Point2i(0, 5));
1265 node_ddd->set_size(Size2i(10, 10));
1266 node_ddd->set_position(Point2i(0, 5));
1267 node_a->add_child(node_aa);
1268 node_aa->add_child(node_aaa);
1269 node_d->add_child(node_dd);
1270 node_dd->add_child(node_ddd);
1271 Point2i on_aaa = on_a + Point2i(-2, 2);
1272 Point2i on_ddd = on_d + Point2i(-2, 2);
1273
1274 SUBCASE("[Viewport][GuiInputEvent] Drag and Drop propagation to parent Controls.") {
1275 node_aaa->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1276 node_ddd->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1277
1278 SEND_GUI_MOUSE_BUTTON_EVENT(on_aaa, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1279 CHECK_FALSE(root->gui_is_dragging());
1280
1281 SEND_GUI_MOUSE_MOTION_EVENT(on_aaa + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
1282 CHECK(root->gui_is_dragging());
1283
1284 SEND_GUI_MOUSE_MOTION_EVENT(on_ddd, MouseButtonMask::LEFT, Key::NONE);
1285
1286 CHECK(root->gui_is_dragging());
1287 CHECK_FALSE(root->gui_is_drag_successful());
1288 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_ddd, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1289 CHECK_FALSE(root->gui_is_dragging());
1290 CHECK(root->gui_is_drag_successful());
1291
1292 node_aaa->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1293 node_ddd->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1294 }
1295
1296 SUBCASE("[Viewport][GuiInputEvent] Drag and Drop grab-propagation stopped by Top Level.") {
1297 node_aaa->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1298 node_aaa->set_as_top_level(true);
1299
1300 SEND_GUI_MOUSE_BUTTON_EVENT(on_aaa, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1301 CHECK_FALSE(root->gui_is_dragging());
1302
1303 SEND_GUI_MOUSE_MOTION_EVENT(on_aaa + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
1304 CHECK_FALSE(root->gui_is_dragging());
1305
1306 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1307 node_aaa->set_as_top_level(false);
1308 node_aaa->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1309 }
1310
1311 SUBCASE("[Viewport][GuiInputEvent] Drag and Drop target-propagation stopped by Top Level.") {
1312 node_aaa->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1313 node_ddd->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1314 node_ddd->set_as_top_level(true);
1315 node_ddd->set_position(Point2i(30, 100));
1316
1317 SEND_GUI_MOUSE_BUTTON_EVENT(on_aaa, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1318 CHECK_FALSE(root->gui_is_dragging());
1319
1320 SEND_GUI_MOUSE_MOTION_EVENT(on_aaa + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
1321 CHECK(root->gui_is_dragging());
1322
1323 SEND_GUI_MOUSE_MOTION_EVENT(Point2i(35, 105), MouseButtonMask::LEFT, Key::NONE);
1324
1325 CHECK(root->gui_is_dragging());
1326 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(Point2i(35, 105), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1327 CHECK_FALSE(root->gui_is_dragging());
1328 CHECK_FALSE(root->gui_is_drag_successful());
1329
1330 node_ddd->set_position(Point2i(0, 5));
1331 node_ddd->set_as_top_level(false);
1332 node_aaa->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1333 node_ddd->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1334 }
1335
1336 SUBCASE("[Viewport][GuiInputEvent] Drag and Drop grab-propagation stopped by non-CanvasItem.") {
1337 node_g->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1338
1339 SEND_GUI_MOUSE_BUTTON_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1340 SEND_GUI_MOUSE_MOTION_EVENT(on_g + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
1341 CHECK_FALSE(root->gui_is_dragging());
1342
1343 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1344 node_g->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1345 }
1346
1347 SUBCASE("[Viewport][GuiInputEvent] Drag and Drop target-propagation stopped by non-CanvasItem.") {
1348 node_g->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1349
1350 SEND_GUI_MOUSE_BUTTON_EVENT(on_a - Point2i(1, 1), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE); // Offset for node_aaa.
1351 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
1352 CHECK(root->gui_is_dragging());
1353
1354 SEND_GUI_MOUSE_MOTION_EVENT(on_g, MouseButtonMask::LEFT, Key::NONE);
1355 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1356 CHECK_FALSE(root->gui_is_dragging());
1357
1358 node_g->set_mouse_filter(Control::MOUSE_FILTER_STOP);
1359 }
1360
1361 memdelete(node_ddd);
1362 memdelete(node_dd);
1363 memdelete(node_aaa);
1364 memdelete(node_aa);
1365 }
1366
1367 SUBCASE("[Viewport][GuiInputEvent][DnD] Force Drag and Drop.") {
1368 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
1369 CHECK_FALSE(root->gui_is_dragging());
1370 node_a->force_drag(SNAME("Drag Data"), nullptr);
1371 CHECK(root->gui_is_dragging());
1372
1373 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
1374
1375 // Force Drop doesn't get triggered by mouse Buttons other than LMB.
1376 SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::MIDDLE, MouseButtonMask::MIDDLE, Key::NONE);
1377 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::MIDDLE, MouseButtonMask::NONE, Key::NONE);
1378 CHECK(root->gui_is_dragging());
1379
1380 // Force Drop with LMB-Down.
1381 SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1382 CHECK_FALSE(root->gui_is_dragging());
1383 CHECK(root->gui_is_drag_successful());
1384
1385 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1386
1387 node_a->force_drag(SNAME("Drag Data"), nullptr);
1388 CHECK(root->gui_is_dragging());
1389
1390 // Cancel with RMB.
1391 SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
1392 CHECK_FALSE(root->gui_is_dragging());
1393 CHECK_FALSE(root->gui_is_drag_successful());
1394 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::RIGHT, MouseButtonMask::NONE, Key::NONE);
1395 }
1396 }
1397
1398 SUBCASE("[Viewport][GuiInputEvent][DnD] Drag to a different Viewport.") {
1399 SubViewportContainer *svc = memnew(SubViewportContainer);
1400 svc->set_size(Size2(100, 100));
1401 svc->set_position(Point2(200, 50));
1402 root->add_child(svc);
1403
1404 SubViewport *sv = memnew(SubViewport);
1405 sv->set_embedding_subwindows(true);
1406 sv->set_size(Size2i(100, 100));
1407 svc->add_child(sv);
1408
1409 DragStart *sv_a = memnew(DragStart);
1410 sv_a->set_position(Point2(10, 10));
1411 sv_a->set_size(Size2(10, 10));
1412 sv->add_child(sv_a);
1413 Point2i on_sva = Point2i(215, 65);
1414
1415 DragTarget *sv_b = memnew(DragTarget);
1416 sv_b->set_position(Point2(30, 30));
1417 sv_b->set_size(Size2(20, 20));
1418 sv->add_child(sv_b);
1419 Point2i on_svb = Point2i(235, 85);
1420
1421 Window *ew = memnew(Window);
1422 ew->set_position(Point2(50, 200));
1423 ew->set_size(Size2(100, 100));
1424 root->add_child(ew);
1425
1426 DragStart *ew_a = memnew(DragStart);
1427 ew_a->set_position(Point2(10, 10));
1428 ew_a->set_size(Size2(10, 10));
1429 ew->add_child(ew_a);
1430 Point2i on_ewa = Point2i(65, 215);
1431
1432 DragTarget *ew_b = memnew(DragTarget);
1433 ew_b->set_position(Point2(30, 30));
1434 ew_b->set_size(Size2(20, 20));
1435 ew->add_child(ew_b);
1436 Point2i on_ewb = Point2i(85, 235);
1437
1438 SUBCASE("[Viewport][GuiInputEvent][DnD] Drag to SubViewport") {
1439 sv_b->valid_drop = false;
1440 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1441 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
1442 CHECK(root->gui_is_dragging());
1443 CHECK(sv_b->during_drag);
1444 SEND_GUI_MOUSE_MOTION_EVENT(on_svb, MouseButtonMask::LEFT, Key::NONE);
1445 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_CAN_DROP);
1446
1447 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_svb, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1448 CHECK(sv_b->valid_drop);
1449 CHECK(!sv_b->during_drag);
1450 }
1451
1452 SUBCASE("[Viewport][GuiInputEvent][DnD] Drag from SubViewport") {
1453 node_d->valid_drop = false;
1454 SEND_GUI_MOUSE_BUTTON_EVENT(on_sva, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1455 SEND_GUI_MOUSE_MOTION_EVENT(on_sva + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
1456 CHECK(sv->gui_is_dragging());
1457 CHECK(node_d->during_drag);
1458 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::LEFT, Key::NONE);
1459 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_CAN_DROP);
1460
1461 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1462 CHECK(node_d->valid_drop);
1463 CHECK(!node_d->during_drag);
1464 }
1465
1466 SUBCASE("[Viewport][GuiInputEvent][DnD] Drag to embedded Window") {
1467 ew_b->valid_drop = false;
1468 SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1469 SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
1470 CHECK(root->gui_is_dragging());
1471 CHECK(ew_b->during_drag);
1472 SEND_GUI_MOUSE_MOTION_EVENT(on_ewb, MouseButtonMask::LEFT, Key::NONE);
1473 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_CAN_DROP);
1474
1475 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_ewb, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1476 CHECK(ew_b->valid_drop);
1477 CHECK(!ew_b->during_drag);
1478 }
1479
1480 SUBCASE("[Viewport][GuiInputEvent][DnD] Drag from embedded Window") {
1481 node_d->valid_drop = false;
1482 SEND_GUI_MOUSE_BUTTON_EVENT(on_ewa, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1483 SEND_GUI_MOUSE_MOTION_EVENT(on_ewa + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
1484 CHECK(ew->gui_is_dragging());
1485 CHECK(node_d->during_drag);
1486 SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::LEFT, Key::NONE);
1487 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_CAN_DROP);
1488
1489 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1490 CHECK(node_d->valid_drop);
1491 CHECK(!node_d->during_drag);
1492 }
1493
1494 memdelete(ew_a);
1495 memdelete(ew_b);
1496 memdelete(ew);
1497 memdelete(sv_a);
1498 memdelete(sv_b);
1499 memdelete(sv);
1500 memdelete(svc);
1501 }
1502 }
1503
1504 memdelete(node_j);
1505 memdelete(node_i);
1506 memdelete(node_h);
1507 memdelete(node_g);
1508 memdelete(node_f);
1509 memdelete(node_e);
1510 memdelete(node_d);
1511 memdelete(node_c);
1512 memdelete(node_b);
1513 memdelete(node_a);
1514 }
1515
1516 TEST_CASE("[SceneTree][Viewport] Control mouse cursor shape") {
1517 SUBCASE("[Viewport][CursorShape] Mouse cursor is not overridden by SubViewportContainer") {
1518 SubViewportContainer *node_a = memnew(SubViewportContainer);
1519 SubViewport *node_b = memnew(SubViewport);
1520 Control *node_c = memnew(Control);
1521
1522 node_a->set_name("SubViewportContainer");
1523 node_b->set_name("SubViewport");
1524 node_c->set_name("Control");
1525 node_a->set_position(Point2i(0, 0));
1526 node_c->set_position(Point2i(0, 0));
1527 node_a->set_size(Point2i(100, 100));
1528 node_b->set_size(Point2i(100, 100));
1529 node_c->set_size(Point2i(100, 100));
1530 node_a->set_default_cursor_shape(Control::CURSOR_ARROW);
1531 node_c->set_default_cursor_shape(Control::CURSOR_FORBIDDEN);
1532 Window *root = SceneTree::get_singleton()->get_root();
1533 DisplayServerMock *DS = (DisplayServerMock *)(DisplayServer::get_singleton());
1534
1535 // Scene tree:
1536 // - root
1537 // - node_a (SubViewportContainer)
1538 // - node_b (SubViewport)
1539 // - node_c (Control)
1540
1541 root->add_child(node_a);
1542 node_a->add_child(node_b);
1543 node_b->add_child(node_c);
1544
1545 Point2i on_c = Point2i(5, 5);
1546
1547 SEND_GUI_MOUSE_MOTION_EVENT(on_c, MouseButtonMask::NONE, Key::NONE);
1548 CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_FORBIDDEN); // GH-74805
1549
1550 memdelete(node_c);
1551 memdelete(node_b);
1552 memdelete(node_a);
1553 }
1554 }
1555
1556 #ifndef PHYSICS_2D_DISABLED
1557 class TestArea2D : public Area2D {
1558 GDCLASS(TestArea2D, Area2D);
1559
1560 void _on_mouse_entered() {
1561 enter_id = ++TestArea2D::counter; // > 0, if activated.
1562 }
1563
1564 void _on_mouse_exited() {
1565 exit_id = ++TestArea2D::counter; // > 0, if activated.
1566 }
1567
1568 void _on_input_event(Node *p_vp, Ref<InputEvent> p_ev, int p_shape) {
1569 last_input_event = p_ev;
1570 }
1571
1572 public:
1573 static int counter;
1574 int enter_id = 0;
1575 int exit_id = 0;
1576 Ref<InputEvent> last_input_event;
1577
1578 void init_signals() {
1579 connect(SceneStringName(mouse_entered), callable_mp(this, &TestArea2D::_on_mouse_entered));
1580 connect(SceneStringName(mouse_exited), callable_mp(this, &TestArea2D::_on_mouse_exited));
1581 connect(SceneStringName(input_event), callable_mp(this, &TestArea2D::_on_input_event));
1582 }
1583
1584 void test_reset() {
1585 enter_id = 0;
1586 exit_id = 0;
1587 last_input_event.unref();
1588 }
1589 };
1590
1591 int TestArea2D::counter = 0;
1592
1593 TEST_CASE("[SceneTree][Viewport] Physics Picking 2D") {
1594 // FIXME: MOUSE_MODE_CAPTURED if-conditions are not testable, because DisplayServerMock doesn't support it.
1595
1596 // NOTE: This test requires a real physics server.
1597 PhysicsServer2DDummy *physics_server_2d_dummy = Object::cast_to<PhysicsServer2DDummy>(PhysicsServer2D::get_singleton());
1598 if (physics_server_2d_dummy) {
1599 return;
1600 }
1601
1602 struct PickingCollider {
1603 TestArea2D *a;
1604 CollisionShape2D *c;
1605 Ref<RectangleShape2D> r;
1606 };
1607
1608 SceneTree *tree = SceneTree::get_singleton();
1609 Window *root = tree->get_root();
1610 root->set_physics_object_picking(true);
1611
1612 Point2i on_background = Point2i(800, 800);
1613 Point2i on_outside = Point2i(-1, -1);
1614 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
1615 tree->physics_process(1);
1616
1617 Vector<PickingCollider> v;
1618 for (int i = 0; i < 4; i++) {
1619 PickingCollider pc;
1620 pc.a = memnew(TestArea2D);
1621 pc.c = memnew(CollisionShape2D);
1622 pc.r.instantiate();
1623 pc.r->set_size(Size2(150, 150));
1624 pc.c->set_shape(pc.r);
1625 pc.a->add_child(pc.c);
1626 pc.a->set_name("A" + itos(i));
1627 pc.c->set_name("C" + itos(i));
1628 v.push_back(pc);
1629 SIGNAL_WATCH(pc.a, SceneStringName(mouse_entered));
1630 SIGNAL_WATCH(pc.a, SceneStringName(mouse_exited));
1631 }
1632
1633 Node2D *node_a = memnew(Node2D);
1634 node_a->set_position(Point2i(0, 0));
1635 v[0].a->set_position(Point2i(0, 0));
1636 v[1].a->set_position(Point2i(0, 100));
1637 node_a->add_child(v[0].a);
1638 node_a->add_child(v[1].a);
1639 Node2D *node_b = memnew(Node2D);
1640 node_b->set_position(Point2i(100, 0));
1641 v[2].a->set_position(Point2i(0, 0));
1642 v[3].a->set_position(Point2i(0, 100));
1643 node_b->add_child(v[2].a);
1644 node_b->add_child(v[3].a);
1645 root->add_child(node_a);
1646 root->add_child(node_b);
1647 Point2i on_all = Point2i(50, 50);
1648 Point2i on_0 = Point2i(10, 10);
1649 Point2i on_01 = Point2i(10, 50);
1650 Point2i on_02 = Point2i(50, 10);
1651
1652 Array empty_signal_args_2 = { Array(), Array() };
1653 Array empty_signal_args_4 = { Array(), Array(), Array(), Array() };
1654
1655 for (PickingCollider E : v) {
1656 E.a->init_signals();
1657 }
1658
1659 SUBCASE("[Viewport][Picking2D] Mouse Motion") {
1660 SEND_GUI_MOUSE_MOTION_EVENT(on_all, MouseButtonMask::NONE, Key::NONE);
1661 tree->physics_process(1);
1662 SIGNAL_CHECK(SceneStringName(mouse_entered), empty_signal_args_4);
1663 SIGNAL_CHECK_FALSE(SceneStringName(mouse_exited));
1664 for (PickingCollider E : v) {
1665 CHECK(E.a->enter_id);
1666 CHECK_FALSE(E.a->exit_id);
1667 E.a->test_reset();
1668 }
1669
1670 SEND_GUI_MOUSE_MOTION_EVENT(on_01, MouseButtonMask::NONE, Key::NONE);
1671 tree->physics_process(1);
1672 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
1673 SIGNAL_CHECK(SceneStringName(mouse_exited), empty_signal_args_2);
1674
1675 for (int i = 0; i < v.size(); i++) {
1676 CHECK_FALSE(v[i].a->enter_id);
1677 if (i < 2) {
1678 CHECK_FALSE(v[i].a->exit_id);
1679 } else {
1680 CHECK(v[i].a->exit_id);
1681 }
1682 v[i].a->test_reset();
1683 }
1684
1685 SEND_GUI_MOUSE_MOTION_EVENT(on_outside, MouseButtonMask::NONE, Key::NONE);
1686 tree->physics_process(1);
1687 SIGNAL_CHECK_FALSE(SceneStringName(mouse_entered));
1688 SIGNAL_CHECK(SceneStringName(mouse_exited), empty_signal_args_2);
1689 for (int i = 0; i < v.size(); i++) {
1690 CHECK_FALSE(v[i].a->enter_id);
1691 if (i < 2) {
1692 CHECK(v[i].a->exit_id);
1693 } else {
1694 CHECK_FALSE(v[i].a->exit_id);
1695 }
1696 v[i].a->test_reset();
1697 }
1698 }
1699
1700 SUBCASE("[Viewport][Picking2D] Object moved / passive hovering") {
1701 SEND_GUI_MOUSE_MOTION_EVENT(on_all, MouseButtonMask::NONE, Key::NONE);
1702 tree->physics_process(1);
1703 for (int i = 0; i < v.size(); i++) {
1704 CHECK(v[i].a->enter_id);
1705 CHECK_FALSE(v[i].a->exit_id);
1706 v[i].a->test_reset();
1707 }
1708
1709 node_b->set_position(Point2i(200, 0));
1710 tree->physics_process(1);
1711 for (int i = 0; i < v.size(); i++) {
1712 CHECK_FALSE(v[i].a->enter_id);
1713 if (i < 2) {
1714 CHECK_FALSE(v[i].a->exit_id);
1715 } else {
1716 CHECK(v[i].a->exit_id);
1717 }
1718 v[i].a->test_reset();
1719 }
1720
1721 node_b->set_position(Point2i(100, 0));
1722 tree->physics_process(1);
1723 for (int i = 0; i < v.size(); i++) {
1724 if (i < 2) {
1725 CHECK_FALSE(v[i].a->enter_id);
1726 } else {
1727 CHECK(v[i].a->enter_id);
1728 }
1729 CHECK_FALSE(v[i].a->exit_id);
1730 v[i].a->test_reset();
1731 }
1732 }
1733
1734 SUBCASE("[Viewport][Picking2D] No Processing") {
1735 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
1736 tree->physics_process(1);
1737 for (PickingCollider E : v) {
1738 E.a->test_reset();
1739 }
1740
1741 v[0].a->set_process_mode(Node::PROCESS_MODE_DISABLED);
1742 v[0].c->set_process_mode(Node::PROCESS_MODE_DISABLED);
1743 SEND_GUI_MOUSE_MOTION_EVENT(on_02, MouseButtonMask::NONE, Key::NONE);
1744 tree->physics_process(1);
1745 CHECK_FALSE(v[0].a->enter_id);
1746 CHECK_FALSE(v[0].a->exit_id);
1747 CHECK(v[2].a->enter_id);
1748 CHECK_FALSE(v[2].a->exit_id);
1749 for (PickingCollider E : v) {
1750 E.a->test_reset();
1751 }
1752
1753 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
1754 tree->physics_process(1);
1755 CHECK_FALSE(v[0].a->enter_id);
1756 CHECK_FALSE(v[0].a->exit_id);
1757 CHECK_FALSE(v[2].a->enter_id);
1758 CHECK(v[2].a->exit_id);
1759
1760 for (PickingCollider E : v) {
1761 E.a->test_reset();
1762 }
1763 v[0].a->set_process_mode(Node::PROCESS_MODE_ALWAYS);
1764 v[0].c->set_process_mode(Node::PROCESS_MODE_ALWAYS);
1765 }
1766
1767 SUBCASE("[Viewport][Picking2D] Multiple events in series") {
1768 SEND_GUI_MOUSE_MOTION_EVENT(on_0, MouseButtonMask::NONE, Key::NONE);
1769 SEND_GUI_MOUSE_MOTION_EVENT(on_0 + Point2i(10, 0), MouseButtonMask::NONE, Key::NONE);
1770 tree->physics_process(1);
1771
1772 for (int i = 0; i < v.size(); i++) {
1773 if (i < 1) {
1774 CHECK(v[i].a->enter_id);
1775 } else {
1776 CHECK_FALSE(v[i].a->enter_id);
1777 }
1778 CHECK_FALSE(v[i].a->exit_id);
1779 v[i].a->test_reset();
1780 }
1781
1782 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
1783 SEND_GUI_MOUSE_MOTION_EVENT(on_background + Point2i(10, 10), MouseButtonMask::NONE, Key::NONE);
1784 tree->physics_process(1);
1785
1786 for (int i = 0; i < v.size(); i++) {
1787 CHECK_FALSE(v[i].a->enter_id);
1788 if (i < 1) {
1789 CHECK(v[i].a->exit_id);
1790 } else {
1791 CHECK_FALSE(v[i].a->exit_id);
1792 }
1793 v[i].a->test_reset();
1794 }
1795 }
1796
1797 SUBCASE("[Viewport][Picking2D] Disable Picking") {
1798 SEND_GUI_MOUSE_MOTION_EVENT(on_02, MouseButtonMask::NONE, Key::NONE);
1799
1800 root->set_physics_object_picking(false);
1801 CHECK_FALSE(root->get_physics_object_picking());
1802
1803 tree->physics_process(1);
1804
1805 for (int i = 0; i < v.size(); i++) {
1806 CHECK_FALSE(v[i].a->enter_id);
1807 v[i].a->test_reset();
1808 }
1809
1810 root->set_physics_object_picking(true);
1811 CHECK(root->get_physics_object_picking());
1812 }
1813
1814 SUBCASE("[Viewport][Picking2D] CollisionObject in CanvasLayer") {
1815 CanvasLayer *node_c = memnew(CanvasLayer);
1816 node_c->set_rotation(Math::PI);
1817 node_c->set_offset(Point2i(100, 100));
1818 root->add_child(node_c);
1819
1820 v[2].a->reparent(node_c, false);
1821 v[3].a->reparent(node_c, false);
1822
1823 SEND_GUI_MOUSE_MOTION_EVENT(on_02, MouseButtonMask::NONE, Key::NONE);
1824 tree->physics_process(1);
1825
1826 for (int i = 0; i < v.size(); i++) {
1827 if (i == 0 || i == 3) {
1828 CHECK(v[i].a->enter_id);
1829 } else {
1830 CHECK_FALSE(v[i].a->enter_id);
1831 }
1832 v[i].a->test_reset();
1833 }
1834
1835 v[2].a->reparent(node_b, false);
1836 v[3].a->reparent(node_b, false);
1837 root->remove_child(node_c);
1838 memdelete(node_c);
1839 }
1840
1841 SUBCASE("[Viewport][Picking2D] Picking Sort") {
1842 root->set_physics_object_picking_sort(true);
1843 CHECK(root->get_physics_object_picking_sort());
1844
1845 SUBCASE("[Viewport][Picking2D] Picking Sort Z-Index") {
1846 node_a->set_z_index(10);
1847 v[0].a->set_z_index(0);
1848 v[1].a->set_z_index(2);
1849 node_b->set_z_index(5);
1850 v[2].a->set_z_index(8);
1851 v[3].a->set_z_index(11);
1852 v[3].a->set_z_as_relative(false);
1853
1854 TestArea2D::counter = 0;
1855 SEND_GUI_MOUSE_MOTION_EVENT(on_all, MouseButtonMask::NONE, Key::NONE);
1856 tree->physics_process(1);
1857
1858 CHECK(v[0].a->enter_id == 4);
1859 CHECK(v[1].a->enter_id == 2);
1860 CHECK(v[2].a->enter_id == 1);
1861 CHECK(v[3].a->enter_id == 3);
1862 for (int i = 0; i < v.size(); i++) {
1863 CHECK_FALSE(v[i].a->exit_id);
1864 v[i].a->test_reset();
1865 }
1866
1867 TestArea2D::counter = 0;
1868 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
1869 tree->physics_process(1);
1870
1871 CHECK(v[0].a->exit_id == 4);
1872 CHECK(v[1].a->exit_id == 2);
1873 CHECK(v[2].a->exit_id == 1);
1874 CHECK(v[3].a->exit_id == 3);
1875 for (int i = 0; i < v.size(); i++) {
1876 CHECK_FALSE(v[i].a->enter_id);
1877 v[i].a->set_z_as_relative(true);
1878 v[i].a->set_z_index(0);
1879 v[i].a->test_reset();
1880 }
1881
1882 node_a->set_z_index(0);
1883 node_b->set_z_index(0);
1884 }
1885
1886 SUBCASE("[Viewport][Picking2D] Picking Sort Scene Tree Location") {
1887 TestArea2D::counter = 0;
1888 SEND_GUI_MOUSE_MOTION_EVENT(on_all, MouseButtonMask::NONE, Key::NONE);
1889 tree->physics_process(1);
1890
1891 for (int i = 0; i < v.size(); i++) {
1892 CHECK(v[i].a->enter_id == 4 - i);
1893 CHECK_FALSE(v[i].a->exit_id);
1894 v[i].a->test_reset();
1895 }
1896
1897 TestArea2D::counter = 0;
1898 SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
1899 tree->physics_process(1);
1900
1901 for (int i = 0; i < v.size(); i++) {
1902 CHECK_FALSE(v[i].a->enter_id);
1903 CHECK(v[i].a->exit_id == 4 - i);
1904 v[i].a->test_reset();
1905 }
1906 }
1907
1908 root->set_physics_object_picking_sort(false);
1909 CHECK_FALSE(root->get_physics_object_picking_sort());
1910 }
1911
1912 SUBCASE("[Viewport][Picking2D] Mouse Button") {
1913 SEND_GUI_MOUSE_BUTTON_EVENT(on_0, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
1914 tree->physics_process(1);
1915
1916 for (int i = 0; i < v.size(); i++) {
1917 if (i == 0) {
1918 CHECK(v[i].a->enter_id);
1919 } else {
1920 CHECK_FALSE(v[i].a->enter_id);
1921 }
1922 CHECK_FALSE(v[i].a->exit_id);
1923 v[i].a->test_reset();
1924 }
1925
1926 SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_0, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
1927 tree->physics_process(1);
1928
1929 for (int i = 0; i < v.size(); i++) {
1930 CHECK_FALSE(v[i].a->enter_id);
1931 CHECK_FALSE(v[i].a->exit_id);
1932 v[i].a->test_reset();
1933 }
1934 }
1935
1936 SUBCASE("[Viewport][Picking2D] Screen Touch") {
1937 SEND_GUI_TOUCH_EVENT(on_01, true, false);
1938 tree->physics_process(1);
1939 for (int i = 0; i < v.size(); i++) {
1940 if (i < 2) {
1941 Ref<InputEventScreenTouch> st = v[i].a->last_input_event;
1942 CHECK(st.is_valid());
1943 } else {
1944 CHECK(v[i].a->last_input_event.is_null());
1945 }
1946 v[i].a->test_reset();
1947 }
1948 }
1949
1950 for (PickingCollider E : v) {
1951 SIGNAL_UNWATCH(E.a, SceneStringName(mouse_entered));
1952 SIGNAL_UNWATCH(E.a, SceneStringName(mouse_exited));
1953 memdelete(E.c);
1954 memdelete(E.a);
1955 }
1956 }
1957 #endif // PHYSICS_2D_DISABLED
1958
1959 TEST_CASE("[SceneTree][Viewport] Embedded Windows") {
1960 Window *root = SceneTree::get_singleton()->get_root();
1961 Window *w = memnew(Window);
1962
1963 SUBCASE("[Viewport] Safe-rect of embedded Window") {
1964 root->add_child(w);
1965 root->subwindow_set_popup_safe_rect(w, Rect2i(10, 10, 10, 10));
1966 CHECK_EQ(root->subwindow_get_popup_safe_rect(w), Rect2i(10, 10, 10, 10));
1967 root->remove_child(w);
1968 CHECK_EQ(root->subwindow_get_popup_safe_rect(w), Rect2i());
1969 }
1970
1971 memdelete(w);
1972 }
1973
1974 } // namespace TestViewport