test_control.h 42 KB

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