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