GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/drawspeciallayer1.cc Lines: 212 212 100.0 %
Date: 2021-03-17 Branches: 497 1224 40.6 %

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 (specialLayer)", "")
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
9
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
64
        GraphicsManager::createWindow(640, 480, 0,
65
9
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
66
#else  // USE_SDL2
67
68
    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->getSpecialLayer();
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->getSpecialLayer();
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->getSpecialLayer();
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->getSpecialLayer();
178
1
        specialLayer->setTile(1, 0, MapItemType::ARROW_UP);
179
1
        specialLayer->updateCache();
180
181
        layer->drawSpecialLayer(mock,
182
            0,
183
            0, 2,
184
1
            0, 0);
185



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



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



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



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



63
    SECTION("simple 5")
192
    {
193
1
        map = new Map("map",
194
            2, 1,
195

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

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



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



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



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



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



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



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



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



63
    SECTION("simple 6")
224
    {
225
1
        map = new Map("map",
226
            3, 1,
227

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

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



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



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



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



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



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



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



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



63
    SECTION("simple 7")
256
    {
257
1
        map = new Map("map",
258
            3, 1,
259

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

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



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



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



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



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



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



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



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



63
    SECTION("simple 8")
288
    {
289
1
        map = new Map("map",
290
            3, 1,
291

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

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



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



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



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



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



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



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



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



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



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



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



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

3
            32, 32);
330
1
        layer = new MapLayer("test",
331
            0, 0,
332
            maxX, maxY,
333
            true,
334
            0,
335

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



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



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



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



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



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



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



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



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



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



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



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



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



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



4
        REQUIRE(mock->mDraws.empty());
373
374
        layer->drawSpecialLayer(mock,
375
            6,
376
            0, maxX,
377
1
            0, 0);
378



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


4
        REQUIRE(mock->mDraws.size() == 1);
385



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



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



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

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

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

12
}