GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/xml.cc Lines: 225 225 100.0 %
Date: 2018-07-14 Branches: 879 2220 39.6 %

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
14
TEST_CASE("xml doc", "")
59
{
60
12
    setEnv("SDL_VIDEODRIVER", "dummy");
61
62
12
    client = new Client;
63
12
    XML::initXML();
64
36
    VirtFs::mountDirSilent("data", Append_false);
65
36
    VirtFs::mountDirSilent("../data", Append_false);
66
12
    mainGraphics = new SDLGraphics;
67
24
    imageHelper = new SDLImageHelper();
68
69
12
    Dirs::initRootDir();
70
12
    Dirs::initHomeDir();
71
72
12
    ConfigManager::initConfiguration();
73
12
    setConfigDefaults2(config);
74
12
    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
12
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
83
#endif  // USE_SDL2
84
85
12
    theme = new Theme;
86
12
    Theme::selectSkin();
87
88
12
    const char *const tempXmlName = "tempxml.xml";
89
12
    ActorSprite::load();
90
12
    gui = new Gui();
91
12
    gui->postInit(mainGraphics);
92
93



84
    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



84
    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



84
    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



84
    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



84
    SECTION("properties")
159
    {
160
        XML::Document doc("graphics/gui/browserbox.xml",
161
            UseVirtFs_true,
162

4
            SkipError_false);
163
164
1
        XmlNodeConstPtr rootNode = doc.rootNode();
165




8
        REQUIRE(XML::getProperty(rootNode, "image", "") == "window.png");
166
    }
167
168



84
    SECTION("for each")
169
    {
170
        XML::Document doc("graphics/gui/browserbox.xml",
171
            UseVirtFs_true,
172

4
            SkipError_false);
173
174
1
        XmlNodeConstPtr rootNode = doc.rootNode();
175
//        REQUIRE(XmlHaveChildContent(rootNode) == true);
176
1
        XmlNodePtr node = XmlNodeDefault;
177
2
        for_each_xml_child_node(widgetNode, rootNode)
178
        {
179
2
            node = widgetNode;
180

2
            if (xmlNameEqual(node, "widget"))
181
                break;
182
        }
183



4
        REQUIRE(node != nullptr);
184



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




4
        REQUIRE(xmlNameEqual(node, "widget") == true);
186
//        REQUIRE(XmlHaveChildContent(node) == true);
187
2
        for_each_xml_child_node(optionNode, node)
188
        {
189
2
            node = optionNode;
190

2
            if (xmlNameEqual(node, "option"))
191
                break;
192
        }
193



4
        REQUIRE(node != nullptr);
194



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




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




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




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




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




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




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




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




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




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




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




4
        REQUIRE(XML::getIntProperty(node, "value", -1, -10, 100) == 1);
206
    }
207
208



84
    SECTION("child1")
209
    {
210
        XML::Document doc("graphics/gui/browserbox.xml",
211
            UseVirtFs_true,
212

4
            SkipError_false);
213
214
1
        XmlNodeConstPtr rootNode = doc.rootNode();
215
1
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "widget");
216



4
        REQUIRE(node != nullptr);
217



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




4
        REQUIRE(xmlNameEqual(node, "widget") == true);
219
//        REQUIRE(XmlHaveChildContent(node) == true);
220
1
        node = XML::findFirstChildByName(node, "option");
221



4
        REQUIRE(node != nullptr);
222



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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




4
        REQUIRE(XML::getIntProperty(node, "value123", -1, -10, 100) == -1);
238
    }
239
240



84
    SECTION("child2")
241
    {
242
        const char *const xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
243
            "<root><data option1=\"false\" option2=\"true\" "
244
1
            "option3=\"10.5\"/></root>";
245
2
        XML::Document doc(xml, strlen(xml));
246
1
        XmlNodeConstPtr rootNode = doc.rootNode();
247




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



4
        REQUIRE(node != nullptr);
250



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




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




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




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




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




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




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



4
        REQUIRE(opt3 > 10);
259



4
        REQUIRE(opt3 < 11);
260
    }
261
262



84
    SECTION("child3")
263
    {
264
        const std::string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
265
            "<!-- comment --><root><!-- comment -->"
266
            "<data option1=\"false\" option2=\"true\" "
267
3
            "option3=\"10.5\"/><!-- comment --></root>";
268
3
        XML::Document doc(xml.c_str(), xml.size());
269
1
        XmlNodeConstPtr rootNode = doc.rootNode();
270
//        REQUIRE(XmlHaveChildContent(rootNode) == true);
271
1
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "data");
272



4
        REQUIRE(node != nullptr);
273



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




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




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




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




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




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




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



4
        REQUIRE(opt3 > 10);
282



4
        REQUIRE(opt3 < 11);
283
    }
284
285



84
    SECTION("validate")
286
    {
287
//        REQUIRE(XML::Document::validateXml(
288
//            "graphics/gui/browserbox.xml") == true);
289




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




7
        REQUIRE(XML::Document::validateXml(
292
            "graphics/gui/testfile123.xml") == false);
293
    }
294
295



84
    SECTION("save1")
296
    {
297
        // clean
298
1
        ::remove(tempXmlName);
299
300
        // save
301
1
        FILE *const testFile = fopen(tempXmlName, "w");
302



3
        REQUIRE(testFile);
303
1
        fclose(testFile);
304
1
        XmlTextWriterPtr writer = XmlNewTextWriterFilename(
305
            tempXmlName,
306
            0);
307
1
        XmlTextWriterSetIndent(writer, 1);
308
1
        XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
309
1
        XmlTextWriterStartRootElement(writer, "root");
310
1
        XmlTextWriterEndDocument(writer);
311
        XmlSaveTextWriterFilename(writer, tempXmlName);
312
1
        XmlFreeTextWriter(writer);
313
314
        // load
315
        XML::Document doc(tempXmlName,
316
            UseVirtFs_false,
317

4
            SkipError_false);
318



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



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




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




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




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




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
324
//        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
325
326
        // clean again
327
1
        ::remove(tempXmlName);
328
    }
329
330



84
    SECTION("save2")
331
    {
332
        // clean
333
1
        ::remove(tempXmlName);
334
335
        // save
336
1
        FILE *const testFile = fopen(tempXmlName, "w");
337



3
        REQUIRE(testFile);
338
1
        fclose(testFile);
339
1
        XmlTextWriterPtr writer = XmlNewTextWriterFilename(
340
            tempXmlName,
341
            0);
342
1
        XmlTextWriterSetIndent(writer, 1);
343
1
        XmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
344
1
        XmlTextWriterStartRootElement(writer, "root");
345
346
1
        XmlTextWriterStartElement(writer, "option");
347
1
        XmlTextWriterWriteAttribute(writer, "name", "the name");
348
1
        XmlTextWriterWriteAttribute(writer, "value", "the value");
349
1
        XmlTextWriterEndElement(writer);
350
351
1
        XmlTextWriterEndDocument(writer);
352
        XmlSaveTextWriterFilename(writer, tempXmlName);
353
1
        XmlFreeTextWriter(writer);
354
355
        // load
356
        XML::Document doc(tempXmlName,
357
            UseVirtFs_false,
358

4
            SkipError_false);
359



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



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




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




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




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




4
        REQUIRE(xmlTypeEqual(doc.rootNode(), XML_ELEMENT_NODE) == true);
365
//        REQUIRE(XmlHaveChildContent(doc.rootNode()) == true);
366

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



4
        REQUIRE(node != nullptr);
368



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




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




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




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




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




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




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




8
        REQUIRE(XML::getProperty(node, "value", "") == "the value");
376
377
        // clean again
378
1
        ::remove(tempXmlName);
379
    }
380
381
12
    delete2(theme);
382
12
    delete2(client);
383
12
    ResourceManager::deleteInstance();
384
36
    VirtFs::unmountDirSilent("data");
385
36
    VirtFs::unmountDirSilent("../data");
386
12
}
387
388
3
TEST_CASE("xml test2", "")
389
{
390



4
    REQUIRE(gui == nullptr);
391
1
    ResourceManager::cleanOrphans(true);
392
1
    ResourceManager::deleteInstance();
393

4
}