ST_engine  0.3-ALPHA
assets_manager_tests.cpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #include <gtest/gtest.h>
11 #include <assets_manager.hpp>
12 #include <ST_util/test_util.hpp>
13 
15 class asset_manager_test : public ::testing::Test {
16 
17 protected:
18 
19  ST::assets get_assets() {
20  return test_mngr->all_assets;
21  }
22 
23  int8_t load_asset(const std::string &path) {
24  return test_mngr->load_asset(path);
25  }
26 
27  int8_t unload_asset(const std::string &path) {
28  return test_mngr->unload_asset(path);
29  }
30 
31  int8_t load_assets_from_binary(const std::string &path) {
32  return test_mngr->load_assets_from_binary(path);
33  }
34 
35  int8_t load_assets_from_list(const std::string &path) {
36  return test_mngr->load_assets_from_list(path);
37  }
38 
39  int8_t unload_assets_from_list(const std::string &path) {
40  return test_mngr->unload_assets_from_list(path);
41  }
42 
43  uint16_t get_count(const std::string &asset_name) {
44  return test_mngr->count[asset_name];
45  };
46 
47  assets_manager *test_mngr{};
48 
49  static void SetUpTestCase() {
50  initialize_SDL();
51  }
52 
53  static void TearDownTestCase() {
54  close_SDL();
55  }
56 
57  message_bus *msg_bus{};
58  task_manager *task_mngr{};
59 
60  void SetUp() override {
61  msg_bus = new message_bus();
62  task_mngr = new task_manager(0);
63  test_mngr = new assets_manager(*msg_bus, *task_mngr);
64  }
65 
66  void TearDown() override {
67  delete test_mngr;
68  delete msg_bus;
69  delete task_mngr;
70  }
71 };
72 
73 TEST_F(asset_manager_test, loadPNG_nonExistant) {
74  load_asset("nothing.png");
75  ASSERT_FALSE(get_assets().surfaces[ST::hash_string("nothing.png")]);
76 }
77 
78 TEST_F(asset_manager_test, loadPNG) {
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);
84 }
85 
86 TEST_F(asset_manager_test, loadWEBP_nonExistant) {
87  load_asset("nothing.webp");
88  ASSERT_FALSE(get_assets().surfaces[ST::hash_string("nothing.webp")]);
89 }
90 
91 TEST_F(asset_manager_test, loadWEBP) {
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);
97 }
98 
99 TEST_F(asset_manager_test, loadWAV_nonExistant) {
100  load_asset("nothing.wav");
101  ASSERT_FALSE(get_assets().chunks[(ST::hash_string("nothing.wav"))]);
102 }
103 
104 TEST_F(asset_manager_test, loadWAV) {
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);
111 
112 
113  for (Uint32 i = 0; i < expected_chunk->alen; i++) {
114  ASSERT_EQ(expected_chunk->abuf[i], result_chunk->abuf[i]);
115  }
116  Mix_FreeChunk(expected_chunk);
117 }
118 
119 TEST_F(asset_manager_test, loadOGG_nonExistant) {
120  ASSERT_EQ(-1, load_asset("nothing.ogg"));
121  ASSERT_FALSE(get_assets().music[ST::hash_string("nothing.ogg")]);
122 }
123 
124 TEST_F(asset_manager_test, loadOGG) {
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();
129  printf("%s\n", s);
130  }
131  ASSERT_TRUE(result_music);
132  ASSERT_EQ(MUS_OGG, Mix_GetMusicType(result_music));
133 }
134 
135 TEST_F(asset_manager_test, loadTTF_nonExistant) {
136  ASSERT_EQ(-1, load_asset("nothing.ttf 50"));
137  ASSERT_FALSE(get_assets().fonts[ST::hash_string("nothing.ttf 50")]);
138 }
139 
140 TEST_F(asset_manager_test, loadTTF_noSize) {
141  ASSERT_EQ(-1, load_asset("test_font.ttf"));
142  ASSERT_FALSE(get_assets().fonts[ST::hash_string("test_font.ttf")]);
143 }
144 
145 TEST_F(asset_manager_test, loadTTF) {
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);
157 }
158 
159 TEST_F(asset_manager_test, loadBinary_PNG) {
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);
165 }
166 
167 TEST_F(asset_manager_test, loadBinary_WEBP) {
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);
173 }
174 
175 TEST_F(asset_manager_test, loadBinary_WAV) {
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]);
184  }
185  Mix_FreeChunk(expected_chunk);
186 }
187 
188 TEST_F(asset_manager_test, loadBinary_OGG) {
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));
193 }
194 
195 TEST_F(asset_manager_test, loadBinary_complex) {
196  ASSERT_EQ(0, load_assets_from_binary("test_binary_complex.bin"));
197 
198  //Test music_1
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));
202 
203  //Test music_2
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));
207 
208  //Test sound_1
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]);
216  }
217  Mix_FreeChunk(expected_chunk_1);
218 
219  //Test sound_2
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]);
227  }
228  Mix_FreeChunk(expected_chunk_2);
229 
230  //Test image_1
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);
235 
236  //Test image_2
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);
241 
242  //Test image_3
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);
247 }
248 
249 TEST_F(asset_manager_test, test_load_assets_from_list) {
250 
251  ASSERT_EQ(0, load_assets_from_list("test_list_1.list"));
252 
253  //Test image_1
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);
258 
259  //Test image_2
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);
264 
265  //Test image_3
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);
270 
271  //Test sound_1
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]);
279  }
280  Mix_FreeChunk(expected_chunk_1);
281 
282  //Test sound_2
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]);
290  }
291  Mix_FreeChunk(expected_chunk_2);
292 }
293 
294 TEST_F(asset_manager_test, test_fail_when_list_does_not_exist) {
295  ASSERT_EQ(-1, load_assets_from_list("no_list.list"));
296 }
297 
298 
299 TEST_F(asset_manager_test, test_load_asset_twice) {
300 
301  ASSERT_EQ(0, load_asset("test_image_1.png"));
302  ASSERT_EQ(0, load_asset("test_image_1.png"));
303 
304  ASSERT_EQ(2, get_count("test_image_1.png"));
305 
306  SDL_Surface *test_surface = IMG_Load("test_image_1.png");
307 
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")]));
310 
311  SDL_FreeSurface(test_surface);
312 }
313 
314 
315 TEST_F(asset_manager_test, test_load_and_unload_asset) {
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")]);
321 }
322 
323 TEST_F(asset_manager_test, test_load_twice_and_unload_asset) {
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")]);
330 }
331 
332 TEST_F(asset_manager_test, test_load_and_unload_assets_from_list) {
333  ASSERT_EQ(0, load_assets_from_list("test_list_1.list"));
334 
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"));
339 
340  ASSERT_EQ(0, unload_assets_from_list("test_list_1.list"));
341 
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"));
346 }
347 
348 int main(int argc, char **argv) {
349  ::testing::InitGoogleTest(&argc, argv);
350  return RUN_ALL_TESTS();
351 }
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.
Definition: message_bus.hpp:29
The Task Manager handles all things multi-threaded in the engine.
A container that holds all asset types used in the game.
Definition: assets.hpp:24