test_packed_scene.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**************************************************************************/
  2. /* test_packed_scene.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_PACKED_SCENE_H
  31. #define TEST_PACKED_SCENE_H
  32. #include "scene/resources/packed_scene.h"
  33. #include "tests/test_macros.h"
  34. namespace TestPackedScene {
  35. TEST_CASE("[PackedScene] Pack Scene and Retrieve State") {
  36. // Create a scene to pack.
  37. Node *scene = memnew(Node);
  38. scene->set_name("TestScene");
  39. // Pack the scene.
  40. PackedScene packed_scene;
  41. const Error err = packed_scene.pack(scene);
  42. CHECK(err == OK);
  43. // Retrieve the packed state.
  44. Ref<SceneState> state = packed_scene.get_state();
  45. CHECK(state.is_valid());
  46. CHECK(state->get_node_count() == 1);
  47. CHECK(state->get_node_name(0) == "TestScene");
  48. memdelete(scene);
  49. }
  50. TEST_CASE("[PackedScene] Signals Preserved when Packing Scene") {
  51. // Create main scene
  52. // root
  53. // `- sub_node (local)
  54. // `- sub_scene (instance of another scene)
  55. // `- sub_scene_node (owned by sub_scene)
  56. Node *main_scene_root = memnew(Node);
  57. Node *sub_node = memnew(Node);
  58. Node *sub_scene_root = memnew(Node);
  59. Node *sub_scene_node = memnew(Node);
  60. main_scene_root->add_child(sub_node);
  61. sub_node->set_owner(main_scene_root);
  62. sub_scene_root->add_child(sub_scene_node);
  63. sub_scene_node->set_owner(sub_scene_root);
  64. main_scene_root->add_child(sub_scene_root);
  65. sub_scene_root->set_owner(main_scene_root);
  66. SUBCASE("Signals that should be saved") {
  67. int main_flags = Object::CONNECT_PERSIST;
  68. // sub node to a node in main scene
  69. sub_node->connect("ready", callable_mp(main_scene_root, &Node::is_ready), main_flags);
  70. // subscene root to a node in main scene
  71. sub_scene_root->connect("ready", callable_mp(main_scene_root, &Node::is_ready), main_flags);
  72. //subscene root to subscene root (connected within main scene)
  73. sub_scene_root->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), main_flags);
  74. // Pack the scene.
  75. Ref<PackedScene> packed_scene;
  76. packed_scene.instantiate();
  77. const Error err = packed_scene->pack(main_scene_root);
  78. CHECK(err == OK);
  79. // Make sure the right connections are in packed scene.
  80. Ref<SceneState> state = packed_scene->get_state();
  81. CHECK_EQ(state->get_connection_count(), 3);
  82. }
  83. SUBCASE("Signals that should not be saved") {
  84. int subscene_flags = Object::CONNECT_PERSIST | Object::CONNECT_INHERITED;
  85. // subscene node to itself
  86. sub_scene_node->connect("ready", callable_mp(sub_scene_node, &Node::is_ready), subscene_flags);
  87. // subscene node to subscene root
  88. sub_scene_node->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), subscene_flags);
  89. //subscene root to subscene root (connected within sub scene)
  90. sub_scene_root->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), subscene_flags);
  91. // Pack the scene.
  92. Ref<PackedScene> packed_scene;
  93. packed_scene.instantiate();
  94. const Error err = packed_scene->pack(main_scene_root);
  95. CHECK(err == OK);
  96. // Make sure the right connections are in packed scene.
  97. Ref<SceneState> state = packed_scene->get_state();
  98. CHECK_EQ(state->get_connection_count(), 0);
  99. }
  100. memdelete(main_scene_root);
  101. }
  102. TEST_CASE("[PackedScene] Clear Packed Scene") {
  103. // Create a scene to pack.
  104. Node *scene = memnew(Node);
  105. scene->set_name("TestScene");
  106. // Pack the scene.
  107. PackedScene packed_scene;
  108. packed_scene.pack(scene);
  109. // Clear the packed scene.
  110. packed_scene.clear();
  111. // Check if it has been cleared.
  112. Ref<SceneState> state = packed_scene.get_state();
  113. CHECK_FALSE(state->get_node_count() == 1);
  114. memdelete(scene);
  115. }
  116. TEST_CASE("[PackedScene] Can Instantiate Packed Scene") {
  117. // Create a scene to pack.
  118. Node *scene = memnew(Node);
  119. scene->set_name("TestScene");
  120. // Pack the scene.
  121. PackedScene packed_scene;
  122. packed_scene.pack(scene);
  123. // Check if the packed scene can be instantiated.
  124. const bool can_instantiate = packed_scene.can_instantiate();
  125. CHECK(can_instantiate == true);
  126. memdelete(scene);
  127. }
  128. TEST_CASE("[PackedScene] Instantiate Packed Scene") {
  129. // Create a scene to pack.
  130. Node *scene = memnew(Node);
  131. scene->set_name("TestScene");
  132. // Pack the scene.
  133. PackedScene packed_scene;
  134. packed_scene.pack(scene);
  135. // Instantiate the packed scene.
  136. Node *instance = packed_scene.instantiate();
  137. CHECK(instance != nullptr);
  138. CHECK(instance->get_name() == "TestScene");
  139. memdelete(scene);
  140. memdelete(instance);
  141. }
  142. TEST_CASE("[PackedScene] Instantiate Packed Scene With Children") {
  143. // Create a scene to pack.
  144. Node *scene = memnew(Node);
  145. scene->set_name("TestScene");
  146. // Add persisting child nodes to the scene.
  147. Node *child1 = memnew(Node);
  148. child1->set_name("Child1");
  149. scene->add_child(child1);
  150. child1->set_owner(scene);
  151. Node *child2 = memnew(Node);
  152. child2->set_name("Child2");
  153. scene->add_child(child2);
  154. child2->set_owner(scene);
  155. // Add non persisting child node to the scene.
  156. Node *child3 = memnew(Node);
  157. child3->set_name("Child3");
  158. scene->add_child(child3);
  159. // Pack the scene.
  160. PackedScene packed_scene;
  161. packed_scene.pack(scene);
  162. // Instantiate the packed scene.
  163. Node *instance = packed_scene.instantiate();
  164. CHECK(instance != nullptr);
  165. CHECK(instance->get_name() == "TestScene");
  166. // Validate the child nodes of the instantiated scene.
  167. CHECK(instance->get_child_count() == 2);
  168. CHECK(instance->get_child(0)->get_name() == "Child1");
  169. CHECK(instance->get_child(1)->get_name() == "Child2");
  170. CHECK(instance->get_child(0)->get_owner() == instance);
  171. CHECK(instance->get_child(1)->get_owner() == instance);
  172. memdelete(scene);
  173. memdelete(instance);
  174. }
  175. TEST_CASE("[PackedScene] Set Path") {
  176. // Create a scene to pack.
  177. Node *scene = memnew(Node);
  178. scene->set_name("TestScene");
  179. // Pack the scene.
  180. PackedScene packed_scene;
  181. packed_scene.pack(scene);
  182. // Set a new path for the packed scene.
  183. const String new_path = "NewTestPath";
  184. packed_scene.set_path(new_path);
  185. // Check if the path has been set correctly.
  186. Ref<SceneState> state = packed_scene.get_state();
  187. CHECK(state.is_valid());
  188. CHECK(state->get_path() == new_path);
  189. memdelete(scene);
  190. }
  191. TEST_CASE("[PackedScene] Replace State") {
  192. // Create a scene to pack.
  193. Node *scene = memnew(Node);
  194. scene->set_name("TestScene");
  195. // Pack the scene.
  196. PackedScene packed_scene;
  197. packed_scene.pack(scene);
  198. // Create another scene state to replace with.
  199. Ref<SceneState> new_state = memnew(SceneState);
  200. new_state->set_path("NewPath");
  201. // Replace the state.
  202. packed_scene.replace_state(new_state);
  203. // Check if the state has been replaced.
  204. Ref<SceneState> state = packed_scene.get_state();
  205. CHECK(state.is_valid());
  206. CHECK(state == new_state);
  207. memdelete(scene);
  208. }
  209. TEST_CASE("[PackedScene] Recreate State") {
  210. // Create a scene to pack.
  211. Node *scene = memnew(Node);
  212. scene->set_name("TestScene");
  213. // Pack the scene.
  214. PackedScene packed_scene;
  215. packed_scene.pack(scene);
  216. // Recreate the state.
  217. packed_scene.recreate_state();
  218. // Check if the state has been recreated.
  219. Ref<SceneState> state = packed_scene.get_state();
  220. CHECK(state.is_valid());
  221. CHECK(state->get_node_count() == 0); // Since the state was recreated, it should be empty.
  222. memdelete(scene);
  223. }
  224. } // namespace TestPackedScene
  225. #endif // TEST_PACKED_SCENE_H