GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/sprite/animatedsprite.cc Lines: 97 97 100.0 %
Date: 2017-11-29 Branches: 345 908 38.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "unittests/unittests.h"
22
23
#include "configuration.h"
24
#include "configmanager.h"
25
#include "client.h"
26
#include "dirs.h"
27
#include "graphicsmanager.h"
28
29
#include "const/resources/spriteaction.h"
30
31
#include "being/actorsprite.h"
32
33
#include "fs/virtfs/fs.h"
34
35
#include "gui/gui.h"
36
#include "gui/theme.h"
37
38
#include "render/sdlgraphics.h"
39
40
#include "resources/sdlimagehelper.h"
41
42
#include "resources/animation/animation.h"
43
44
#include "resources/sprite/animatedsprite.h"
45
46
#include "utils/env.h"
47
#include "utils/delete2.h"
48
#include "utils/mrand.h"
49
50
PRAGMA48(GCC diagnostic push)
51
PRAGMA48(GCC diagnostic ignored "-Wshadow")
52
#ifndef USE_SDL2
53
#include <SDL.h>
54
#endif  // USE_SDL2
55
PRAGMA48(GCC diagnostic pop)
56
57
#include "debug.h"
58
59
10
TEST_CASE("AnimatedSprite tests", "animatedsprite")
60
{
61
6
    setEnv("SDL_VIDEODRIVER", "dummy");
62
63
6
    initRand();
64
6
    client = new Client;
65
6
    XML::initXML();
66
6
    SDL_Init(SDL_INIT_VIDEO);
67

24
    VirtFs::mountDirSilent("data", Append_false);
68

24
    VirtFs::mountDirSilent("../data", Append_false);
69
6
    mainGraphics = new SDLGraphics;
70
12
    imageHelper = new SDLImageHelper();
71
72
6
    Dirs::initRootDir();
73
6
    Dirs::initHomeDir();
74
75
6
    ConfigManager::initConfiguration();
76
6
    setConfigDefaults2(config);
77
6
    setBrandingDefaults(branding);
78
79
#ifdef USE_SDL2
80
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
81
        graphicsManager.createWindow(640, 480, 0,
82
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
83
#else  // USE_SDL2
84
85
6
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
86
#endif  // USE_SDL2
87
88
6
    theme = new Theme;
89
6
    Theme::selectSkin();
90
91
6
    ActorSprite::load();
92
6
    gui = new Gui();
93
6
    gui->postInit(mainGraphics);
94
95



42
    SECTION("basic test 1")
96
    {
97

8
        AnimatedSprite *sprite = AnimatedSprite::load(
98
2
            "graphics/sprites/error.xml", 0);
99
2
        sprite->play(SpriteAction::DEFAULT);
100
101



8
        REQUIRE_FALSE(sprite == nullptr);
102



8
        REQUIRE_FALSE(sprite->getSprite() == nullptr);
103



8
        REQUIRE_FALSE(sprite->getAnimation() == nullptr);
104



8
        REQUIRE_FALSE(sprite->getFrame() == nullptr);
105



8
        REQUIRE(0 == sprite->getFrameIndex());
106



8
        REQUIRE(0 == sprite->getFrameTime());
107




8
        REQUIRE(false == sprite->update(1));
108



8
        REQUIRE(0 == sprite->getFrameTime());
109




8
        REQUIRE(false == sprite->update(11));
110



8
        REQUIRE(10 == sprite->getFrameTime());
111



8
        REQUIRE(0 == sprite->getFrameIndex());
112
2
        delete sprite;
113
2
        logger->log("test4");
114
    }
115
116



42
    SECTION("basic test 2")
117
    {
118

8
        AnimatedSprite *sprite = AnimatedSprite::load(
119
2
            "graphics/sprites/test.xml", 0);
120
2
        sprite->play(SpriteAction::STAND);
121
122



10
        REQUIRE(10 == const_cast<Animation*>(sprite->getAnimation())
123
            ->getFrames().size());
124
125



8
        REQUIRE_FALSE(nullptr == sprite);
126
127




8
        REQUIRE(false == sprite->update(1));
128



8
        REQUIRE(0 == sprite->getFrameTime());
129



8
        REQUIRE(10 == sprite->getFrame()->delay);
130
131




8
        REQUIRE(false == sprite->update(1 + 10));
132



8
        REQUIRE(0 == sprite->getFrameIndex());
133



8
        REQUIRE(10 == sprite->getFrameTime());
134
135




8
        REQUIRE(true == sprite->update(1 + 10 + 5));
136



8
        REQUIRE(1 == sprite->getFrameIndex());
137



8
        REQUIRE(5 == sprite->getFrameTime());
138
139




8
        REQUIRE(false == sprite->update(1 + 10 + 5));
140



8
        REQUIRE(1 == sprite->getFrameIndex());
141



8
        REQUIRE(5 == sprite->getFrameTime());
142
143




8
        REQUIRE(false == sprite->update(1 + 10 + 20));
144



8
        REQUIRE(1 == sprite->getFrameIndex());
145



8
        REQUIRE(20 == sprite->getFrameTime());
146
147




8
        REQUIRE(true == sprite->update(1 + 10 + 20 + 1));
148



8
        REQUIRE(2 == sprite->getFrameIndex());
149



8
        REQUIRE(1 == sprite->getFrameTime());
150
151




8
        REQUIRE(false == sprite->update(1 + 10 + 20 + 10));
152



8
        REQUIRE(2 == sprite->getFrameIndex());
153



8
        REQUIRE(10 == sprite->getFrameTime());
154
155




8
        REQUIRE(true == sprite->update(1 + 10 + 20 + 10 + 1));
156



8
        REQUIRE(4 == sprite->getFrameIndex());
157



8
        REQUIRE(1 == sprite->getFrameTime());
158
159




8
        REQUIRE(false == sprite->update(1 + 10 + 20 + 10 + 25));
160



8
        REQUIRE(4 == sprite->getFrameIndex());
161



8
        REQUIRE(25 == sprite->getFrameTime());
162
163




8
        REQUIRE(true == sprite->update(1 + 10 + 20 + 10 + 25 + 1));
164



8
        REQUIRE(6 == sprite->getFrameIndex());
165



8
        REQUIRE(1 == sprite->getFrameTime());
166
167




8
        REQUIRE(true == sprite->update(1 + 10 + 20 + 10 + 25 + 10 + 1));
168



8
        REQUIRE(8 == sprite->getFrameIndex());
169



8
        REQUIRE(1 == sprite->getFrameTime());
170
171




8
        REQUIRE(true == sprite->update(1 + 10 + 20 + 10 + 25 + 10 + 10 + 1));
172



8
        REQUIRE(4 == sprite->getFrameIndex());
173



8
        REQUIRE(1 == sprite->getFrameTime());
174
2
        delete sprite;
175
    }
176
177



42
    SECTION("basic test 3")
178
    {
179
8
        AnimatedSprite *sprite2 = AnimatedSprite::load(
180
2
            "graphics/sprites/test.xml", 0);
181
2
        sprite2->play(SpriteAction::SIT);
182
183




8
        REQUIRE(false == sprite2->update(1));
184



10
        REQUIRE(2 == const_cast<Animation*>(sprite2->getAnimation())
185
            ->getFrames().size());
186



8
        REQUIRE(0 == sprite2->getFrameTime());
187



8
        REQUIRE(85 == sprite2->getFrame()->delay);
188
189




8
        REQUIRE(true == sprite2->update(1 + 10 + 20 + 10 + 25 + 10 + 10 + 1));
190



8
        REQUIRE(1 == sprite2->getFrameIndex());
191



8
        REQUIRE(1 == sprite2->getFrameTime());
192
2
        delete sprite2;
193
    }
194
195
6
    delete2(client);
196

24
    VirtFs::unmountDirSilent("data");
197

24
    VirtFs::unmountDirSilent("../data");
198

12
}