test_packed_scene.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /*
  84. // FIXME: This subcase requires GH-48064 to be fixed.
  85. SUBCASE("Signals that should not be saved") {
  86. int subscene_flags = Object::CONNECT_PERSIST | Object::CONNECT_INHERITED;
  87. // subscene node to itself
  88. sub_scene_node->connect("ready", callable_mp(sub_scene_node, &Node::is_ready), subscene_flags);
  89. // subscene node to subscene root
  90. sub_scene_node->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), subscene_flags);
  91. //subscene root to subscene root (connected within sub scene)
  92. sub_scene_root->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), subscene_flags);
  93. // Pack the scene.
  94. Ref<PackedScene> packed_scene;
  95. packed_scene.instantiate();
  96. const Error err = packed_scene->pack(main_scene_root);
  97. CHECK(err == OK);
  98. // Make sure the right connections are in packed scene.
  99. Ref<SceneState> state = packed_scene->get_state();
  100. CHECK_EQ(state->get_connection_count(), 0);
  101. }
  102. */
  103. memdelete(main_scene_root);
  104. }
  105. TEST_CASE("[PackedScene] Clear Packed Scene") {
  106. // Create a scene to pack.
  107. Node *scene = memnew(Node);
  108. scene->set_name("TestScene");
  109. // Pack the scene.
  110. PackedScene packed_scene;
  111. packed_scene.pack(scene);
  112. // Clear the packed scene.
  113. packed_scene.clear();
  114. // Check if it has been cleared.
  115. Ref<SceneState> state = packed_scene.get_state();
  116. CHECK_FALSE(state->get_node_count() == 1);
  117. memdelete(scene);
  118. }
  119. TEST_CASE("[PackedScene] Can Instantiate Packed Scene") {
  120. // Create a scene to pack.
  121. Node *scene = memnew(Node);
  122. scene->set_name("TestScene");
  123. // Pack the scene.
  124. PackedScene packed_scene;
  125. packed_scene.pack(scene);
  126. // Check if the packed scene can be instantiated.
  127. const bool can_instantiate = packed_scene.can_instantiate();
  128. CHECK(can_instantiate == true);
  129. memdelete(scene);
  130. }
  131. TEST_CASE("[PackedScene] Instantiate Packed Scene") {
  132. // Create a scene to pack.
  133. Node *scene = memnew(Node);
  134. scene->set_name("TestScene");
  135. // Pack the scene.
  136. PackedScene packed_scene;
  137. packed_scene.pack(scene);
  138. // Instantiate the packed scene.
  139. Node *instance = packed_scene.instantiate();
  140. CHECK(instance != nullptr);
  141. CHECK(instance->get_name() == "TestScene");
  142. memdelete(scene);
  143. memdelete(instance);
  144. }
  145. TEST_CASE("[PackedScene] Instantiate Packed Scene With Children") {
  146. // Create a scene to pack.
  147. Node *scene = memnew(Node);
  148. scene->set_name("TestScene");
  149. // Add persisting child nodes to the scene.
  150. Node *child1 = memnew(Node);
  151. child1->set_name("Child1");
  152. scene->add_child(child1);
  153. child1->set_owner(scene);
  154. Node *child2 = memnew(Node);
  155. child2->set_name("Child2");
  156. scene->add_child(child2);
  157. child2->set_owner(scene);
  158. // Add non persisting child node to the scene.
  159. Node *child3 = memnew(Node);
  160. child3->set_name("Child3");
  161. scene->add_child(child3);
  162. // Pack the scene.
  163. PackedScene packed_scene;
  164. packed_scene.pack(scene);
  165. // Instantiate the packed scene.
  166. Node *instance = packed_scene.instantiate();
  167. CHECK(instance != nullptr);
  168. CHECK(instance->get_name() == "TestScene");
  169. // Validate the child nodes of the instantiated scene.
  170. CHECK(instance->get_child_count() == 2);
  171. CHECK(instance->get_child(0)->get_name() == "Child1");
  172. CHECK(instance->get_child(1)->get_name() == "Child2");
  173. CHECK(instance->get_child(0)->get_owner() == instance);
  174. CHECK(instance->get_child(1)->get_owner() == instance);
  175. memdelete(scene);
  176. memdelete(instance);
  177. }
  178. TEST_CASE("[PackedScene] Set Path") {
  179. // Create a scene to pack.
  180. Node *scene = memnew(Node);
  181. scene->set_name("TestScene");
  182. // Pack the scene.
  183. PackedScene packed_scene;
  184. packed_scene.pack(scene);
  185. // Set a new path for the packed scene.
  186. const String new_path = "NewTestPath";
  187. packed_scene.set_path(new_path);
  188. // Check if the path has been set correctly.
  189. Ref<SceneState> state = packed_scene.get_state();
  190. CHECK(state.is_valid());
  191. CHECK(state->get_path() == new_path);
  192. memdelete(scene);
  193. }
  194. TEST_CASE("[PackedScene] Replace State") {
  195. // Create a scene to pack.
  196. Node *scene = memnew(Node);
  197. scene->set_name("TestScene");
  198. // Pack the scene.
  199. PackedScene packed_scene;
  200. packed_scene.pack(scene);
  201. // Create another scene state to replace with.
  202. Ref<SceneState> new_state = memnew(SceneState);
  203. new_state->set_path("NewPath");
  204. // Replace the state.
  205. packed_scene.replace_state(new_state);
  206. // Check if the state has been replaced.
  207. Ref<SceneState> state = packed_scene.get_state();
  208. CHECK(state.is_valid());
  209. CHECK(state == new_state);
  210. memdelete(scene);
  211. }
  212. TEST_CASE("[PackedScene] Recreate State") {
  213. // Create a scene to pack.
  214. Node *scene = memnew(Node);
  215. scene->set_name("TestScene");
  216. // Pack the scene.
  217. PackedScene packed_scene;
  218. packed_scene.pack(scene);
  219. // Recreate the state.
  220. packed_scene.recreate_state();
  221. // Check if the state has been recreated.
  222. Ref<SceneState> state = packed_scene.get_state();
  223. CHECK(state.is_valid());
  224. CHECK(state->get_node_count() == 0); // Since the state was recreated, it should be empty.
  225. memdelete(scene);
  226. }
  227. } // namespace TestPackedScene
  228. #endif // TEST_PACKED_SCENE_H