GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/resources/map/maplayer/drawspeciallayer2.cc Lines: 216 216 100.0 %
Date: 2018-07-14 Branches: 517 1284 40.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-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 "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
11
TEST_CASE("MapLayer drawSpecialLayer (tempLayer)", "")
47
{
48
9
    setEnv("SDL_VIDEODRIVER", "dummy");
49
50
27
    VirtFs::mountDirSilent("data", Append_false);
51
27
    VirtFs::mountDirSilent("../data", Append_false);
52
53
18
    imageHelper = new SDLImageHelper;
54
55
9
    Dirs::initRootDir();
56
9
    Dirs::initHomeDir();
57
58
9
    setBrandingDefaults(branding);
59
9
    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
9
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
68
#endif  // USE_SDL2
69
70
9
    Map *map = nullptr;
71
9
    MapLayer *layer = nullptr;
72
9
    SpecialLayer *specialLayer = nullptr;
73
9
    MockGraphics *const mock = new MockGraphics;
74
18
    const Actors actors;
75
76



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

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

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



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



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

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

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



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



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



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



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



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

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

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



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



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



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



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



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

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

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



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



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



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



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



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



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



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



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



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

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

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



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



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



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



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



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



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



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



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

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

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



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



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



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



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



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



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



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



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

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

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



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



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



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



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



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



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



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



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

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

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



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



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



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



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



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



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



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



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



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



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



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

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

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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

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

12
}