/**************************************************************************/1/* test_packed_scene.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "scene/resources/packed_scene.h"3334#include "tests/test_macros.h"3536namespace TestPackedScene {3738TEST_CASE("[PackedScene] Pack Scene and Retrieve State") {39// Create a scene to pack.40Node *scene = memnew(Node);41scene->set_name("TestScene");4243// Pack the scene.44PackedScene packed_scene;45const Error err = packed_scene.pack(scene);46CHECK(err == OK);4748// Retrieve the packed state.49Ref<SceneState> state = packed_scene.get_state();50CHECK(state.is_valid());51CHECK(state->get_node_count() == 1);52CHECK(state->get_node_name(0) == "TestScene");5354memdelete(scene);55}5657TEST_CASE("[PackedScene] Signals Preserved when Packing Scene") {58// Create main scene59// root60// `- sub_node (local)61// `- sub_scene (instance of another scene)62// `- sub_scene_node (owned by sub_scene)63Node *main_scene_root = memnew(Node);64Node *sub_node = memnew(Node);65Node *sub_scene_root = memnew(Node);66Node *sub_scene_node = memnew(Node);6768main_scene_root->add_child(sub_node);69sub_node->set_owner(main_scene_root);7071sub_scene_root->add_child(sub_scene_node);72sub_scene_node->set_owner(sub_scene_root);7374main_scene_root->add_child(sub_scene_root);75sub_scene_root->set_owner(main_scene_root);7677SUBCASE("Signals that should be saved") {78int main_flags = Object::CONNECT_PERSIST;79// sub node to a node in main scene80sub_node->connect("ready", callable_mp(main_scene_root, &Node::is_ready), main_flags);81// subscene root to a node in main scene82sub_scene_root->connect("ready", callable_mp(main_scene_root, &Node::is_ready), main_flags);83//subscene root to subscene root (connected within main scene)84sub_scene_root->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), main_flags);8586// Pack the scene.87Ref<PackedScene> packed_scene;88packed_scene.instantiate();89const Error err = packed_scene->pack(main_scene_root);90CHECK(err == OK);9192// Make sure the right connections are in packed scene.93Ref<SceneState> state = packed_scene->get_state();94CHECK_EQ(state->get_connection_count(), 3);95}9697/*98// FIXME: This subcase requires GH-48064 to be fixed.99SUBCASE("Signals that should not be saved") {100int subscene_flags = Object::CONNECT_PERSIST | Object::CONNECT_INHERITED;101// subscene node to itself102sub_scene_node->connect("ready", callable_mp(sub_scene_node, &Node::is_ready), subscene_flags);103// subscene node to subscene root104sub_scene_node->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), subscene_flags);105//subscene root to subscene root (connected within sub scene)106sub_scene_root->connect("ready", callable_mp(sub_scene_root, &Node::is_ready), subscene_flags);107108// Pack the scene.109Ref<PackedScene> packed_scene;110packed_scene.instantiate();111const Error err = packed_scene->pack(main_scene_root);112CHECK(err == OK);113114// Make sure the right connections are in packed scene.115Ref<SceneState> state = packed_scene->get_state();116CHECK_EQ(state->get_connection_count(), 0);117}118*/119120memdelete(main_scene_root);121}122123TEST_CASE("[PackedScene] Clear Packed Scene") {124// Create a scene to pack.125Node *scene = memnew(Node);126scene->set_name("TestScene");127128// Pack the scene.129PackedScene packed_scene;130packed_scene.pack(scene);131132// Clear the packed scene.133packed_scene.clear();134135// Check if it has been cleared.136Ref<SceneState> state = packed_scene.get_state();137CHECK_FALSE(state->get_node_count() == 1);138139memdelete(scene);140}141142TEST_CASE("[PackedScene] Can Instantiate Packed Scene") {143// Create a scene to pack.144Node *scene = memnew(Node);145scene->set_name("TestScene");146147// Pack the scene.148PackedScene packed_scene;149packed_scene.pack(scene);150151// Check if the packed scene can be instantiated.152const bool can_instantiate = packed_scene.can_instantiate();153CHECK(can_instantiate == true);154155memdelete(scene);156}157158TEST_CASE("[PackedScene] Instantiate Packed Scene") {159// Create a scene to pack.160Node *scene = memnew(Node);161scene->set_name("TestScene");162163// Pack the scene.164PackedScene packed_scene;165packed_scene.pack(scene);166167// Instantiate the packed scene.168Node *instance = packed_scene.instantiate();169CHECK(instance != nullptr);170CHECK(instance->get_name() == "TestScene");171172memdelete(scene);173memdelete(instance);174}175176TEST_CASE("[PackedScene] Instantiate Packed Scene With Children") {177// Create a scene to pack.178Node *scene = memnew(Node);179scene->set_name("TestScene");180181// Add persisting child nodes to the scene.182Node *child1 = memnew(Node);183child1->set_name("Child1");184scene->add_child(child1);185child1->set_owner(scene);186187Node *child2 = memnew(Node);188child2->set_name("Child2");189scene->add_child(child2);190child2->set_owner(scene);191192// Add non persisting child node to the scene.193Node *child3 = memnew(Node);194child3->set_name("Child3");195scene->add_child(child3);196197// Pack the scene.198PackedScene packed_scene;199packed_scene.pack(scene);200201// Instantiate the packed scene.202Node *instance = packed_scene.instantiate();203CHECK(instance != nullptr);204CHECK(instance->get_name() == "TestScene");205206// Validate the child nodes of the instantiated scene.207CHECK(instance->get_child_count() == 2);208CHECK(instance->get_child(0)->get_name() == "Child1");209CHECK(instance->get_child(1)->get_name() == "Child2");210CHECK(instance->get_child(0)->get_owner() == instance);211CHECK(instance->get_child(1)->get_owner() == instance);212213memdelete(scene);214memdelete(instance);215}216217TEST_CASE("[PackedScene] Set Path") {218// Create a scene to pack.219Node *scene = memnew(Node);220scene->set_name("TestScene");221222// Pack the scene.223PackedScene packed_scene;224packed_scene.pack(scene);225226// Set a new path for the packed scene.227const String new_path = "NewTestPath";228packed_scene.set_path(new_path);229230// Check if the path has been set correctly.231Ref<SceneState> state = packed_scene.get_state();232CHECK(state.is_valid());233CHECK(state->get_path() == new_path);234235memdelete(scene);236}237238TEST_CASE("[PackedScene] Replace State") {239// Create a scene to pack.240Node *scene = memnew(Node);241scene->set_name("TestScene");242243// Pack the scene.244PackedScene packed_scene;245packed_scene.pack(scene);246247// Create another scene state to replace with.248Ref<SceneState> new_state = memnew(SceneState);249new_state->set_path("NewPath");250251// Replace the state.252packed_scene.replace_state(new_state);253254// Check if the state has been replaced.255Ref<SceneState> state = packed_scene.get_state();256CHECK(state.is_valid());257CHECK(state == new_state);258259memdelete(scene);260}261262TEST_CASE("[PackedScene] Recreate State") {263// Create a scene to pack.264Node *scene = memnew(Node);265scene->set_name("TestScene");266267// Pack the scene.268PackedScene packed_scene;269packed_scene.pack(scene);270271// Recreate the state.272packed_scene.recreate_state();273274// Check if the state has been recreated.275Ref<SceneState> state = packed_scene.get_state();276CHECK(state.is_valid());277CHECK(state->get_node_count() == 0); // Since the state was recreated, it should be empty.278279memdelete(scene);280}281282} // namespace TestPackedScene283284285