GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/drawspeciallayer1.cc Lines: 211 211 100.0 %
Date: 2017-11-29 Branches: 498 1230 40.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-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 "configmanager.h"
24
#include "configuration.h"
25
#include "dirs.h"
26
#include "graphicsmanager.h"
27
28
#include "enums/resources/map/mapitemtype.h"
29
30
#include "fs/virtfs/fs.h"
31
32
#include "unittests/render/mockgraphics.h"
33
34
#include "utils/env.h"
35
36
#include "resources/resourcemanager/resourcemanager.h"
37
38
#include "resources/sdlimagehelper.h"
39
40
#include "resources/map/map.h"
41
#include "resources/map/maplayer.h"
42
#include "resources/map/speciallayer.h"
43
44
#include "debug.h"
45
46
22
TEST_CASE("MapLayer drawSpecialLayer (specialLayer)", "")
47
{
48
18
    setEnv("SDL_VIDEODRIVER", "dummy");
49
50

72
    VirtFs::mountDirSilent("data", Append_false);
51

72
    VirtFs::mountDirSilent("../data", Append_false);
52
53
36
    imageHelper = new SDLImageHelper;
54
55
18
    Dirs::initRootDir();
56
18
    Dirs::initHomeDir();
57
58
18
    setBrandingDefaults(branding);
59
18
    ConfigManager::initConfiguration();
60
61
#ifdef USE_SDL2
62
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
63
        graphicsManager.createWindow(640, 480, 0,
64
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
65
#else  // USE_SDL2
66
67
18
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
68
#endif  // USE_SDL2
69
70
18
    Map *map = nullptr;
71
18
    MapLayer *layer = nullptr;
72
18
    SpecialLayer *specialLayer = nullptr;
73
18
    MockGraphics *const mock = new MockGraphics;
74
36
    const Actors actors;
75
76



126
    SECTION("simple 1")
77
    {
78
10
        map = new Map("map",
79
            1, 1,
80

2
            32, 32);
81
10
        layer = new MapLayer("test",
82
            0, 0,
83
            1, 1,
84
            true,
85
            0,
86

2
            0);
87
2
        map->addLayer(layer);
88
4
        layer->setSpecialLayer(map->getSpecialLayer());
89
4
        layer->setTempLayer(map->getTempLayer());
90
2
        specialLayer = map->getSpecialLayer();
91
92
2
        layer->drawSpecialLayer(mock,
93
            0,
94
            0, 1,
95
            0, 0);
96



8
        REQUIRE(mock->mDraws.empty());
97
98
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
99
2
        layer->drawSpecialLayer(mock,
100
            0,
101
            0, 1,
102
            0, 0);
103
//        REQUIRE(mock->mDraws.size() == 1);
104
    }
105
106



126
    SECTION("simple 2")
107
    {
108
10
        map = new Map("map",
109
            1, 1,
110

2
            32, 32);
111
10
        layer = new MapLayer("test",
112
            0, 0,
113
            1, 1,
114
            true,
115
            0,
116

2
            0);
117
2
        map->addLayer(layer);
118
4
        layer->setSpecialLayer(map->getSpecialLayer());
119
4
        layer->setTempLayer(map->getTempLayer());
120
2
        specialLayer = map->getSpecialLayer();
121
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
122
2
        specialLayer->updateCache();
123
124
2
        layer->drawSpecialLayer(mock,
125
            0,
126
            0, 1,
127
            0, 0);
128



10
        REQUIRE(mock->mDraws.size() == 1);
129



8
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
130



8
        REQUIRE(mock->mDraws[0].x == 0);
131



8
        REQUIRE(mock->mDraws[0].y == 0);
132
    }
133
134



126
    SECTION("simple 3")
135
    {
136
10
        map = new Map("map",
137
            2, 1,
138

2
            32, 32);
139
8
        layer = new MapLayer("test",
140
            0, 0,
141
            2, 1,
142
            true,
143
            0,
144

2
            0);
145
2
        map->addLayer(layer);
146
4
        layer->setSpecialLayer(map->getSpecialLayer());
147
4
        layer->setTempLayer(map->getTempLayer());
148
2
        specialLayer = map->getSpecialLayer();
149
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
150
2
        specialLayer->updateCache();
151
152
2
        layer->drawSpecialLayer(mock,
153
            0,
154
            0, 2,
155
            0, 0);
156



10
        REQUIRE(mock->mDraws.size() == 1);
157



8
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
158



8
        REQUIRE(mock->mDraws[0].x == 0);
159



8
        REQUIRE(mock->mDraws[0].y == 0);
160
    }
161
162



126
    SECTION("simple 4")
163
    {
164
10
        map = new Map("map",
165
            2, 1,
166

2
            32, 32);
167
10
        layer = new MapLayer("test",
168
            0, 0,
169
            2, 1,
170
            true,
171
            0,
172

2
            0);
173
2
        map->addLayer(layer);
174
4
        layer->setSpecialLayer(map->getSpecialLayer());
175
4
        layer->setTempLayer(map->getTempLayer());
176
2
        specialLayer = map->getSpecialLayer();
177
2
        specialLayer->setTile(1, 0, MapItemType::ARROW_UP);
178
2
        specialLayer->updateCache();
179
180
2
        layer->drawSpecialLayer(mock,
181
            0,
182
            0, 2,
183
            0, 0);
184



10
        REQUIRE(mock->mDraws.size() == 1);
185



8
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
186



8
        REQUIRE(mock->mDraws[0].x == 32);
187



8
        REQUIRE(mock->mDraws[0].y == 0);
188
    }
189
190



126
    SECTION("simple 5")
191
    {
192
10
        map = new Map("map",
193
            2, 1,
194

2
            32, 32);
195
10
        layer = new MapLayer("test",
196
            0, 0,
197
            2, 1,
198
            true,
199
            0,
200

2
            0);
201
2
        map->addLayer(layer);
202
4
        layer->setSpecialLayer(map->getSpecialLayer());
203
4
        layer->setTempLayer(map->getTempLayer());
204
2
        specialLayer = map->getSpecialLayer();
205
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
206
2
        specialLayer->setTile(1, 0, MapItemType::ARROW_UP);
207
2
        specialLayer->updateCache();
208
209
2
        layer->drawSpecialLayer(mock,
210
            0,
211
            0, 2,
212
            0, 0);
213



10
        REQUIRE(mock->mDraws.size() == 2);
214



8
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
215



8
        REQUIRE(mock->mDraws[0].x == 0);
216



8
        REQUIRE(mock->mDraws[0].y == 0);
217



8
        REQUIRE(mock->mDraws[1].drawType == MockDrawType::DrawImage);
218



8
        REQUIRE(mock->mDraws[1].x == 32);
219



8
        REQUIRE(mock->mDraws[1].y == 0);
220
    }
221
222



126
    SECTION("simple 6")
223
    {
224
10
        map = new Map("map",
225
            3, 1,
226

2
            32, 32);
227
10
        layer = new MapLayer("test",
228
            0, 0,
229
            3, 1,
230
            true,
231
            0,
232

2
            0);
233
2
        map->addLayer(layer);
234
4
        layer->setSpecialLayer(map->getSpecialLayer());
235
2
        layer->setTempLayer(map->getTempLayer());
236
2
        specialLayer = map->getSpecialLayer();
237
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
238
2
        specialLayer->setTile(2, 0, MapItemType::ARROW_UP);
239
2
        specialLayer->updateCache();
240
241
2
        layer->drawSpecialLayer(mock,
242
            0,
243
            0, 3,
244
            0, 0);
245



10
        REQUIRE(mock->mDraws.size() == 2);
246



8
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
247



8
        REQUIRE(mock->mDraws[0].x == 0);
248



8
        REQUIRE(mock->mDraws[0].y == 0);
249



8
        REQUIRE(mock->mDraws[1].drawType == MockDrawType::DrawImage);
250



8
        REQUIRE(mock->mDraws[1].x == 64);
251



8
        REQUIRE(mock->mDraws[1].y == 0);
252
    }
253
254



126
    SECTION("simple 7")
255
    {
256
10
        map = new Map("map",
257
            3, 1,
258

2
            32, 32);
259
10
        layer = new MapLayer("test",
260
            0, 0,
261
            3, 1,
262
            true,
263
            0,
264

2
            0);
265
2
        map->addLayer(layer);
266
4
        layer->setSpecialLayer(map->getSpecialLayer());
267
4
        layer->setTempLayer(map->getTempLayer());
268
2
        specialLayer = map->getSpecialLayer();
269
2
        specialLayer->setTile(1, 0, MapItemType::ARROW_UP);
270
2
        specialLayer->setTile(2, 0, MapItemType::ARROW_UP);
271
2
        specialLayer->updateCache();
272
273
2
        layer->drawSpecialLayer(mock,
274
            0,
275
            0, 3,
276
            0, 0);
277



10
        REQUIRE(mock->mDraws.size() == 2);
278



8
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
279



8
        REQUIRE(mock->mDraws[0].x == 32);
280



8
        REQUIRE(mock->mDraws[0].y == 0);
281



8
        REQUIRE(mock->mDraws[1].drawType == MockDrawType::DrawImage);
282



8
        REQUIRE(mock->mDraws[1].x == 64);
283



8
        REQUIRE(mock->mDraws[1].y == 0);
284
    }
285
286



126
    SECTION("simple 8")
287
    {
288
10
        map = new Map("map",
289
            3, 1,
290

2
            32, 32);
291
10
        layer = new MapLayer("test",
292
            0, 0,
293
            3, 1,
294
            true,
295
            0,
296

2
            0);
297
2
        map->addLayer(layer);
298
4
        layer->setSpecialLayer(map->getSpecialLayer());
299
4
        layer->setTempLayer(map->getTempLayer());
300
2
        specialLayer = map->getSpecialLayer();
301
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
302
2
        specialLayer->setTile(1, 0, MapItemType::ARROW_DOWN);
303
2
        specialLayer->setTile(2, 0, MapItemType::ARROW_UP);
304
2
        specialLayer->updateCache();
305
306
2
        layer->drawSpecialLayer(mock,
307
            0,
308
            0, 3,
309
            0, 0);
310



10
        REQUIRE(mock->mDraws.size() == 3);
311



8
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
312



8
        REQUIRE(mock->mDraws[0].x == 0);
313



8
        REQUIRE(mock->mDraws[0].y == 0);
314



8
        REQUIRE(mock->mDraws[1].drawType == MockDrawType::DrawImage);
315



8
        REQUIRE(mock->mDraws[1].x == 32);
316



8
        REQUIRE(mock->mDraws[1].y == 0);
317



8
        REQUIRE(mock->mDraws[2].drawType == MockDrawType::DrawImage);
318



8
        REQUIRE(mock->mDraws[2].x == 64);
319



8
        REQUIRE(mock->mDraws[2].y == 0);
320
    }
321
322



126
    SECTION("normal 1")
323
    {
324
2
        const int maxX = 100;
325
2
        const int maxY = 50;
326
10
        map = new Map("map",
327
            maxX, maxY,
328

2
            32, 32);
329
10
        layer = new MapLayer("test",
330
            0, 0,
331
            maxX, maxY,
332
            true,
333
            0,
334

2
            0);
335
2
        map->addLayer(layer);
336
4
        layer->setSpecialLayer(map->getSpecialLayer());
337
4
        layer->setTempLayer(map->getTempLayer());
338
2
        specialLayer = map->getSpecialLayer();
339
2
        specialLayer->setTile(0, 5, MapItemType::ARROW_UP);
340
2
        specialLayer->setTile(1, 5, MapItemType::ARROW_DOWN);
341
2
        specialLayer->setTile(2, 5, MapItemType::ARROW_UP);
342
2
        specialLayer->setTile(3, 5, MapItemType::EMPTY);
343
2
        specialLayer->setTile(4, 5, MapItemType::EMPTY);
344
2
        specialLayer->setTile(6, 5, MapItemType::ARROW_LEFT);
345
2
        specialLayer->setTile(10, 20, MapItemType::ARROW_LEFT);
346
2
        specialLayer->updateCache();
347
348
2
        layer->drawSpecialLayer(mock,
349
            5,
350
            0, maxX,
351
            0, 0);
352



10
        REQUIRE(mock->mDraws.size() == 4);
353



8
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
354



8
        REQUIRE(mock->mDraws[0].x == 0 * 32);
355



8
        REQUIRE(mock->mDraws[0].y == 5 * 32);
356



8
        REQUIRE(mock->mDraws[1].drawType == MockDrawType::DrawImage);
357



8
        REQUIRE(mock->mDraws[1].x == 1 * 32);
358



8
        REQUIRE(mock->mDraws[1].y == 5 * 32);
359



8
        REQUIRE(mock->mDraws[2].drawType == MockDrawType::DrawImage);
360



8
        REQUIRE(mock->mDraws[2].x == 2 * 32);
361



8
        REQUIRE(mock->mDraws[2].y == 5 * 32);
362



8
        REQUIRE(mock->mDraws[3].drawType == MockDrawType::DrawImage);
363



8
        REQUIRE(mock->mDraws[3].x == 6 * 32);
364



8
        REQUIRE(mock->mDraws[3].y == 5 * 32);
365
366
4
        mock->mDraws.clear();
367
2
        layer->drawSpecialLayer(mock,
368
            4,
369
            0, maxX,
370
            0, 0);
371



8
        REQUIRE(mock->mDraws.empty());
372
373
2
        layer->drawSpecialLayer(mock,
374
            6,
375
            0, maxX,
376
            0, 0);
377



8
        REQUIRE(mock->mDraws.empty());
378
379
2
        layer->drawSpecialLayer(mock,
380
            20,
381
            0, maxX,
382
            0, 0);
383



10
        REQUIRE(mock->mDraws.size() == 1);
384



6
        REQUIRE(mock->mDraws[0].drawType == MockDrawType::DrawImage);
385



8
        REQUIRE(mock->mDraws[0].x == 10 * 32);
386



8
        REQUIRE(mock->mDraws[0].y == 20 * 32);
387
    }
388
389
18
    delete map;
390
18
    delete mock;
391
18
    GraphicsManager::deleteRenderers();
392
18
    ResourceManager::cleanOrphans(true);
393
18
    ResourceManager::deleteInstance();
394

72
    VirtFs::unmountDirSilent("data");
395

72
    VirtFs::unmountDirSilent("../data");
396

24
}