test_viewport.h 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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. #ifndef TEST_VIEWPORT_H
  31. #define TEST_VIEWPORT_H
  32. #include "scene/2d/area_2d.h"
  33. #include "scene/2d/collision_shape_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. #include "scene/resources/rectangle_shape_2d.h"
  39. #include "tests/test_macros.h"
  40. namespace TestViewport {
  41. class NotificationControlViewport : public Control {
  42. GDCLASS(NotificationControlViewport, Control);
  43. protected:
  44. void _notification(int p_what) {
  45. switch (p_what) {
  46. case NOTIFICATION_MOUSE_ENTER: {
  47. if (mouse_over) {
  48. invalid_order = true;
  49. }
  50. mouse_over = true;
  51. } break;
  52. case NOTIFICATION_MOUSE_EXIT: {
  53. if (!mouse_over) {
  54. invalid_order = true;
  55. }
  56. mouse_over = false;
  57. } break;
  58. case NOTIFICATION_MOUSE_ENTER_SELF: {
  59. if (mouse_over_self) {
  60. invalid_order = true;
  61. }
  62. mouse_over_self = true;
  63. } break;
  64. case NOTIFICATION_MOUSE_EXIT_SELF: {
  65. if (!mouse_over_self) {
  66. invalid_order = true;
  67. }
  68. mouse_over_self = false;
  69. } break;
  70. }
  71. }
  72. public:
  73. bool mouse_over = false;
  74. bool mouse_over_self = false;
  75. bool invalid_order = false;
  76. };
  77. // `NotificationControlViewport`-derived class that additionally
  78. // - allows start Dragging
  79. // - stores mouse information of last event
  80. class DragStart : public NotificationControlViewport {
  81. GDCLASS(DragStart, NotificationControlViewport);
  82. public:
  83. MouseButton last_mouse_button;
  84. Point2i last_mouse_move_position;
  85. StringName drag_data_name = SNAME("Drag Data");
  86. virtual Variant get_drag_data(const Point2 &p_point) override {
  87. return drag_data_name;
  88. }
  89. virtual void gui_input(const Ref<InputEvent> &p_event) override {
  90. Ref<InputEventMouseButton> mb = p_event;
  91. if (mb.is_valid()) {
  92. last_mouse_button = mb->get_button_index();
  93. return;
  94. }
  95. Ref<InputEventMouseMotion> mm = p_event;
  96. if (mm.is_valid()) {
  97. last_mouse_move_position = mm->get_position();
  98. return;
  99. }
  100. }
  101. };
  102. // `NotificationControlViewport`-derived class that acts as a Drag and Drop target.
  103. class DragTarget : public NotificationControlViewport {
  104. GDCLASS(DragTarget, NotificationControlViewport);
  105. public:
  106. Variant drag_data;
  107. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override {
  108. StringName string_data = p_data;
  109. // Verify drag data is compatible.
  110. if (string_data != SNAME("Drag Data")) {
  111. return false;
  112. }
  113. // Only the left half is droppable area.
  114. if (p_point.x * 2 > get_size().x) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. virtual void drop_data(const Point2 &p_point, const Variant &p_data) override {
  120. drag_data = p_data;
  121. }
  122. };
  123. TEST_CASE("[SceneTree][Viewport] Controls and InputEvent handling") {
  124. DragStart *node_a = memnew(DragStart);
  125. NotificationControlViewport *node_b = memnew(NotificationControlViewport);
  126. Node2D *node_c = memnew(Node2D);
  127. DragTarget *node_d = memnew(DragTarget);
  128. NotificationControlViewport *node_e = memnew(NotificationControlViewport);
  129. Node *node_f = memnew(Node);
  130. NotificationControlViewport *node_g = memnew(NotificationControlViewport);
  131. NotificationControlViewport *node_h = memnew(NotificationControlViewport);
  132. NotificationControlViewport *node_i = memnew(NotificationControlViewport);
  133. NotificationControlViewport *node_j = memnew(NotificationControlViewport);
  134. node_a->set_name(SNAME("NodeA"));
  135. node_b->set_name(SNAME("NodeB"));
  136. node_c->set_name(SNAME("NodeC"));
  137. node_d->set_name(SNAME("NodeD"));
  138. node_e->set_name(SNAME("NodeE"));
  139. node_f->set_name(SNAME("NodeF"));
  140. node_g->set_name(SNAME("NodeG"));
  141. node_h->set_name(SNAME("NodeH"));
  142. node_i->set_name(SNAME("NodeI"));
  143. node_j->set_name(SNAME("NodeJ"));
  144. node_a->set_position(Point2i(0, 0));
  145. node_b->set_position(Point2i(10, 10));
  146. node_c->set_position(Point2i(0, 0));
  147. node_d->set_position(Point2i(10, 10));
  148. node_e->set_position(Point2i(10, 100));
  149. node_g->set_position(Point2i(10, 100));
  150. node_h->set_position(Point2i(10, 120));
  151. node_i->set_position(Point2i(2, 0));
  152. node_j->set_position(Point2i(2, 0));
  153. node_a->set_size(Point2i(30, 30));
  154. node_b->set_size(Point2i(30, 30));
  155. node_d->set_size(Point2i(30, 30));
  156. node_e->set_size(Point2i(10, 10));
  157. node_g->set_size(Point2i(10, 10));
  158. node_h->set_size(Point2i(10, 10));
  159. node_i->set_size(Point2i(10, 10));
  160. node_j->set_size(Point2i(10, 10));
  161. node_a->set_focus_mode(Control::FOCUS_CLICK);
  162. node_b->set_focus_mode(Control::FOCUS_CLICK);
  163. node_d->set_focus_mode(Control::FOCUS_CLICK);
  164. node_e->set_focus_mode(Control::FOCUS_CLICK);
  165. node_g->set_focus_mode(Control::FOCUS_CLICK);
  166. node_h->set_focus_mode(Control::FOCUS_CLICK);
  167. node_i->set_focus_mode(Control::FOCUS_CLICK);
  168. node_j->set_focus_mode(Control::FOCUS_CLICK);
  169. Window *root = SceneTree::get_singleton()->get_root();
  170. DisplayServerMock *DS = (DisplayServerMock *)(DisplayServer::get_singleton());
  171. // Scene tree:
  172. // - root
  173. // - a (Control)
  174. // - b (Control)
  175. // - c (Node2D)
  176. // - d (Control)
  177. // - e (Control)
  178. // - f (Node)
  179. // - g (Control)
  180. // - h (Control)
  181. // - i (Control)
  182. // - j (Control)
  183. root->add_child(node_a);
  184. root->add_child(node_b);
  185. node_b->add_child(node_c);
  186. node_c->add_child(node_d);
  187. root->add_child(node_e);
  188. node_e->add_child(node_f);
  189. node_f->add_child(node_g);
  190. root->add_child(node_h);
  191. node_h->add_child(node_i);
  192. node_i->add_child(node_j);
  193. Point2i on_a = Point2i(5, 5);
  194. Point2i on_b = Point2i(15, 15);
  195. Point2i on_d = Point2i(25, 25);
  196. Point2i on_e = Point2i(15, 105);
  197. Point2i on_g = Point2i(15, 105);
  198. Point2i on_i = Point2i(13, 125);
  199. Point2i on_j = Point2i(15, 125);
  200. Point2i on_background = Point2i(500, 500);
  201. Point2i on_outside = Point2i(-1, -1);
  202. // Unit tests for Viewport::gui_find_control and Viewport::_gui_find_control_at_pos
  203. SUBCASE("[VIEWPORT][GuiFindControl] Finding Controls at a Viewport-position") {
  204. // FIXME: It is extremely difficult to create a situation where the Control has a zero determinant.
  205. // Leaving that if-branch untested.
  206. SUBCASE("[VIEWPORT][GuiFindControl] Basic position tests") {
  207. CHECK(root->gui_find_control(on_a) == node_a);
  208. CHECK(root->gui_find_control(on_b) == node_b);
  209. CHECK(root->gui_find_control(on_d) == node_d);
  210. CHECK(root->gui_find_control(on_e) == node_g); // Node F makes G a Root Control at the same position as E
  211. CHECK(root->gui_find_control(on_g) == node_g);
  212. CHECK_FALSE(root->gui_find_control(on_background));
  213. }
  214. SUBCASE("[VIEWPORT][GuiFindControl] Invisible nodes are not considered as results.") {
  215. // Non-Root Control
  216. node_d->hide();
  217. CHECK(root->gui_find_control(on_d) == node_b);
  218. // Root Control
  219. node_b->hide();
  220. CHECK(root->gui_find_control(on_b) == node_a);
  221. }
  222. SUBCASE("[VIEWPORT][GuiFindControl] Root Control with CanvasItem as parent is affected by parent's transform.") {
  223. node_b->remove_child(node_c);
  224. node_c->set_position(Point2i(50, 50));
  225. root->add_child(node_c);
  226. CHECK(root->gui_find_control(Point2i(65, 65)) == node_d);
  227. }
  228. SUBCASE("[VIEWPORT][GuiFindControl] Control Contents Clipping clips accessible position of children.") {
  229. CHECK_FALSE(node_b->is_clipping_contents());
  230. CHECK(root->gui_find_control(on_d + Point2i(20, 20)) == node_d);
  231. node_b->set_clip_contents(true);
  232. CHECK(root->gui_find_control(on_d) == node_d);
  233. CHECK_FALSE(root->gui_find_control(on_d + Point2i(20, 20)));
  234. }
  235. SUBCASE("[VIEWPORT][GuiFindControl] Top Level Control as descendant of CanvasItem isn't affected by parent's transform.") {
  236. CHECK(root->gui_find_control(on_d + Point2i(20, 20)) == node_d);
  237. node_d->set_as_top_level(true);
  238. CHECK_FALSE(root->gui_find_control(on_d + Point2i(20, 20)));
  239. CHECK(root->gui_find_control(on_b) == node_d);
  240. }
  241. }
  242. SUBCASE("[Viewport][GuiInputEvent] nullptr as argument doesn't lead to a crash.") {
  243. ERR_PRINT_OFF;
  244. root->push_input(nullptr);
  245. ERR_PRINT_ON;
  246. }
  247. // Unit tests for Viewport::_gui_input_event (Mouse Buttons)
  248. SUBCASE("[Viewport][GuiInputEvent] Mouse Button Down/Up.") {
  249. SUBCASE("[Viewport][GuiInputEvent] Mouse Button Control Focus Change.") {
  250. SUBCASE("[Viewport][GuiInputEvent] Grab Focus while no Control has focus.") {
  251. CHECK_FALSE(root->gui_get_focus_owner());
  252. // Click on A
  253. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  254. CHECK(node_a->has_focus());
  255. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  256. }
  257. SUBCASE("[Viewport][GuiInputEvent] Grab Focus from other Control.") {
  258. node_a->grab_focus();
  259. CHECK(node_a->has_focus());
  260. // Click on D
  261. SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  262. CHECK(node_d->has_focus());
  263. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  264. }
  265. SUBCASE("[Viewport][GuiInputEvent] Non-CanvasItem breaks Transform hierarchy.") {
  266. CHECK_FALSE(root->gui_get_focus_owner());
  267. // Click on G absolute coordinates
  268. SEND_GUI_MOUSE_BUTTON_EVENT(Point2i(15, 105), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  269. CHECK(node_g->has_focus());
  270. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(Point2i(15, 105), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  271. }
  272. SUBCASE("[Viewport][GuiInputEvent] No Focus change when clicking in background.") {
  273. CHECK_FALSE(root->gui_get_focus_owner());
  274. SEND_GUI_MOUSE_BUTTON_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  275. CHECK_FALSE(root->gui_get_focus_owner());
  276. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  277. node_a->grab_focus();
  278. CHECK(node_a->has_focus());
  279. SEND_GUI_MOUSE_BUTTON_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  280. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  281. CHECK(node_a->has_focus());
  282. }
  283. SUBCASE("[Viewport][GuiInputEvent] Mouse Button No Focus Steal while other Mouse Button is pressed.") {
  284. CHECK_FALSE(root->gui_get_focus_owner());
  285. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  286. CHECK(node_a->has_focus());
  287. SEND_GUI_MOUSE_BUTTON_EVENT(on_b, MouseButton::RIGHT, (int)MouseButtonMask::LEFT | (int)MouseButtonMask::RIGHT, Key::NONE);
  288. CHECK(node_a->has_focus());
  289. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_b, MouseButton::RIGHT, MouseButtonMask::LEFT, Key::NONE);
  290. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  291. CHECK(node_a->has_focus());
  292. }
  293. SUBCASE("[Viewport][GuiInputEvent] Allow Focus Steal with LMB while other Mouse Button is held down and was initially pressed without being over a Control.") {
  294. // TODO: Not sure, if this is intended behavior, but this is an edge case.
  295. CHECK_FALSE(root->gui_get_focus_owner());
  296. SEND_GUI_MOUSE_BUTTON_EVENT(on_background, MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
  297. CHECK_FALSE(root->gui_get_focus_owner());
  298. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, (int)MouseButtonMask::LEFT | (int)MouseButtonMask::RIGHT, Key::NONE);
  299. CHECK(node_a->has_focus());
  300. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::RIGHT, Key::NONE);
  301. CHECK(node_a->has_focus());
  302. SEND_GUI_MOUSE_BUTTON_EVENT(on_b, MouseButton::LEFT, (int)MouseButtonMask::LEFT | (int)MouseButtonMask::RIGHT, Key::NONE);
  303. CHECK(node_b->has_focus());
  304. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::RIGHT, Key::NONE);
  305. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::RIGHT, MouseButtonMask::NONE, Key::NONE);
  306. CHECK(node_b->has_focus());
  307. }
  308. SUBCASE("[Viewport][GuiInputEvent] Ignore Focus from Mouse Buttons when mouse-filter is set to ignore.") {
  309. node_d->grab_focus();
  310. node_d->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  311. CHECK(node_d->has_focus());
  312. // Click on overlapping area B&D.
  313. SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  314. CHECK(node_b->has_focus());
  315. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  316. }
  317. SUBCASE("[Viewport][GuiInputEvent] RMB doesn't grab focus.") {
  318. node_a->grab_focus();
  319. CHECK(node_a->has_focus());
  320. SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
  321. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::RIGHT, MouseButtonMask::NONE, Key::NONE);
  322. CHECK(node_a->has_focus());
  323. }
  324. SUBCASE("[Viewport][GuiInputEvent] LMB on unfocusable Control doesn't grab focus.") {
  325. CHECK_FALSE(node_g->has_focus());
  326. node_g->set_focus_mode(Control::FOCUS_NONE);
  327. SEND_GUI_MOUSE_BUTTON_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  328. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  329. CHECK_FALSE(node_g->has_focus());
  330. // Now verify the opposite with FOCUS_CLICK
  331. node_g->set_focus_mode(Control::FOCUS_CLICK);
  332. SEND_GUI_MOUSE_BUTTON_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  333. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  334. CHECK(node_g->has_focus());
  335. node_g->set_focus_mode(Control::FOCUS_CLICK);
  336. }
  337. SUBCASE("[Viewport][GuiInputEvent] Signal 'gui_focus_changed' is only emitted if a previously unfocused Control grabs focus.") {
  338. SIGNAL_WATCH(root, SNAME("gui_focus_changed"));
  339. Array node_array;
  340. node_array.push_back(node_a);
  341. Array signal_args;
  342. signal_args.push_back(node_array);
  343. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  344. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  345. SIGNAL_CHECK(SNAME("gui_focus_changed"), signal_args);
  346. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  347. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  348. CHECK(node_a->has_focus());
  349. SIGNAL_CHECK_FALSE(SNAME("gui_focus_changed"));
  350. SIGNAL_UNWATCH(root, SNAME("gui_focus_changed"));
  351. }
  352. SUBCASE("[Viewport][GuiInputEvent] Focus Propagation to parent items.") {
  353. SUBCASE("[Viewport][GuiInputEvent] Unfocusable Control with MOUSE_FILTER_PASS propagates focus to parent CanvasItem.") {
  354. node_d->set_focus_mode(Control::FOCUS_NONE);
  355. node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  356. SEND_GUI_MOUSE_BUTTON_EVENT(on_d + Point2i(20, 20), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  357. CHECK(node_b->has_focus());
  358. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d + Point2i(20, 20), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  359. // Verify break condition for Root Control.
  360. node_a->set_focus_mode(Control::FOCUS_NONE);
  361. node_a->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  362. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  363. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  364. CHECK(node_b->has_focus());
  365. }
  366. SUBCASE("[Viewport][GuiInputEvent] Top Level CanvasItem stops focus propagation.") {
  367. node_d->set_focus_mode(Control::FOCUS_NONE);
  368. node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  369. node_c->set_as_top_level(true);
  370. SEND_GUI_MOUSE_BUTTON_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  371. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  372. CHECK_FALSE(root->gui_get_focus_owner());
  373. node_d->set_focus_mode(Control::FOCUS_CLICK);
  374. SEND_GUI_MOUSE_BUTTON_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  375. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_b, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  376. CHECK(node_d->has_focus());
  377. }
  378. }
  379. }
  380. SUBCASE("[Viewport][GuiInputEvent] Process-Mode affects, if GUI Mouse Button Events are processed.") {
  381. node_a->last_mouse_button = MouseButton::NONE;
  382. node_a->set_process_mode(Node::PROCESS_MODE_DISABLED);
  383. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  384. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  385. CHECK(node_a->last_mouse_button == MouseButton::NONE);
  386. // Now verify that with allowed processing the event is processed.
  387. node_a->set_process_mode(Node::PROCESS_MODE_ALWAYS);
  388. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  389. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  390. CHECK(node_a->last_mouse_button == MouseButton::LEFT);
  391. }
  392. }
  393. // Unit tests for Viewport::_gui_input_event (Mouse Motion)
  394. SUBCASE("[Viewport][GuiInputEvent] Mouse Motion") {
  395. // FIXME: Tooltips are not yet tested. They likely require an internal clock.
  396. SUBCASE("[Viewport][GuiInputEvent] Mouse Motion changes the Control that it is over.") {
  397. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  398. CHECK_FALSE(node_a->mouse_over);
  399. CHECK_FALSE(node_a->mouse_over_self);
  400. // Move over Control.
  401. SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::NONE, Key::NONE);
  402. CHECK(node_a->mouse_over);
  403. CHECK(node_a->mouse_over_self);
  404. // No change.
  405. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(1, 1), MouseButtonMask::NONE, Key::NONE);
  406. CHECK(node_a->mouse_over);
  407. CHECK(node_a->mouse_over_self);
  408. // Move over other Control.
  409. SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
  410. CHECK_FALSE(node_a->mouse_over);
  411. CHECK_FALSE(node_a->mouse_over_self);
  412. CHECK(node_d->mouse_over);
  413. CHECK(node_d->mouse_over_self);
  414. // Move to background.
  415. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  416. CHECK_FALSE(node_d->mouse_over);
  417. CHECK_FALSE(node_d->mouse_over_self);
  418. CHECK_FALSE(node_a->invalid_order);
  419. CHECK_FALSE(node_d->invalid_order);
  420. }
  421. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation.") {
  422. node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  423. node_g->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  424. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  425. CHECK_FALSE(node_b->mouse_over);
  426. CHECK_FALSE(node_b->mouse_over_self);
  427. CHECK_FALSE(node_d->mouse_over);
  428. CHECK_FALSE(node_d->mouse_over_self);
  429. // Move to Control node_d. node_b receives mouse over since it is only separated by a CanvasItem.
  430. SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
  431. CHECK(node_b->mouse_over);
  432. CHECK_FALSE(node_b->mouse_over_self);
  433. CHECK(node_d->mouse_over);
  434. CHECK(node_d->mouse_over_self);
  435. // Move to background.
  436. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  437. CHECK_FALSE(node_b->mouse_over);
  438. CHECK_FALSE(node_b->mouse_over_self);
  439. CHECK_FALSE(node_d->mouse_over);
  440. CHECK_FALSE(node_d->mouse_over_self);
  441. CHECK_FALSE(node_e->mouse_over);
  442. CHECK_FALSE(node_e->mouse_over_self);
  443. CHECK_FALSE(node_g->mouse_over);
  444. CHECK_FALSE(node_g->mouse_over_self);
  445. // Move to Control node_g. node_g receives mouse over but node_e does not since it is separated by a non-CanvasItem.
  446. SEND_GUI_MOUSE_MOTION_EVENT(on_g, MouseButtonMask::NONE, Key::NONE);
  447. CHECK_FALSE(node_e->mouse_over);
  448. CHECK_FALSE(node_e->mouse_over_self);
  449. CHECK(node_g->mouse_over);
  450. CHECK(node_g->mouse_over_self);
  451. // Move to background.
  452. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  453. CHECK_FALSE(node_e->mouse_over);
  454. CHECK_FALSE(node_e->mouse_over_self);
  455. CHECK_FALSE(node_g->mouse_over);
  456. CHECK_FALSE(node_g->mouse_over_self);
  457. CHECK_FALSE(node_b->invalid_order);
  458. CHECK_FALSE(node_d->invalid_order);
  459. CHECK_FALSE(node_e->invalid_order);
  460. CHECK_FALSE(node_g->invalid_order);
  461. node_d->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  462. node_g->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  463. }
  464. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation when moving into child.") {
  465. SIGNAL_WATCH(node_i, SNAME("mouse_entered"));
  466. SIGNAL_WATCH(node_i, SNAME("mouse_exited"));
  467. Array signal_args;
  468. signal_args.push_back(Array());
  469. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  470. // Move to background.
  471. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  472. CHECK_FALSE(node_i->mouse_over);
  473. CHECK_FALSE(node_i->mouse_over_self);
  474. CHECK_FALSE(node_j->mouse_over);
  475. CHECK_FALSE(node_j->mouse_over_self);
  476. // Move to Control node_i.
  477. SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
  478. CHECK(node_i->mouse_over);
  479. CHECK(node_i->mouse_over_self);
  480. CHECK_FALSE(node_j->mouse_over);
  481. CHECK_FALSE(node_j->mouse_over_self);
  482. SIGNAL_CHECK(SNAME("mouse_entered"), signal_args);
  483. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  484. // Move to child Control node_j. node_i should not receive any new Mouse Enter signals.
  485. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  486. CHECK(node_i->mouse_over);
  487. CHECK_FALSE(node_i->mouse_over_self);
  488. CHECK(node_j->mouse_over);
  489. CHECK(node_j->mouse_over_self);
  490. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  491. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  492. // Move to parent Control node_i. node_i should not receive any new Mouse Enter signals.
  493. SEND_GUI_MOUSE_MOTION_EVENT(on_i, MouseButtonMask::NONE, Key::NONE);
  494. CHECK(node_i->mouse_over);
  495. CHECK(node_i->mouse_over_self);
  496. CHECK_FALSE(node_j->mouse_over);
  497. CHECK_FALSE(node_j->mouse_over_self);
  498. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  499. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  500. // Move to background.
  501. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  502. CHECK_FALSE(node_i->mouse_over);
  503. CHECK_FALSE(node_i->mouse_over_self);
  504. CHECK_FALSE(node_j->mouse_over);
  505. CHECK_FALSE(node_j->mouse_over_self);
  506. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  507. SIGNAL_CHECK(SNAME("mouse_exited"), signal_args);
  508. CHECK_FALSE(node_i->invalid_order);
  509. CHECK_FALSE(node_j->invalid_order);
  510. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  511. SIGNAL_UNWATCH(node_i, SNAME("mouse_entered"));
  512. SIGNAL_UNWATCH(node_i, SNAME("mouse_exited"));
  513. }
  514. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation with top level.") {
  515. node_c->set_as_top_level(true);
  516. node_i->set_as_top_level(true);
  517. node_c->set_position(node_b->get_global_position());
  518. node_i->set_position(node_h->get_global_position());
  519. node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  520. node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  521. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  522. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  523. CHECK_FALSE(node_b->mouse_over);
  524. CHECK_FALSE(node_b->mouse_over_self);
  525. CHECK_FALSE(node_d->mouse_over);
  526. CHECK_FALSE(node_d->mouse_over_self);
  527. // Move to Control node_d. node_b does not receive mouse over since node_c is top level.
  528. SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
  529. CHECK_FALSE(node_b->mouse_over);
  530. CHECK_FALSE(node_b->mouse_over_self);
  531. CHECK(node_d->mouse_over);
  532. CHECK(node_d->mouse_over_self);
  533. // Move to background.
  534. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  535. CHECK_FALSE(node_b->mouse_over);
  536. CHECK_FALSE(node_b->mouse_over_self);
  537. CHECK_FALSE(node_d->mouse_over);
  538. CHECK_FALSE(node_d->mouse_over_self);
  539. CHECK_FALSE(node_g->mouse_over);
  540. CHECK_FALSE(node_g->mouse_over_self);
  541. CHECK_FALSE(node_h->mouse_over);
  542. CHECK_FALSE(node_h->mouse_over_self);
  543. CHECK_FALSE(node_i->mouse_over);
  544. CHECK_FALSE(node_i->mouse_over_self);
  545. // Move to Control node_j. node_h does not receive mouse over since node_i is top level.
  546. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  547. CHECK_FALSE(node_h->mouse_over);
  548. CHECK_FALSE(node_h->mouse_over_self);
  549. CHECK(node_i->mouse_over);
  550. CHECK_FALSE(node_i->mouse_over_self);
  551. CHECK(node_j->mouse_over);
  552. CHECK(node_j->mouse_over_self);
  553. // Move to background.
  554. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  555. CHECK_FALSE(node_h->mouse_over);
  556. CHECK_FALSE(node_h->mouse_over_self);
  557. CHECK_FALSE(node_i->mouse_over);
  558. CHECK_FALSE(node_i->mouse_over_self);
  559. CHECK_FALSE(node_j->mouse_over);
  560. CHECK_FALSE(node_j->mouse_over_self);
  561. CHECK_FALSE(node_b->invalid_order);
  562. CHECK_FALSE(node_d->invalid_order);
  563. CHECK_FALSE(node_e->invalid_order);
  564. CHECK_FALSE(node_h->invalid_order);
  565. CHECK_FALSE(node_i->invalid_order);
  566. CHECK_FALSE(node_j->invalid_order);
  567. node_c->set_as_top_level(false);
  568. node_i->set_as_top_level(false);
  569. node_c->set_position(Point2i(0, 0));
  570. node_i->set_position(Point2i(0, 0));
  571. node_d->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  572. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  573. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  574. }
  575. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation with mouse filter stop.") {
  576. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  577. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  578. // Move to background.
  579. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  580. CHECK_FALSE(node_h->mouse_over);
  581. CHECK_FALSE(node_h->mouse_over_self);
  582. CHECK_FALSE(node_i->mouse_over);
  583. CHECK_FALSE(node_i->mouse_over_self);
  584. CHECK_FALSE(node_j->mouse_over);
  585. CHECK_FALSE(node_j->mouse_over_self);
  586. // Move to Control node_j. node_h does not receive mouse over since node_i is MOUSE_FILTER_STOP.
  587. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  588. CHECK_FALSE(node_h->mouse_over);
  589. CHECK_FALSE(node_h->mouse_over_self);
  590. CHECK(node_i->mouse_over);
  591. CHECK_FALSE(node_i->mouse_over_self);
  592. CHECK(node_j->mouse_over);
  593. CHECK(node_j->mouse_over_self);
  594. // Move to background.
  595. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  596. CHECK_FALSE(node_h->mouse_over);
  597. CHECK_FALSE(node_h->mouse_over_self);
  598. CHECK_FALSE(node_i->mouse_over);
  599. CHECK_FALSE(node_i->mouse_over_self);
  600. CHECK_FALSE(node_j->mouse_over);
  601. CHECK_FALSE(node_j->mouse_over_self);
  602. CHECK_FALSE(node_h->invalid_order);
  603. CHECK_FALSE(node_i->invalid_order);
  604. CHECK_FALSE(node_j->invalid_order);
  605. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  606. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  607. }
  608. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification propagation with mouse filter ignore.") {
  609. node_i->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  610. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  611. // Move to background.
  612. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  613. CHECK_FALSE(node_h->mouse_over);
  614. CHECK_FALSE(node_h->mouse_over_self);
  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. // Move to Control node_j. node_i does not receive mouse over since node_i is MOUSE_FILTER_IGNORE.
  620. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  621. CHECK(node_h->mouse_over);
  622. CHECK_FALSE(node_h->mouse_over_self);
  623. CHECK_FALSE(node_i->mouse_over);
  624. CHECK_FALSE(node_i->mouse_over_self);
  625. CHECK(node_j->mouse_over);
  626. CHECK(node_j->mouse_over_self);
  627. // Move to background.
  628. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  629. CHECK_FALSE(node_h->mouse_over);
  630. CHECK_FALSE(node_h->mouse_over_self);
  631. CHECK_FALSE(node_i->mouse_over);
  632. CHECK_FALSE(node_i->mouse_over_self);
  633. CHECK_FALSE(node_j->mouse_over);
  634. CHECK_FALSE(node_j->mouse_over_self);
  635. CHECK_FALSE(node_h->invalid_order);
  636. CHECK_FALSE(node_i->invalid_order);
  637. CHECK_FALSE(node_j->invalid_order);
  638. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  639. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  640. }
  641. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when changing top level.") {
  642. SIGNAL_WATCH(node_i, SNAME("mouse_entered"));
  643. SIGNAL_WATCH(node_i, SNAME("mouse_exited"));
  644. Array signal_args;
  645. signal_args.push_back(Array());
  646. node_d->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  647. node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  648. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  649. // Move to Control node_d.
  650. SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
  651. CHECK(node_b->mouse_over);
  652. CHECK_FALSE(node_b->mouse_over_self);
  653. CHECK(node_d->mouse_over);
  654. CHECK(node_d->mouse_over_self);
  655. // Change node_c to be top level. node_b should receive Mouse Exit.
  656. node_c->set_as_top_level(true);
  657. CHECK_FALSE(node_b->mouse_over);
  658. CHECK_FALSE(node_b->mouse_over_self);
  659. CHECK(node_d->mouse_over);
  660. CHECK(node_d->mouse_over_self);
  661. // Change node_c to be not top level. node_b should receive Mouse Enter.
  662. node_c->set_as_top_level(false);
  663. CHECK(node_b->mouse_over);
  664. CHECK_FALSE(node_b->mouse_over_self);
  665. CHECK(node_d->mouse_over);
  666. CHECK(node_d->mouse_over_self);
  667. // Move to Control node_j.
  668. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  669. CHECK(node_h->mouse_over);
  670. CHECK_FALSE(node_h->mouse_over_self);
  671. CHECK(node_i->mouse_over);
  672. CHECK_FALSE(node_i->mouse_over_self);
  673. CHECK(node_j->mouse_over);
  674. CHECK(node_j->mouse_over_self);
  675. SIGNAL_CHECK(SNAME("mouse_entered"), signal_args);
  676. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  677. // Change node_i to top level. node_h should receive Mouse Exit. node_i should not receive any new signals.
  678. node_i->set_as_top_level(true);
  679. CHECK_FALSE(node_h->mouse_over);
  680. CHECK_FALSE(node_h->mouse_over_self);
  681. CHECK(node_i->mouse_over);
  682. CHECK_FALSE(node_i->mouse_over_self);
  683. CHECK(node_j->mouse_over);
  684. CHECK(node_j->mouse_over_self);
  685. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  686. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  687. // Change node_i to not top level. node_h should receive Mouse Enter. node_i should not receive any new signals.
  688. node_i->set_as_top_level(false);
  689. CHECK(node_h->mouse_over);
  690. CHECK_FALSE(node_h->mouse_over_self);
  691. CHECK(node_i->mouse_over);
  692. CHECK_FALSE(node_i->mouse_over_self);
  693. CHECK(node_j->mouse_over);
  694. CHECK(node_j->mouse_over_self);
  695. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  696. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  697. CHECK_FALSE(node_b->invalid_order);
  698. CHECK_FALSE(node_d->invalid_order);
  699. CHECK_FALSE(node_e->invalid_order);
  700. CHECK_FALSE(node_h->invalid_order);
  701. CHECK_FALSE(node_i->invalid_order);
  702. CHECK_FALSE(node_j->invalid_order);
  703. node_d->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  704. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  705. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  706. SIGNAL_UNWATCH(node_i, SNAME("mouse_entered"));
  707. SIGNAL_UNWATCH(node_i, SNAME("mouse_exited"));
  708. }
  709. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when changing the mouse filter to stop.") {
  710. SIGNAL_WATCH(node_i, SNAME("mouse_entered"));
  711. SIGNAL_WATCH(node_i, SNAME("mouse_exited"));
  712. Array signal_args;
  713. signal_args.push_back(Array());
  714. node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  715. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  716. // Move to Control node_j.
  717. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  718. CHECK(node_h->mouse_over);
  719. CHECK_FALSE(node_h->mouse_over_self);
  720. CHECK(node_i->mouse_over);
  721. CHECK_FALSE(node_i->mouse_over_self);
  722. CHECK(node_j->mouse_over);
  723. CHECK(node_j->mouse_over_self);
  724. SIGNAL_CHECK(SNAME("mouse_entered"), signal_args);
  725. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  726. // Change node_i to MOUSE_FILTER_STOP. node_h should receive Mouse Exit. node_i should not receive any new signals.
  727. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  728. CHECK_FALSE(node_h->mouse_over);
  729. CHECK_FALSE(node_h->mouse_over_self);
  730. CHECK(node_i->mouse_over);
  731. CHECK_FALSE(node_i->mouse_over_self);
  732. CHECK(node_j->mouse_over);
  733. CHECK(node_j->mouse_over_self);
  734. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  735. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  736. // Change node_i to MOUSE_FILTER_PASS. node_h should receive Mouse Enter. node_i should not receive any new signals.
  737. node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  738. CHECK(node_h->mouse_over);
  739. CHECK_FALSE(node_h->mouse_over_self);
  740. CHECK(node_i->mouse_over);
  741. CHECK_FALSE(node_i->mouse_over_self);
  742. CHECK(node_j->mouse_over);
  743. CHECK(node_j->mouse_over_self);
  744. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  745. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  746. CHECK_FALSE(node_h->invalid_order);
  747. CHECK_FALSE(node_i->invalid_order);
  748. CHECK_FALSE(node_j->invalid_order);
  749. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  750. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  751. SIGNAL_UNWATCH(node_i, SNAME("mouse_entered"));
  752. SIGNAL_UNWATCH(node_i, SNAME("mouse_exited"));
  753. }
  754. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when changing the mouse filter to ignore.") {
  755. SIGNAL_WATCH(node_i, SNAME("mouse_entered"));
  756. SIGNAL_WATCH(node_i, SNAME("mouse_exited"));
  757. Array signal_args;
  758. signal_args.push_back(Array());
  759. node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  760. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  761. // Move to Control node_j.
  762. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  763. CHECK(node_h->mouse_over);
  764. CHECK_FALSE(node_h->mouse_over_self);
  765. CHECK(node_i->mouse_over);
  766. CHECK_FALSE(node_i->mouse_over_self);
  767. CHECK(node_j->mouse_over);
  768. CHECK(node_j->mouse_over_self);
  769. SIGNAL_CHECK(SNAME("mouse_entered"), signal_args);
  770. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  771. // Change node_i to MOUSE_FILTER_IGNORE. node_i should receive Mouse Exit.
  772. node_i->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  773. CHECK(node_h->mouse_over);
  774. CHECK_FALSE(node_h->mouse_over_self);
  775. CHECK_FALSE(node_i->mouse_over);
  776. CHECK_FALSE(node_i->mouse_over_self);
  777. CHECK(node_j->mouse_over);
  778. CHECK(node_j->mouse_over_self);
  779. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  780. SIGNAL_CHECK(SNAME("mouse_exited"), signal_args);
  781. // Change node_i to MOUSE_FILTER_PASS. node_i should receive Mouse Enter.
  782. node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  783. CHECK(node_h->mouse_over);
  784. CHECK_FALSE(node_h->mouse_over_self);
  785. CHECK(node_i->mouse_over);
  786. CHECK_FALSE(node_i->mouse_over_self);
  787. CHECK(node_j->mouse_over);
  788. CHECK(node_j->mouse_over_self);
  789. SIGNAL_CHECK(SNAME("mouse_entered"), signal_args);
  790. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  791. // Change node_j to MOUSE_FILTER_IGNORE. After updating the mouse motion, node_i should now have mouse_over_self.
  792. node_j->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  793. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  794. CHECK(node_h->mouse_over);
  795. CHECK_FALSE(node_h->mouse_over_self);
  796. CHECK(node_i->mouse_over);
  797. CHECK(node_i->mouse_over_self);
  798. CHECK_FALSE(node_j->mouse_over);
  799. CHECK_FALSE(node_j->mouse_over_self);
  800. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  801. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  802. // Change node_j to MOUSE_FILTER_PASS. After updating the mouse motion, node_j should now have mouse_over_self.
  803. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  804. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  805. CHECK(node_h->mouse_over);
  806. CHECK_FALSE(node_h->mouse_over_self);
  807. CHECK(node_i->mouse_over);
  808. CHECK_FALSE(node_i->mouse_over_self);
  809. CHECK(node_j->mouse_over);
  810. CHECK(node_j->mouse_over_self);
  811. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  812. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  813. CHECK_FALSE(node_h->invalid_order);
  814. CHECK_FALSE(node_i->invalid_order);
  815. CHECK_FALSE(node_j->invalid_order);
  816. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  817. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  818. SIGNAL_UNWATCH(node_i, SNAME("mouse_entered"));
  819. SIGNAL_UNWATCH(node_i, SNAME("mouse_exited"));
  820. }
  821. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when removing the hovered Control.") {
  822. SIGNAL_WATCH(node_h, SNAME("mouse_entered"));
  823. SIGNAL_WATCH(node_h, SNAME("mouse_exited"));
  824. Array signal_args;
  825. signal_args.push_back(Array());
  826. node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  827. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  828. // Move to Control node_j.
  829. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  830. CHECK(node_h->mouse_over);
  831. CHECK_FALSE(node_h->mouse_over_self);
  832. CHECK(node_i->mouse_over);
  833. CHECK_FALSE(node_i->mouse_over_self);
  834. CHECK(node_j->mouse_over);
  835. CHECK(node_j->mouse_over_self);
  836. SIGNAL_CHECK(SNAME("mouse_entered"), signal_args);
  837. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  838. // Remove node_i from the tree. node_i and node_j should receive Mouse Exit. node_h should not receive any new signals.
  839. node_h->remove_child(node_i);
  840. CHECK(node_h->mouse_over);
  841. CHECK_FALSE(node_h->mouse_over_self);
  842. CHECK_FALSE(node_i->mouse_over);
  843. CHECK_FALSE(node_i->mouse_over_self);
  844. CHECK_FALSE(node_j->mouse_over);
  845. CHECK_FALSE(node_j->mouse_over_self);
  846. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  847. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  848. // 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.
  849. node_h->add_child(node_i);
  850. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  851. CHECK(node_h->mouse_over);
  852. CHECK_FALSE(node_h->mouse_over_self);
  853. CHECK(node_i->mouse_over);
  854. CHECK_FALSE(node_i->mouse_over_self);
  855. CHECK(node_j->mouse_over);
  856. CHECK(node_j->mouse_over_self);
  857. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  858. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  859. CHECK_FALSE(node_h->invalid_order);
  860. CHECK_FALSE(node_i->invalid_order);
  861. CHECK_FALSE(node_j->invalid_order);
  862. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  863. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  864. SIGNAL_UNWATCH(node_h, SNAME("mouse_entered"));
  865. SIGNAL_UNWATCH(node_h, SNAME("mouse_exited"));
  866. }
  867. SUBCASE("[Viewport][GuiInputEvent] Mouse Enter/Exit notification when hiding the hovered Control.") {
  868. SIGNAL_WATCH(node_h, SNAME("mouse_entered"));
  869. SIGNAL_WATCH(node_h, SNAME("mouse_exited"));
  870. Array signal_args;
  871. signal_args.push_back(Array());
  872. node_i->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  873. node_j->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  874. // Move to Control node_j.
  875. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  876. CHECK(node_h->mouse_over);
  877. CHECK_FALSE(node_h->mouse_over_self);
  878. CHECK(node_i->mouse_over);
  879. CHECK_FALSE(node_i->mouse_over_self);
  880. CHECK(node_j->mouse_over);
  881. CHECK(node_j->mouse_over_self);
  882. SIGNAL_CHECK(SNAME("mouse_entered"), signal_args);
  883. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  884. // Hide node_i. node_i and node_j should receive Mouse Exit. node_h should not receive any new signals.
  885. node_i->hide();
  886. CHECK(node_h->mouse_over);
  887. CHECK_FALSE(node_h->mouse_over_self);
  888. CHECK_FALSE(node_i->mouse_over);
  889. CHECK_FALSE(node_i->mouse_over_self);
  890. CHECK_FALSE(node_j->mouse_over);
  891. CHECK_FALSE(node_j->mouse_over_self);
  892. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  893. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  894. // Show node_i and update the mouse. node_i and node_j should receive Mouse Enter. node_h should not receive any new signals.
  895. node_i->show();
  896. SEND_GUI_MOUSE_MOTION_EVENT(on_j, MouseButtonMask::NONE, Key::NONE);
  897. CHECK(node_h->mouse_over);
  898. CHECK_FALSE(node_h->mouse_over_self);
  899. CHECK(node_i->mouse_over);
  900. CHECK_FALSE(node_i->mouse_over_self);
  901. CHECK(node_j->mouse_over);
  902. CHECK(node_j->mouse_over_self);
  903. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  904. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  905. CHECK_FALSE(node_h->invalid_order);
  906. CHECK_FALSE(node_i->invalid_order);
  907. CHECK_FALSE(node_j->invalid_order);
  908. node_i->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  909. node_j->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  910. SIGNAL_UNWATCH(node_h, SNAME("mouse_entered"));
  911. SIGNAL_UNWATCH(node_h, SNAME("mouse_exited"));
  912. }
  913. SUBCASE("[Viewport][GuiInputEvent] Window Mouse Enter/Exit signals.") {
  914. SIGNAL_WATCH(root, SNAME("mouse_entered"));
  915. SIGNAL_WATCH(root, SNAME("mouse_exited"));
  916. Array signal_args;
  917. signal_args.push_back(Array());
  918. SEND_GUI_MOUSE_MOTION_EVENT(on_outside, MouseButtonMask::NONE, Key::NONE);
  919. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  920. SIGNAL_CHECK(SNAME("mouse_exited"), signal_args);
  921. SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::NONE, Key::NONE);
  922. SIGNAL_CHECK(SNAME("mouse_entered"), signal_args);
  923. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  924. SIGNAL_UNWATCH(root, SNAME("mouse_entered"));
  925. SIGNAL_UNWATCH(root, SNAME("mouse_exited"));
  926. }
  927. SUBCASE("[Viewport][GuiInputEvent] Process-Mode affects, if GUI Mouse Motion Events are processed.") {
  928. node_a->last_mouse_move_position = on_outside;
  929. node_a->set_process_mode(Node::PROCESS_MODE_DISABLED);
  930. SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::NONE, Key::NONE);
  931. CHECK(node_a->last_mouse_move_position == on_outside);
  932. // Now verify that with allowed processing the event is processed.
  933. node_a->set_process_mode(Node::PROCESS_MODE_ALWAYS);
  934. SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::NONE, Key::NONE);
  935. CHECK(node_a->last_mouse_move_position == on_a);
  936. }
  937. }
  938. // Unit tests for Viewport::_gui_input_event (Drag and Drop)
  939. SUBCASE("[Viewport][GuiInputEvent] Drag and Drop") {
  940. // FIXME: Drag-Preview will likely change. Tests for this part would have to be rewritten anyway.
  941. // See https://github.com/godotengine/godot/pull/67531#issuecomment-1385353430 for details.
  942. // FIXME: Testing Drag and Drop with non-embedded windows would require DisplayServerMock additions
  943. // FIXME: Drag and Drop currently doesn't work with embedded Windows and SubViewports - not testing.
  944. // See https://github.com/godotengine/godot/issues/28522 for example.
  945. int min_grab_movement = 11;
  946. SUBCASE("[Viewport][GuiInputEvent] Drag from one Control to another in the same viewport.") {
  947. SUBCASE("[Viewport][GuiInputEvent] Perform successful Drag and Drop on a different Control.") {
  948. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  949. CHECK_FALSE(root->gui_is_dragging());
  950. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
  951. CHECK(root->gui_is_dragging());
  952. // Move above a Control, that is a Drop target and allows dropping at this point.
  953. SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::LEFT, Key::NONE);
  954. CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_CAN_DROP);
  955. CHECK(root->gui_is_dragging());
  956. CHECK_FALSE(root->gui_is_drag_successful());
  957. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  958. CHECK_FALSE(root->gui_is_dragging());
  959. CHECK(root->gui_is_drag_successful());
  960. CHECK((StringName)node_d->drag_data == SNAME("Drag Data"));
  961. }
  962. SUBCASE("[Viewport][GuiInputEvent] Perform unsuccessful drop on Control.") {
  963. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  964. CHECK_FALSE(root->gui_is_dragging());
  965. // Move, but don't trigger DnD yet.
  966. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(0, min_grab_movement - 1), MouseButtonMask::LEFT, Key::NONE);
  967. CHECK_FALSE(root->gui_is_dragging());
  968. // Move and trigger DnD.
  969. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
  970. CHECK(root->gui_is_dragging());
  971. // Move above a Control, that is not a Drop target.
  972. SEND_GUI_MOUSE_MOTION_EVENT(on_a, MouseButtonMask::LEFT, Key::NONE);
  973. CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_FORBIDDEN);
  974. // Move above a Control, that is a Drop target, but has disallowed this point.
  975. SEND_GUI_MOUSE_MOTION_EVENT(on_d + Point2i(20, 0), MouseButtonMask::LEFT, Key::NONE);
  976. CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_FORBIDDEN);
  977. CHECK(root->gui_is_dragging());
  978. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d + Point2i(20, 0), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  979. CHECK_FALSE(root->gui_is_dragging());
  980. CHECK_FALSE(root->gui_is_drag_successful());
  981. }
  982. SUBCASE("[Viewport][GuiInputEvent] Perform unsuccessful drop on No-Control.") {
  983. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  984. CHECK_FALSE(root->gui_is_dragging());
  985. // Move, but don't trigger DnD yet.
  986. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement - 1, 0), MouseButtonMask::LEFT, Key::NONE);
  987. CHECK_FALSE(root->gui_is_dragging());
  988. // Move and trigger DnD.
  989. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
  990. CHECK(root->gui_is_dragging());
  991. // Move away from Controls.
  992. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::LEFT, Key::NONE);
  993. CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_ARROW); // This could also be CURSOR_FORBIDDEN.
  994. CHECK(root->gui_is_dragging());
  995. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  996. CHECK_FALSE(root->gui_is_dragging());
  997. CHECK_FALSE(root->gui_is_drag_successful());
  998. }
  999. SUBCASE("[Viewport][GuiInputEvent] Perform unsuccessful drop outside of window.") {
  1000. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  1001. CHECK_FALSE(root->gui_is_dragging());
  1002. // Move and trigger DnD.
  1003. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::LEFT, Key::NONE);
  1004. CHECK(root->gui_is_dragging());
  1005. SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::LEFT, Key::NONE);
  1006. CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_CAN_DROP);
  1007. // Move outside of window.
  1008. SEND_GUI_MOUSE_MOTION_EVENT(on_outside, MouseButtonMask::LEFT, Key::NONE);
  1009. CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_ARROW);
  1010. CHECK(root->gui_is_dragging());
  1011. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_outside, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  1012. CHECK_FALSE(root->gui_is_dragging());
  1013. CHECK_FALSE(root->gui_is_drag_successful());
  1014. }
  1015. SUBCASE("[Viewport][GuiInputEvent] Drag and Drop doesn't work with other Mouse Buttons than LMB.") {
  1016. SEND_GUI_MOUSE_BUTTON_EVENT(on_a, MouseButton::MIDDLE, MouseButtonMask::MIDDLE, Key::NONE);
  1017. CHECK_FALSE(root->gui_is_dragging());
  1018. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(min_grab_movement, 0), MouseButtonMask::MIDDLE, Key::NONE);
  1019. CHECK_FALSE(root->gui_is_dragging());
  1020. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::MIDDLE, MouseButtonMask::NONE, Key::NONE);
  1021. }
  1022. SUBCASE("[Viewport][GuiInputEvent] Drag and Drop parent propagation.") {
  1023. Node2D *node_aa = memnew(Node2D);
  1024. Control *node_aaa = memnew(Control);
  1025. Node2D *node_dd = memnew(Node2D);
  1026. Control *node_ddd = memnew(Control);
  1027. node_aaa->set_size(Size2i(10, 10));
  1028. node_aaa->set_position(Point2i(0, 5));
  1029. node_ddd->set_size(Size2i(10, 10));
  1030. node_ddd->set_position(Point2i(0, 5));
  1031. node_a->add_child(node_aa);
  1032. node_aa->add_child(node_aaa);
  1033. node_d->add_child(node_dd);
  1034. node_dd->add_child(node_ddd);
  1035. Point2i on_aaa = on_a + Point2i(-2, 2);
  1036. Point2i on_ddd = on_d + Point2i(-2, 2);
  1037. SUBCASE("[Viewport][GuiInputEvent] Drag and Drop propagation to parent Controls.") {
  1038. node_aaa->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1039. node_ddd->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1040. SEND_GUI_MOUSE_BUTTON_EVENT(on_aaa, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  1041. CHECK_FALSE(root->gui_is_dragging());
  1042. SEND_GUI_MOUSE_MOTION_EVENT(on_aaa + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
  1043. CHECK(root->gui_is_dragging());
  1044. SEND_GUI_MOUSE_MOTION_EVENT(on_ddd, MouseButtonMask::LEFT, Key::NONE);
  1045. CHECK(root->gui_is_dragging());
  1046. CHECK_FALSE(root->gui_is_drag_successful());
  1047. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_ddd, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  1048. CHECK_FALSE(root->gui_is_dragging());
  1049. CHECK(root->gui_is_drag_successful());
  1050. node_aaa->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1051. node_ddd->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1052. }
  1053. SUBCASE("[Viewport][GuiInputEvent] Drag and Drop grab-propagation stopped by Top Level.") {
  1054. node_aaa->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1055. node_aaa->set_as_top_level(true);
  1056. SEND_GUI_MOUSE_BUTTON_EVENT(on_aaa, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  1057. CHECK_FALSE(root->gui_is_dragging());
  1058. SEND_GUI_MOUSE_MOTION_EVENT(on_aaa + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
  1059. CHECK_FALSE(root->gui_is_dragging());
  1060. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  1061. node_aaa->set_as_top_level(false);
  1062. node_aaa->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1063. }
  1064. SUBCASE("[Viewport][GuiInputEvent] Drag and Drop target-propagation stopped by Top Level.") {
  1065. node_aaa->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1066. node_ddd->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1067. node_ddd->set_as_top_level(true);
  1068. node_ddd->set_position(Point2i(30, 100));
  1069. SEND_GUI_MOUSE_BUTTON_EVENT(on_aaa, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  1070. CHECK_FALSE(root->gui_is_dragging());
  1071. SEND_GUI_MOUSE_MOTION_EVENT(on_aaa + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
  1072. CHECK(root->gui_is_dragging());
  1073. SEND_GUI_MOUSE_MOTION_EVENT(Point2i(35, 105), MouseButtonMask::LEFT, Key::NONE);
  1074. CHECK(root->gui_is_dragging());
  1075. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(Point2i(35, 105), MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  1076. CHECK_FALSE(root->gui_is_dragging());
  1077. CHECK_FALSE(root->gui_is_drag_successful());
  1078. node_ddd->set_position(Point2i(0, 5));
  1079. node_ddd->set_as_top_level(false);
  1080. node_aaa->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1081. node_ddd->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1082. }
  1083. SUBCASE("[Viewport][GuiInputEvent] Drag and Drop grab-propagation stopped by non-CanvasItem.") {
  1084. node_g->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1085. SEND_GUI_MOUSE_BUTTON_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  1086. SEND_GUI_MOUSE_MOTION_EVENT(on_g + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
  1087. CHECK_FALSE(root->gui_is_dragging());
  1088. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_background, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  1089. node_g->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1090. }
  1091. SUBCASE("[Viewport][GuiInputEvent] Drag and Drop target-propagation stopped by non-CanvasItem.") {
  1092. node_g->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1093. SEND_GUI_MOUSE_BUTTON_EVENT(on_a - Point2i(1, 1), MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE); // Offset for node_aaa.
  1094. SEND_GUI_MOUSE_MOTION_EVENT(on_a + Point2i(0, min_grab_movement), MouseButtonMask::LEFT, Key::NONE);
  1095. CHECK(root->gui_is_dragging());
  1096. SEND_GUI_MOUSE_MOTION_EVENT(on_g, MouseButtonMask::LEFT, Key::NONE);
  1097. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_g, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  1098. CHECK_FALSE(root->gui_is_dragging());
  1099. node_g->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1100. }
  1101. memdelete(node_ddd);
  1102. memdelete(node_dd);
  1103. memdelete(node_aaa);
  1104. memdelete(node_aa);
  1105. }
  1106. SUBCASE("[Viewport][GuiInputEvent] Force Drag and Drop.") {
  1107. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  1108. CHECK_FALSE(root->gui_is_dragging());
  1109. node_a->force_drag(SNAME("Drag Data"), nullptr);
  1110. CHECK(root->gui_is_dragging());
  1111. SEND_GUI_MOUSE_MOTION_EVENT(on_d, MouseButtonMask::NONE, Key::NONE);
  1112. // Force Drop doesn't get triggered by mouse Buttons other than LMB.
  1113. SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::RIGHT, MouseButtonMask::RIGHT, Key::NONE);
  1114. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_a, MouseButton::RIGHT, MouseButtonMask::NONE, Key::NONE);
  1115. CHECK(root->gui_is_dragging());
  1116. // Force Drop with LMB-Down.
  1117. SEND_GUI_MOUSE_BUTTON_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  1118. CHECK_FALSE(root->gui_is_dragging());
  1119. CHECK(root->gui_is_drag_successful());
  1120. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_d, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  1121. }
  1122. }
  1123. }
  1124. memdelete(node_j);
  1125. memdelete(node_i);
  1126. memdelete(node_h);
  1127. memdelete(node_g);
  1128. memdelete(node_f);
  1129. memdelete(node_e);
  1130. memdelete(node_d);
  1131. memdelete(node_c);
  1132. memdelete(node_b);
  1133. memdelete(node_a);
  1134. }
  1135. TEST_CASE("[SceneTree][Viewport] Control mouse cursor shape") {
  1136. SUBCASE("[Viewport][CursorShape] Mouse cursor is not overridden by SubViewportContainer") {
  1137. SubViewportContainer *node_a = memnew(SubViewportContainer);
  1138. SubViewport *node_b = memnew(SubViewport);
  1139. Control *node_c = memnew(Control);
  1140. node_a->set_name("SubViewportContainer");
  1141. node_b->set_name("SubViewport");
  1142. node_c->set_name("Control");
  1143. node_a->set_position(Point2i(0, 0));
  1144. node_c->set_position(Point2i(0, 0));
  1145. node_a->set_size(Point2i(100, 100));
  1146. node_b->set_size(Point2i(100, 100));
  1147. node_c->set_size(Point2i(100, 100));
  1148. node_a->set_default_cursor_shape(Control::CURSOR_ARROW);
  1149. node_c->set_default_cursor_shape(Control::CURSOR_FORBIDDEN);
  1150. Window *root = SceneTree::get_singleton()->get_root();
  1151. DisplayServerMock *DS = (DisplayServerMock *)(DisplayServer::get_singleton());
  1152. // Scene tree:
  1153. // - root
  1154. // - node_a (SubViewportContainer)
  1155. // - node_b (SubViewport)
  1156. // - node_c (Control)
  1157. root->add_child(node_a);
  1158. node_a->add_child(node_b);
  1159. node_b->add_child(node_c);
  1160. Point2i on_c = Point2i(5, 5);
  1161. SEND_GUI_MOUSE_MOTION_EVENT(on_c, MouseButtonMask::NONE, Key::NONE);
  1162. CHECK(DS->get_cursor_shape() == DisplayServer::CURSOR_FORBIDDEN); // GH-74805
  1163. memdelete(node_c);
  1164. memdelete(node_b);
  1165. memdelete(node_a);
  1166. }
  1167. }
  1168. class TestArea2D : public Area2D {
  1169. GDCLASS(TestArea2D, Area2D);
  1170. void _on_mouse_entered() {
  1171. enter_id = ++TestArea2D::counter; // > 0, if activated.
  1172. }
  1173. void _on_mouse_exited() {
  1174. exit_id = ++TestArea2D::counter; // > 0, if activated.
  1175. }
  1176. void _on_input_event(Node *p_vp, Ref<InputEvent> p_ev, int p_shape) {
  1177. last_input_event = p_ev;
  1178. }
  1179. public:
  1180. static int counter;
  1181. int enter_id = 0;
  1182. int exit_id = 0;
  1183. Ref<InputEvent> last_input_event;
  1184. void init_signals() {
  1185. connect(SNAME("mouse_entered"), callable_mp(this, &TestArea2D::_on_mouse_entered));
  1186. connect(SNAME("mouse_exited"), callable_mp(this, &TestArea2D::_on_mouse_exited));
  1187. connect(SNAME("input_event"), callable_mp(this, &TestArea2D::_on_input_event));
  1188. }
  1189. void test_reset() {
  1190. enter_id = 0;
  1191. exit_id = 0;
  1192. last_input_event.unref();
  1193. }
  1194. };
  1195. int TestArea2D::counter = 0;
  1196. TEST_CASE("[SceneTree][Viewport] Physics Picking 2D") {
  1197. // FIXME: MOUSE_MODE_CAPTURED if-conditions are not testable, because DisplayServerMock doesn't support it.
  1198. struct PickingCollider {
  1199. TestArea2D *a;
  1200. CollisionShape2D *c;
  1201. Ref<RectangleShape2D> r;
  1202. };
  1203. SceneTree *tree = SceneTree::get_singleton();
  1204. Window *root = tree->get_root();
  1205. root->set_physics_object_picking(true);
  1206. Point2i on_background = Point2i(800, 800);
  1207. Point2i on_outside = Point2i(-1, -1);
  1208. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  1209. tree->physics_process(1);
  1210. Vector<PickingCollider> v;
  1211. for (int i = 0; i < 4; i++) {
  1212. PickingCollider pc;
  1213. pc.a = memnew(TestArea2D);
  1214. pc.c = memnew(CollisionShape2D);
  1215. pc.r = Ref<RectangleShape2D>(memnew(RectangleShape2D));
  1216. pc.r->set_size(Size2(150, 150));
  1217. pc.c->set_shape(pc.r);
  1218. pc.a->add_child(pc.c);
  1219. pc.a->set_name("A" + itos(i));
  1220. pc.c->set_name("C" + itos(i));
  1221. v.push_back(pc);
  1222. SIGNAL_WATCH(pc.a, SNAME("mouse_entered"));
  1223. SIGNAL_WATCH(pc.a, SNAME("mouse_exited"));
  1224. }
  1225. Node2D *node_a = memnew(Node2D);
  1226. node_a->set_position(Point2i(0, 0));
  1227. v[0].a->set_position(Point2i(0, 0));
  1228. v[1].a->set_position(Point2i(0, 100));
  1229. node_a->add_child(v[0].a);
  1230. node_a->add_child(v[1].a);
  1231. Node2D *node_b = memnew(Node2D);
  1232. node_b->set_position(Point2i(100, 0));
  1233. v[2].a->set_position(Point2i(0, 0));
  1234. v[3].a->set_position(Point2i(0, 100));
  1235. node_b->add_child(v[2].a);
  1236. node_b->add_child(v[3].a);
  1237. root->add_child(node_a);
  1238. root->add_child(node_b);
  1239. Point2i on_all = Point2i(50, 50);
  1240. Point2i on_0 = Point2i(10, 10);
  1241. Point2i on_01 = Point2i(10, 50);
  1242. Point2i on_02 = Point2i(50, 10);
  1243. Array empty_signal_args_2;
  1244. empty_signal_args_2.push_back(Array());
  1245. empty_signal_args_2.push_back(Array());
  1246. Array empty_signal_args_4;
  1247. empty_signal_args_4.push_back(Array());
  1248. empty_signal_args_4.push_back(Array());
  1249. empty_signal_args_4.push_back(Array());
  1250. empty_signal_args_4.push_back(Array());
  1251. for (PickingCollider E : v) {
  1252. E.a->init_signals();
  1253. }
  1254. SUBCASE("[Viewport][Picking2D] Mouse Motion") {
  1255. SEND_GUI_MOUSE_MOTION_EVENT(on_all, MouseButtonMask::NONE, Key::NONE);
  1256. tree->physics_process(1);
  1257. SIGNAL_CHECK(SNAME("mouse_entered"), empty_signal_args_4);
  1258. SIGNAL_CHECK_FALSE(SNAME("mouse_exited"));
  1259. for (PickingCollider E : v) {
  1260. CHECK(E.a->enter_id);
  1261. CHECK_FALSE(E.a->exit_id);
  1262. E.a->test_reset();
  1263. }
  1264. SEND_GUI_MOUSE_MOTION_EVENT(on_01, MouseButtonMask::NONE, Key::NONE);
  1265. tree->physics_process(1);
  1266. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  1267. SIGNAL_CHECK(SNAME("mouse_exited"), empty_signal_args_2);
  1268. for (int i = 0; i < v.size(); i++) {
  1269. CHECK_FALSE(v[i].a->enter_id);
  1270. if (i < 2) {
  1271. CHECK_FALSE(v[i].a->exit_id);
  1272. } else {
  1273. CHECK(v[i].a->exit_id);
  1274. }
  1275. v[i].a->test_reset();
  1276. }
  1277. SEND_GUI_MOUSE_MOTION_EVENT(on_outside, MouseButtonMask::NONE, Key::NONE);
  1278. tree->physics_process(1);
  1279. SIGNAL_CHECK_FALSE(SNAME("mouse_entered"));
  1280. SIGNAL_CHECK(SNAME("mouse_exited"), empty_signal_args_2);
  1281. for (int i = 0; i < v.size(); i++) {
  1282. CHECK_FALSE(v[i].a->enter_id);
  1283. if (i < 2) {
  1284. CHECK(v[i].a->exit_id);
  1285. } else {
  1286. CHECK_FALSE(v[i].a->exit_id);
  1287. }
  1288. v[i].a->test_reset();
  1289. }
  1290. }
  1291. SUBCASE("[Viewport][Picking2D] Object moved / passive hovering") {
  1292. SEND_GUI_MOUSE_MOTION_EVENT(on_all, MouseButtonMask::NONE, Key::NONE);
  1293. tree->physics_process(1);
  1294. for (int i = 0; i < v.size(); i++) {
  1295. CHECK(v[i].a->enter_id);
  1296. CHECK_FALSE(v[i].a->exit_id);
  1297. v[i].a->test_reset();
  1298. }
  1299. node_b->set_position(Point2i(200, 0));
  1300. tree->physics_process(1);
  1301. for (int i = 0; i < v.size(); i++) {
  1302. CHECK_FALSE(v[i].a->enter_id);
  1303. if (i < 2) {
  1304. CHECK_FALSE(v[i].a->exit_id);
  1305. } else {
  1306. CHECK(v[i].a->exit_id);
  1307. }
  1308. v[i].a->test_reset();
  1309. }
  1310. node_b->set_position(Point2i(100, 0));
  1311. tree->physics_process(1);
  1312. for (int i = 0; i < v.size(); i++) {
  1313. if (i < 2) {
  1314. CHECK_FALSE(v[i].a->enter_id);
  1315. } else {
  1316. CHECK(v[i].a->enter_id);
  1317. }
  1318. CHECK_FALSE(v[i].a->exit_id);
  1319. v[i].a->test_reset();
  1320. }
  1321. }
  1322. SUBCASE("[Viewport][Picking2D] No Processing") {
  1323. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  1324. tree->physics_process(1);
  1325. for (PickingCollider E : v) {
  1326. E.a->test_reset();
  1327. }
  1328. v[0].a->set_process_mode(Node::PROCESS_MODE_DISABLED);
  1329. v[0].c->set_process_mode(Node::PROCESS_MODE_DISABLED);
  1330. SEND_GUI_MOUSE_MOTION_EVENT(on_02, MouseButtonMask::NONE, Key::NONE);
  1331. tree->physics_process(1);
  1332. CHECK_FALSE(v[0].a->enter_id);
  1333. CHECK_FALSE(v[0].a->exit_id);
  1334. CHECK(v[2].a->enter_id);
  1335. CHECK_FALSE(v[2].a->exit_id);
  1336. for (PickingCollider E : v) {
  1337. E.a->test_reset();
  1338. }
  1339. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  1340. tree->physics_process(1);
  1341. CHECK_FALSE(v[0].a->enter_id);
  1342. CHECK_FALSE(v[0].a->exit_id);
  1343. CHECK_FALSE(v[2].a->enter_id);
  1344. CHECK(v[2].a->exit_id);
  1345. for (PickingCollider E : v) {
  1346. E.a->test_reset();
  1347. }
  1348. v[0].a->set_process_mode(Node::PROCESS_MODE_ALWAYS);
  1349. v[0].c->set_process_mode(Node::PROCESS_MODE_ALWAYS);
  1350. }
  1351. SUBCASE("[Viewport][Picking2D] Multiple events in series") {
  1352. SEND_GUI_MOUSE_MOTION_EVENT(on_0, MouseButtonMask::NONE, Key::NONE);
  1353. SEND_GUI_MOUSE_MOTION_EVENT(on_0 + Point2i(10, 0), MouseButtonMask::NONE, Key::NONE);
  1354. tree->physics_process(1);
  1355. for (int i = 0; i < v.size(); i++) {
  1356. if (i < 1) {
  1357. CHECK(v[i].a->enter_id);
  1358. } else {
  1359. CHECK_FALSE(v[i].a->enter_id);
  1360. }
  1361. CHECK_FALSE(v[i].a->exit_id);
  1362. v[i].a->test_reset();
  1363. }
  1364. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  1365. SEND_GUI_MOUSE_MOTION_EVENT(on_background + Point2i(10, 10), MouseButtonMask::NONE, Key::NONE);
  1366. tree->physics_process(1);
  1367. for (int i = 0; i < v.size(); i++) {
  1368. CHECK_FALSE(v[i].a->enter_id);
  1369. if (i < 1) {
  1370. CHECK(v[i].a->exit_id);
  1371. } else {
  1372. CHECK_FALSE(v[i].a->exit_id);
  1373. }
  1374. v[i].a->test_reset();
  1375. }
  1376. }
  1377. SUBCASE("[Viewport][Picking2D] Disable Picking") {
  1378. SEND_GUI_MOUSE_MOTION_EVENT(on_02, MouseButtonMask::NONE, Key::NONE);
  1379. root->set_physics_object_picking(false);
  1380. CHECK_FALSE(root->get_physics_object_picking());
  1381. tree->physics_process(1);
  1382. for (int i = 0; i < v.size(); i++) {
  1383. CHECK_FALSE(v[i].a->enter_id);
  1384. v[i].a->test_reset();
  1385. }
  1386. root->set_physics_object_picking(true);
  1387. CHECK(root->get_physics_object_picking());
  1388. }
  1389. SUBCASE("[Viewport][Picking2D] CollisionObject in CanvasLayer") {
  1390. CanvasLayer *node_c = memnew(CanvasLayer);
  1391. node_c->set_rotation(Math_PI);
  1392. node_c->set_offset(Point2i(100, 100));
  1393. root->add_child(node_c);
  1394. v[2].a->reparent(node_c, false);
  1395. v[3].a->reparent(node_c, false);
  1396. SEND_GUI_MOUSE_MOTION_EVENT(on_02, MouseButtonMask::NONE, Key::NONE);
  1397. tree->physics_process(1);
  1398. for (int i = 0; i < v.size(); i++) {
  1399. if (i == 0 || i == 3) {
  1400. CHECK(v[i].a->enter_id);
  1401. } else {
  1402. CHECK_FALSE(v[i].a->enter_id);
  1403. }
  1404. v[i].a->test_reset();
  1405. }
  1406. v[2].a->reparent(node_b, false);
  1407. v[3].a->reparent(node_b, false);
  1408. root->remove_child(node_c);
  1409. memdelete(node_c);
  1410. }
  1411. SUBCASE("[Viewport][Picking2D] Picking Sort") {
  1412. root->set_physics_object_picking_sort(true);
  1413. CHECK(root->get_physics_object_picking_sort());
  1414. SUBCASE("[Viewport][Picking2D] Picking Sort Z-Index") {
  1415. node_a->set_z_index(10);
  1416. v[0].a->set_z_index(0);
  1417. v[1].a->set_z_index(2);
  1418. node_b->set_z_index(5);
  1419. v[2].a->set_z_index(8);
  1420. v[3].a->set_z_index(11);
  1421. v[3].a->set_z_as_relative(false);
  1422. TestArea2D::counter = 0;
  1423. SEND_GUI_MOUSE_MOTION_EVENT(on_all, MouseButtonMask::NONE, Key::NONE);
  1424. tree->physics_process(1);
  1425. CHECK(v[0].a->enter_id == 4);
  1426. CHECK(v[1].a->enter_id == 2);
  1427. CHECK(v[2].a->enter_id == 1);
  1428. CHECK(v[3].a->enter_id == 3);
  1429. for (int i = 0; i < v.size(); i++) {
  1430. CHECK_FALSE(v[i].a->exit_id);
  1431. v[i].a->test_reset();
  1432. }
  1433. TestArea2D::counter = 0;
  1434. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  1435. tree->physics_process(1);
  1436. CHECK(v[0].a->exit_id == 4);
  1437. CHECK(v[1].a->exit_id == 2);
  1438. CHECK(v[2].a->exit_id == 1);
  1439. CHECK(v[3].a->exit_id == 3);
  1440. for (int i = 0; i < v.size(); i++) {
  1441. CHECK_FALSE(v[i].a->enter_id);
  1442. v[i].a->set_z_as_relative(true);
  1443. v[i].a->set_z_index(0);
  1444. v[i].a->test_reset();
  1445. }
  1446. node_a->set_z_index(0);
  1447. node_b->set_z_index(0);
  1448. }
  1449. SUBCASE("[Viewport][Picking2D] Picking Sort Scene Tree Location") {
  1450. TestArea2D::counter = 0;
  1451. SEND_GUI_MOUSE_MOTION_EVENT(on_all, MouseButtonMask::NONE, Key::NONE);
  1452. tree->physics_process(1);
  1453. for (int i = 0; i < v.size(); i++) {
  1454. CHECK(v[i].a->enter_id == 4 - i);
  1455. CHECK_FALSE(v[i].a->exit_id);
  1456. v[i].a->test_reset();
  1457. }
  1458. TestArea2D::counter = 0;
  1459. SEND_GUI_MOUSE_MOTION_EVENT(on_background, MouseButtonMask::NONE, Key::NONE);
  1460. tree->physics_process(1);
  1461. for (int i = 0; i < v.size(); i++) {
  1462. CHECK_FALSE(v[i].a->enter_id);
  1463. CHECK(v[i].a->exit_id == 4 - i);
  1464. v[i].a->test_reset();
  1465. }
  1466. }
  1467. root->set_physics_object_picking_sort(false);
  1468. CHECK_FALSE(root->get_physics_object_picking_sort());
  1469. }
  1470. SUBCASE("[Viewport][Picking2D] Mouse Button") {
  1471. SEND_GUI_MOUSE_BUTTON_EVENT(on_0, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
  1472. tree->physics_process(1);
  1473. for (int i = 0; i < v.size(); i++) {
  1474. if (i == 0) {
  1475. CHECK(v[i].a->enter_id);
  1476. } else {
  1477. CHECK_FALSE(v[i].a->enter_id);
  1478. }
  1479. CHECK_FALSE(v[i].a->exit_id);
  1480. v[i].a->test_reset();
  1481. }
  1482. SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(on_0, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
  1483. tree->physics_process(1);
  1484. for (int i = 0; i < v.size(); i++) {
  1485. CHECK_FALSE(v[i].a->enter_id);
  1486. CHECK_FALSE(v[i].a->exit_id);
  1487. v[i].a->test_reset();
  1488. }
  1489. }
  1490. SUBCASE("[Viewport][Picking2D] Screen Touch") {
  1491. SEND_GUI_TOUCH_EVENT(on_01, true, false);
  1492. tree->physics_process(1);
  1493. for (int i = 0; i < v.size(); i++) {
  1494. if (i < 2) {
  1495. Ref<InputEventScreenTouch> st = v[i].a->last_input_event;
  1496. CHECK(st.is_valid());
  1497. } else {
  1498. CHECK(v[i].a->last_input_event.is_null());
  1499. }
  1500. v[i].a->test_reset();
  1501. }
  1502. }
  1503. for (PickingCollider E : v) {
  1504. SIGNAL_UNWATCH(E.a, SNAME("mouse_entered"));
  1505. SIGNAL_UNWATCH(E.a, SNAME("mouse_exited"));
  1506. memdelete(E.c);
  1507. memdelete(E.a);
  1508. }
  1509. }
  1510. TEST_CASE("[SceneTree][Viewport] Embedded Windows") {
  1511. Window *root = SceneTree::get_singleton()->get_root();
  1512. Window *w = memnew(Window);
  1513. SUBCASE("[Viewport] Safe-rect of embedded Window") {
  1514. root->add_child(w);
  1515. root->subwindow_set_popup_safe_rect(w, Rect2i(10, 10, 10, 10));
  1516. CHECK_EQ(root->subwindow_get_popup_safe_rect(w), Rect2i(10, 10, 10, 10));
  1517. root->remove_child(w);
  1518. CHECK_EQ(root->subwindow_get_popup_safe_rect(w), Rect2i());
  1519. }
  1520. memdelete(w);
  1521. }
  1522. } // namespace TestViewport
  1523. #endif // TEST_VIEWPORT_H