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