summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2013-06-10 00:51:35 +0300
committerAndrei Karas <akaras@inbox.ru>2013-06-26 23:35:59 +0300
commit5fe07db8992b924069c4c358e557e88d2493f85f (patch)
tree8d959a23a5f8dde22e4f208d1bbf5665ee5068f7
parent1cf2a378e64315d4ddf7e10f115e96d86365c3f8 (diff)
downloadmv-5fe07db8992b924069c4c358e557e88d2493f85f.tar.gz
mv-5fe07db8992b924069c4c358e557e88d2493f85f.tar.bz2
mv-5fe07db8992b924069c4c358e557e88d2493f85f.tar.xz
mv-5fe07db8992b924069c4c358e557e88d2493f85f.zip
add basic animation unit tests.
-rw-r--r--data/graphics/sprites/CMakeLists.txt1
-rw-r--r--data/graphics/sprites/Makefile.am3
-rw-r--r--src/Makefile.am1
-rw-r--r--src/animatedsprite.h17
-rw-r--r--src/animatedsprite_unittest.cc151
-rw-r--r--src/resources/animation.h13
6 files changed, 181 insertions, 5 deletions
diff --git a/data/graphics/sprites/CMakeLists.txt b/data/graphics/sprites/CMakeLists.txt
index d8b3c4ea9..b076b3e75 100644
--- a/data/graphics/sprites/CMakeLists.txt
+++ b/data/graphics/sprites/CMakeLists.txt
@@ -8,6 +8,7 @@ SET(FILES
manaplus_emotions.png
manaplus_emote.xml
manaplus_emotes.xml
+ test.xml
)
INSTALL(FILES ${FILES} DESTINATION ${DATA_DIR}/graphics/sprites)
diff --git a/data/graphics/sprites/Makefile.am b/data/graphics/sprites/Makefile.am
index eeb13f915..dc2d29e5e 100644
--- a/data/graphics/sprites/Makefile.am
+++ b/data/graphics/sprites/Makefile.am
@@ -9,7 +9,8 @@ sprites_DATA = \
error.xml \
manaplus_emotions.png\
manaplus_emote.xml \
- manaplus_emotes.xml
+ manaplus_emotes.xml \
+ test.xml
EXTRA_DIST = \
$(sprites_DATA) \
diff --git a/src/Makefile.am b/src/Makefile.am
index 662ae3f74..fb379d024 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -849,6 +849,7 @@ manaplus_SOURCES += \
if ENABLE_UNITTESTS
manaplus_CXXFLAGS += -DUNITTESTS
manaplus_SOURCES += \
+ animatedsprite_unittest.cc \
gui/sdlfont_unittest.cc \
gui/widgets/browserbox_unittest.cc \
utils/stringutils_unittest.cc
diff --git a/src/animatedsprite.h b/src/animatedsprite.h
index 5a7dbde56..60df2f6e4 100644
--- a/src/animatedsprite.h
+++ b/src/animatedsprite.h
@@ -107,6 +107,23 @@ class AnimatedSprite final : public Sprite
static void setEnableCache(const bool b)
{ mEnableCache = b; }
+#ifdef UNITTESTS
+ SpriteDef *getSprite()
+ { return mSprite; }
+
+ Frame *getFrame()
+ { return mFrame; }
+
+ Animation *getAnimation()
+ { return mAnimation; }
+
+ unsigned int getFrameIndex()
+ { return mFrameIndex; }
+
+ unsigned int getFrameTime()
+ { return mFrameTime; }
+#endif
+
private:
bool updateCurrentAnimation(const unsigned int dt);
diff --git a/src/animatedsprite_unittest.cc b/src/animatedsprite_unittest.cc
new file mode 100644
index 000000000..d429f1329
--- /dev/null
+++ b/src/animatedsprite_unittest.cc
@@ -0,0 +1,151 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2013 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "animatedsprite.h"
+
+#include "client.h"
+
+#include "gui/theme.h"
+
+#include "resources/animation.h"
+#include "resources/resourcemanager.h"
+#include "resources/sdlimagehelper.h"
+
+#include "gtest/gtest.h"
+
+#include "utils/physfstools.h"
+
+#include "debug.h"
+
+static void init()
+{
+ PHYSFS_init("manaplus");
+ dirSeparator = "/";
+ XML::initXML();
+ SDL_Init(SDL_INIT_VIDEO);
+ logger = new Logger();
+ ResourceManager *resman = ResourceManager::getInstance();
+ resman->addToSearchPath("data", false);
+ resman->addToSearchPath("../data", false);
+
+ imageHelper = new SDLImageHelper();
+ SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
+ Theme *theme = Theme::instance();
+}
+
+TEST(AnimatedSprite, basic)
+{
+ Client::Options options;
+ Client client(options);
+
+ init();
+ AnimatedSprite *sprite = AnimatedSprite::load(
+ "graphics/sprites/error.xml", 0);
+ sprite->play(SpriteAction::DEFAULT);
+
+ EXPECT_NE(nullptr, sprite);
+ EXPECT_NE(nullptr, sprite->getSprite());
+ EXPECT_NE(nullptr, sprite->getAnimation());
+ EXPECT_NE(nullptr, sprite->getFrame());
+ EXPECT_EQ(0, sprite->getFrameIndex());
+ EXPECT_EQ(0, sprite->getFrameTime());
+ EXPECT_EQ(false, sprite->update(1));
+ EXPECT_EQ(0, sprite->getFrameTime());
+ EXPECT_EQ(false, sprite->update(11));
+ EXPECT_EQ(10, sprite->getFrameTime());
+ EXPECT_EQ(0, sprite->getFrameIndex());
+}
+
+TEST(AnimatedSprite, basic2)
+{
+ Client::Options options;
+ Client client(options);
+
+ init();
+ AnimatedSprite *sprite = AnimatedSprite::load(
+ "graphics/sprites/test.xml", 0);
+ sprite->play(SpriteAction::STAND);
+
+ EXPECT_EQ(10, sprite->getAnimation()->getFrames().size());
+
+ EXPECT_NE(nullptr, sprite);
+
+ EXPECT_EQ(false, sprite->update(1));
+ EXPECT_EQ(0, sprite->getFrameTime());
+ EXPECT_EQ(10, sprite->getFrame()->delay);
+
+ EXPECT_EQ(false, sprite->update(1 + 10));
+ EXPECT_EQ(0, sprite->getFrameIndex());
+ EXPECT_EQ(10, sprite->getFrameTime());
+
+ EXPECT_EQ(true, sprite->update(1 + 10 + 5));
+ EXPECT_EQ(1, sprite->getFrameIndex());
+ EXPECT_EQ(5, sprite->getFrameTime());
+
+ EXPECT_EQ(false, sprite->update(1 + 10 + 5));
+ EXPECT_EQ(1, sprite->getFrameIndex());
+ EXPECT_EQ(5, sprite->getFrameTime());
+
+ EXPECT_EQ(false, sprite->update(1 + 10 + 20));
+ EXPECT_EQ(1, sprite->getFrameIndex());
+ EXPECT_EQ(20, sprite->getFrameTime());
+
+ EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 1));
+ EXPECT_EQ(2, sprite->getFrameIndex());
+ EXPECT_EQ(1, sprite->getFrameTime());
+
+ EXPECT_EQ(false, sprite->update(1 + 10 + 20 + 10));
+ EXPECT_EQ(2, sprite->getFrameIndex());
+ EXPECT_EQ(10, sprite->getFrameTime());
+
+ EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 10 + 1));
+ EXPECT_EQ(4, sprite->getFrameIndex());
+ EXPECT_EQ(1, sprite->getFrameTime());
+
+ EXPECT_EQ(false, sprite->update(1 + 10 + 20 + 10 + 25));
+ EXPECT_EQ(4, sprite->getFrameIndex());
+ EXPECT_EQ(25, sprite->getFrameTime());
+
+ EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 10 + 25 + 1));
+ EXPECT_EQ(6, sprite->getFrameIndex());
+ EXPECT_EQ(1, sprite->getFrameTime());
+
+ EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 10 + 25 + 10 + 1));
+ EXPECT_EQ(8, sprite->getFrameIndex());
+ EXPECT_EQ(1, sprite->getFrameTime());
+
+ EXPECT_EQ(true, sprite->update(1 + 10 + 20 + 10 + 25 + 10 + 10 + 1));
+ EXPECT_EQ(4, sprite->getFrameIndex());
+ EXPECT_EQ(1, sprite->getFrameTime());
+
+ AnimatedSprite *sprite2 = AnimatedSprite::load(
+ "graphics/sprites/test.xml", 0);
+ sprite2->play(SpriteAction::SIT);
+
+ EXPECT_EQ(false, sprite2->update(1));
+ EXPECT_EQ(2, sprite2->getAnimation()->getFrames().size());
+ EXPECT_EQ(0, sprite2->getFrameTime());
+ EXPECT_EQ(85, sprite2->getFrame()->delay);
+
+ EXPECT_EQ(true, sprite2->update(1 + 10 + 20 + 10 + 25 + 10 + 10 + 1));
+ EXPECT_EQ(1, sprite2->getFrameIndex());
+ EXPECT_EQ(1, sprite2->getFrameTime());
+}
+
diff --git a/src/resources/animation.h b/src/resources/animation.h
index 09738620c..9851e1c24 100644
--- a/src/resources/animation.h
+++ b/src/resources/animation.h
@@ -95,16 +95,21 @@ class Animation final
void setLastFrameDelay(const int delay);
+ typedef std::vector<Frame> Frames;
+ typedef Frames::iterator FramesIter;
+ typedef Frames::reverse_iterator FramesRevIter;
+
+#ifdef UNITTESTS
+ Frames &getFrames()
+ { return mFrames; }
+#endif
+
/**
* Determines whether the given animation frame is a terminator.
*/
static bool isTerminator(const Frame &phase) A_WARN_UNUSED;
protected:
- typedef std::vector<Frame> Frames;
- typedef Frames::iterator FramesIter;
- typedef Frames::reverse_iterator FramesRevIter;
-
Frames mFrames;
int mDuration;
};