test_tree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**************************************************************************/
  2. /* test_tree.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_TREE_H
  31. #define TEST_TREE_H
  32. #include "scene/gui/tree.h"
  33. #include "tests/test_macros.h"
  34. namespace TestTree {
  35. TEST_CASE("[SceneTree][Tree]") {
  36. SUBCASE("[Tree] Create and remove items.") {
  37. Tree *tree = memnew(Tree);
  38. TreeItem *root = tree->create_item();
  39. TreeItem *child1 = tree->create_item();
  40. CHECK_EQ(root->get_child_count(), 1);
  41. TreeItem *child2 = tree->create_item(root);
  42. CHECK_EQ(root->get_child_count(), 2);
  43. TreeItem *child3 = tree->create_item(root, 0);
  44. CHECK_EQ(root->get_child_count(), 3);
  45. CHECK_EQ(root->get_child(0), child3);
  46. CHECK_EQ(root->get_child(1), child1);
  47. CHECK_EQ(root->get_child(2), child2);
  48. root->remove_child(child3);
  49. CHECK_EQ(root->get_child_count(), 2);
  50. root->add_child(child3);
  51. CHECK_EQ(root->get_child_count(), 3);
  52. TreeItem *child4 = root->create_child();
  53. CHECK_EQ(root->get_child_count(), 4);
  54. CHECK_EQ(root->get_child(0), child1);
  55. CHECK_EQ(root->get_child(1), child2);
  56. CHECK_EQ(root->get_child(2), child3);
  57. CHECK_EQ(root->get_child(3), child4);
  58. memdelete(tree);
  59. }
  60. SUBCASE("[Tree] Clear items.") {
  61. Tree *tree = memnew(Tree);
  62. TreeItem *root = tree->create_item();
  63. for (int i = 0; i < 10; i++) {
  64. tree->create_item();
  65. }
  66. CHECK_EQ(root->get_child_count(), 10);
  67. root->clear_children();
  68. CHECK_EQ(root->get_child_count(), 0);
  69. memdelete(tree);
  70. }
  71. SUBCASE("[Tree] Get last item.") {
  72. Tree *tree = memnew(Tree);
  73. TreeItem *root = tree->create_item();
  74. TreeItem *last;
  75. for (int i = 0; i < 10; i++) {
  76. last = tree->create_item();
  77. }
  78. CHECK_EQ(root->get_child_count(), 10);
  79. CHECK_EQ(tree->get_last_item(), last);
  80. // Check nested.
  81. TreeItem *old_last = last;
  82. for (int i = 0; i < 10; i++) {
  83. last = tree->create_item(old_last);
  84. }
  85. CHECK_EQ(tree->get_last_item(), last);
  86. memdelete(tree);
  87. }
  88. // https://github.com/godotengine/godot/issues/96205
  89. SUBCASE("[Tree] Get last item after removal.") {
  90. Tree *tree = memnew(Tree);
  91. TreeItem *root = tree->create_item();
  92. TreeItem *child1 = tree->create_item(root);
  93. TreeItem *child2 = tree->create_item(root);
  94. CHECK_EQ(root->get_child_count(), 2);
  95. CHECK_EQ(tree->get_last_item(), child2);
  96. root->remove_child(child2);
  97. CHECK_EQ(root->get_child_count(), 1);
  98. CHECK_EQ(tree->get_last_item(), child1);
  99. root->add_child(child2);
  100. CHECK_EQ(root->get_child_count(), 2);
  101. CHECK_EQ(tree->get_last_item(), child2);
  102. memdelete(tree);
  103. }
  104. SUBCASE("[Tree] Previous and Next items.") {
  105. Tree *tree = memnew(Tree);
  106. TreeItem *root = tree->create_item();
  107. TreeItem *child1 = tree->create_item();
  108. TreeItem *child2 = tree->create_item();
  109. TreeItem *child3 = tree->create_item();
  110. CHECK_EQ(root->get_next(), nullptr);
  111. CHECK_EQ(root->get_next_visible(), child1);
  112. CHECK_EQ(root->get_next_in_tree(), child1);
  113. CHECK_EQ(child1->get_next(), child2);
  114. CHECK_EQ(child1->get_next_visible(), child2);
  115. CHECK_EQ(child1->get_next_in_tree(), child2);
  116. CHECK_EQ(child2->get_next(), child3);
  117. CHECK_EQ(child2->get_next_visible(), child3);
  118. CHECK_EQ(child2->get_next_in_tree(), child3);
  119. CHECK_EQ(child3->get_next(), nullptr);
  120. CHECK_EQ(child3->get_next_visible(), nullptr);
  121. CHECK_EQ(child3->get_next_in_tree(), nullptr);
  122. CHECK_EQ(root->get_prev(), nullptr);
  123. CHECK_EQ(root->get_prev_visible(), nullptr);
  124. CHECK_EQ(root->get_prev_in_tree(), nullptr);
  125. CHECK_EQ(child1->get_prev(), nullptr);
  126. CHECK_EQ(child1->get_prev_visible(), root);
  127. CHECK_EQ(child1->get_prev_in_tree(), root);
  128. CHECK_EQ(child2->get_prev(), child1);
  129. CHECK_EQ(child2->get_prev_visible(), child1);
  130. CHECK_EQ(child2->get_prev_in_tree(), child1);
  131. CHECK_EQ(child3->get_prev(), child2);
  132. CHECK_EQ(child3->get_prev_visible(), child2);
  133. CHECK_EQ(child3->get_prev_in_tree(), child2);
  134. TreeItem *nested1 = tree->create_item(child2);
  135. TreeItem *nested2 = tree->create_item(child2);
  136. TreeItem *nested3 = tree->create_item(child2);
  137. CHECK_EQ(child1->get_next(), child2);
  138. CHECK_EQ(child1->get_next_visible(), child2);
  139. CHECK_EQ(child1->get_next_in_tree(), child2);
  140. CHECK_EQ(child2->get_next(), child3);
  141. CHECK_EQ(child2->get_next_visible(), nested1);
  142. CHECK_EQ(child2->get_next_in_tree(), nested1);
  143. CHECK_EQ(child3->get_prev(), child2);
  144. CHECK_EQ(child3->get_prev_visible(), nested3);
  145. CHECK_EQ(child3->get_prev_in_tree(), nested3);
  146. CHECK_EQ(nested1->get_prev_in_tree(), child2);
  147. CHECK_EQ(nested1->get_next_in_tree(), nested2);
  148. CHECK_EQ(nested3->get_next_in_tree(), child3);
  149. memdelete(tree);
  150. }
  151. SUBCASE("[Tree] Previous and Next items with hide root.") {
  152. Tree *tree = memnew(Tree);
  153. tree->set_hide_root(true);
  154. TreeItem *root = tree->create_item();
  155. TreeItem *child1 = tree->create_item();
  156. TreeItem *child2 = tree->create_item();
  157. TreeItem *child3 = tree->create_item();
  158. CHECK_EQ(root->get_next(), nullptr);
  159. CHECK_EQ(root->get_next_visible(), child1);
  160. CHECK_EQ(root->get_next_in_tree(), child1);
  161. CHECK_EQ(child1->get_next(), child2);
  162. CHECK_EQ(child1->get_next_visible(), child2);
  163. CHECK_EQ(child1->get_next_in_tree(), child2);
  164. CHECK_EQ(child2->get_next(), child3);
  165. CHECK_EQ(child2->get_next_visible(), child3);
  166. CHECK_EQ(child2->get_next_in_tree(), child3);
  167. CHECK_EQ(child3->get_next(), nullptr);
  168. CHECK_EQ(child3->get_next_visible(), nullptr);
  169. CHECK_EQ(child3->get_next_in_tree(), nullptr);
  170. CHECK_EQ(root->get_prev(), nullptr);
  171. CHECK_EQ(root->get_prev_visible(), nullptr);
  172. CHECK_EQ(root->get_prev_in_tree(), nullptr);
  173. CHECK_EQ(child1->get_prev(), nullptr);
  174. CHECK_EQ(child1->get_prev_visible(), nullptr);
  175. CHECK_EQ(child1->get_prev_in_tree(), nullptr);
  176. CHECK_EQ(child2->get_prev(), child1);
  177. CHECK_EQ(child2->get_prev_visible(), child1);
  178. CHECK_EQ(child2->get_prev_in_tree(), child1);
  179. CHECK_EQ(child3->get_prev(), child2);
  180. CHECK_EQ(child3->get_prev_visible(), child2);
  181. CHECK_EQ(child3->get_prev_in_tree(), child2);
  182. memdelete(tree);
  183. }
  184. SUBCASE("[Tree] Previous and Next items wrapping.") {
  185. Tree *tree = memnew(Tree);
  186. TreeItem *root = tree->create_item();
  187. TreeItem *child1 = tree->create_item();
  188. TreeItem *child2 = tree->create_item();
  189. TreeItem *child3 = tree->create_item();
  190. CHECK_EQ(root->get_next_visible(true), child1);
  191. CHECK_EQ(root->get_next_in_tree(true), child1);
  192. CHECK_EQ(child1->get_next_visible(true), child2);
  193. CHECK_EQ(child1->get_next_in_tree(true), child2);
  194. CHECK_EQ(child2->get_next_visible(true), child3);
  195. CHECK_EQ(child2->get_next_in_tree(true), child3);
  196. CHECK_EQ(child3->get_next_visible(true), root);
  197. CHECK_EQ(child3->get_next_in_tree(true), root);
  198. CHECK_EQ(root->get_prev_visible(true), child3);
  199. CHECK_EQ(root->get_prev_in_tree(true), child3);
  200. CHECK_EQ(child1->get_prev_visible(true), root);
  201. CHECK_EQ(child1->get_prev_in_tree(true), root);
  202. CHECK_EQ(child2->get_prev_visible(true), child1);
  203. CHECK_EQ(child2->get_prev_in_tree(true), child1);
  204. CHECK_EQ(child3->get_prev_visible(true), child2);
  205. CHECK_EQ(child3->get_prev_in_tree(true), child2);
  206. TreeItem *nested1 = tree->create_item(child2);
  207. TreeItem *nested2 = tree->create_item(child2);
  208. TreeItem *nested3 = tree->create_item(child2);
  209. CHECK_EQ(child1->get_next_visible(true), child2);
  210. CHECK_EQ(child1->get_next_in_tree(true), child2);
  211. CHECK_EQ(child2->get_next_visible(true), nested1);
  212. CHECK_EQ(child2->get_next_in_tree(true), nested1);
  213. CHECK_EQ(nested3->get_next_visible(true), child3);
  214. CHECK_EQ(nested3->get_next_in_tree(true), child3);
  215. CHECK_EQ(child3->get_prev_visible(true), nested3);
  216. CHECK_EQ(child3->get_prev_in_tree(true), nested3);
  217. CHECK_EQ(nested1->get_prev_in_tree(true), child2);
  218. CHECK_EQ(nested1->get_next_in_tree(true), nested2);
  219. CHECK_EQ(nested3->get_next_in_tree(true), child3);
  220. memdelete(tree);
  221. }
  222. SUBCASE("[Tree] Previous and Next items wrapping with hide root.") {
  223. Tree *tree = memnew(Tree);
  224. tree->set_hide_root(true);
  225. TreeItem *root = tree->create_item();
  226. TreeItem *child1 = tree->create_item();
  227. TreeItem *child2 = tree->create_item();
  228. TreeItem *child3 = tree->create_item();
  229. CHECK_EQ(root->get_next_visible(true), child1);
  230. CHECK_EQ(root->get_next_in_tree(true), child1);
  231. CHECK_EQ(child1->get_next_visible(true), child2);
  232. CHECK_EQ(child1->get_next_in_tree(true), child2);
  233. CHECK_EQ(child2->get_next_visible(true), child3);
  234. CHECK_EQ(child2->get_next_in_tree(true), child3);
  235. CHECK_EQ(child3->get_next_visible(true), root);
  236. CHECK_EQ(child3->get_next_in_tree(true), root);
  237. CHECK_EQ(root->get_prev_visible(true), child3);
  238. CHECK_EQ(root->get_prev_in_tree(true), child3);
  239. CHECK_EQ(child1->get_prev_visible(true), child3);
  240. CHECK_EQ(child1->get_prev_in_tree(true), child3);
  241. CHECK_EQ(child2->get_prev_visible(true), child1);
  242. CHECK_EQ(child2->get_prev_in_tree(true), child1);
  243. CHECK_EQ(child3->get_prev_visible(true), child2);
  244. CHECK_EQ(child3->get_prev_in_tree(true), child2);
  245. memdelete(tree);
  246. }
  247. }
  248. } // namespace TestTree
  249. #endif // TEST_TREE_H