test_control.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #ifndef TEST_CONTROL_H
  31. #define TEST_CONTROL_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_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_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(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(ctrl->has_focus());
  77. ctrl->release_focus();
  78. CHECK_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(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(other_ctrl->has_focus());
  89. CHECK_FALSE(ctrl->has_focus());
  90. memdelete(other_ctrl);
  91. }
  92. memdelete(ctrl);
  93. }
  94. TEST_CASE("[SceneTree][Control] Anchoring") {
  95. Control *test_control = memnew(Control);
  96. Control *test_child = memnew(Control);
  97. test_control->add_child(test_child);
  98. test_control->set_size(Size2(2, 2));
  99. Window *root = SceneTree::get_singleton()->get_root();
  100. root->add_child(test_control);
  101. SUBCASE("Anchoring without offsets") {
  102. test_child->set_anchor(SIDE_RIGHT, 0.75);
  103. test_child->set_anchor(SIDE_BOTTOM, 0.1);
  104. CHECK_MESSAGE(
  105. test_child->get_size().is_equal_approx(Vector2(1.5, 0.2)),
  106. "With no LEFT or TOP anchors, positive RIGHT and BOTTOM anchors should be proportional to the size.");
  107. CHECK_MESSAGE(
  108. test_child->get_position().is_equal_approx(Vector2(0, 0)),
  109. "With positive RIGHT and BOTTOM anchors set and no LEFT or TOP anchors, the position should not change.");
  110. test_child->set_anchor(SIDE_LEFT, 0.5);
  111. test_child->set_anchor(SIDE_TOP, 0.01);
  112. CHECK_MESSAGE(
  113. test_child->get_size().is_equal_approx(Vector2(0.5, 0.18)),
  114. "With all anchors set, the size should fit between all four anchors.");
  115. CHECK_MESSAGE(
  116. test_child->get_position().is_equal_approx(Vector2(1, 0.02)),
  117. "With all anchors set, the LEFT and TOP anchors should proportional to the position.");
  118. }
  119. SUBCASE("Anchoring with offsets") {
  120. test_child->set_offset(SIDE_RIGHT, 0.33);
  121. test_child->set_offset(SIDE_BOTTOM, 0.2);
  122. CHECK_MESSAGE(
  123. test_child->get_size().is_equal_approx(Vector2(0.33, 0.2)),
  124. "With no anchors or LEFT or TOP offsets set, the RIGHT and BOTTOM offsets should be equal to size.");
  125. CHECK_MESSAGE(
  126. test_child->get_position().is_equal_approx(Vector2(0, 0)),
  127. "With only positive RIGHT and BOTTOM offsets set, the position should not change.");
  128. test_child->set_offset(SIDE_LEFT, 0.1);
  129. test_child->set_offset(SIDE_TOP, 0.05);
  130. CHECK_MESSAGE(
  131. test_child->get_size().is_equal_approx(Vector2(0.23, 0.15)),
  132. "With no anchors set, the size should fit between all four offsets.");
  133. CHECK_MESSAGE(
  134. test_child->get_position().is_equal_approx(Vector2(0.1, 0.05)),
  135. "With no anchors set, the LEFT and TOP offsets should be equal to the position.");
  136. test_child->set_anchor(SIDE_RIGHT, 0.5);
  137. test_child->set_anchor(SIDE_BOTTOM, 0.3);
  138. test_child->set_anchor(SIDE_LEFT, 0.2);
  139. test_child->set_anchor(SIDE_TOP, 0.1);
  140. CHECK_MESSAGE(
  141. test_child->get_size().is_equal_approx(Vector2(0.83, 0.55)),
  142. "Anchors adjust size first then it is affected by offsets.");
  143. CHECK_MESSAGE(
  144. test_child->get_position().is_equal_approx(Vector2(0.5, 0.25)),
  145. "Anchors adjust positions first then it is affected by offsets.");
  146. test_child->set_offset(SIDE_RIGHT, -0.1);
  147. test_child->set_offset(SIDE_BOTTOM, -0.01);
  148. test_child->set_offset(SIDE_LEFT, -0.33);
  149. test_child->set_offset(SIDE_TOP, -0.16);
  150. CHECK_MESSAGE(
  151. test_child->get_size().is_equal_approx(Vector2(0.83, 0.55)),
  152. "Keeping offset distance equal when changing offsets, keeps size equal.");
  153. CHECK_MESSAGE(
  154. test_child->get_position().is_equal_approx(Vector2(0.07, 0.04)),
  155. "Negative offsets move position in top left direction.");
  156. }
  157. SUBCASE("Anchoring is preserved on parent size changed") {
  158. test_child->set_offset(SIDE_RIGHT, -0.05);
  159. test_child->set_offset(SIDE_BOTTOM, 0.1);
  160. test_child->set_offset(SIDE_LEFT, 0.05);
  161. test_child->set_offset(SIDE_TOP, 0.1);
  162. test_child->set_anchor(SIDE_RIGHT, 0.3);
  163. test_child->set_anchor(SIDE_BOTTOM, 0.85);
  164. test_child->set_anchor(SIDE_LEFT, 0.2);
  165. test_child->set_anchor(SIDE_TOP, 0.55);
  166. CHECK(test_child->get_rect().is_equal_approx(
  167. Rect2(Vector2(0.45, 1.2), Size2(0.1, 0.6))));
  168. test_control->set_size(Size2(4, 1));
  169. CHECK(test_child->get_rect().is_equal_approx(
  170. Rect2(Vector2(0.85, 0.65), Size2(0.3, 0.3))));
  171. }
  172. memdelete(test_child);
  173. memdelete(test_control);
  174. }
  175. TEST_CASE("[SceneTree][Control] Custom minimum size") {
  176. Control *test_control = memnew(Control);
  177. test_control->set_custom_minimum_size(Size2(4, 2));
  178. Window *root = SceneTree::get_singleton()->get_root();
  179. root->add_child(test_control);
  180. CHECK_MESSAGE(
  181. test_control->get_size().is_equal_approx(Vector2(4, 2)),
  182. "Size increases to match custom minimum size.");
  183. test_control->set_size(Size2(5, 4));
  184. CHECK_MESSAGE(
  185. test_control->get_size().is_equal_approx(Vector2(5, 4)),
  186. "Size does not change if above custom minimum size.");
  187. test_control->set_size(Size2(1, 1));
  188. CHECK_MESSAGE(
  189. test_control->get_size().is_equal_approx(Vector2(4, 2)),
  190. "Size matches minimum size if set below custom minimum size.");
  191. test_control->set_size(Size2(3, 3));
  192. CHECK_MESSAGE(
  193. test_control->get_size().is_equal_approx(Vector2(4, 3)),
  194. "Adjusts only x axis size if x is below custom minimum size.");
  195. test_control->set_size(Size2(10, 0.1));
  196. CHECK_MESSAGE(
  197. test_control->get_size().is_equal_approx(Vector2(10, 2)),
  198. "Adjusts only y axis size if y is below custom minimum size.");
  199. memdelete(test_control);
  200. }
  201. TEST_CASE("[SceneTree][Control] Grow direction") {
  202. Control *test_control = memnew(Control);
  203. test_control->set_size(Size2(1, 1));
  204. Window *root = SceneTree::get_singleton()->get_root();
  205. root->add_child(test_control);
  206. SUBCASE("Defaults") {
  207. CHECK(test_control->get_h_grow_direction() == Control::GROW_DIRECTION_END);
  208. CHECK(test_control->get_v_grow_direction() == Control::GROW_DIRECTION_END);
  209. }
  210. SIGNAL_WATCH(test_control, SNAME("minimum_size_changed"))
  211. Array signal_args;
  212. signal_args.push_back(Array());
  213. SUBCASE("Horizontal grow direction begin") {
  214. test_control->set_h_grow_direction(Control::GROW_DIRECTION_BEGIN);
  215. test_control->set_custom_minimum_size(Size2(2, 2));
  216. SceneTree::get_singleton()->process(0);
  217. SIGNAL_CHECK("minimum_size_changed", signal_args)
  218. CHECK_MESSAGE(
  219. test_control->get_rect().is_equal_approx(
  220. Rect2(Vector2(-1, 0), Size2(2, 2))),
  221. "Expand leftwards.");
  222. }
  223. SUBCASE("Vertical grow direction begin") {
  224. test_control->set_v_grow_direction(Control::GROW_DIRECTION_BEGIN);
  225. test_control->set_custom_minimum_size(Size2(4, 3));
  226. SceneTree::get_singleton()->process(0);
  227. SIGNAL_CHECK("minimum_size_changed", signal_args);
  228. CHECK_MESSAGE(
  229. test_control->get_rect().is_equal_approx(
  230. Rect2(Vector2(0, -2), Size2(4, 3))),
  231. "Expand upwards.");
  232. }
  233. SUBCASE("Horizontal grow direction end") {
  234. test_control->set_h_grow_direction(Control::GROW_DIRECTION_END);
  235. test_control->set_custom_minimum_size(Size2(5, 3));
  236. SceneTree::get_singleton()->process(0);
  237. SIGNAL_CHECK("minimum_size_changed", signal_args);
  238. CHECK_MESSAGE(
  239. test_control->get_rect().is_equal_approx(
  240. Rect2(Vector2(0, 0), Size2(5, 3))),
  241. "Expand rightwards.");
  242. }
  243. SUBCASE("Vertical grow direction end") {
  244. test_control->set_v_grow_direction(Control::GROW_DIRECTION_END);
  245. test_control->set_custom_minimum_size(Size2(4, 4));
  246. SceneTree::get_singleton()->process(0);
  247. SIGNAL_CHECK("minimum_size_changed", signal_args);
  248. CHECK_MESSAGE(
  249. test_control->get_rect().is_equal_approx(
  250. Rect2(Vector2(0, 0), Size2(4, 4))),
  251. "Expand downwards.");
  252. ;
  253. }
  254. SUBCASE("Horizontal grow direction both") {
  255. test_control->set_h_grow_direction(Control::GROW_DIRECTION_BOTH);
  256. test_control->set_custom_minimum_size(Size2(2, 4));
  257. SceneTree::get_singleton()->process(0);
  258. SIGNAL_CHECK("minimum_size_changed", signal_args);
  259. CHECK_MESSAGE(
  260. test_control->get_rect().is_equal_approx(
  261. Rect2(Vector2(-0.5, 0), Size2(2, 4))),
  262. "Expand equally leftwards and rightwards.");
  263. }
  264. SUBCASE("Vertical grow direction both") {
  265. test_control->set_v_grow_direction(Control::GROW_DIRECTION_BOTH);
  266. test_control->set_custom_minimum_size(Size2(6, 3));
  267. SceneTree::get_singleton()->process(0);
  268. SIGNAL_CHECK("minimum_size_changed", signal_args);
  269. CHECK_MESSAGE(
  270. test_control->get_rect().is_equal_approx(
  271. Rect2(Vector2(0, -1), Size2(6, 3))),
  272. "Expand equally upwards and downwards.");
  273. }
  274. memdelete(test_control);
  275. }
  276. } // namespace TestControl
  277. #endif // TEST_CONTROL_H