123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- /**************************************************************************/
- /* test_packed_scene.h */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #ifndef TEST_PACKED_SCENE_H
- #define TEST_PACKED_SCENE_H
- #include "scene/resources/packed_scene.h"
- #include "tests/test_macros.h"
- namespace TestPackedScene {
- TEST_CASE("[PackedScene] Pack Scene and Retrieve State") {
- // Create a scene to pack.
- Node *scene = memnew(Node);
- scene->set_name("TestScene");
- // Pack the scene.
- PackedScene packed_scene;
- const Error err = packed_scene.pack(scene);
- CHECK(err == OK);
- // Retrieve the packed state.
- Ref<SceneState> state = packed_scene.get_state();
- CHECK(state.is_valid());
- CHECK(state->get_node_count() == 1);
- CHECK(state->get_node_name(0) == "TestScene");
- memdelete(scene);
- }
- TEST_CASE("[PackedScene] Signals Preserved when Packing Scene") {
- // Create main scene
- // root
- // `- sub_node (local)
- // `- sub_scene (instance of another scene)
- // `- sub_scene_node (owned by sub_scene)
- Node *main_scene_root = memnew(Node);
- Node *sub_node = memnew(Node);
- Node *sub_scene_root = memnew(Node);
- Node *sub_scene_node = memnew(Node);
- main_scene_root->add_child(sub_node);
- sub_node->set_owner(main_scene_root);
- sub_scene_root->add_child(sub_scene_node);
- sub_scene_node->set_owner(sub_scene_root);
- main_scene_root->add_child(sub_scene_root);
- sub_scene_root->set_owner(main_scene_root);
- SUBCASE("Signals that should be saved") {
- int main_flags = Object::CONNECT_PERSIST;
- // sub node to a node in main scene
- sub_node->connect("ready", callable_mp(main_scene_root, &Node::is_ready), main_flags);
- // subscene root to a node in main scene
- sub_scene_root->connect("ready", callable_mp(main_scene_root, &Node::is_ready), main_flags);
- //subscene root to subscene root (connected within main scene)
- sub_scene_root->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), main_flags);
- // Pack the scene.
- Ref<PackedScene> packed_scene;
- packed_scene.instantiate();
- const Error err = packed_scene->pack(main_scene_root);
- CHECK(err == OK);
- // Make sure the right connections are in packed scene.
- Ref<SceneState> state = packed_scene->get_state();
- CHECK_EQ(state->get_connection_count(), 3);
- }
- /*
- // FIXME: This subcase requires GH-48064 to be fixed.
- SUBCASE("Signals that should not be saved") {
- int subscene_flags = Object::CONNECT_PERSIST | Object::CONNECT_INHERITED;
- // subscene node to itself
- sub_scene_node->connect("ready", callable_mp(sub_scene_node, &Node::is_ready), subscene_flags);
- // subscene node to subscene root
- sub_scene_node->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), subscene_flags);
- //subscene root to subscene root (connected within sub scene)
- sub_scene_root->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), subscene_flags);
- // Pack the scene.
- Ref<PackedScene> packed_scene;
- packed_scene.instantiate();
- const Error err = packed_scene->pack(main_scene_root);
- CHECK(err == OK);
- // Make sure the right connections are in packed scene.
- Ref<SceneState> state = packed_scene->get_state();
- CHECK_EQ(state->get_connection_count(), 0);
- }
- */
- memdelete(main_scene_root);
- }
- TEST_CASE("[PackedScene] Clear Packed Scene") {
- // Create a scene to pack.
- Node *scene = memnew(Node);
- scene->set_name("TestScene");
- // Pack the scene.
- PackedScene packed_scene;
- packed_scene.pack(scene);
- // Clear the packed scene.
- packed_scene.clear();
- // Check if it has been cleared.
- Ref<SceneState> state = packed_scene.get_state();
- CHECK_FALSE(state->get_node_count() == 1);
- memdelete(scene);
- }
- TEST_CASE("[PackedScene] Can Instantiate Packed Scene") {
- // Create a scene to pack.
- Node *scene = memnew(Node);
- scene->set_name("TestScene");
- // Pack the scene.
- PackedScene packed_scene;
- packed_scene.pack(scene);
- // Check if the packed scene can be instantiated.
- const bool can_instantiate = packed_scene.can_instantiate();
- CHECK(can_instantiate == true);
- memdelete(scene);
- }
- TEST_CASE("[PackedScene] Instantiate Packed Scene") {
- // Create a scene to pack.
- Node *scene = memnew(Node);
- scene->set_name("TestScene");
- // Pack the scene.
- PackedScene packed_scene;
- packed_scene.pack(scene);
- // Instantiate the packed scene.
- Node *instance = packed_scene.instantiate();
- CHECK(instance != nullptr);
- CHECK(instance->get_name() == "TestScene");
- memdelete(scene);
- memdelete(instance);
- }
- TEST_CASE("[PackedScene] Instantiate Packed Scene With Children") {
- // Create a scene to pack.
- Node *scene = memnew(Node);
- scene->set_name("TestScene");
- // Add persisting child nodes to the scene.
- Node *child1 = memnew(Node);
- child1->set_name("Child1");
- scene->add_child(child1);
- child1->set_owner(scene);
- Node *child2 = memnew(Node);
- child2->set_name("Child2");
- scene->add_child(child2);
- child2->set_owner(scene);
- // Add non persisting child node to the scene.
- Node *child3 = memnew(Node);
- child3->set_name("Child3");
- scene->add_child(child3);
- // Pack the scene.
- PackedScene packed_scene;
- packed_scene.pack(scene);
- // Instantiate the packed scene.
- Node *instance = packed_scene.instantiate();
- CHECK(instance != nullptr);
- CHECK(instance->get_name() == "TestScene");
- // Validate the child nodes of the instantiated scene.
- CHECK(instance->get_child_count() == 2);
- CHECK(instance->get_child(0)->get_name() == "Child1");
- CHECK(instance->get_child(1)->get_name() == "Child2");
- CHECK(instance->get_child(0)->get_owner() == instance);
- CHECK(instance->get_child(1)->get_owner() == instance);
- memdelete(scene);
- memdelete(instance);
- }
- TEST_CASE("[PackedScene] Set Path") {
- // Create a scene to pack.
- Node *scene = memnew(Node);
- scene->set_name("TestScene");
- // Pack the scene.
- PackedScene packed_scene;
- packed_scene.pack(scene);
- // Set a new path for the packed scene.
- const String new_path = "NewTestPath";
- packed_scene.set_path(new_path);
- // Check if the path has been set correctly.
- Ref<SceneState> state = packed_scene.get_state();
- CHECK(state.is_valid());
- CHECK(state->get_path() == new_path);
- memdelete(scene);
- }
- TEST_CASE("[PackedScene] Replace State") {
- // Create a scene to pack.
- Node *scene = memnew(Node);
- scene->set_name("TestScene");
- // Pack the scene.
- PackedScene packed_scene;
- packed_scene.pack(scene);
- // Create another scene state to replace with.
- Ref<SceneState> new_state = memnew(SceneState);
- new_state->set_path("NewPath");
- // Replace the state.
- packed_scene.replace_state(new_state);
- // Check if the state has been replaced.
- Ref<SceneState> state = packed_scene.get_state();
- CHECK(state.is_valid());
- CHECK(state == new_state);
- memdelete(scene);
- }
- TEST_CASE("[PackedScene] Recreate State") {
- // Create a scene to pack.
- Node *scene = memnew(Node);
- scene->set_name("TestScene");
- // Pack the scene.
- PackedScene packed_scene;
- packed_scene.pack(scene);
- // Recreate the state.
- packed_scene.recreate_state();
- // Check if the state has been recreated.
- Ref<SceneState> state = packed_scene.get_state();
- CHECK(state.is_valid());
- CHECK(state->get_node_count() == 0); // Since the state was recreated, it should be empty.
- memdelete(scene);
- }
- } // namespace TestPackedScene
- #endif // TEST_PACKED_SCENE_H
|