#pragma once
#include "scene/resources/sprite_frames.h"
#include "tests/test_macros.h"
namespace TestSpriteFrames {
const String test_animation_name = "GodotTest";
TEST_CASE("[SpriteFrames] Constructor methods") {
const SpriteFrames frames;
CHECK_MESSAGE(
frames.get_animation_names().size() == 1,
"Should be initialized with 1 entry.");
CHECK_MESSAGE(
frames.get_animation_names().get(0) == "default",
"Should be initialized with default entry.");
}
TEST_CASE("[SpriteFrames] Animation addition, list getter, renaming, removal, and retrieval") {
SpriteFrames frames;
Vector<String> test_names = { "default", "2", "1", "3" };
frames.add_animation("2");
frames.add_animation("1");
frames.add_animation("3");
for (int i = 0; i < test_names.size(); i++) {
CHECK_MESSAGE(
frames.has_animation(test_names[i]),
"Add animation properly worked for each value");
}
CHECK_MESSAGE(
!frames.has_animation("999"),
"Return false when checking for animation that does not exist");
List<StringName> sname_list;
frames.get_animation_list(&sname_list);
CHECK_MESSAGE(
sname_list.size() == test_names.size(),
"StringName List getter returned list of expected size");
int idx = 0;
for (List<StringName>::ConstIterator itr = sname_list.begin(); itr != sname_list.end(); ++itr, ++idx) {
CHECK_MESSAGE(
*itr == StringName(test_names[idx]),
"StringName List getter returned expected values");
}
Vector<String> string_vector = frames.get_animation_names();
test_names.sort();
for (int i = 0; i < test_names.size(); i++) {
CHECK_MESSAGE(
string_vector[i] == test_names[i],
"String Vector getter returned expected values");
}
ERR_PRINT_OFF;
frames.rename_animation("This does not exist", "0");
ERR_PRINT_ON;
CHECK_MESSAGE(
!frames.has_animation("0"),
"Correctly handles rename error when entry does not exist");
ERR_PRINT_OFF;
frames.rename_animation("3", "1");
ERR_PRINT_ON;
CHECK_MESSAGE(
frames.has_animation("3"),
"Correctly handles rename error when entry exists, but new name already exists");
ERR_PRINT_OFF;
frames.add_animation("1");
ERR_PRINT_ON;
CHECK_MESSAGE(
frames.get_animation_names().size() == 4,
"Correctly does not add when entry already exists");
frames.rename_animation("3", "9");
CHECK_MESSAGE(
frames.has_animation("9"),
"Animation renamed correctly");
frames.remove_animation("9");
CHECK_MESSAGE(
!frames.has_animation("9"),
"Animation removed correctly");
frames.clear_all();
CHECK_MESSAGE(
frames.get_animation_names().size() == 1,
"Clear all removed all animations and re-added the default animation entry");
}
TEST_CASE("[SpriteFrames] Animation Speed getter and setter") {
SpriteFrames frames;
frames.add_animation(test_animation_name);
CHECK_MESSAGE(
frames.get_animation_speed(test_animation_name) == 5.0,
"Sets new animation to default speed");
frames.set_animation_speed(test_animation_name, 123.0004);
CHECK_MESSAGE(
frames.get_animation_speed(test_animation_name) == 123.0004,
"Sets animation to positive double");
ERR_PRINT_OFF;
frames.get_animation_speed("This does not exist");
frames.set_animation_speed("This does not exist", 100);
frames.set_animation_speed(test_animation_name, -999.999);
ERR_PRINT_ON;
CHECK_MESSAGE(
frames.get_animation_speed(test_animation_name) == 123.0004,
"Prevents speed of animation being set to a negative value");
frames.set_animation_speed(test_animation_name, 0.0);
CHECK_MESSAGE(
frames.get_animation_speed(test_animation_name) == 0.0,
"Sets animation to zero");
}
TEST_CASE("[SpriteFrames] Animation Loop getter and setter") {
SpriteFrames frames;
frames.add_animation(test_animation_name);
CHECK_MESSAGE(
frames.get_animation_loop(test_animation_name),
"Sets new animation to default loop value.");
frames.set_animation_loop(test_animation_name, true);
CHECK_MESSAGE(
frames.get_animation_loop(test_animation_name),
"Sets animation loop to true");
frames.set_animation_loop(test_animation_name, false);
CHECK_MESSAGE(
!frames.get_animation_loop(test_animation_name),
"Sets animation loop to false");
ERR_PRINT_OFF;
frames.get_animation_loop("This does not exist");
frames.set_animation_loop("This does not exist", false);
ERR_PRINT_ON;
}
TEST_CASE("[SpriteFrames] Frame addition, removal, and retrieval") {
Ref<Texture2D> dummy_frame1;
dummy_frame1.instantiate();
SpriteFrames frames;
frames.add_animation(test_animation_name);
frames.add_animation("1");
frames.add_animation("2");
CHECK_MESSAGE(
frames.get_frame_count(test_animation_name) == 0,
"Animation has a default frame count of 0");
frames.add_frame(test_animation_name, dummy_frame1, 1.0, 0);
frames.add_frame(test_animation_name, dummy_frame1, 1.0, 1);
frames.add_frame(test_animation_name, dummy_frame1, 1.0, 2);
CHECK_MESSAGE(
frames.get_frame_count(test_animation_name) == 3,
"Adds multiple frames");
frames.remove_frame(test_animation_name, 1);
frames.remove_frame(test_animation_name, 0);
CHECK_MESSAGE(
frames.get_frame_count(test_animation_name) == 1,
"Removes multiple frames");
ERR_PRINT_OFF;
frames.add_frame("does not exist", dummy_frame1, 1.0, 0);
frames.remove_frame(test_animation_name, -99);
frames.remove_frame("does not exist", 0);
ERR_PRINT_ON;
CHECK_MESSAGE(
frames.get_frame_count(test_animation_name) == 1,
"Handles bad values when adding or removing frames.");
frames.clear(test_animation_name);
CHECK_MESSAGE(
frames.get_frame_count(test_animation_name) == 0,
"Clears frames.");
}
}