test_packed_scene.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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] Clear Packed Scene") {
  51. // Create a scene to pack.
  52. Node *scene = memnew(Node);
  53. scene->set_name("TestScene");
  54. // Pack the scene.
  55. PackedScene packed_scene;
  56. packed_scene.pack(scene);
  57. // Clear the packed scene.
  58. packed_scene.clear();
  59. // Check if it has been cleared.
  60. Ref<SceneState> state = packed_scene.get_state();
  61. CHECK_FALSE(state->get_node_count() == 1);
  62. memdelete(scene);
  63. }
  64. TEST_CASE("[PackedScene] Can Instantiate Packed Scene") {
  65. // Create a scene to pack.
  66. Node *scene = memnew(Node);
  67. scene->set_name("TestScene");
  68. // Pack the scene.
  69. PackedScene packed_scene;
  70. packed_scene.pack(scene);
  71. // Check if the packed scene can be instantiated.
  72. const bool can_instantiate = packed_scene.can_instantiate();
  73. CHECK(can_instantiate == true);
  74. memdelete(scene);
  75. }
  76. TEST_CASE("[PackedScene] Instantiate Packed Scene") {
  77. // Create a scene to pack.
  78. Node *scene = memnew(Node);
  79. scene->set_name("TestScene");
  80. // Pack the scene.
  81. PackedScene packed_scene;
  82. packed_scene.pack(scene);
  83. // Instantiate the packed scene.
  84. Node *instance = packed_scene.instantiate();
  85. CHECK(instance != nullptr);
  86. CHECK(instance->get_name() == "TestScene");
  87. memdelete(scene);
  88. memdelete(instance);
  89. }
  90. TEST_CASE("[PackedScene] Instantiate Packed Scene With Children") {
  91. // Create a scene to pack.
  92. Node *scene = memnew(Node);
  93. scene->set_name("TestScene");
  94. // Add persisting child nodes to the scene.
  95. Node *child1 = memnew(Node);
  96. child1->set_name("Child1");
  97. scene->add_child(child1);
  98. child1->set_owner(scene);
  99. Node *child2 = memnew(Node);
  100. child2->set_name("Child2");
  101. scene->add_child(child2);
  102. child2->set_owner(scene);
  103. // Add non persisting child node to the scene.
  104. Node *child3 = memnew(Node);
  105. child3->set_name("Child3");
  106. scene->add_child(child3);
  107. // Pack the scene.
  108. PackedScene packed_scene;
  109. packed_scene.pack(scene);
  110. // Instantiate the packed scene.
  111. Node *instance = packed_scene.instantiate();
  112. CHECK(instance != nullptr);
  113. CHECK(instance->get_name() == "TestScene");
  114. // Validate the child nodes of the instantiated scene.
  115. CHECK(instance->get_child_count() == 2);
  116. CHECK(instance->get_child(0)->get_name() == "Child1");
  117. CHECK(instance->get_child(1)->get_name() == "Child2");
  118. CHECK(instance->get_child(0)->get_owner() == instance);
  119. CHECK(instance->get_child(1)->get_owner() == instance);
  120. memdelete(scene);
  121. memdelete(instance);
  122. }
  123. TEST_CASE("[PackedScene] Set Path") {
  124. // Create a scene to pack.
  125. Node *scene = memnew(Node);
  126. scene->set_name("TestScene");
  127. // Pack the scene.
  128. PackedScene packed_scene;
  129. packed_scene.pack(scene);
  130. // Set a new path for the packed scene.
  131. const String new_path = "NewTestPath";
  132. packed_scene.set_path(new_path);
  133. // Check if the path has been set correctly.
  134. Ref<SceneState> state = packed_scene.get_state();
  135. CHECK(state.is_valid());
  136. CHECK(state->get_path() == new_path);
  137. memdelete(scene);
  138. }
  139. TEST_CASE("[PackedScene] Replace State") {
  140. // Create a scene to pack.
  141. Node *scene = memnew(Node);
  142. scene->set_name("TestScene");
  143. // Pack the scene.
  144. PackedScene packed_scene;
  145. packed_scene.pack(scene);
  146. // Create another scene state to replace with.
  147. Ref<SceneState> new_state = memnew(SceneState);
  148. new_state->set_path("NewPath");
  149. // Replace the state.
  150. packed_scene.replace_state(new_state);
  151. // Check if the state has been replaced.
  152. Ref<SceneState> state = packed_scene.get_state();
  153. CHECK(state.is_valid());
  154. CHECK(state == new_state);
  155. memdelete(scene);
  156. }
  157. TEST_CASE("[PackedScene] Recreate State") {
  158. // Create a scene to pack.
  159. Node *scene = memnew(Node);
  160. scene->set_name("TestScene");
  161. // Pack the scene.
  162. PackedScene packed_scene;
  163. packed_scene.pack(scene);
  164. // Recreate the state.
  165. packed_scene.recreate_state();
  166. // Check if the state has been recreated.
  167. Ref<SceneState> state = packed_scene.get_state();
  168. CHECK(state.is_valid());
  169. CHECK(state->get_node_count() == 0); // Since the state was recreated, it should be empty.
  170. memdelete(scene);
  171. }
  172. } // namespace TestPackedScene
  173. #endif // TEST_PACKED_SCENE_H