test_node.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /**************************************************************************/
  2. /* test_node.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_NODE_H
  31. #define TEST_NODE_H
  32. #include "scene/main/node.h"
  33. #include "tests/test_macros.h"
  34. namespace TestNode {
  35. class TestNode : public Node {
  36. GDCLASS(TestNode, Node);
  37. protected:
  38. void _notification(int p_what) {
  39. switch (p_what) {
  40. case NOTIFICATION_INTERNAL_PROCESS: {
  41. internal_process_counter++;
  42. push_self();
  43. } break;
  44. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  45. internal_physics_process_counter++;
  46. push_self();
  47. } break;
  48. case NOTIFICATION_PROCESS: {
  49. process_counter++;
  50. push_self();
  51. } break;
  52. case NOTIFICATION_PHYSICS_PROCESS: {
  53. physics_process_counter++;
  54. push_self();
  55. } break;
  56. }
  57. }
  58. private:
  59. void push_self() {
  60. if (callback_list) {
  61. callback_list->push_back(this);
  62. }
  63. }
  64. public:
  65. int internal_process_counter = 0;
  66. int internal_physics_process_counter = 0;
  67. int process_counter = 0;
  68. int physics_process_counter = 0;
  69. List<Node *> *callback_list = nullptr;
  70. };
  71. TEST_CASE("[SceneTree][Node] Testing node operations with a very simple scene tree") {
  72. Node *node = memnew(Node);
  73. // Check initial scene tree setup.
  74. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 0);
  75. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 1);
  76. // Check initial node setup.
  77. CHECK(node->get_name() == StringName());
  78. CHECK_FALSE(node->is_inside_tree());
  79. CHECK_EQ(node->get_parent(), nullptr);
  80. ERR_PRINT_OFF;
  81. CHECK(node->get_path().is_empty());
  82. ERR_PRINT_ON;
  83. CHECK_EQ(node->get_child_count(), 0);
  84. SceneTree::get_singleton()->get_root()->add_child(node);
  85. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 1);
  86. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 2);
  87. CHECK(node->get_name() != StringName());
  88. CHECK(node->is_inside_tree());
  89. CHECK_EQ(SceneTree::get_singleton()->get_root(), node->get_parent());
  90. CHECK_FALSE(node->get_path().is_empty());
  91. CHECK_EQ(node->get_child_count(), 0);
  92. SUBCASE("Node should be accessible as first child") {
  93. Node *child = SceneTree::get_singleton()->get_root()->get_child(0);
  94. CHECK_EQ(child, node);
  95. }
  96. SUBCASE("Node should be accessible via the node path") {
  97. Node *child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(node->get_path());
  98. CHECK_EQ(child_by_path, node);
  99. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(NodePath("Node"));
  100. CHECK_EQ(child_by_path, nullptr);
  101. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(NodePath("/root/Node"));
  102. CHECK_EQ(child_by_path, nullptr);
  103. node->set_name("Node");
  104. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(node->get_path());
  105. CHECK_EQ(child_by_path, node);
  106. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(NodePath("Node"));
  107. CHECK_EQ(child_by_path, node);
  108. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(NodePath("/root/Node"));
  109. CHECK_EQ(child_by_path, node);
  110. }
  111. SUBCASE("Node should be accessible via group") {
  112. List<Node *> nodes;
  113. SceneTree::get_singleton()->get_nodes_in_group("nodes", &nodes);
  114. CHECK(nodes.is_empty());
  115. node->add_to_group("nodes");
  116. SceneTree::get_singleton()->get_nodes_in_group("nodes", &nodes);
  117. CHECK_EQ(nodes.size(), 1);
  118. List<Node *>::Element *E = nodes.front();
  119. CHECK_EQ(E->get(), node);
  120. }
  121. SUBCASE("Node should be possible to find") {
  122. Node *child = SceneTree::get_singleton()->get_root()->find_child("Node", true, false);
  123. CHECK_EQ(child, nullptr);
  124. node->set_name("Node");
  125. child = SceneTree::get_singleton()->get_root()->find_child("Node", true, false);
  126. CHECK_EQ(child, node);
  127. }
  128. SUBCASE("Node should be possible to remove") {
  129. SceneTree::get_singleton()->get_root()->remove_child(node);
  130. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 0);
  131. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 1);
  132. CHECK_FALSE(node->is_inside_tree());
  133. CHECK_EQ(node->get_parent(), nullptr);
  134. ERR_PRINT_OFF;
  135. CHECK(node->get_path().is_empty());
  136. ERR_PRINT_ON;
  137. }
  138. SUBCASE("Node should be possible to move") {
  139. SceneTree::get_singleton()->get_root()->move_child(node, 0);
  140. Node *child = SceneTree::get_singleton()->get_root()->get_child(0);
  141. CHECK_EQ(child, node);
  142. CHECK(node->is_inside_tree());
  143. }
  144. SUBCASE("Node should be possible to reparent") {
  145. node->reparent(SceneTree::get_singleton()->get_root());
  146. Node *child = SceneTree::get_singleton()->get_root()->get_child(0);
  147. CHECK_EQ(child, node);
  148. CHECK(node->is_inside_tree());
  149. }
  150. SUBCASE("Node should be possible to duplicate") {
  151. node->set_name("MyName");
  152. Node *duplicate = node->duplicate();
  153. CHECK_FALSE(node == duplicate);
  154. CHECK_FALSE(duplicate->is_inside_tree());
  155. CHECK_EQ(duplicate->get_name(), node->get_name());
  156. memdelete(duplicate);
  157. }
  158. memdelete(node);
  159. }
  160. TEST_CASE("[SceneTree][Node] Testing node operations with a more complex simple scene tree") {
  161. Node *node1 = memnew(Node);
  162. Node *node2 = memnew(Node);
  163. Node *node1_1 = memnew(Node);
  164. SceneTree::get_singleton()->get_root()->add_child(node1);
  165. SceneTree::get_singleton()->get_root()->add_child(node2);
  166. node1->add_child(node1_1);
  167. CHECK(node1_1->is_inside_tree());
  168. CHECK_EQ(node1_1->get_parent(), node1);
  169. CHECK_EQ(node1->get_child_count(), 1);
  170. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 2);
  171. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 4);
  172. SUBCASE("Nodes should be accessible via get_child(..)") {
  173. Node *child1 = SceneTree::get_singleton()->get_root()->get_child(0);
  174. CHECK_EQ(child1, node1);
  175. Node *child2 = SceneTree::get_singleton()->get_root()->get_child(1);
  176. CHECK_EQ(child2, node2);
  177. Node *child1_1 = node1->get_child(0);
  178. CHECK_EQ(child1_1, node1_1);
  179. }
  180. SUBCASE("Removed nodes should also remove their children from the scene tree") {
  181. // Should also remove node1_1 from the scene tree.
  182. SceneTree::get_singleton()->get_root()->remove_child(node1);
  183. CHECK_EQ(node1->get_child_count(), 1);
  184. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 1);
  185. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 2);
  186. // First child should now be the second node.
  187. Node *child1 = SceneTree::get_singleton()->get_root()->get_child(0);
  188. CHECK_EQ(child1, node2);
  189. }
  190. SUBCASE("Removed children nodes should not affect their parent in the scene tree") {
  191. node1->remove_child(node1_1);
  192. CHECK_EQ(node1_1->get_parent(), nullptr);
  193. CHECK_EQ(node1->get_child_count(), 0);
  194. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 3);
  195. }
  196. SUBCASE("Nodes should be in the expected order when a node is moved to the back") {
  197. SceneTree::get_singleton()->get_root()->move_child(node1, 1);
  198. Node *child1 = SceneTree::get_singleton()->get_root()->get_child(0);
  199. CHECK_EQ(child1, node2);
  200. Node *child2 = SceneTree::get_singleton()->get_root()->get_child(1);
  201. CHECK_EQ(child2, node1);
  202. }
  203. SUBCASE("Nodes should be in the expected order when a node is moved to the front") {
  204. SceneTree::get_singleton()->get_root()->move_child(node2, 0);
  205. Node *child1 = SceneTree::get_singleton()->get_root()->get_child(0);
  206. CHECK_EQ(child1, node2);
  207. Node *child2 = SceneTree::get_singleton()->get_root()->get_child(1);
  208. CHECK_EQ(child2, node1);
  209. }
  210. SUBCASE("Nodes should be in the expected order when reparented (remove/add)") {
  211. CHECK_EQ(node2->get_child_count(), 0);
  212. node1->remove_child(node1_1);
  213. CHECK_EQ(node1->get_child_count(), 0);
  214. CHECK_EQ(node1_1->get_parent(), nullptr);
  215. node2->add_child(node1_1);
  216. CHECK_EQ(node2->get_child_count(), 1);
  217. CHECK_EQ(node1_1->get_parent(), node2);
  218. Node *child = node2->get_child(0);
  219. CHECK_EQ(child, node1_1);
  220. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 2);
  221. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 4);
  222. }
  223. SUBCASE("Nodes should be in the expected order when reparented") {
  224. CHECK_EQ(node2->get_child_count(), 0);
  225. node1_1->reparent(node2);
  226. CHECK_EQ(node1->get_child_count(), 0);
  227. CHECK_EQ(node2->get_child_count(), 1);
  228. CHECK_EQ(node1_1->get_parent(), node2);
  229. Node *child = node2->get_child(0);
  230. CHECK_EQ(child, node1_1);
  231. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 2);
  232. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 4);
  233. }
  234. SUBCASE("Nodes should be possible to find") {
  235. Node *child = SceneTree::get_singleton()->get_root()->find_child("NestedNode", true, false);
  236. CHECK_EQ(child, nullptr);
  237. TypedArray<Node> children = SceneTree::get_singleton()->get_root()->find_children("NestedNode", "", true, false);
  238. CHECK_EQ(children.size(), 0);
  239. node1->set_name("Node1");
  240. node2->set_name("Node2");
  241. node1_1->set_name("NestedNode");
  242. child = SceneTree::get_singleton()->get_root()->find_child("NestedNode", true, false);
  243. CHECK_EQ(child, node1_1);
  244. children = SceneTree::get_singleton()->get_root()->find_children("NestedNode", "", true, false);
  245. CHECK_EQ(children.size(), 1);
  246. CHECK_EQ(Object::cast_to<Node>(children[0]), node1_1);
  247. // First node that matches with the name is node1.
  248. child = SceneTree::get_singleton()->get_root()->find_child("Node?", true, false);
  249. CHECK_EQ(child, node1);
  250. children = SceneTree::get_singleton()->get_root()->find_children("Node?", "", true, false);
  251. CHECK_EQ(children.size(), 2);
  252. CHECK_EQ(Object::cast_to<Node>(children[0]), node1);
  253. CHECK_EQ(Object::cast_to<Node>(children[1]), node2);
  254. SceneTree::get_singleton()->get_root()->move_child(node2, 0);
  255. // It should be node2, as it is now the first one in the tree.
  256. child = SceneTree::get_singleton()->get_root()->find_child("Node?", true, false);
  257. CHECK_EQ(child, node2);
  258. children = SceneTree::get_singleton()->get_root()->find_children("Node?", "", true, false);
  259. CHECK_EQ(children.size(), 2);
  260. CHECK_EQ(Object::cast_to<Node>(children[0]), node2);
  261. CHECK_EQ(Object::cast_to<Node>(children[1]), node1);
  262. }
  263. SUBCASE("Nodes should be accessible via their node path") {
  264. Node *child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(node1->get_path());
  265. CHECK_EQ(child_by_path, node1);
  266. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(node2->get_path());
  267. CHECK_EQ(child_by_path, node2);
  268. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(node1_1->get_path());
  269. CHECK_EQ(child_by_path, node1_1);
  270. node1->set_name("Node1");
  271. node1_1->set_name("NestedNode");
  272. child_by_path = node1->get_node_or_null(NodePath("NestedNode"));
  273. CHECK_EQ(child_by_path, node1_1);
  274. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(NodePath("/root/Node1/NestedNode"));
  275. CHECK_EQ(child_by_path, node1_1);
  276. child_by_path = SceneTree::get_singleton()->get_root()->get_node_or_null(NodePath("Node1/NestedNode"));
  277. CHECK_EQ(child_by_path, node1_1);
  278. }
  279. SUBCASE("Nodes should be accessible via their groups") {
  280. List<Node *> nodes;
  281. SceneTree::get_singleton()->get_nodes_in_group("nodes", &nodes);
  282. CHECK(nodes.is_empty());
  283. SceneTree::get_singleton()->get_nodes_in_group("other_nodes", &nodes);
  284. CHECK(nodes.is_empty());
  285. node1->add_to_group("nodes");
  286. node2->add_to_group("other_nodes");
  287. node1_1->add_to_group("nodes");
  288. node1_1->add_to_group("other_nodes");
  289. SceneTree::get_singleton()->get_nodes_in_group("nodes", &nodes);
  290. CHECK_EQ(nodes.size(), 2);
  291. List<Node *>::Element *E = nodes.front();
  292. CHECK_EQ(E->get(), node1);
  293. E = E->next();
  294. CHECK_EQ(E->get(), node1_1);
  295. // Clear and try again with the other group.
  296. nodes.clear();
  297. SceneTree::get_singleton()->get_nodes_in_group("other_nodes", &nodes);
  298. CHECK_EQ(nodes.size(), 2);
  299. E = nodes.front();
  300. CHECK_EQ(E->get(), node1_1);
  301. E = E->next();
  302. CHECK_EQ(E->get(), node2);
  303. // Clear and try again with the other group and one node removed.
  304. nodes.clear();
  305. node1->remove_from_group("nodes");
  306. SceneTree::get_singleton()->get_nodes_in_group("nodes", &nodes);
  307. CHECK_EQ(nodes.size(), 1);
  308. E = nodes.front();
  309. CHECK_EQ(E->get(), node1_1);
  310. }
  311. SUBCASE("Nodes added as siblings of another node should be right next to it") {
  312. node1->remove_child(node1_1);
  313. node1->add_sibling(node1_1);
  314. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 3);
  315. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 4);
  316. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child(0), node1);
  317. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child(1), node1_1);
  318. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child(2), node2);
  319. }
  320. SUBCASE("Replaced nodes should be be removed and the replacing node added") {
  321. SceneTree::get_singleton()->get_root()->remove_child(node2);
  322. node1->replace_by(node2);
  323. CHECK_EQ(SceneTree::get_singleton()->get_root()->get_child_count(), 1);
  324. CHECK_EQ(SceneTree::get_singleton()->get_node_count(), 3);
  325. CHECK_FALSE(node1->is_inside_tree());
  326. CHECK(node2->is_inside_tree());
  327. CHECK_EQ(node1->get_parent(), nullptr);
  328. CHECK_EQ(node2->get_parent(), SceneTree::get_singleton()->get_root());
  329. CHECK_EQ(node2->get_child_count(), 1);
  330. CHECK_EQ(node2->get_child(0), node1_1);
  331. }
  332. SUBCASE("Replacing nodes should keep the groups of the replaced nodes") {
  333. SceneTree::get_singleton()->get_root()->remove_child(node2);
  334. node1->add_to_group("nodes");
  335. node1->replace_by(node2, true);
  336. List<Node *> nodes;
  337. SceneTree::get_singleton()->get_nodes_in_group("nodes", &nodes);
  338. CHECK_EQ(nodes.size(), 1);
  339. List<Node *>::Element *E = nodes.front();
  340. CHECK_EQ(E->get(), node2);
  341. }
  342. SUBCASE("Duplicating a node should also duplicate the children") {
  343. node1->set_name("MyName1");
  344. node1_1->set_name("MyName1_1");
  345. Node *duplicate1 = node1->duplicate();
  346. CHECK_EQ(duplicate1->get_child_count(), node1->get_child_count());
  347. Node *duplicate1_1 = duplicate1->get_child(0);
  348. CHECK_EQ(duplicate1_1->get_child_count(), node1_1->get_child_count());
  349. CHECK_EQ(duplicate1->get_name(), node1->get_name());
  350. CHECK_EQ(duplicate1_1->get_name(), node1_1->get_name());
  351. CHECK_FALSE(duplicate1->is_inside_tree());
  352. CHECK_FALSE(duplicate1_1->is_inside_tree());
  353. memdelete(duplicate1_1);
  354. memdelete(duplicate1);
  355. }
  356. memdelete(node1_1);
  357. memdelete(node1);
  358. memdelete(node2);
  359. }
  360. TEST_CASE("[Node] Processing checks") {
  361. Node *node = memnew(Node);
  362. SUBCASE("Processing") {
  363. CHECK_FALSE(node->is_processing());
  364. node->set_process(true);
  365. CHECK(node->is_processing());
  366. node->set_process(false);
  367. CHECK_FALSE(node->is_processing());
  368. }
  369. SUBCASE("Physics processing") {
  370. CHECK_FALSE(node->is_physics_processing());
  371. node->set_physics_process(true);
  372. CHECK(node->is_physics_processing());
  373. node->set_physics_process(false);
  374. CHECK_FALSE(node->is_physics_processing());
  375. }
  376. SUBCASE("Unhandled input processing") {
  377. CHECK_FALSE(node->is_processing_unhandled_input());
  378. node->set_process_unhandled_input(true);
  379. CHECK(node->is_processing_unhandled_input());
  380. node->set_process_unhandled_input(false);
  381. CHECK_FALSE(node->is_processing_unhandled_input());
  382. }
  383. SUBCASE("Input processing") {
  384. CHECK_FALSE(node->is_processing_input());
  385. node->set_process_input(true);
  386. CHECK(node->is_processing_input());
  387. node->set_process_input(false);
  388. CHECK_FALSE(node->is_processing_input());
  389. }
  390. SUBCASE("Unhandled key input processing") {
  391. CHECK_FALSE(node->is_processing_unhandled_key_input());
  392. node->set_process_unhandled_key_input(true);
  393. CHECK(node->is_processing_unhandled_key_input());
  394. node->set_process_unhandled_key_input(false);
  395. CHECK_FALSE(node->is_processing_unhandled_key_input());
  396. }
  397. SUBCASE("Shortcut input processing") {
  398. CHECK_FALSE(node->is_processing_shortcut_input());
  399. node->set_process_shortcut_input(true);
  400. CHECK(node->is_processing_shortcut_input());
  401. node->set_process_shortcut_input(false);
  402. CHECK_FALSE(node->is_processing_shortcut_input());
  403. }
  404. SUBCASE("Internal processing") {
  405. CHECK_FALSE(node->is_processing_internal());
  406. node->set_process_internal(true);
  407. CHECK(node->is_processing_internal());
  408. node->set_process_internal(false);
  409. CHECK_FALSE(node->is_processing_internal());
  410. }
  411. SUBCASE("Process priority") {
  412. CHECK_EQ(0, node->get_process_priority());
  413. node->set_process_priority(1);
  414. CHECK_EQ(1, node->get_process_priority());
  415. }
  416. SUBCASE("Physics process priority") {
  417. CHECK_EQ(0, node->get_physics_process_priority());
  418. node->set_physics_process_priority(1);
  419. CHECK_EQ(1, node->get_physics_process_priority());
  420. }
  421. memdelete(node);
  422. }
  423. TEST_CASE("[SceneTree][Node] Test the processing") {
  424. TestNode *node = memnew(TestNode);
  425. SceneTree::get_singleton()->get_root()->add_child(node);
  426. SUBCASE("No process") {
  427. CHECK_EQ(0, node->process_counter);
  428. CHECK_EQ(0, node->physics_process_counter);
  429. }
  430. SUBCASE("Process") {
  431. node->set_process(true);
  432. SceneTree::get_singleton()->process(0);
  433. CHECK_EQ(1, node->process_counter);
  434. CHECK_EQ(0, node->physics_process_counter);
  435. CHECK_EQ(0, node->internal_process_counter);
  436. CHECK_EQ(0, node->internal_physics_process_counter);
  437. }
  438. SUBCASE("Physics process") {
  439. node->set_physics_process(true);
  440. SceneTree::get_singleton()->physics_process(0);
  441. CHECK_EQ(0, node->process_counter);
  442. CHECK_EQ(1, node->physics_process_counter);
  443. CHECK_EQ(0, node->internal_process_counter);
  444. CHECK_EQ(0, node->internal_physics_process_counter);
  445. }
  446. SUBCASE("Normal and physics process") {
  447. node->set_process(true);
  448. node->set_physics_process(true);
  449. SceneTree::get_singleton()->process(0);
  450. SceneTree::get_singleton()->physics_process(0);
  451. CHECK_EQ(1, node->process_counter);
  452. CHECK_EQ(1, node->physics_process_counter);
  453. CHECK_EQ(0, node->internal_process_counter);
  454. CHECK_EQ(0, node->internal_physics_process_counter);
  455. }
  456. SUBCASE("Internal, normal and physics process") {
  457. node->set_process_internal(true);
  458. node->set_physics_process_internal(true);
  459. SceneTree::get_singleton()->process(0);
  460. SceneTree::get_singleton()->physics_process(0);
  461. CHECK_EQ(0, node->process_counter);
  462. CHECK_EQ(0, node->physics_process_counter);
  463. CHECK_EQ(1, node->internal_process_counter);
  464. CHECK_EQ(1, node->internal_physics_process_counter);
  465. }
  466. SUBCASE("All processing") {
  467. node->set_process(true);
  468. node->set_physics_process(true);
  469. node->set_process_internal(true);
  470. node->set_physics_process_internal(true);
  471. SceneTree::get_singleton()->process(0);
  472. SceneTree::get_singleton()->physics_process(0);
  473. CHECK_EQ(1, node->process_counter);
  474. CHECK_EQ(1, node->physics_process_counter);
  475. CHECK_EQ(1, node->internal_process_counter);
  476. CHECK_EQ(1, node->internal_physics_process_counter);
  477. }
  478. SUBCASE("All processing twice") {
  479. node->set_process(true);
  480. node->set_physics_process(true);
  481. node->set_process_internal(true);
  482. node->set_physics_process_internal(true);
  483. SceneTree::get_singleton()->process(0);
  484. SceneTree::get_singleton()->physics_process(0);
  485. SceneTree::get_singleton()->process(0);
  486. SceneTree::get_singleton()->physics_process(0);
  487. CHECK_EQ(2, node->process_counter);
  488. CHECK_EQ(2, node->physics_process_counter);
  489. CHECK_EQ(2, node->internal_process_counter);
  490. CHECK_EQ(2, node->internal_physics_process_counter);
  491. }
  492. SUBCASE("Enable and disable processing") {
  493. node->set_process(true);
  494. node->set_physics_process(true);
  495. node->set_process_internal(true);
  496. node->set_physics_process_internal(true);
  497. SceneTree::get_singleton()->process(0);
  498. SceneTree::get_singleton()->physics_process(0);
  499. node->set_process(false);
  500. node->set_physics_process(false);
  501. node->set_process_internal(false);
  502. node->set_physics_process_internal(false);
  503. SceneTree::get_singleton()->process(0);
  504. SceneTree::get_singleton()->physics_process(0);
  505. CHECK_EQ(1, node->process_counter);
  506. CHECK_EQ(1, node->physics_process_counter);
  507. CHECK_EQ(1, node->internal_process_counter);
  508. CHECK_EQ(1, node->internal_physics_process_counter);
  509. }
  510. memdelete(node);
  511. }
  512. TEST_CASE("[SceneTree][Node] Test the process priority") {
  513. List<Node *> process_order;
  514. TestNode *node = memnew(TestNode);
  515. node->callback_list = &process_order;
  516. SceneTree::get_singleton()->get_root()->add_child(node);
  517. TestNode *node2 = memnew(TestNode);
  518. node2->callback_list = &process_order;
  519. SceneTree::get_singleton()->get_root()->add_child(node2);
  520. TestNode *node3 = memnew(TestNode);
  521. node3->callback_list = &process_order;
  522. SceneTree::get_singleton()->get_root()->add_child(node3);
  523. TestNode *node4 = memnew(TestNode);
  524. node4->callback_list = &process_order;
  525. SceneTree::get_singleton()->get_root()->add_child(node4);
  526. SUBCASE("Process priority") {
  527. node->set_process(true);
  528. node->set_process_priority(20);
  529. node2->set_process(true);
  530. node2->set_process_priority(10);
  531. node3->set_process(true);
  532. node3->set_process_priority(40);
  533. node4->set_process(true);
  534. node4->set_process_priority(30);
  535. SceneTree::get_singleton()->process(0);
  536. CHECK_EQ(4, process_order.size());
  537. List<Node *>::Element *E = process_order.front();
  538. CHECK_EQ(E->get(), node2);
  539. E = E->next();
  540. CHECK_EQ(E->get(), node);
  541. E = E->next();
  542. CHECK_EQ(E->get(), node4);
  543. E = E->next();
  544. CHECK_EQ(E->get(), node3);
  545. }
  546. SUBCASE("Physics process priority") {
  547. node->set_physics_process(true);
  548. node->set_physics_process_priority(20);
  549. node2->set_physics_process(true);
  550. node2->set_physics_process_priority(10);
  551. node3->set_physics_process(true);
  552. node3->set_physics_process_priority(40);
  553. node4->set_physics_process(true);
  554. node4->set_physics_process_priority(30);
  555. SceneTree::get_singleton()->physics_process(0);
  556. CHECK_EQ(4, process_order.size());
  557. List<Node *>::Element *E = process_order.front();
  558. CHECK_EQ(E->get(), node2);
  559. E = E->next();
  560. CHECK_EQ(E->get(), node);
  561. E = E->next();
  562. CHECK_EQ(E->get(), node4);
  563. E = E->next();
  564. CHECK_EQ(E->get(), node3);
  565. }
  566. memdelete(node);
  567. memdelete(node2);
  568. memdelete(node3);
  569. memdelete(node4);
  570. }
  571. } // namespace TestNode
  572. #endif // TEST_NODE_H