GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/sprite/animatedsprite.cc Lines: 97 97 100.0 %
Date: 2018-07-14 Branches: 342 902 37.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2018  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
5
TEST_CASE("AnimatedSprite tests", "animatedsprite")
60
{
61
3
    setEnv("SDL_VIDEODRIVER", "dummy");
62
63
3
    initRand();
64
3
    client = new Client;
65
3
    XML::initXML();
66
3
    SDL_Init(SDL_INIT_VIDEO);
67
9
    VirtFs::mountDirSilent("data", Append_false);
68
9
    VirtFs::mountDirSilent("../data", Append_false);
69
3
    mainGraphics = new SDLGraphics;
70
6
    imageHelper = new SDLImageHelper();
71
72
3
    Dirs::initRootDir();
73
3
    Dirs::initHomeDir();
74
75
3
    ConfigManager::initConfiguration();
76
3
    setConfigDefaults2(config);
77
3
    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
3
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
86
#endif  // USE_SDL2
87
88
3
    theme = new Theme;
89
3
    Theme::selectSkin();
90
91
3
    ActorSprite::load();
92
3
    gui = new Gui();
93
3
    gui->postInit(mainGraphics);
94
95



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

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



4
        REQUIRE_FALSE(sprite == nullptr);
102



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



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



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



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



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




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



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




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



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



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



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

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



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



4
        REQUIRE_FALSE(nullptr == sprite);
126
127




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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




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



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



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



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




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



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



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



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




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



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



4
        REQUIRE(1 == sprite2->getFrameTime());
192
1
        delete sprite2;
193
    }
194
195
3
    delete2(client);
196
9
    VirtFs::unmountDirSilent("data");
197
9
    VirtFs::unmountDirSilent("../data");
198

6
}