test_node.h 27 KB

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