| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |