Path: blob/master/tests/scene/test_sprite_frames.cpp
45991 views
/**************************************************************************/1/* test_sprite_frames.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_sprite_frames)3334#include "scene/resources/sprite_frames.h"3536namespace TestSpriteFrames {3738const String test_animation_name = "GodotTest";3940TEST_CASE("[SpriteFrames] Constructor methods") {41const SpriteFrames frames;42CHECK_MESSAGE(43frames.get_animation_names().size() == 1,44"Should be initialized with 1 entry.");45CHECK_MESSAGE(46frames.get_animation_names().get(0) == "default",47"Should be initialized with default entry.");48}4950TEST_CASE("[SpriteFrames] Animation addition, list getter, renaming, removal, and retrieval") {51SpriteFrames frames;52Vector<String> test_names = { "default", "2", "1", "3" };5354// "default" is there already55frames.add_animation("2");56frames.add_animation("1");57frames.add_animation("3");5859for (int i = 0; i < test_names.size(); i++) {60CHECK_MESSAGE(61frames.has_animation(test_names[i]),62"Add animation properly worked for each value");63}6465CHECK_MESSAGE(66!frames.has_animation("999"),67"Return false when checking for animation that does not exist");6869List<StringName> sname_list;70frames.get_animation_list(&sname_list);7172CHECK_MESSAGE(73sname_list.size() == test_names.size(),74"StringName List getter returned list of expected size");7576int idx = 0;77for (List<StringName>::ConstIterator itr = sname_list.begin(); itr != sname_list.end(); ++itr, ++idx) {78CHECK_MESSAGE(79*itr == StringName(test_names[idx]),80"StringName List getter returned expected values");81}8283// get_animation_names() sorts the results.84Vector<String> string_vector = frames.get_animation_names();85test_names.sort();8687for (int i = 0; i < test_names.size(); i++) {88CHECK_MESSAGE(89string_vector[i] == test_names[i],90"String Vector getter returned expected values");91}9293// These error handling cases should not crash.94ERR_PRINT_OFF;95frames.rename_animation("This does not exist", "0");96ERR_PRINT_ON;9798CHECK_MESSAGE(99!frames.has_animation("0"),100"Correctly handles rename error when entry does not exist");101102// These error handling cases should not crash.103ERR_PRINT_OFF;104frames.rename_animation("3", "1");105ERR_PRINT_ON;106107CHECK_MESSAGE(108frames.has_animation("3"),109"Correctly handles rename error when entry exists, but new name already exists");110111ERR_PRINT_OFF;112frames.add_animation("1");113ERR_PRINT_ON;114115CHECK_MESSAGE(116frames.get_animation_names().size() == 4,117"Correctly does not add when entry already exists");118119frames.rename_animation("3", "9");120121CHECK_MESSAGE(122frames.has_animation("9"),123"Animation renamed correctly");124125frames.remove_animation("9");126127CHECK_MESSAGE(128!frames.has_animation("9"),129"Animation removed correctly");130131frames.clear_all();132133CHECK_MESSAGE(134frames.get_animation_names().size() == 1,135"Clear all removed all animations and re-added the default animation entry");136}137138TEST_CASE("[SpriteFrames] Animation Speed getter and setter") {139SpriteFrames frames;140141frames.add_animation(test_animation_name);142143CHECK_MESSAGE(144frames.get_animation_speed(test_animation_name) == 5.0,145"Sets new animation to default speed");146147frames.set_animation_speed(test_animation_name, 123.0004);148149CHECK_MESSAGE(150frames.get_animation_speed(test_animation_name) == 123.0004,151"Sets animation to positive double");152153// These error handling cases should not crash.154ERR_PRINT_OFF;155frames.get_animation_speed("This does not exist");156frames.set_animation_speed("This does not exist", 100);157frames.set_animation_speed(test_animation_name, -999.999);158ERR_PRINT_ON;159160CHECK_MESSAGE(161frames.get_animation_speed(test_animation_name) == 123.0004,162"Prevents speed of animation being set to a negative value");163164frames.set_animation_speed(test_animation_name, 0.0);165166CHECK_MESSAGE(167frames.get_animation_speed(test_animation_name) == 0.0,168"Sets animation to zero");169}170171TEST_CASE("[SpriteFrames] Animation Loop getter and setter") {172SpriteFrames frames;173174frames.add_animation(test_animation_name);175176CHECK_MESSAGE(177frames.get_animation_loop(test_animation_name),178"Sets new animation to default loop value.");179180frames.set_animation_loop(test_animation_name, true);181182CHECK_MESSAGE(183frames.get_animation_loop(test_animation_name),184"Sets animation loop to true");185186frames.set_animation_loop(test_animation_name, false);187188CHECK_MESSAGE(189!frames.get_animation_loop(test_animation_name),190"Sets animation loop to false");191192// These error handling cases should not crash.193ERR_PRINT_OFF;194frames.get_animation_loop("This does not exist");195frames.set_animation_loop("This does not exist", false);196ERR_PRINT_ON;197}198199// TODO200TEST_CASE("[SpriteFrames] Frame addition, removal, and retrieval") {201Ref<Texture2D> dummy_frame1;202dummy_frame1.instantiate();203204SpriteFrames frames;205frames.add_animation(test_animation_name);206frames.add_animation("1");207frames.add_animation("2");208209CHECK_MESSAGE(210frames.get_frame_count(test_animation_name) == 0,211"Animation has a default frame count of 0");212213frames.add_frame(test_animation_name, dummy_frame1, 1.0, 0);214frames.add_frame(test_animation_name, dummy_frame1, 1.0, 1);215frames.add_frame(test_animation_name, dummy_frame1, 1.0, 2);216217CHECK_MESSAGE(218frames.get_frame_count(test_animation_name) == 3,219"Adds multiple frames");220221frames.remove_frame(test_animation_name, 1);222frames.remove_frame(test_animation_name, 0);223224CHECK_MESSAGE(225frames.get_frame_count(test_animation_name) == 1,226"Removes multiple frames");227228// These error handling cases should not crash.229ERR_PRINT_OFF;230frames.add_frame("does not exist", dummy_frame1, 1.0, 0);231frames.remove_frame(test_animation_name, -99);232frames.remove_frame("does not exist", 0);233ERR_PRINT_ON;234235CHECK_MESSAGE(236frames.get_frame_count(test_animation_name) == 1,237"Handles bad values when adding or removing frames.");238239frames.clear(test_animation_name);240241CHECK_MESSAGE(242frames.get_frame_count(test_animation_name) == 0,243"Clears frames.");244}245246} // namespace TestSpriteFrames247248249