GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/xml.cc Lines: 237 237 100.0 %
Date: 2018-09-20 Branches: 948 2388 39.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-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 "client.h"
24
#include "configuration.h"
25
#include "configmanager.h"
26
#include "dirs.h"
27
#include "graphicsmanager.h"
28
29
#include "being/actorsprite.h"
30
31
#include "fs/virtfs/fs.h"
32
33
#include "gui/gui.h"
34
#include "gui/theme.h"
35
36
#include "utils/delete2.h"
37
#include "utils/env.h"
38
#ifdef ENABLE_PUGIXML
39
#include "utils/xmlwriter.h"
40
#endif  // ENABLE_PUGIXML
41
42
#include "render/sdlgraphics.h"
43
44
#include "resources/sdlimagehelper.h"
45
46
#include "resources/resourcemanager/resourcemanager.h"
47
48
#include "debug.h"
49
50
3
TEST_CASE("xml test1", "")
51
{
52



4
    REQUIRE(client == nullptr);
53



4
    REQUIRE(gui == nullptr);
54
1
    ResourceManager::cleanOrphans(true);
55
1
    ResourceManager::deleteInstance();
56
1
}
57
58
15
TEST_CASE("xml doc", "")
59
{
60
13
    setEnv("SDL_VIDEODRIVER", "dummy");
61
62
13
    client = new Client;
63
13
    XML::initXML();
64
39
    VirtFs::mountDirSilent("data", Append_false);
65
39
    VirtFs::mountDirSilent("../data", Append_false);
66
13
    mainGraphics = new SDLGraphics;
67
26
    imageHelper = new SDLImageHelper();
68
69
13
    Dirs::initRootDir();
70
13
    Dirs::initHomeDir();
71
72
13
    ConfigManager::initConfiguration();
73
13
    setConfigDefaults2(config);
74
13
    setBrandingDefaults(branding);
75
76
#ifdef USE_SDL2
77
    SDLImageHelper::setRenderer(graphicsManager.createRenderer(
78
        graphicsManager.createWindow(640, 480, 0,
79
        SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
80
#else  // USE_SDL2
81
82
13
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
83
#endif  // USE_SDL2
84
85
13
    theme = new Theme;
86
13
    Theme::selectSkin();
87
88
13
    const char *const tempXmlName = "tempxml.xml";
89
13
    ActorSprite::load();
90
13
    gui = new Gui();
91
13
    gui->postInit(mainGraphics);
92
93



91
    SECTION("load1")
94
    {
95
        XML::Document doc("graphics/gui/browserbox.xml",
96
            UseVirtFs_true,
97

4
            SkipError_false);
98



5
        REQUIRE(doc.isLoaded() == true);
99



4
        REQUIRE(doc.isValid() == true);
100




4
        REQUIRE(doc.rootNode() != nullptr);
101




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "skinset") == true);
102




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false);
103




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
104
//        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
105
    }
106
107



91
    SECTION("load2")
108
    {
109
        const char *const xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
110
            "<root><data option1=\"false\" option2=\"true\"/>"
111
1
            "<cont>this is test</cont></root>";
112
2
        XML::Document doc(xml, strlen(xml));
113



5
        REQUIRE(doc.isLoaded() == true);
114



4
        REQUIRE(doc.isValid() == true);
115




4
        REQUIRE(doc.rootNode() != nullptr);
116




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
117




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "root123") == false);
118




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
119




4
        REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false);
120




4
        REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false);
121





4
        REQUIRE(XmlHaveChildContent(doc.rootNode()) == false);
122
    }
123
124



91
    SECTION("load3")
125
    {
126
        const std::string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
127
            "<!-- comment here\nand here -->"
128
3
            "<root><data option1=\"false\" option2=\"true\"/></root>";
129
3
        XML::Document doc(xml.c_str(), xml.size());
130



5
        REQUIRE(doc.isLoaded() == true);
131



4
        REQUIRE(doc.isValid() == true);
132




4
        REQUIRE(doc.rootNode() != nullptr);
133




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
134




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "root123") == false);
135




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
136




4
        REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false);
137




4
        REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false);
138





4
        REQUIRE(XmlHaveChildContent(doc.rootNode()) == false);
139
    }
140
141



91
    SECTION("load4")
142
    {
143
        const char *const xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
144
1
            "<root>this is test</root>";
145
2
        XML::Document doc(xml, strlen(xml));
146



5
        REQUIRE(doc.isLoaded() == true);
147



4
        REQUIRE(doc.isValid() == true);
148




4
        REQUIRE(doc.rootNode() != nullptr);
149




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
150




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "root123") == false);
151




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
152




4
        REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false);
153




4
        REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false);
154





4
        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
155




3
        REQUIRE(!strcmp(XmlChildContent(doc.rootNode()), "this is test"));
156
    }
157
158



91
    SECTION("load5")
159
    {
160


8
        XML::Document doc(pathJoin(VirtFs::getRealDir(
161
            "test/serverlistplus.xml"), "test/serverlistplus.xml"),
162
            UseVirtFs_false,
163
3
            SkipError_false);
164



5
        REQUIRE(doc.isLoaded() == true);
165



4
        REQUIRE(doc.isValid() == true);
166




4
        REQUIRE(doc.rootNode() != nullptr);
167




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "serverlist") == true);
168




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "serverlist123") == false);
169




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
170




4
        REQUIRE(XmlHasProp(doc.rootNode(), "option1") == false);
171




4
        REQUIRE(XmlHasProp(doc.rootNode(), "option123") == false);
172




4
        REQUIRE(XmlHasProp(doc.rootNode(), "version") == true);
173
    }
174
175



91
    SECTION("properties")
176
    {
177
        XML::Document doc("graphics/gui/browserbox.xml",
178
            UseVirtFs_true,
179

4
            SkipError_false);
180
181
1
        XmlNodeConstPtr rootNode = doc.rootNode();
182




8
        REQUIRE(XML::getProperty(rootNode, "image", "") == "window.png");
183
    }
184
185



91
    SECTION("for each")
186
    {
187
        XML::Document doc("graphics/gui/browserbox.xml",
188
            UseVirtFs_true,
189

4
            SkipError_false);
190
191
1
        XmlNodeConstPtr rootNode = doc.rootNode();
192
//        REQUIRE(XmlHaveChildContent(rootNode) == true);
193
1
        XmlNodePtr node = XmlNodeDefault;
194
2
        for_each_xml_child_node(widgetNode, rootNode)
195
        {
196
2
            node = widgetNode;
197

2
            if (xmlNameEqual(node, "widget"))
198
                break;
199
        }
200



4
        REQUIRE(node != nullptr);
201



4
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
202




4
        REQUIRE(xmlNameEqual(node, "widget") == true);
203
//        REQUIRE(XmlHaveChildContent(node) == true);
204
2
        for_each_xml_child_node(optionNode, node)
205
        {
206
2
            node = optionNode;
207

2
            if (xmlNameEqual(node, "option"))
208
                break;
209
        }
210



4
        REQUIRE(node != nullptr);
211



4
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
212




4
        REQUIRE(xmlNameEqual(node, "option") == true);
213




4
        REQUIRE(XmlHaveChildContent(node) == false);
214




4
        REQUIRE(XmlHasProp(node, "name") == true);
215




4
        REQUIRE(XmlHasProp(node, "value") == true);
216




4
        REQUIRE(XmlHasProp(node, "option123") == false);
217




8
        REQUIRE(XML::getProperty(node, "name", "") == "padding");
218




8
        REQUIRE(XML::langProperty(node, "name", "") == "padding");
219




4
        REQUIRE(XML::getProperty(node, "value", 0) == 1);
220




4
        REQUIRE(XML::getBoolProperty(node, "value", true) == true);
221




4
        REQUIRE(XML::getBoolProperty(node, "value", false) == false);
222




4
        REQUIRE(XML::getIntProperty(node, "value", -1, -10, 100) == 1);
223
    }
224
225



91
    SECTION("child1")
226
    {
227
        XML::Document doc("graphics/gui/browserbox.xml",
228
            UseVirtFs_true,
229

4
            SkipError_false);
230
231
1
        XmlNodeConstPtr rootNode = doc.rootNode();
232
1
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "widget");
233



4
        REQUIRE(node != nullptr);
234



4
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
235




4
        REQUIRE(xmlNameEqual(node, "widget") == true);
236
//        REQUIRE(XmlHaveChildContent(node) == true);
237
1
        node = XML::findFirstChildByName(node, "option");
238



4
        REQUIRE(node != nullptr);
239



4
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
240




4
        REQUIRE(xmlNameEqual(node, "option") == true);
241




4
        REQUIRE(XmlHaveChildContent(node) == false);
242




4
        REQUIRE(XmlHasProp(node, "name") == true);
243




4
        REQUIRE(XmlHasProp(node, "value") == true);
244




4
        REQUIRE(XmlHasProp(node, "option123") == false);
245




8
        REQUIRE(XML::getProperty(node, "name", "") == "padding");
246




8
        REQUIRE(XML::langProperty(node, "name", "") == "padding");
247




8
        REQUIRE(XML::langProperty(node, "name123", "").empty());
248




4
        REQUIRE(XML::getProperty(node, "value", 0) == 1);
249




4
        REQUIRE(XML::getProperty(node, "value123", -1) == -1);
250




4
        REQUIRE(XML::getBoolProperty(node, "value", true) == true);
251




4
        REQUIRE(XML::getBoolProperty(node, "value", false) == false);
252




4
        REQUIRE(XML::getBoolProperty(node, "value123", true) == true);
253




4
        REQUIRE(XML::getIntProperty(node, "value", -1, -10, 100) == 1);
254




4
        REQUIRE(XML::getIntProperty(node, "value123", -1, -10, 100) == -1);
255
    }
256
257



91
    SECTION("child2")
258
    {
259
        const char *const xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
260
            "<root><data option1=\"false\" option2=\"true\" "
261
1
            "option3=\"10.5\"/></root>";
262
2
        XML::Document doc(xml, strlen(xml));
263
1
        XmlNodeConstPtr rootNode = doc.rootNode();
264




4
        REQUIRE(XmlHaveChildContent(rootNode) == false);
265
1
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "data");
266



4
        REQUIRE(node != nullptr);
267



4
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
268




4
        REQUIRE(xmlNameEqual(node, "data") == true);
269




4
        REQUIRE(XmlHaveChildContent(node) == false);
270




4
        REQUIRE(XmlHasProp(node, "option1") == true);
271




4
        REQUIRE(XmlHasProp(node, "option123") == false);
272




4
        REQUIRE(XML::getBoolProperty(node, "option1", true) == false);
273




4
        REQUIRE(XML::getBoolProperty(node, "option2", false) == true);
274
1
        const float opt3 = XML::getFloatProperty(node, "option3", 0.0);
275



4
        REQUIRE(opt3 > 10);
276



4
        REQUIRE(opt3 < 11);
277
    }
278
279



91
    SECTION("child3")
280
    {
281
        const std::string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
282
            "<!-- comment --><root><!-- comment -->"
283
            "<data option1=\"false\" option2=\"true\" "
284
3
            "option3=\"10.5\"/><!-- comment --></root>";
285
3
        XML::Document doc(xml.c_str(), xml.size());
286
1
        XmlNodeConstPtr rootNode = doc.rootNode();
287
//        REQUIRE(XmlHaveChildContent(rootNode) == true);
288
1
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "data");
289



4
        REQUIRE(node != nullptr);
290



4
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
291




4
        REQUIRE(xmlNameEqual(node, "data") == true);
292




4
        REQUIRE(XmlHaveChildContent(node) == false);
293




4
        REQUIRE(XmlHasProp(node, "option1") == true);
294




4
        REQUIRE(XmlHasProp(node, "option123") == false);
295




4
        REQUIRE(XML::getBoolProperty(node, "option1", true) == false);
296




4
        REQUIRE(XML::getBoolProperty(node, "option2", false) == true);
297
1
        const float opt3 = XML::getFloatProperty(node, "option3", 0.0);
298



4
        REQUIRE(opt3 > 10);
299



4
        REQUIRE(opt3 < 11);
300
    }
301
302



91
    SECTION("validate")
303
    {
304
//        REQUIRE(XML::Document::validateXml(
305
//            "graphics/gui/browserbox.xml") == true);
306




7
        REQUIRE(XML::Document::validateXml(
307
            "graphics/gui/bubble.png") == false);
308




7
        REQUIRE(XML::Document::validateXml(
309
            "graphics/gui/testfile123.xml") == false);
310
    }
311
312



91
    SECTION("save1")
313
    {
314
        // clean
315
1
        ::remove(tempXmlName);
316
317
        // save
318
1
        FILE *const testFile = fopen(tempXmlName, "w");
319



3
        REQUIRE(testFile);
320
1
        fclose(testFile);
321
1
        XmlTextWriterPtr writer = XmlNewTextWriterFilename(
322
            tempXmlName,
323
            0);
324
1
        XmlTextWriterSetIndent(writer, 1);
325
1
        XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
326
1
        XmlTextWriterStartRootElement(writer, "root");
327
1
        XmlTextWriterEndDocument(writer);
328
        XmlSaveTextWriterFilename(writer, tempXmlName);
329
1
        XmlFreeTextWriter(writer);
330
331
        // load
332
        XML::Document doc(tempXmlName,
333
            UseVirtFs_false,
334

4
            SkipError_false);
335



5
        REQUIRE(doc.isLoaded() == true);
336



4
        REQUIRE(doc.isValid() == true);
337




4
        REQUIRE(doc.rootNode() != nullptr);
338




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
339




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false);
340




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
341
//        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
342
343
        // clean again
344
1
        ::remove(tempXmlName);
345
    }
346
347



91
    SECTION("save2")
348
    {
349
        // clean
350
1
        ::remove(tempXmlName);
351
352
        // save
353
1
        FILE *const testFile = fopen(tempXmlName, "w");
354



3
        REQUIRE(testFile);
355
1
        fclose(testFile);
356
1
        XmlTextWriterPtr writer = XmlNewTextWriterFilename(
357
            tempXmlName,
358
            0);
359
1
        XmlTextWriterSetIndent(writer, 1);
360
1
        XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
361
1
        XmlTextWriterStartRootElement(writer, "root");
362
363
1
        XmlTextWriterStartElement(writer, "option");
364
1
        XmlTextWriterWriteAttribute(writer, "name", "the name");
365
1
        XmlTextWriterWriteAttribute(writer, "value", "the value");
366
1
        XmlTextWriterEndElement(writer);
367
368
1
        XmlTextWriterEndDocument(writer);
369
        XmlSaveTextWriterFilename(writer, tempXmlName);
370
1
        XmlFreeTextWriter(writer);
371
372
        // load
373
        XML::Document doc(tempXmlName,
374
            UseVirtFs_false,
375

4
            SkipError_false);
376



5
        REQUIRE(doc.isLoaded() == true);
377



4
        REQUIRE(doc.isValid() == true);
378




4
        REQUIRE(doc.rootNode() != nullptr);
379




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "root") == true);
380




4
        REQUIRE(xmlNameEqual(doc.rootNode(), "skinset123") == false);
381




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
382
//        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
383

1
        XmlNodePtr node = XML::findFirstChildByName(doc.rootNode(), "option");
384


3
        REQUIRE(node != nullptr);
385



4
        REQUIRE(xmlTypeEqual(node, XML_ELEMENT_NODE) == true);
386




4
        REQUIRE(xmlNameEqual(node, "option") == true);
387




4
        REQUIRE(XmlHaveChildContent(node) == false);
388




4
        REQUIRE(XmlHasProp(node, "name") == true);
389




4
        REQUIRE(XmlHasProp(node, "value") == true);
390




4
        REQUIRE(XmlHasProp(node, "option123") == false);
391




8
        REQUIRE(XML::getProperty(node, "name", "") == "the name");
392




8
        REQUIRE(XML::getProperty(node, "value", "") == "the value");
393
394
        // clean again
395
1
        ::remove(tempXmlName);
396
    }
397
398
13
    delete2(theme);
399
13
    delete2(client);
400
13
    ResourceManager::deleteInstance();
401
39
    VirtFs::unmountDirSilent("data");
402
39
    VirtFs::unmountDirSilent("../data");
403
13
}
404
405
3
TEST_CASE("xml test2", "")
406
{
407



4
    REQUIRE(gui == nullptr);
408
1
    ResourceManager::cleanOrphans(true);
409
1
    ResourceManager::deleteInstance();
410

4
}