10 #include <gtest/gtest.h>
11 #include <assets_manager.hpp>
12 #include <ST_util/test_util.hpp>
20 return test_mngr->all_assets;
23 int8_t load_asset(
const std::string &path) {
24 return test_mngr->load_asset(path);
27 int8_t unload_asset(
const std::string &path) {
28 return test_mngr->unload_asset(path);
31 int8_t load_assets_from_binary(
const std::string &path) {
32 return test_mngr->load_assets_from_binary(path);
35 int8_t load_assets_from_list(
const std::string &path) {
36 return test_mngr->load_assets_from_list(path);
39 int8_t unload_assets_from_list(
const std::string &path) {
40 return test_mngr->unload_assets_from_list(path);
43 uint16_t get_count(
const std::string &asset_name) {
44 return test_mngr->count[asset_name];
49 static void SetUpTestCase() {
53 static void TearDownTestCase() {
60 void SetUp()
override {
66 void TearDown()
override {
74 load_asset(
"nothing.png");
75 ASSERT_FALSE(get_assets().surfaces[ST::hash_string(
"nothing.png")]);
79 load_asset(
"test_image_1.png");
80 SDL_Surface *test_surface = IMG_Load(
"test_image_1.png");
81 ASSERT_TRUE(test_surface);
82 ASSERT_TRUE(compare_surfaces(test_surface, get_assets().surfaces[ST::hash_string(
"test_image_1.png")]));
83 SDL_FreeSurface(test_surface);
87 load_asset(
"nothing.webp");
88 ASSERT_FALSE(get_assets().surfaces[ST::hash_string(
"nothing.webp")]);
92 load_asset(
"test_image_3.webp");
93 SDL_Surface *test_surface = IMG_Load(
"test_image_3.webp");
94 ASSERT_TRUE(
static_cast<bool>(test_surface));
95 ASSERT_TRUE(compare_surfaces(test_surface, get_assets().surfaces[ST::hash_string(
"test_image_3.webp")]));
96 SDL_FreeSurface(test_surface);
100 load_asset(
"nothing.wav");
101 ASSERT_FALSE(get_assets().chunks[(ST::hash_string(
"nothing.wav"))]);
105 load_asset(
"test_sound.wav");
106 Mix_Chunk *expected_chunk = Mix_LoadWAV(
"test_sound.wav");
107 Mix_Chunk *result_chunk = get_assets().chunks[ST::hash_string(
"test_sound.wav")];
108 ASSERT_TRUE(expected_chunk);
109 ASSERT_TRUE(result_chunk);
110 ASSERT_EQ(expected_chunk->alen, result_chunk->alen);
113 for (Uint32 i = 0; i < expected_chunk->alen; i++) {
114 ASSERT_EQ(expected_chunk->abuf[i], result_chunk->abuf[i]);
116 Mix_FreeChunk(expected_chunk);
120 ASSERT_EQ(-1, load_asset(
"nothing.ogg"));
121 ASSERT_FALSE(get_assets().music[ST::hash_string(
"nothing.ogg")]);
125 ASSERT_EQ(0, load_asset(
"test_music.ogg"));
126 auto result_music = get_assets().music[ST::hash_string(
"test_music.ogg")];
127 if (result_music ==
nullptr) {
128 const char *s = Mix_GetError();
131 ASSERT_TRUE(result_music);
132 ASSERT_EQ(MUS_OGG, Mix_GetMusicType(result_music));
136 ASSERT_EQ(-1, load_asset(
"nothing.ttf 50"));
137 ASSERT_FALSE(get_assets().fonts[ST::hash_string(
"nothing.ttf 50")]);
141 ASSERT_EQ(-1, load_asset(
"test_font.ttf"));
142 ASSERT_FALSE(get_assets().fonts[ST::hash_string(
"test_font.ttf")]);
146 load_asset(
"test_font.ttf 50");
147 TTF_Font *expected_font = TTF_OpenFont(
"test_font.ttf", 50);
148 TTF_Font *result_font = get_assets().fonts[ST::hash_string(
"test_font.ttf 50")];
149 ASSERT_TRUE(expected_font);
150 ASSERT_TRUE(result_font);
151 SDL_Surface *expected_render = TTF_RenderText_Blended(result_font,
"TEST", {200, 200, 200, 255});
152 SDL_Surface *result_render = TTF_RenderText_Blended(result_font,
"TEST", {200, 200, 200, 255});
153 ASSERT_TRUE(compare_surfaces(expected_render, result_render));
154 TTF_CloseFont(expected_font);
155 SDL_FreeSurface(expected_render);
156 SDL_FreeSurface(result_render);
160 ASSERT_EQ(0, load_assets_from_binary(
"test_binary_png.bin"));
161 SDL_Surface *test_surface = IMG_Load(
"test_image_1.png");
162 ASSERT_TRUE(test_surface);
163 ASSERT_TRUE(compare_surfaces(test_surface, get_assets().surfaces[ST::hash_string(
"test_image.png")]));
164 SDL_FreeSurface(test_surface);
168 ASSERT_EQ(0, load_assets_from_binary(
"test_binary_webp.bin"));
169 SDL_Surface *test_surface = IMG_Load(
"test_image_3.webp");
170 ASSERT_TRUE(test_surface);
171 ASSERT_TRUE(compare_surfaces(test_surface, get_assets().surfaces[ST::hash_string(
"test_image_3.webp")]));
172 SDL_FreeSurface(test_surface);
176 ASSERT_EQ(0, load_assets_from_binary(
"test_binary_wav.bin"));
177 Mix_Chunk *expected_chunk = Mix_LoadWAV(
"test_sound.wav");
178 Mix_Chunk *result_chunk = get_assets().chunks[ST::hash_string(
"test_sound.wav")];
179 ASSERT_TRUE(expected_chunk);
180 ASSERT_TRUE(result_chunk);
181 ASSERT_EQ(expected_chunk->alen, result_chunk->alen);
182 for (Uint32 i = 0; i < expected_chunk->alen; i++) {
183 ASSERT_EQ(expected_chunk->abuf[i], result_chunk->abuf[i]);
185 Mix_FreeChunk(expected_chunk);
189 ASSERT_EQ(0, load_assets_from_binary(
"test_binary_ogg.bin"));
190 auto result_music = get_assets().music[ST::hash_string(
"test_music.ogg")];
191 ASSERT_TRUE(result_music);
192 ASSERT_EQ(MUS_OGG, Mix_GetMusicType(result_music));
196 ASSERT_EQ(0, load_assets_from_binary(
"test_binary_complex.bin"));
199 auto result_music_1 = get_assets().music[ST::hash_string(
"test_music_1.ogg")];
200 ASSERT_TRUE(result_music_1);
201 ASSERT_EQ(MUS_OGG, Mix_GetMusicType(result_music_1));
204 auto result_music_2 = get_assets().music[ST::hash_string(
"test_music_2.ogg")];
205 ASSERT_TRUE(result_music_2);
206 ASSERT_EQ(MUS_OGG, Mix_GetMusicType(result_music_2));
209 Mix_Chunk *expected_chunk_1 = Mix_LoadWAV(
"test_sound_1.wav");
210 Mix_Chunk *result_chunk_1 =
static_cast<Mix_Chunk *
>(get_assets().chunks[ST::hash_string(
"test_sound_1.wav")]);
211 ASSERT_TRUE(expected_chunk_1);
212 ASSERT_TRUE(result_chunk_1);
213 ASSERT_EQ(expected_chunk_1->alen, result_chunk_1->alen);
214 for (Uint32 i = 0; i < expected_chunk_1->alen; i++) {
215 ASSERT_EQ(expected_chunk_1->abuf[i], result_chunk_1->abuf[i]);
217 Mix_FreeChunk(expected_chunk_1);
220 Mix_Chunk *expected_chunk_2 = Mix_LoadWAV(
"test_sound_2.wav");
221 Mix_Chunk *result_chunk_2 =
static_cast<Mix_Chunk *
>(get_assets().chunks[ST::hash_string(
"test_sound_2.wav")]);
222 ASSERT_TRUE(expected_chunk_2);
223 ASSERT_TRUE(result_chunk_2);
224 ASSERT_EQ(expected_chunk_2->alen, result_chunk_2->alen);
225 for (Uint32 i = 0; i < expected_chunk_2->alen; i++) {
226 ASSERT_EQ(expected_chunk_2->abuf[i], result_chunk_2->abuf[i]);
228 Mix_FreeChunk(expected_chunk_2);
231 SDL_Surface *test_surface_1 = IMG_Load(
"test_image_1.png");
232 ASSERT_TRUE(test_surface_1);
233 ASSERT_TRUE(compare_surfaces(test_surface_1, get_assets().surfaces[ST::hash_string(
"test_image_1.png")]));
234 SDL_FreeSurface(test_surface_1);
237 SDL_Surface *test_surface_2 = IMG_Load(
"test_image_2.png");
238 ASSERT_TRUE(test_surface_2);
239 ASSERT_TRUE(compare_surfaces(test_surface_2, get_assets().surfaces[ST::hash_string(
"test_image_2.png")]));
240 SDL_FreeSurface(test_surface_2);
243 SDL_Surface *test_surface_3 = IMG_Load(
"test_image_3.webp");
244 ASSERT_TRUE(test_surface_3);
245 ASSERT_TRUE(compare_surfaces(test_surface_3, get_assets().surfaces[ST::hash_string(
"test_image_3.webp")]));
246 SDL_FreeSurface(test_surface_3);
251 ASSERT_EQ(0, load_assets_from_list(
"test_list_1.list"));
254 SDL_Surface *test_surface_1 = IMG_Load(
"test_image_1.png");
255 ASSERT_TRUE(test_surface_1);
256 ASSERT_TRUE(compare_surfaces(test_surface_1, get_assets().surfaces[ST::hash_string(
"test_image_1.png")]));
257 SDL_FreeSurface(test_surface_1);
260 SDL_Surface *test_surface_2 = IMG_Load(
"test_sprite.png");
261 ASSERT_TRUE(test_surface_2);
262 ASSERT_TRUE(compare_surfaces(test_surface_2, get_assets().surfaces[ST::hash_string(
"test_sprite.png")]));
263 SDL_FreeSurface(test_surface_2);
266 SDL_Surface *test_surface_3 = IMG_Load(
"test_image_3.webp");
267 ASSERT_TRUE(test_surface_3);
268 ASSERT_TRUE(compare_surfaces(test_surface_3, get_assets().surfaces[ST::hash_string(
"test_image_3.webp")]));
269 SDL_FreeSurface(test_surface_3);
272 Mix_Chunk *expected_chunk_1 = Mix_LoadWAV(
"test_sound.wav");
273 Mix_Chunk *result_chunk_1 =
static_cast<Mix_Chunk *
>(get_assets().chunks[ST::hash_string(
"test_sound.wav")]);
274 ASSERT_TRUE(expected_chunk_1);
275 ASSERT_TRUE(result_chunk_1);
276 ASSERT_EQ(expected_chunk_1->alen, result_chunk_1->alen);
277 for (Uint32 i = 0; i < expected_chunk_1->alen; i++) {
278 ASSERT_EQ(expected_chunk_1->abuf[i], result_chunk_1->abuf[i]);
280 Mix_FreeChunk(expected_chunk_1);
283 Mix_Chunk *expected_chunk_2 = Mix_LoadWAV(
"test_sound_2.wav");
284 Mix_Chunk *result_chunk_2 =
static_cast<Mix_Chunk *
>(get_assets().chunks[ST::hash_string(
"test_sound_2.wav")]);
285 ASSERT_TRUE(expected_chunk_2);
286 ASSERT_TRUE(result_chunk_2);
287 ASSERT_EQ(expected_chunk_2->alen, result_chunk_2->alen);
288 for (Uint32 i = 0; i < expected_chunk_2->alen; i++) {
289 ASSERT_EQ(expected_chunk_2->abuf[i], result_chunk_2->abuf[i]);
291 Mix_FreeChunk(expected_chunk_2);
295 ASSERT_EQ(-1, load_assets_from_list(
"no_list.list"));
301 ASSERT_EQ(0, load_asset(
"test_image_1.png"));
302 ASSERT_EQ(0, load_asset(
"test_image_1.png"));
304 ASSERT_EQ(2, get_count(
"test_image_1.png"));
306 SDL_Surface *test_surface = IMG_Load(
"test_image_1.png");
308 ASSERT_TRUE(
static_cast<bool>(test_surface));
309 ASSERT_TRUE(compare_surfaces(test_surface, get_assets().surfaces[ST::hash_string(
"test_image_1.png")]));
311 SDL_FreeSurface(test_surface);
316 ASSERT_EQ(0, load_asset(
"test_image_1.png"));
317 ASSERT_EQ(1, get_count(
"test_image_1.png"));
318 ASSERT_EQ(0, unload_asset(
"test_image_1.png"));
319 ASSERT_EQ(0, get_count(
"test_image_1.png"));
320 ASSERT_FALSE(get_assets().surfaces[ST::hash_string(
"test_image_1.png")]);
324 ASSERT_EQ(0, load_asset(
"test_image_1.png"));
325 ASSERT_EQ(0, load_asset(
"test_image_1.png"));
326 ASSERT_EQ(2, get_count(
"test_image_1.png"));
327 ASSERT_EQ(0, unload_asset(
"test_image_1.png"));
328 ASSERT_EQ(1, get_count(
"test_image_1.png"));
329 ASSERT_TRUE(get_assets().surfaces[ST::hash_string(
"test_image_1.png")]);
333 ASSERT_EQ(0, load_assets_from_list(
"test_list_1.list"));
335 ASSERT_EQ(1, get_count(
"test_image_1.png"));
336 ASSERT_EQ(1, get_count(
"test_sound.wav"));
337 ASSERT_EQ(1, get_count(
"test_sprite.png"));
338 ASSERT_EQ(1, get_count(
"test_sound_2.wav"));
340 ASSERT_EQ(0, unload_assets_from_list(
"test_list_1.list"));
342 ASSERT_EQ(0, get_count(
"test_image_1.png"));
343 ASSERT_EQ(0, get_count(
"test_sound.wav"));
344 ASSERT_EQ(0, get_count(
"test_sprite.png"));
345 ASSERT_EQ(0, get_count(
"test_sound_2.wav"));
348 int main(
int argc,
char **argv) {
349 ::testing::InitGoogleTest(&argc, argv);
350 return RUN_ALL_TESTS();
Tests fixture for the assets_manager.
This object is responsible for loading/unloading assets.
The central messaging system of the engine. All subsystem make extensive use of it.
The Task Manager handles all things multi-threaded in the engine.
A container that holds all asset types used in the game.