GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/drawspeciallayer2.cc Lines: 216 216 100.0 %
Date: 2017-11-29 Branches: 518 1286 40.3 %

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 (tempLayer)", "")
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->getTempLayer();
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->getTempLayer();
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->getTempLayer();
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->getTempLayer();
177
2
        const int *const cache = specialLayer->getCache();
178



8
        REQUIRE(cache[0] == 10000);
179



8
        REQUIRE(cache[1] == 10000);
180
181
2
        specialLayer->setTile(1, 0, MapItemType::ARROW_UP);
182
2
        specialLayer->updateCache();
183



8
        REQUIRE(cache[0] == 0);
184



8
        REQUIRE(cache[1] == 10000);
185
186
2
        layer->drawSpecialLayer(mock,
187
            0,
188
            0, 2,
189
            0, 0);
190



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



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



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



8
        REQUIRE(mock->mDraws[0].y == 0);
194
    }
195
196



126
    SECTION("simple 5")
197
    {
198
10
        map = new Map("map",
199
            2, 1,
200

2
            32, 32);
201
10
        layer = new MapLayer("test",
202
            0, 0,
203
            2, 1,
204
            true,
205
            0,
206

2
            0);
207
2
        map->addLayer(layer);
208
4
        layer->setSpecialLayer(map->getSpecialLayer());
209
4
        layer->setTempLayer(map->getTempLayer());
210
2
        specialLayer = map->getTempLayer();
211
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
212
2
        specialLayer->setTile(1, 0, MapItemType::ARROW_UP);
213
2
        specialLayer->updateCache();
214
215
2
        layer->drawSpecialLayer(mock,
216
            0,
217
            0, 2,
218
            0, 0);
219



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



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



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



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



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



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



8
        REQUIRE(mock->mDraws[1].y == 0);
226
    }
227
228



126
    SECTION("simple 6")
229
    {
230
10
        map = new Map("map",
231
            3, 1,
232

2
            32, 32);
233
10
        layer = new MapLayer("test",
234
            0, 0,
235
            3, 1,
236
            true,
237
            0,
238

2
            0);
239
2
        map->addLayer(layer);
240
4
        layer->setSpecialLayer(map->getSpecialLayer());
241
4
        layer->setTempLayer(map->getTempLayer());
242
2
        specialLayer = map->getTempLayer();
243
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
244
2
        specialLayer->setTile(2, 0, MapItemType::ARROW_UP);
245
2
        specialLayer->updateCache();
246
247
2
        layer->drawSpecialLayer(mock,
248
            0,
249
            0, 3,
250
            0, 0);
251



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



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



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



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



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



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



8
        REQUIRE(mock->mDraws[1].y == 0);
258
    }
259
260



126
    SECTION("simple 7")
261
    {
262
10
        map = new Map("map",
263
            3, 1,
264

2
            32, 32);
265
10
        layer = new MapLayer("test",
266
            0, 0,
267
            3, 1,
268
            true,
269
            0,
270

2
            0);
271
2
        map->addLayer(layer);
272
4
        layer->setSpecialLayer(map->getSpecialLayer());
273
4
        layer->setTempLayer(map->getTempLayer());
274
2
        specialLayer = map->getTempLayer();
275
2
        specialLayer->setTile(1, 0, MapItemType::ARROW_UP);
276
2
        specialLayer->setTile(2, 0, MapItemType::ARROW_UP);
277
2
        specialLayer->updateCache();
278
279
2
        layer->drawSpecialLayer(mock,
280
            0,
281
            0, 3,
282
            0, 0);
283



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



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



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



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



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



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



8
        REQUIRE(mock->mDraws[1].y == 0);
290
    }
291
292



126
    SECTION("simple 8")
293
    {
294
10
        map = new Map("map",
295
            3, 1,
296

2
            32, 32);
297
10
        layer = new MapLayer("test",
298
            0, 0,
299
            3, 1,
300
            true,
301
            0,
302

2
            0);
303
2
        map->addLayer(layer);
304
4
        layer->setSpecialLayer(map->getSpecialLayer());
305
4
        layer->setTempLayer(map->getTempLayer());
306
2
        specialLayer = map->getTempLayer();
307
2
        specialLayer->setTile(0, 0, MapItemType::ARROW_UP);
308
2
        specialLayer->setTile(1, 0, MapItemType::ARROW_DOWN);
309
2
        specialLayer->setTile(2, 0, MapItemType::ARROW_UP);
310
2
        specialLayer->updateCache();
311
312
2
        layer->drawSpecialLayer(mock,
313
            0,
314
            0, 3,
315
            0, 0);
316



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



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



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



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



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



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



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



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



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



8
        REQUIRE(mock->mDraws[2].y == 0);
326
    }
327
328



126
    SECTION("normal 1")
329
    {
330
2
        const int maxX = 100;
331
2
        const int maxY = 50;
332
10
        map = new Map("map",
333
            maxX, maxY,
334

2
            32, 32);
335
10
        layer = new MapLayer("test",
336
            0, 0,
337
            maxX, maxY,
338
            true,
339
            0,
340

2
            0);
341
2
        map->addLayer(layer);
342
4
        layer->setSpecialLayer(map->getSpecialLayer());
343
4
        layer->setTempLayer(map->getTempLayer());
344
2
        specialLayer = map->getTempLayer();
345
2
        specialLayer->setTile(0, 5, MapItemType::ARROW_UP);
346
2
        specialLayer->setTile(1, 5, MapItemType::ARROW_DOWN);
347
2
        specialLayer->setTile(2, 5, MapItemType::ARROW_UP);
348
2
        specialLayer->setTile(3, 5, MapItemType::EMPTY);
349
2
        specialLayer->setTile(4, 5, MapItemType::EMPTY);
350
2
        specialLayer->setTile(6, 5, MapItemType::ARROW_LEFT);
351
2
        specialLayer->setTile(10, 20, MapItemType::ARROW_LEFT);
352
2
        specialLayer->updateCache();
353
354
2
        layer->drawSpecialLayer(mock,
355
            5,
356
            0, maxX,
357
            0, 0);
358



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



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



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



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



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



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



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



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



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



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



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



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



8
        REQUIRE(mock->mDraws[3].y == 5 * 32);
371
372
4
        mock->mDraws.clear();
373
2
        layer->drawSpecialLayer(mock,
374
            4,
375
            0, maxX,
376
            0, 0);
377



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



8
        REQUIRE(mock->mDraws.empty());
384
385
2
        layer->drawSpecialLayer(mock,
386
            20,
387
            0, maxX,
388
            0, 0);
389



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



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



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



8
        REQUIRE(mock->mDraws[0].y == 20 * 32);
393
    }
394
395
18
    delete map;
396
18
    delete mock;
397
18
    GraphicsManager::deleteRenderers();
398
18
    ResourceManager::cleanOrphans(true);
399
18
    ResourceManager::deleteInstance();
400

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

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

24
}