Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_sprite_frames.cpp
45991 views
1
/**************************************************************************/
2
/* test_sprite_frames.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_sprite_frames)
34
35
#include "scene/resources/sprite_frames.h"
36
37
namespace TestSpriteFrames {
38
39
const String test_animation_name = "GodotTest";
40
41
TEST_CASE("[SpriteFrames] Constructor methods") {
42
const SpriteFrames frames;
43
CHECK_MESSAGE(
44
frames.get_animation_names().size() == 1,
45
"Should be initialized with 1 entry.");
46
CHECK_MESSAGE(
47
frames.get_animation_names().get(0) == "default",
48
"Should be initialized with default entry.");
49
}
50
51
TEST_CASE("[SpriteFrames] Animation addition, list getter, renaming, removal, and retrieval") {
52
SpriteFrames frames;
53
Vector<String> test_names = { "default", "2", "1", "3" };
54
55
// "default" is there already
56
frames.add_animation("2");
57
frames.add_animation("1");
58
frames.add_animation("3");
59
60
for (int i = 0; i < test_names.size(); i++) {
61
CHECK_MESSAGE(
62
frames.has_animation(test_names[i]),
63
"Add animation properly worked for each value");
64
}
65
66
CHECK_MESSAGE(
67
!frames.has_animation("999"),
68
"Return false when checking for animation that does not exist");
69
70
List<StringName> sname_list;
71
frames.get_animation_list(&sname_list);
72
73
CHECK_MESSAGE(
74
sname_list.size() == test_names.size(),
75
"StringName List getter returned list of expected size");
76
77
int idx = 0;
78
for (List<StringName>::ConstIterator itr = sname_list.begin(); itr != sname_list.end(); ++itr, ++idx) {
79
CHECK_MESSAGE(
80
*itr == StringName(test_names[idx]),
81
"StringName List getter returned expected values");
82
}
83
84
// get_animation_names() sorts the results.
85
Vector<String> string_vector = frames.get_animation_names();
86
test_names.sort();
87
88
for (int i = 0; i < test_names.size(); i++) {
89
CHECK_MESSAGE(
90
string_vector[i] == test_names[i],
91
"String Vector getter returned expected values");
92
}
93
94
// These error handling cases should not crash.
95
ERR_PRINT_OFF;
96
frames.rename_animation("This does not exist", "0");
97
ERR_PRINT_ON;
98
99
CHECK_MESSAGE(
100
!frames.has_animation("0"),
101
"Correctly handles rename error when entry does not exist");
102
103
// These error handling cases should not crash.
104
ERR_PRINT_OFF;
105
frames.rename_animation("3", "1");
106
ERR_PRINT_ON;
107
108
CHECK_MESSAGE(
109
frames.has_animation("3"),
110
"Correctly handles rename error when entry exists, but new name already exists");
111
112
ERR_PRINT_OFF;
113
frames.add_animation("1");
114
ERR_PRINT_ON;
115
116
CHECK_MESSAGE(
117
frames.get_animation_names().size() == 4,
118
"Correctly does not add when entry already exists");
119
120
frames.rename_animation("3", "9");
121
122
CHECK_MESSAGE(
123
frames.has_animation("9"),
124
"Animation renamed correctly");
125
126
frames.remove_animation("9");
127
128
CHECK_MESSAGE(
129
!frames.has_animation("9"),
130
"Animation removed correctly");
131
132
frames.clear_all();
133
134
CHECK_MESSAGE(
135
frames.get_animation_names().size() == 1,
136
"Clear all removed all animations and re-added the default animation entry");
137
}
138
139
TEST_CASE("[SpriteFrames] Animation Speed getter and setter") {
140
SpriteFrames frames;
141
142
frames.add_animation(test_animation_name);
143
144
CHECK_MESSAGE(
145
frames.get_animation_speed(test_animation_name) == 5.0,
146
"Sets new animation to default speed");
147
148
frames.set_animation_speed(test_animation_name, 123.0004);
149
150
CHECK_MESSAGE(
151
frames.get_animation_speed(test_animation_name) == 123.0004,
152
"Sets animation to positive double");
153
154
// These error handling cases should not crash.
155
ERR_PRINT_OFF;
156
frames.get_animation_speed("This does not exist");
157
frames.set_animation_speed("This does not exist", 100);
158
frames.set_animation_speed(test_animation_name, -999.999);
159
ERR_PRINT_ON;
160
161
CHECK_MESSAGE(
162
frames.get_animation_speed(test_animation_name) == 123.0004,
163
"Prevents speed of animation being set to a negative value");
164
165
frames.set_animation_speed(test_animation_name, 0.0);
166
167
CHECK_MESSAGE(
168
frames.get_animation_speed(test_animation_name) == 0.0,
169
"Sets animation to zero");
170
}
171
172
TEST_CASE("[SpriteFrames] Animation Loop getter and setter") {
173
SpriteFrames frames;
174
175
frames.add_animation(test_animation_name);
176
177
CHECK_MESSAGE(
178
frames.get_animation_loop(test_animation_name),
179
"Sets new animation to default loop value.");
180
181
frames.set_animation_loop(test_animation_name, true);
182
183
CHECK_MESSAGE(
184
frames.get_animation_loop(test_animation_name),
185
"Sets animation loop to true");
186
187
frames.set_animation_loop(test_animation_name, false);
188
189
CHECK_MESSAGE(
190
!frames.get_animation_loop(test_animation_name),
191
"Sets animation loop to false");
192
193
// These error handling cases should not crash.
194
ERR_PRINT_OFF;
195
frames.get_animation_loop("This does not exist");
196
frames.set_animation_loop("This does not exist", false);
197
ERR_PRINT_ON;
198
}
199
200
// TODO
201
TEST_CASE("[SpriteFrames] Frame addition, removal, and retrieval") {
202
Ref<Texture2D> dummy_frame1;
203
dummy_frame1.instantiate();
204
205
SpriteFrames frames;
206
frames.add_animation(test_animation_name);
207
frames.add_animation("1");
208
frames.add_animation("2");
209
210
CHECK_MESSAGE(
211
frames.get_frame_count(test_animation_name) == 0,
212
"Animation has a default frame count of 0");
213
214
frames.add_frame(test_animation_name, dummy_frame1, 1.0, 0);
215
frames.add_frame(test_animation_name, dummy_frame1, 1.0, 1);
216
frames.add_frame(test_animation_name, dummy_frame1, 1.0, 2);
217
218
CHECK_MESSAGE(
219
frames.get_frame_count(test_animation_name) == 3,
220
"Adds multiple frames");
221
222
frames.remove_frame(test_animation_name, 1);
223
frames.remove_frame(test_animation_name, 0);
224
225
CHECK_MESSAGE(
226
frames.get_frame_count(test_animation_name) == 1,
227
"Removes multiple frames");
228
229
// These error handling cases should not crash.
230
ERR_PRINT_OFF;
231
frames.add_frame("does not exist", dummy_frame1, 1.0, 0);
232
frames.remove_frame(test_animation_name, -99);
233
frames.remove_frame("does not exist", 0);
234
ERR_PRINT_ON;
235
236
CHECK_MESSAGE(
237
frames.get_frame_count(test_animation_name) == 1,
238
"Handles bad values when adding or removing frames.");
239
240
frames.clear(test_animation_name);
241
242
CHECK_MESSAGE(
243
frames.get_frame_count(test_animation_name) == 0,
244
"Clears frames.");
245
}
246
247
} // namespace TestSpriteFrames
248
249