test_sprite_frames.h 8.1 KB

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