GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/drawspeciallayer2.cc Lines: 216 216 100.0 %
Date: 2021-03-17 Branches: 517 1280 40.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "unittests/unittests.h"
23
24
#include "configmanager.h"
25
#include "configuration.h"
26
#include "dirs.h"
27
#include "graphicsmanager.h"
28
29
#include "enums/resources/map/mapitemtype.h"
30
31
#include "fs/virtfs/fs.h"
32
33
#include "unittests/render/mockgraphics.h"
34
35
#include "utils/env.h"
36
37
#include "resources/resourcemanager/resourcemanager.h"
38
39
#include "resources/sdlimagehelper.h"
40
41
#include "resources/map/map.h"
42
#include "resources/map/maplayer.h"
43
#include "resources/map/speciallayer.h"
44
45
#include "debug.h"
46
47
11
TEST_CASE("MapLayer drawSpecialLayer (tempLayer)", "")
48
{
49
9
    setEnv("SDL_VIDEODRIVER", "dummy");
50
51
27
    VirtFs::mountDirSilent("data", Append_false);
52
27
    VirtFs::mountDirSilent("../data", Append_false);
53
54
18
    imageHelper = new SDLImageHelper;
55
56
9
    Dirs::initRootDir();
57
9
    Dirs::initHomeDir();
58
59
9
    setBrandingDefaults(branding);
60
9
    ConfigManager::initConfiguration();
61
62
#ifdef USE_SDL2
63
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
64
        GraphicsManager::createWindow(640, 480, 0,
65
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
66
#else  // USE_SDL2
67
68
9
    GraphicsManager::createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
69
#endif  // USE_SDL2
70
71
9
    Map *map = nullptr;
72
9
    MapLayer *layer = nullptr;
73
9
    SpecialLayer *specialLayer = nullptr;
74
9
    MockGraphics *const mock = new MockGraphics;
75
18
    const Actors actors;
76
77



63
    SECTION("simple 1")
78
    {
79
1
        map = new Map("map",
80
            1, 1,
81

3
            32, 32);
82
1
        layer = new MapLayer("test",
83
            0, 0,
84
            1, 1,
85
            true,
86
            0,
87

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



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



63
    SECTION("simple 2")
108
    {
109
1
        map = new Map("map",
110
            1, 1,
111

3
            32, 32);
112
1
        layer = new MapLayer("test",
113
            0, 0,
114
            1, 1,
115
            true,
116
            0,
117

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



5
        REQUIRE(mock->mDraws.size() == 1);
130



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



4
        REQUIRE(mock->mDraws[0].x == 0);
132



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



63
    SECTION("simple 3")
136
    {
137
1
        map = new Map("map",
138
            2, 1,
139

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

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



5
        REQUIRE(mock->mDraws.size() == 1);
158



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



4
        REQUIRE(mock->mDraws[0].x == 0);
160



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



63
    SECTION("simple 4")
164
    {
165
1
        map = new Map("map",
166
            2, 1,
167

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

3
            0);
174
1
        map->addLayer(layer);
175
2
        layer->setSpecialLayer(map->getSpecialLayer());
176
2
        layer->setTempLayer(map->getTempLayer());
177
1
        specialLayer = map->getTempLayer();
178
1
        const int *const cache = specialLayer->getCache();
179



4
        REQUIRE(cache[0] == 10000);
180



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



4
        REQUIRE(cache[0] == 0);
185



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



5
        REQUIRE(mock->mDraws.size() == 1);
192



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



4
        REQUIRE(mock->mDraws[0].x == 32);
194



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



63
    SECTION("simple 5")
198
    {
199
1
        map = new Map("map",
200
            2, 1,
201

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

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



5
        REQUIRE(mock->mDraws.size() == 2);
221



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



4
        REQUIRE(mock->mDraws[0].x == 0);
223



4
        REQUIRE(mock->mDraws[0].y == 0);
224



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



4
        REQUIRE(mock->mDraws[1].x == 32);
226



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



63
    SECTION("simple 6")
230
    {
231
1
        map = new Map("map",
232
            3, 1,
233

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

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



5
        REQUIRE(mock->mDraws.size() == 2);
253



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



4
        REQUIRE(mock->mDraws[0].x == 0);
255



4
        REQUIRE(mock->mDraws[0].y == 0);
256



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



4
        REQUIRE(mock->mDraws[1].x == 64);
258



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



63
    SECTION("simple 7")
262
    {
263
1
        map = new Map("map",
264
            3, 1,
265

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

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



5
        REQUIRE(mock->mDraws.size() == 2);
285



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



4
        REQUIRE(mock->mDraws[0].x == 32);
287



4
        REQUIRE(mock->mDraws[0].y == 0);
288



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



4
        REQUIRE(mock->mDraws[1].x == 64);
290



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



63
    SECTION("simple 8")
294
    {
295
1
        map = new Map("map",
296
            3, 1,
297

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

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



5
        REQUIRE(mock->mDraws.size() == 3);
318



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



4
        REQUIRE(mock->mDraws[0].x == 0);
320



4
        REQUIRE(mock->mDraws[0].y == 0);
321



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



4
        REQUIRE(mock->mDraws[1].x == 32);
323



4
        REQUIRE(mock->mDraws[1].y == 0);
324



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



4
        REQUIRE(mock->mDraws[2].x == 64);
326



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



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

3
            32, 32);
336
1
        layer = new MapLayer("test",
337
            0, 0,
338
            maxX, maxY,
339
            true,
340
            0,
341

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



5
        REQUIRE(mock->mDraws.size() == 4);
360



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



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



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



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



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



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



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



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



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



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



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



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



4
        REQUIRE(mock->mDraws.empty());
379
380
        layer->drawSpecialLayer(mock,
381
            6,
382
            0, maxX,
383
1
            0, 0);
384


3
        REQUIRE(mock->mDraws.empty());
385
386
        layer->drawSpecialLayer(mock,
387
            20,
388
            0, maxX,
389
1
            0, 0);
390



5
        REQUIRE(mock->mDraws.size() == 1);
391



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



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



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

27
    VirtFs::unmountDirSilent("data");
402

36
    VirtFs::unmountDirSilent("../data");
403

12
}