test_control.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /**************************************************************************/
  2. /* test_control.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. #pragma once
  31. #include "scene/2d/node_2d.h"
  32. #include "scene/gui/control.h"
  33. #include "tests/test_macros.h"
  34. namespace TestControl {
  35. TEST_CASE("[SceneTree][Control] Transforms") {
  36. SUBCASE("[Control][Global Transform] Global Transform should be accessible while not in SceneTree.") { // GH-79453
  37. Control *test_node = memnew(Control);
  38. Control *test_child = memnew(Control);
  39. test_node->add_child(test_child);
  40. test_node->set_global_position(Point2(1, 1));
  41. CHECK_EQ(test_node->get_global_position(), Point2(1, 1));
  42. CHECK_EQ(test_child->get_global_position(), Point2(1, 1));
  43. test_node->set_global_position(Point2(2, 2));
  44. CHECK_EQ(test_node->get_global_position(), Point2(2, 2));
  45. test_node->set_scale(Vector2(4, 4));
  46. CHECK_EQ(test_node->get_global_transform(), Transform2D(0, Size2(4, 4), 0, Vector2(2, 2)));
  47. test_node->set_scale(Vector2(1, 1));
  48. test_node->set_rotation_degrees(90);
  49. CHECK_EQ(test_node->get_global_transform(), Transform2D(Math::PI / 2, Vector2(2, 2)));
  50. test_node->set_pivot_offset(Vector2(1, 0));
  51. CHECK_EQ(test_node->get_global_transform(), Transform2D(Math::PI / 2, Vector2(3, 1)));
  52. memdelete(test_child);
  53. memdelete(test_node);
  54. }
  55. }
  56. TEST_CASE("[SceneTree][Control] Focus") {
  57. Control *ctrl = memnew(Control);
  58. SceneTree::get_singleton()->get_root()->add_child(ctrl);
  59. SUBCASE("[SceneTree][Control] Default focus") {
  60. CHECK_UNARY_FALSE(ctrl->has_focus());
  61. }
  62. SUBCASE("[SceneTree][Control] Can't grab focus with default focus mode") {
  63. ERR_PRINT_OFF
  64. ctrl->grab_focus();
  65. ERR_PRINT_ON
  66. CHECK_UNARY_FALSE(ctrl->has_focus());
  67. }
  68. SUBCASE("[SceneTree][Control] Can grab focus") {
  69. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  70. ctrl->grab_focus();
  71. CHECK_UNARY(ctrl->has_focus());
  72. }
  73. SUBCASE("[SceneTree][Control] Can release focus") {
  74. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  75. ctrl->grab_focus();
  76. CHECK_UNARY(ctrl->has_focus());
  77. ctrl->release_focus();
  78. CHECK_UNARY_FALSE(ctrl->has_focus());
  79. }
  80. SUBCASE("[SceneTree][Control] Only one can grab focus at the same time") {
  81. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  82. ctrl->grab_focus();
  83. CHECK_UNARY(ctrl->has_focus());
  84. Control *other_ctrl = memnew(Control);
  85. SceneTree::get_singleton()->get_root()->add_child(other_ctrl);
  86. other_ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  87. other_ctrl->grab_focus();
  88. CHECK_UNARY(other_ctrl->has_focus());
  89. CHECK_UNARY_FALSE(ctrl->has_focus());
  90. memdelete(other_ctrl);
  91. }
  92. SUBCASE("[SceneTree][Control] Hide control will cause the focus to be released") {
  93. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  94. ctrl->grab_focus();
  95. CHECK_UNARY(ctrl->has_focus());
  96. ctrl->hide();
  97. CHECK_UNARY_FALSE(ctrl->has_focus());
  98. ctrl->show();
  99. CHECK_UNARY_FALSE(ctrl->has_focus());
  100. }
  101. SUBCASE("[SceneTree][Control] The parent node is hidden causing the focus to be released") {
  102. Control *child_ctrl = memnew(Control);
  103. ctrl->add_child(child_ctrl);
  104. child_ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  105. child_ctrl->grab_focus();
  106. CHECK_UNARY(child_ctrl->has_focus());
  107. ctrl->hide();
  108. CHECK_UNARY_FALSE(child_ctrl->has_focus());
  109. ctrl->show();
  110. CHECK_UNARY_FALSE(child_ctrl->has_focus());
  111. memdelete(child_ctrl);
  112. }
  113. SUBCASE("[SceneTree][Control] Grab focus with focus behavior recursive") {
  114. CHECK_UNARY_FALSE(ctrl->has_focus());
  115. // Cannot grab focus if focus behavior is disabled.
  116. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  117. ctrl->set_focus_behavior_recursive(Control::FOCUS_BEHAVIOR_DISABLED);
  118. ERR_PRINT_OFF
  119. ctrl->grab_focus();
  120. ERR_PRINT_ON
  121. CHECK_UNARY_FALSE(ctrl->has_focus());
  122. // Cannot grab focus if focus behavior is enabled but focus mode is none.
  123. ctrl->set_focus_mode(Control::FocusMode::FOCUS_NONE);
  124. ctrl->set_focus_behavior_recursive(Control::FOCUS_BEHAVIOR_ENABLED);
  125. ERR_PRINT_OFF
  126. ctrl->grab_focus();
  127. ERR_PRINT_ON
  128. CHECK_UNARY_FALSE(ctrl->has_focus());
  129. // Can grab focus if focus behavior is enabled and focus mode is all.
  130. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  131. ctrl->set_focus_behavior_recursive(Control::FOCUS_BEHAVIOR_ENABLED);
  132. ctrl->grab_focus();
  133. CHECK_UNARY(ctrl->has_focus());
  134. }
  135. SUBCASE("[SceneTree][Control] Children focus with focus behavior recursive") {
  136. Control *child_control = memnew(Control);
  137. ctrl->add_child(child_control);
  138. // Can grab focus on child if parent focus behavior is inherit.
  139. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  140. ctrl->set_focus_behavior_recursive(Control::FOCUS_BEHAVIOR_INHERITED);
  141. child_control->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  142. child_control->set_focus_behavior_recursive(Control::FOCUS_BEHAVIOR_INHERITED);
  143. child_control->grab_focus();
  144. CHECK_UNARY(child_control->has_focus());
  145. // Cannot grab focus on child if parent focus behavior is disabled.
  146. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  147. ctrl->set_focus_behavior_recursive(Control::FOCUS_BEHAVIOR_DISABLED);
  148. child_control->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  149. child_control->set_focus_behavior_recursive(Control::FOCUS_BEHAVIOR_INHERITED);
  150. ERR_PRINT_OFF
  151. child_control->grab_focus();
  152. ERR_PRINT_ON
  153. CHECK_UNARY_FALSE(child_control->has_focus());
  154. memdelete(child_control);
  155. }
  156. memdelete(ctrl);
  157. }
  158. TEST_CASE("[SceneTree][Control] Find next/prev valid focus") {
  159. Node *intermediate = memnew(Node);
  160. Control *ctrl = memnew(Control);
  161. intermediate->add_child(ctrl);
  162. SceneTree::get_singleton()->get_root()->add_child(intermediate);
  163. SUBCASE("[SceneTree][Control] In FOCUS_CLICK mode") {
  164. ctrl->set_focus_mode(Control::FocusMode::FOCUS_CLICK);
  165. ctrl->grab_focus();
  166. REQUIRE_UNARY(ctrl->has_focus());
  167. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  168. SEND_GUI_ACTION("ui_focus_next");
  169. CHECK_UNARY(ctrl->has_focus());
  170. }
  171. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  172. SEND_GUI_ACTION("ui_focus_prev");
  173. CHECK_UNARY(ctrl->has_focus());
  174. }
  175. SUBCASE("[SceneTree][Control] Has a sibling control and the parent is a window") {
  176. Control *ctrl1 = memnew(Control);
  177. Control *ctrl2 = memnew(Control);
  178. Control *ctrl3 = memnew(Control);
  179. Window *win = SceneTree::get_singleton()->get_root();
  180. ctrl1->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  181. ctrl2->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  182. ctrl3->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  183. ctrl2->add_child(ctrl3);
  184. win->add_child(ctrl1);
  185. win->add_child(ctrl2);
  186. SUBCASE("[SceneTree][Control] Focus Next") {
  187. ctrl1->grab_focus();
  188. CHECK_UNARY(ctrl1->has_focus());
  189. SEND_GUI_ACTION("ui_focus_next");
  190. CHECK_UNARY(ctrl2->has_focus());
  191. SEND_GUI_ACTION("ui_focus_next");
  192. CHECK_UNARY(ctrl3->has_focus());
  193. SEND_GUI_ACTION("ui_focus_next");
  194. CHECK_UNARY(ctrl1->has_focus());
  195. }
  196. SUBCASE("[SceneTree][Control] Focus Prev") {
  197. ctrl1->grab_focus();
  198. CHECK_UNARY(ctrl1->has_focus());
  199. SEND_GUI_ACTION("ui_focus_prev");
  200. CHECK_UNARY(ctrl3->has_focus());
  201. SEND_GUI_ACTION("ui_focus_prev");
  202. CHECK_UNARY(ctrl2->has_focus());
  203. SEND_GUI_ACTION("ui_focus_prev");
  204. CHECK_UNARY(ctrl1->has_focus());
  205. }
  206. memdelete(ctrl3);
  207. memdelete(ctrl1);
  208. memdelete(ctrl2);
  209. }
  210. SUBCASE("[SceneTree][Control] Has a sibling control but the parent node is not a control or window") {
  211. Control *other_ctrl = memnew(Control);
  212. intermediate->add_child(other_ctrl);
  213. SUBCASE("[SceneTree][Control] Has a sibling control with FOCUS_ALL") {
  214. other_ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  215. REQUIRE_EQ(other_ctrl->get_focus_mode(), Control::FocusMode::FOCUS_ALL);
  216. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  217. SEND_GUI_ACTION("ui_focus_next");
  218. CHECK_UNARY(ctrl->has_focus());
  219. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  220. }
  221. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  222. SEND_GUI_ACTION("ui_focus_prev");
  223. CHECK_UNARY(ctrl->has_focus());
  224. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  225. }
  226. SUBCASE("[SceneTree][Control] Manually specify focus next") {
  227. ctrl->set_focus_next(ctrl->get_path_to(other_ctrl));
  228. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  229. SEND_GUI_ACTION("ui_focus_next");
  230. CHECK_UNARY_FALSE(ctrl->has_focus());
  231. CHECK_UNARY(other_ctrl->has_focus());
  232. }
  233. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  234. SEND_GUI_ACTION("ui_focus_prev");
  235. CHECK_UNARY(ctrl->has_focus());
  236. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  237. }
  238. SUBCASE("[SceneTree][Control] Manually specified focus next is hidden") {
  239. other_ctrl->hide();
  240. REQUIRE_UNARY_FALSE(other_ctrl->is_visible());
  241. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  242. SEND_GUI_ACTION("ui_focus_next");
  243. CHECK_UNARY(ctrl->has_focus());
  244. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  245. }
  246. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  247. SEND_GUI_ACTION("ui_focus_prev");
  248. CHECK_UNARY(ctrl->has_focus());
  249. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  250. }
  251. }
  252. }
  253. SUBCASE("[SceneTree][Control] Manually specify focus prev") {
  254. ctrl->set_focus_previous(ctrl->get_path_to(other_ctrl));
  255. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  256. SEND_GUI_ACTION("ui_focus_next");
  257. CHECK_UNARY(ctrl->has_focus());
  258. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  259. }
  260. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  261. SEND_GUI_ACTION("ui_focus_prev");
  262. CHECK_UNARY_FALSE(ctrl->has_focus());
  263. CHECK_UNARY(other_ctrl->has_focus());
  264. }
  265. SUBCASE("[SceneTree][Control] Manually specified focus next is hidden") {
  266. other_ctrl->hide();
  267. REQUIRE_UNARY_FALSE(other_ctrl->is_visible());
  268. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  269. SEND_GUI_ACTION("ui_focus_next");
  270. CHECK_UNARY(ctrl->has_focus());
  271. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  272. }
  273. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  274. SEND_GUI_ACTION("ui_focus_prev");
  275. CHECK_UNARY(ctrl->has_focus());
  276. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  277. }
  278. }
  279. }
  280. }
  281. SUBCASE("[SceneTree][Control] Has a sibling control with FOCUS_CLICK") {
  282. other_ctrl->set_focus_mode(Control::FocusMode::FOCUS_CLICK);
  283. REQUIRE_EQ(other_ctrl->get_focus_mode(), Control::FocusMode::FOCUS_CLICK);
  284. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  285. SEND_GUI_ACTION("ui_focus_next");
  286. CHECK_UNARY(ctrl->has_focus());
  287. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  288. }
  289. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  290. SEND_GUI_ACTION("ui_focus_prev");
  291. CHECK_UNARY(ctrl->has_focus());
  292. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  293. }
  294. SUBCASE("[SceneTree][Control] Manually specify focus next") {
  295. ctrl->set_focus_next(ctrl->get_path_to(other_ctrl));
  296. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  297. SEND_GUI_ACTION("ui_focus_next");
  298. CHECK_UNARY_FALSE(ctrl->has_focus());
  299. CHECK_UNARY(other_ctrl->has_focus());
  300. }
  301. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  302. SEND_GUI_ACTION("ui_focus_prev");
  303. CHECK_UNARY(ctrl->has_focus());
  304. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  305. }
  306. }
  307. SUBCASE("[SceneTree][Control] Manually specify focus prev") {
  308. ctrl->set_focus_previous(ctrl->get_path_to(other_ctrl));
  309. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  310. SEND_GUI_ACTION("ui_focus_next");
  311. CHECK_UNARY(ctrl->has_focus());
  312. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  313. }
  314. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  315. SEND_GUI_ACTION("ui_focus_prev");
  316. CHECK_UNARY_FALSE(ctrl->has_focus());
  317. CHECK_UNARY(other_ctrl->has_focus());
  318. }
  319. }
  320. }
  321. SUBCASE("[SceneTree][Control] Has a sibling control with FOCUS_NONE") {
  322. other_ctrl->set_focus_mode(Control::FocusMode::FOCUS_NONE);
  323. REQUIRE_EQ(other_ctrl->get_focus_mode(), Control::FocusMode::FOCUS_NONE);
  324. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  325. SEND_GUI_ACTION("ui_focus_next");
  326. CHECK_UNARY(ctrl->has_focus());
  327. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  328. }
  329. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  330. SEND_GUI_ACTION("ui_focus_prev");
  331. CHECK_UNARY(ctrl->has_focus());
  332. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  333. }
  334. SUBCASE("[SceneTree][Control] Manually specify focus next") {
  335. ctrl->set_focus_next(ctrl->get_path_to(other_ctrl));
  336. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  337. SEND_GUI_ACTION("ui_focus_next");
  338. CHECK_UNARY(ctrl->has_focus());
  339. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  340. }
  341. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  342. SEND_GUI_ACTION("ui_focus_prev");
  343. CHECK_UNARY(ctrl->has_focus());
  344. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  345. }
  346. }
  347. SUBCASE("[SceneTree][Control] Manually specify focus prev") {
  348. ctrl->set_focus_previous(ctrl->get_path_to(other_ctrl));
  349. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  350. SEND_GUI_ACTION("ui_focus_next");
  351. CHECK_UNARY(ctrl->has_focus());
  352. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  353. }
  354. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  355. SEND_GUI_ACTION("ui_focus_prev");
  356. CHECK_UNARY(ctrl->has_focus());
  357. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  358. }
  359. }
  360. }
  361. memdelete(other_ctrl);
  362. }
  363. }
  364. SUBCASE("[SceneTree][Control] In FOCUS_ALL mode") {
  365. ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  366. REQUIRE_EQ(ctrl->get_focus_mode(), Control::FocusMode::FOCUS_ALL);
  367. ctrl->grab_focus();
  368. REQUIRE_UNARY(ctrl->has_focus());
  369. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  370. SEND_GUI_ACTION("ui_focus_next");
  371. CHECK_UNARY(ctrl->has_focus());
  372. }
  373. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  374. SEND_GUI_ACTION("ui_focus_prev");
  375. CHECK_UNARY(ctrl->has_focus());
  376. }
  377. SUBCASE("[SceneTree][Control] Has a sibling control but the parent node is not a control") {
  378. Control *other_ctrl = memnew(Control);
  379. SceneTree::get_singleton()->get_root()->add_child(other_ctrl);
  380. SUBCASE("[SceneTree][Control] Has a sibling control with FOCUS_ALL") {
  381. other_ctrl->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  382. REQUIRE_EQ(other_ctrl->get_focus_mode(), Control::FocusMode::FOCUS_ALL);
  383. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  384. SEND_GUI_ACTION("ui_focus_next");
  385. CHECK_UNARY(ctrl->has_focus());
  386. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  387. }
  388. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  389. SEND_GUI_ACTION("ui_focus_prev");
  390. CHECK_UNARY(ctrl->has_focus());
  391. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  392. }
  393. SUBCASE("[SceneTree][Control] Manually specify focus next") {
  394. ctrl->set_focus_next(ctrl->get_path_to(other_ctrl));
  395. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  396. SEND_GUI_ACTION("ui_focus_next");
  397. CHECK_UNARY_FALSE(ctrl->has_focus());
  398. CHECK_UNARY(other_ctrl->has_focus());
  399. }
  400. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  401. SEND_GUI_ACTION("ui_focus_prev");
  402. CHECK_UNARY(ctrl->has_focus());
  403. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  404. }
  405. SUBCASE("[SceneTree][Control] Manually specified focus next is hidden") {
  406. other_ctrl->hide();
  407. REQUIRE_UNARY_FALSE(other_ctrl->is_visible());
  408. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  409. SEND_GUI_ACTION("ui_focus_next");
  410. CHECK_UNARY(ctrl->has_focus());
  411. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  412. }
  413. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  414. SEND_GUI_ACTION("ui_focus_prev");
  415. CHECK_UNARY(ctrl->has_focus());
  416. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  417. }
  418. }
  419. }
  420. SUBCASE("[SceneTree][Control] Manually specify focus prev") {
  421. ctrl->set_focus_previous(ctrl->get_path_to(other_ctrl));
  422. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  423. SEND_GUI_ACTION("ui_focus_next");
  424. CHECK_UNARY(ctrl->has_focus());
  425. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  426. }
  427. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  428. SEND_GUI_ACTION("ui_focus_prev");
  429. CHECK_UNARY_FALSE(ctrl->has_focus());
  430. CHECK_UNARY(other_ctrl->has_focus());
  431. }
  432. SUBCASE("[SceneTree][Control] Manually specified focus next is hidden") {
  433. other_ctrl->hide();
  434. REQUIRE_UNARY_FALSE(other_ctrl->is_visible());
  435. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  436. SEND_GUI_ACTION("ui_focus_next");
  437. CHECK_UNARY(ctrl->has_focus());
  438. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  439. }
  440. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  441. SEND_GUI_ACTION("ui_focus_prev");
  442. CHECK_UNARY(ctrl->has_focus());
  443. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  444. }
  445. }
  446. }
  447. }
  448. SUBCASE("[SceneTree][Control] Has a sibling control with FOCUS_CLICK") {
  449. other_ctrl->set_focus_mode(Control::FocusMode::FOCUS_CLICK);
  450. REQUIRE_EQ(other_ctrl->get_focus_mode(), Control::FocusMode::FOCUS_CLICK);
  451. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  452. SEND_GUI_ACTION("ui_focus_next");
  453. CHECK_UNARY(ctrl->has_focus());
  454. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  455. }
  456. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  457. SEND_GUI_ACTION("ui_focus_prev");
  458. CHECK_UNARY(ctrl->has_focus());
  459. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  460. }
  461. SUBCASE("[SceneTree][Control] Manually specify focus next") {
  462. ctrl->set_focus_next(ctrl->get_path_to(other_ctrl));
  463. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  464. SEND_GUI_ACTION("ui_focus_next");
  465. CHECK_UNARY_FALSE(ctrl->has_focus());
  466. CHECK_UNARY(other_ctrl->has_focus());
  467. }
  468. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  469. SEND_GUI_ACTION("ui_focus_prev");
  470. CHECK_UNARY(ctrl->has_focus());
  471. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  472. }
  473. }
  474. SUBCASE("[SceneTree][Control] Manually specify focus prev") {
  475. ctrl->set_focus_previous(ctrl->get_path_to(other_ctrl));
  476. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  477. SEND_GUI_ACTION("ui_focus_next");
  478. CHECK_UNARY(ctrl->has_focus());
  479. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  480. }
  481. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  482. SEND_GUI_ACTION("ui_focus_prev");
  483. CHECK_UNARY_FALSE(ctrl->has_focus());
  484. CHECK_UNARY(other_ctrl->has_focus());
  485. }
  486. }
  487. }
  488. SUBCASE("[SceneTree][Control] Has a sibling control with FOCUS_NONE") {
  489. other_ctrl->set_focus_mode(Control::FocusMode::FOCUS_NONE);
  490. REQUIRE_EQ(other_ctrl->get_focus_mode(), Control::FocusMode::FOCUS_NONE);
  491. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  492. SEND_GUI_ACTION("ui_focus_next");
  493. CHECK_UNARY(ctrl->has_focus());
  494. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  495. }
  496. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  497. SEND_GUI_ACTION("ui_focus_prev");
  498. CHECK_UNARY(ctrl->has_focus());
  499. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  500. }
  501. SUBCASE("[SceneTree][Control] Manually specify focus next") {
  502. ctrl->set_focus_next(ctrl->get_path_to(other_ctrl));
  503. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  504. SEND_GUI_ACTION("ui_focus_next");
  505. CHECK_UNARY(ctrl->has_focus());
  506. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  507. }
  508. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  509. SEND_GUI_ACTION("ui_focus_prev");
  510. CHECK_UNARY(ctrl->has_focus());
  511. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  512. }
  513. }
  514. SUBCASE("[SceneTree][Control] Manually specify focus prev") {
  515. ctrl->set_focus_previous(ctrl->get_path_to(other_ctrl));
  516. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  517. SEND_GUI_ACTION("ui_focus_next");
  518. CHECK_UNARY(ctrl->has_focus());
  519. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  520. }
  521. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  522. SEND_GUI_ACTION("ui_focus_prev");
  523. CHECK_UNARY(ctrl->has_focus());
  524. CHECK_UNARY_FALSE(other_ctrl->has_focus());
  525. }
  526. }
  527. }
  528. memdelete(other_ctrl);
  529. }
  530. SUBCASE("[SceneTree][Control] Simple control tree") {
  531. Control *ctrl_0 = memnew(Control);
  532. Control *ctrl_1 = memnew(Control);
  533. Node2D *node_2d_2 = memnew(Node2D);
  534. ctrl->add_child(ctrl_0);
  535. ctrl->add_child(ctrl_1);
  536. ctrl->add_child(node_2d_2);
  537. ctrl_0->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  538. ctrl_1->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  539. REQUIRE_EQ(ctrl_0->get_focus_mode(), Control::FocusMode::FOCUS_ALL);
  540. REQUIRE_EQ(ctrl_1->get_focus_mode(), Control::FocusMode::FOCUS_ALL);
  541. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  542. SEND_GUI_ACTION("ui_focus_next");
  543. CHECK_UNARY(ctrl_0->has_focus());
  544. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  545. SEND_GUI_ACTION("ui_focus_next");
  546. CHECK_UNARY(ctrl_1->has_focus());
  547. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  548. SEND_GUI_ACTION("ui_focus_next");
  549. CHECK_UNARY(ctrl->has_focus());
  550. }
  551. }
  552. }
  553. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  554. SEND_GUI_ACTION("ui_focus_prev");
  555. CHECK_UNARY(ctrl_1->has_focus());
  556. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  557. SEND_GUI_ACTION("ui_focus_prev");
  558. CHECK_UNARY(ctrl_0->has_focus());
  559. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  560. SEND_GUI_ACTION("ui_focus_prev");
  561. CHECK_UNARY(ctrl->has_focus());
  562. }
  563. }
  564. }
  565. SUBCASE("[SceneTree][Control] Skip next hidden control") {
  566. ctrl_0->hide();
  567. REQUIRE_UNARY_FALSE(ctrl_0->is_visible());
  568. SEND_GUI_ACTION("ui_focus_next");
  569. CHECK_UNARY_FALSE(ctrl_0->has_focus());
  570. CHECK_UNARY(ctrl_1->has_focus());
  571. }
  572. SUBCASE("[SceneTree][Control] Skip next control with FOCUS_NONE") {
  573. ctrl_0->set_focus_mode(Control::FocusMode::FOCUS_NONE);
  574. REQUIRE_EQ(ctrl_0->get_focus_mode(), Control::FocusMode::FOCUS_NONE);
  575. SEND_GUI_ACTION("ui_focus_next");
  576. CHECK_UNARY_FALSE(ctrl_0->has_focus());
  577. CHECK_UNARY(ctrl_1->has_focus());
  578. }
  579. SUBCASE("[SceneTree][Control] Skip next control with FOCUS_CLICK") {
  580. ctrl_0->set_focus_mode(Control::FocusMode::FOCUS_CLICK);
  581. REQUIRE_EQ(ctrl_0->get_focus_mode(), Control::FocusMode::FOCUS_CLICK);
  582. SEND_GUI_ACTION("ui_focus_next");
  583. CHECK_UNARY_FALSE(ctrl_0->has_focus());
  584. CHECK_UNARY(ctrl_1->has_focus());
  585. }
  586. SUBCASE("[SceneTree][Control] Skip next top level control") {
  587. ctrl_0->set_as_top_level(true);
  588. REQUIRE_UNARY(ctrl_0->is_set_as_top_level());
  589. SEND_GUI_ACTION("ui_focus_next");
  590. CHECK_UNARY_FALSE(ctrl_0->has_focus());
  591. CHECK_UNARY(ctrl_1->has_focus());
  592. }
  593. SUBCASE("[SceneTree][Control] Skip prev hidden control") {
  594. ctrl_1->hide();
  595. REQUIRE_UNARY_FALSE(ctrl_1->is_visible());
  596. SEND_GUI_ACTION("ui_focus_prev");
  597. CHECK_UNARY_FALSE(ctrl_1->has_focus());
  598. CHECK_UNARY(ctrl_0->has_focus());
  599. }
  600. SUBCASE("[SceneTree][Control] Skip prev control with FOCUS_NONE") {
  601. ctrl_1->set_focus_mode(Control::FocusMode::FOCUS_NONE);
  602. REQUIRE_EQ(ctrl_1->get_focus_mode(), Control::FocusMode::FOCUS_NONE);
  603. SEND_GUI_ACTION("ui_focus_prev");
  604. CHECK_UNARY_FALSE(ctrl_1->has_focus());
  605. CHECK_UNARY(ctrl_0->has_focus());
  606. }
  607. SUBCASE("[SceneTree][Control] Skip prev control with FOCUS_CLICK") {
  608. ctrl_1->set_focus_mode(Control::FocusMode::FOCUS_CLICK);
  609. REQUIRE_EQ(ctrl_1->get_focus_mode(), Control::FocusMode::FOCUS_CLICK);
  610. SEND_GUI_ACTION("ui_focus_prev");
  611. CHECK_UNARY_FALSE(ctrl_1->has_focus());
  612. CHECK_UNARY(ctrl_0->has_focus());
  613. }
  614. SUBCASE("[SceneTree][Control] Skip prev top level control") {
  615. ctrl_1->set_as_top_level(true);
  616. REQUIRE_UNARY(ctrl_1->is_set_as_top_level());
  617. SEND_GUI_ACTION("ui_focus_prev");
  618. CHECK_UNARY_FALSE(ctrl_1->has_focus());
  619. CHECK_UNARY(ctrl_0->has_focus());
  620. }
  621. SUBCASE("[SceneTree][Control] Add more node controls") {
  622. Control *ctrl_0_0 = memnew(Control);
  623. Control *ctrl_0_1 = memnew(Control);
  624. Control *ctrl_0_2 = memnew(Control);
  625. ctrl_0->add_child(ctrl_0_0);
  626. ctrl_0->add_child(ctrl_0_1);
  627. ctrl_0->add_child(ctrl_0_2);
  628. ctrl_0_0->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  629. ctrl_0_1->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  630. ctrl_0_2->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  631. Control *ctrl_1_0 = memnew(Control);
  632. Control *ctrl_1_1 = memnew(Control);
  633. Control *ctrl_1_2 = memnew(Control);
  634. ctrl_1->add_child(ctrl_1_0);
  635. ctrl_1->add_child(ctrl_1_1);
  636. ctrl_1->add_child(ctrl_1_2);
  637. ctrl_1_0->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  638. ctrl_1_1->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  639. ctrl_1_2->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  640. Control *ctrl_2_0 = memnew(Control);
  641. Control *ctrl_2_1 = memnew(Control);
  642. Control *ctrl_2_2 = memnew(Control);
  643. node_2d_2->add_child(ctrl_2_0);
  644. node_2d_2->add_child(ctrl_2_1);
  645. node_2d_2->add_child(ctrl_2_2);
  646. ctrl_2_0->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  647. ctrl_2_1->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  648. ctrl_2_2->set_focus_mode(Control::FocusMode::FOCUS_ALL);
  649. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  650. SEND_GUI_ACTION("ui_focus_next");
  651. CHECK_UNARY(ctrl_0->has_focus());
  652. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  653. SEND_GUI_ACTION("ui_focus_next");
  654. CHECK_UNARY(ctrl_0_0->has_focus());
  655. }
  656. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  657. SEND_GUI_ACTION("ui_focus_prev");
  658. CHECK_UNARY(ctrl->has_focus());
  659. }
  660. }
  661. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  662. SEND_GUI_ACTION("ui_focus_prev");
  663. CHECK_UNARY(ctrl_1_2->has_focus());
  664. }
  665. SUBCASE("[SceneTree][Control] Exist top level tree") {
  666. ctrl_0->set_as_top_level(true);
  667. REQUIRE_UNARY(ctrl_0->is_set_as_top_level());
  668. SUBCASE("[SceneTree][Control] Outside top level tree") {
  669. ctrl->grab_focus();
  670. REQUIRE_UNARY(ctrl->has_focus());
  671. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  672. SEND_GUI_ACTION("ui_focus_next");
  673. CHECK_UNARY(ctrl_1->has_focus());
  674. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  675. SEND_GUI_ACTION("ui_focus_prev");
  676. CHECK_UNARY(ctrl->has_focus());
  677. }
  678. }
  679. }
  680. SUBCASE("[SceneTree][Control] Inside top level tree") {
  681. ctrl_0->grab_focus();
  682. REQUIRE_UNARY(ctrl_0->has_focus());
  683. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  684. SEND_GUI_ACTION("ui_focus_next");
  685. CHECK_UNARY(ctrl_0_0->has_focus());
  686. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  687. SEND_GUI_ACTION("ui_focus_prev");
  688. CHECK_UNARY(ctrl_0->has_focus());
  689. }
  690. }
  691. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  692. SEND_GUI_ACTION("ui_focus_prev");
  693. CHECK_UNARY(ctrl_0_2->has_focus());
  694. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  695. SEND_GUI_ACTION("ui_focus_next");
  696. CHECK_UNARY(ctrl_0->has_focus());
  697. }
  698. }
  699. }
  700. SUBCASE("[SceneTree][Control] Manually specified focus next") {
  701. ctrl->set_focus_next(ctrl->get_path_to(ctrl_2_1));
  702. ctrl_2_1->set_focus_next(ctrl_2_1->get_path_to(ctrl_1_0));
  703. ctrl_1_0->set_focus_next(ctrl_1_0->get_path_to(ctrl_0));
  704. ctrl_0->set_focus_next(ctrl_0->get_path_to(ctrl));
  705. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  706. SEND_GUI_ACTION("ui_focus_next");
  707. CHECK_UNARY(ctrl_2_1->has_focus());
  708. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  709. SEND_GUI_ACTION("ui_focus_next");
  710. CHECK_UNARY(ctrl_1_0->has_focus());
  711. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  712. SEND_GUI_ACTION("ui_focus_next");
  713. CHECK_UNARY(ctrl_0->has_focus());
  714. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  715. SEND_GUI_ACTION("ui_focus_next");
  716. CHECK_UNARY(ctrl->has_focus());
  717. }
  718. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  719. SEND_GUI_ACTION("ui_focus_prev");
  720. CHECK_UNARY(ctrl_0_2->has_focus());
  721. }
  722. }
  723. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  724. SEND_GUI_ACTION("ui_focus_prev");
  725. CHECK_UNARY(ctrl_1->has_focus());
  726. }
  727. }
  728. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  729. SEND_GUI_ACTION("ui_focus_prev");
  730. CHECK_UNARY(ctrl_2_1->has_focus());
  731. }
  732. }
  733. SUBCASE("[SceneTree][Control] The parent node is not visible") {
  734. node_2d_2->hide();
  735. REQUIRE_UNARY(ctrl_2_1->is_visible());
  736. REQUIRE_UNARY_FALSE(ctrl_2_1->is_visible_in_tree());
  737. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  738. SEND_GUI_ACTION("ui_focus_next");
  739. CHECK_UNARY_FALSE(ctrl->has_focus());
  740. CHECK_UNARY_FALSE(ctrl_2_1->has_focus());
  741. CHECK_UNARY_FALSE(ctrl_0->has_focus());
  742. CHECK_UNARY(ctrl_1->has_focus());
  743. }
  744. }
  745. }
  746. SUBCASE("[SceneTree][Control] Manually specified focus prev") {
  747. ctrl->set_focus_previous(ctrl->get_path_to(ctrl_0_2));
  748. ctrl_0_2->set_focus_previous(ctrl_0_2->get_path_to(ctrl_1_1));
  749. ctrl_1_1->set_focus_previous(ctrl_1_1->get_path_to(ctrl_2_0));
  750. ctrl_2_0->set_focus_previous(ctrl_2_0->get_path_to(ctrl));
  751. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  752. SEND_GUI_ACTION("ui_focus_prev");
  753. CHECK_UNARY(ctrl_0_2->has_focus());
  754. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  755. SEND_GUI_ACTION("ui_focus_prev");
  756. CHECK_UNARY(ctrl_1_1->has_focus());
  757. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  758. SEND_GUI_ACTION("ui_focus_prev");
  759. CHECK_UNARY(ctrl_2_0->has_focus());
  760. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  761. SEND_GUI_ACTION("ui_focus_prev");
  762. CHECK_UNARY(ctrl->has_focus());
  763. }
  764. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  765. SEND_GUI_ACTION("ui_focus_next");
  766. CHECK_UNARY(ctrl_2_0->has_focus());
  767. }
  768. }
  769. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  770. SEND_GUI_ACTION("ui_focus_next");
  771. CHECK_UNARY(ctrl_1_2->has_focus());
  772. }
  773. }
  774. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  775. SEND_GUI_ACTION("ui_focus_next");
  776. CHECK_UNARY(ctrl_0->has_focus());
  777. }
  778. }
  779. SUBCASE("[SceneTree][Control] The parent node is not visible") {
  780. ctrl_0->hide();
  781. REQUIRE_UNARY(ctrl_0_2->is_visible());
  782. REQUIRE_UNARY_FALSE(ctrl_0_2->is_visible_in_tree());
  783. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  784. SEND_GUI_ACTION("ui_focus_prev");
  785. CHECK_UNARY_FALSE(ctrl->has_focus());
  786. CHECK_UNARY_FALSE(ctrl_0_2->has_focus());
  787. CHECK_UNARY(ctrl_1_2->has_focus());
  788. }
  789. }
  790. }
  791. }
  792. SUBCASE("[SceneTree][Control] Exist hidden control tree") {
  793. ctrl_0->hide();
  794. REQUIRE_UNARY_FALSE(ctrl_0->is_visible());
  795. SUBCASE("[SceneTree][Control] Simulate ui_focus_next action") {
  796. SEND_GUI_ACTION("ui_focus_next");
  797. CHECK_UNARY(ctrl_1->has_focus());
  798. SUBCASE("[SceneTree][Control] Simulate ui_focus_prev action") {
  799. SEND_GUI_ACTION("ui_focus_prev");
  800. CHECK_UNARY(ctrl->has_focus());
  801. }
  802. }
  803. }
  804. memdelete(ctrl_2_2);
  805. memdelete(ctrl_2_1);
  806. memdelete(ctrl_2_0);
  807. memdelete(ctrl_1_2);
  808. memdelete(ctrl_1_1);
  809. memdelete(ctrl_1_0);
  810. memdelete(ctrl_0_2);
  811. memdelete(ctrl_0_1);
  812. memdelete(ctrl_0_0);
  813. }
  814. memdelete(node_2d_2);
  815. memdelete(ctrl_1);
  816. memdelete(ctrl_0);
  817. }
  818. }
  819. memdelete(ctrl);
  820. memdelete(intermediate);
  821. }
  822. TEST_CASE("[SceneTree][Control] Anchoring") {
  823. Control *test_control = memnew(Control);
  824. Control *test_child = memnew(Control);
  825. test_control->add_child(test_child);
  826. test_control->set_size(Size2(2, 2));
  827. Window *root = SceneTree::get_singleton()->get_root();
  828. root->add_child(test_control);
  829. SUBCASE("Anchoring without offsets") {
  830. test_child->set_anchor(SIDE_RIGHT, 0.75);
  831. test_child->set_anchor(SIDE_BOTTOM, 0.1);
  832. CHECK_MESSAGE(
  833. test_child->get_size().is_equal_approx(Vector2(1.5, 0.2)),
  834. "With no LEFT or TOP anchors, positive RIGHT and BOTTOM anchors should be proportional to the size.");
  835. CHECK_MESSAGE(
  836. test_child->get_position().is_equal_approx(Vector2(0, 0)),
  837. "With positive RIGHT and BOTTOM anchors set and no LEFT or TOP anchors, the position should not change.");
  838. test_child->set_anchor(SIDE_LEFT, 0.5);
  839. test_child->set_anchor(SIDE_TOP, 0.01);
  840. CHECK_MESSAGE(
  841. test_child->get_size().is_equal_approx(Vector2(0.5, 0.18)),
  842. "With all anchors set, the size should fit between all four anchors.");
  843. CHECK_MESSAGE(
  844. test_child->get_position().is_equal_approx(Vector2(1, 0.02)),
  845. "With all anchors set, the LEFT and TOP anchors should proportional to the position.");
  846. }
  847. SUBCASE("Anchoring with offsets") {
  848. test_child->set_offset(SIDE_RIGHT, 0.33);
  849. test_child->set_offset(SIDE_BOTTOM, 0.2);
  850. CHECK_MESSAGE(
  851. test_child->get_size().is_equal_approx(Vector2(0.33, 0.2)),
  852. "With no anchors or LEFT or TOP offsets set, the RIGHT and BOTTOM offsets should be equal to size.");
  853. CHECK_MESSAGE(
  854. test_child->get_position().is_equal_approx(Vector2(0, 0)),
  855. "With only positive RIGHT and BOTTOM offsets set, the position should not change.");
  856. test_child->set_offset(SIDE_LEFT, 0.1);
  857. test_child->set_offset(SIDE_TOP, 0.05);
  858. CHECK_MESSAGE(
  859. test_child->get_size().is_equal_approx(Vector2(0.23, 0.15)),
  860. "With no anchors set, the size should fit between all four offsets.");
  861. CHECK_MESSAGE(
  862. test_child->get_position().is_equal_approx(Vector2(0.1, 0.05)),
  863. "With no anchors set, the LEFT and TOP offsets should be equal to the position.");
  864. test_child->set_anchor(SIDE_RIGHT, 0.5);
  865. test_child->set_anchor(SIDE_BOTTOM, 0.3);
  866. test_child->set_anchor(SIDE_LEFT, 0.2);
  867. test_child->set_anchor(SIDE_TOP, 0.1);
  868. CHECK_MESSAGE(
  869. test_child->get_size().is_equal_approx(Vector2(0.83, 0.55)),
  870. "Anchors adjust size first then it is affected by offsets.");
  871. CHECK_MESSAGE(
  872. test_child->get_position().is_equal_approx(Vector2(0.5, 0.25)),
  873. "Anchors adjust positions first then it is affected by offsets.");
  874. test_child->set_offset(SIDE_RIGHT, -0.1);
  875. test_child->set_offset(SIDE_BOTTOM, -0.01);
  876. test_child->set_offset(SIDE_LEFT, -0.33);
  877. test_child->set_offset(SIDE_TOP, -0.16);
  878. CHECK_MESSAGE(
  879. test_child->get_size().is_equal_approx(Vector2(0.83, 0.55)),
  880. "Keeping offset distance equal when changing offsets, keeps size equal.");
  881. CHECK_MESSAGE(
  882. test_child->get_position().is_equal_approx(Vector2(0.07, 0.04)),
  883. "Negative offsets move position in top left direction.");
  884. }
  885. SUBCASE("Anchoring is preserved on parent size changed") {
  886. test_child->set_offset(SIDE_RIGHT, -0.05);
  887. test_child->set_offset(SIDE_BOTTOM, 0.1);
  888. test_child->set_offset(SIDE_LEFT, 0.05);
  889. test_child->set_offset(SIDE_TOP, 0.1);
  890. test_child->set_anchor(SIDE_RIGHT, 0.3);
  891. test_child->set_anchor(SIDE_BOTTOM, 0.85);
  892. test_child->set_anchor(SIDE_LEFT, 0.2);
  893. test_child->set_anchor(SIDE_TOP, 0.55);
  894. CHECK(test_child->get_rect().is_equal_approx(
  895. Rect2(Vector2(0.45, 1.2), Size2(0.1, 0.6))));
  896. test_control->set_size(Size2(4, 1));
  897. CHECK(test_child->get_rect().is_equal_approx(
  898. Rect2(Vector2(0.85, 0.65), Size2(0.3, 0.3))));
  899. }
  900. memdelete(test_child);
  901. memdelete(test_control);
  902. }
  903. TEST_CASE("[SceneTree][Control] Custom minimum size") {
  904. Control *test_control = memnew(Control);
  905. test_control->set_custom_minimum_size(Size2(4, 2));
  906. Window *root = SceneTree::get_singleton()->get_root();
  907. root->add_child(test_control);
  908. CHECK_MESSAGE(
  909. test_control->get_size().is_equal_approx(Vector2(4, 2)),
  910. "Size increases to match custom minimum size.");
  911. test_control->set_size(Size2(5, 4));
  912. CHECK_MESSAGE(
  913. test_control->get_size().is_equal_approx(Vector2(5, 4)),
  914. "Size does not change if above custom minimum size.");
  915. test_control->set_size(Size2(1, 1));
  916. CHECK_MESSAGE(
  917. test_control->get_size().is_equal_approx(Vector2(4, 2)),
  918. "Size matches minimum size if set below custom minimum size.");
  919. test_control->set_size(Size2(3, 3));
  920. CHECK_MESSAGE(
  921. test_control->get_size().is_equal_approx(Vector2(4, 3)),
  922. "Adjusts only x axis size if x is below custom minimum size.");
  923. test_control->set_size(Size2(10, 0.1));
  924. CHECK_MESSAGE(
  925. test_control->get_size().is_equal_approx(Vector2(10, 2)),
  926. "Adjusts only y axis size if y is below custom minimum size.");
  927. memdelete(test_control);
  928. }
  929. TEST_CASE("[SceneTree][Control] Grow direction") {
  930. Control *test_control = memnew(Control);
  931. test_control->set_size(Size2(1, 1));
  932. Window *root = SceneTree::get_singleton()->get_root();
  933. root->add_child(test_control);
  934. SUBCASE("Defaults") {
  935. CHECK(test_control->get_h_grow_direction() == Control::GROW_DIRECTION_END);
  936. CHECK(test_control->get_v_grow_direction() == Control::GROW_DIRECTION_END);
  937. }
  938. SIGNAL_WATCH(test_control, SNAME("minimum_size_changed"))
  939. Array signal_args = { {} };
  940. SUBCASE("Horizontal grow direction begin") {
  941. test_control->set_h_grow_direction(Control::GROW_DIRECTION_BEGIN);
  942. test_control->set_custom_minimum_size(Size2(2, 2));
  943. SceneTree::get_singleton()->process(0);
  944. SIGNAL_CHECK("minimum_size_changed", signal_args)
  945. CHECK_MESSAGE(
  946. test_control->get_rect().is_equal_approx(
  947. Rect2(Vector2(-1, 0), Size2(2, 2))),
  948. "Expand leftwards.");
  949. }
  950. SUBCASE("Vertical grow direction begin") {
  951. test_control->set_v_grow_direction(Control::GROW_DIRECTION_BEGIN);
  952. test_control->set_custom_minimum_size(Size2(4, 3));
  953. SceneTree::get_singleton()->process(0);
  954. SIGNAL_CHECK("minimum_size_changed", signal_args);
  955. CHECK_MESSAGE(
  956. test_control->get_rect().is_equal_approx(
  957. Rect2(Vector2(0, -2), Size2(4, 3))),
  958. "Expand upwards.");
  959. }
  960. SUBCASE("Horizontal grow direction end") {
  961. test_control->set_h_grow_direction(Control::GROW_DIRECTION_END);
  962. test_control->set_custom_minimum_size(Size2(5, 3));
  963. SceneTree::get_singleton()->process(0);
  964. SIGNAL_CHECK("minimum_size_changed", signal_args);
  965. CHECK_MESSAGE(
  966. test_control->get_rect().is_equal_approx(
  967. Rect2(Vector2(0, 0), Size2(5, 3))),
  968. "Expand rightwards.");
  969. }
  970. SUBCASE("Vertical grow direction end") {
  971. test_control->set_v_grow_direction(Control::GROW_DIRECTION_END);
  972. test_control->set_custom_minimum_size(Size2(4, 4));
  973. SceneTree::get_singleton()->process(0);
  974. SIGNAL_CHECK("minimum_size_changed", signal_args);
  975. CHECK_MESSAGE(
  976. test_control->get_rect().is_equal_approx(
  977. Rect2(Vector2(0, 0), Size2(4, 4))),
  978. "Expand downwards.");
  979. ;
  980. }
  981. SUBCASE("Horizontal grow direction both") {
  982. test_control->set_h_grow_direction(Control::GROW_DIRECTION_BOTH);
  983. test_control->set_custom_minimum_size(Size2(2, 4));
  984. SceneTree::get_singleton()->process(0);
  985. SIGNAL_CHECK("minimum_size_changed", signal_args);
  986. CHECK_MESSAGE(
  987. test_control->get_rect().is_equal_approx(
  988. Rect2(Vector2(-0.5, 0), Size2(2, 4))),
  989. "Expand equally leftwards and rightwards.");
  990. }
  991. SUBCASE("Vertical grow direction both") {
  992. test_control->set_v_grow_direction(Control::GROW_DIRECTION_BOTH);
  993. test_control->set_custom_minimum_size(Size2(6, 3));
  994. SceneTree::get_singleton()->process(0);
  995. SIGNAL_CHECK("minimum_size_changed", signal_args);
  996. CHECK_MESSAGE(
  997. test_control->get_rect().is_equal_approx(
  998. Rect2(Vector2(0, -1), Size2(6, 3))),
  999. "Expand equally upwards and downwards.");
  1000. }
  1001. memdelete(test_control);
  1002. }
  1003. } // namespace TestControl