GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/xml.cc Lines: 225 225 100.0 %
Date: 2017-11-29 Branches: 883 2228 39.6 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-2017  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
6
TEST_CASE("xml test1", "")
51
{
52



8
    REQUIRE(client == nullptr);
53



8
    REQUIRE(gui == nullptr);
54
2
    ResourceManager::cleanOrphans(true);
55
2
    ResourceManager::deleteInstance();
56
2
}
57
58
28
TEST_CASE("xml doc", "")
59
{
60
24
    setEnv("SDL_VIDEODRIVER", "dummy");
61
62
24
    client = new Client;
63
24
    XML::initXML();
64

96
    VirtFs::mountDirSilent("data", Append_false);
65

96
    VirtFs::mountDirSilent("../data", Append_false);
66
24
    mainGraphics = new SDLGraphics;
67
48
    imageHelper = new SDLImageHelper();
68
69
24
    Dirs::initRootDir();
70
24
    Dirs::initHomeDir();
71
72
24
    ConfigManager::initConfiguration();
73
24
    setConfigDefaults2(config);
74
24
    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
24
    graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
83
#endif  // USE_SDL2
84
85
24
    theme = new Theme;
86
24
    Theme::selectSkin();
87
88
24
    const char *const tempXmlName = "tempxml.xml";
89
24
    ActorSprite::load();
90
24
    gui = new Gui();
91
24
    gui->postInit(mainGraphics);
92
93



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

10
            SkipError_false);
98



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



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




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




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




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




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



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



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



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




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




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




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




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




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




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





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



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



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



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




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




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




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




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




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




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





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



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



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



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




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




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




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




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




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




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





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




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



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

10
            SkipError_false);
163
164
2
        XmlNodeConstPtr rootNode = doc.rootNode();
165




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



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

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

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



8
        REQUIRE(node != nullptr);
184



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




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

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



8
        REQUIRE(node != nullptr);
194



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




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




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




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




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




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




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




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




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




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




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




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



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

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



8
        REQUIRE(node != nullptr);
217



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




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



8
        REQUIRE(node != nullptr);
222



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




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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



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




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



8
        REQUIRE(node != nullptr);
250



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




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




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




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




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




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




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



8
        REQUIRE(opt3 > 10);
259



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



168
    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
8
            "option3=\"10.5\"/><!-- comment --></root>";
268
6
        XML::Document doc(xml.c_str(), xml.size());
269
2
        XmlNodeConstPtr rootNode = doc.rootNode();
270
//        REQUIRE(XmlHaveChildContent(rootNode) == true);
271
2
        XmlNodePtr node = XML::findFirstChildByName(rootNode, "data");
272



8
        REQUIRE(node != nullptr);
273



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




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




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




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




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




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




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



8
        REQUIRE(opt3 > 10);
282



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



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




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




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



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



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

10
            SkipError_false);
318



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



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




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




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




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




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



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



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

10
            SkipError_false);
359



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



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




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




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




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




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

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



8
        REQUIRE(node != nullptr);
368



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




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




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




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




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




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




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




16
        REQUIRE(XML::getProperty(node, "value", "") == "the value");
376
377
        // clean again
378
2
        ::remove(tempXmlName);
379
    }
380
381
24
    delete2(theme);
382
24
    delete2(client);
383
24
    ResourceManager::deleteInstance();
384

96
    VirtFs::unmountDirSilent("data");
385

96
    VirtFs::unmountDirSilent("../data");
386
24
}
387
388
6
TEST_CASE("xml test2", "")
389
{
390



8
    REQUIRE(gui == nullptr);
391
2
    ResourceManager::cleanOrphans(true);
392
2
    ResourceManager::deleteInstance();
393

8
}