1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* This file is part of the libvisio project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <libvisio/libvisio.h>
#include <libxml/xpath.h>
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
#include "xmldrawinggenerator.h"
namespace librevenge
{
/// Allows using CPPUNIT_ASSERT_EQUAL() on librevenge::RVNGString instances.
std::ostream &operator <<(std::ostream &s, const RVNGString &string)
{
return s << string.cstr();
}
}
namespace
{
/// Caller must call xmlXPathFreeObject.
xmlXPathObjectPtr getXPathNode(xmlDocPtr doc, const librevenge::RVNGString &xpath)
{
xmlXPathContextPtr xpathContext = xmlXPathNewContext(doc);
xmlXPathObjectPtr xpathObject = xmlXPathEvalExpression(BAD_CAST(xpath.cstr()), xpathContext);
xmlXPathFreeContext(xpathContext);
return xpathObject;
}
/// Same as the assertXPath(), but don't assert: return the string instead.
librevenge::RVNGString getXPath(xmlDocPtr doc, const librevenge::RVNGString &xpath, const librevenge::RVNGString &attribute)
{
xmlXPathObjectPtr xpathobject = getXPathNode(doc, xpath);
xmlNodeSetPtr nodeset = xpathobject->nodesetval;
librevenge::RVNGString message("XPath '");
message.append(xpath);
message.append("': number of nodes is incorrect.");
CPPUNIT_ASSERT_EQUAL_MESSAGE(message.cstr(), 1, xmlXPathNodeSetGetLength(nodeset));
if (attribute.empty())
return librevenge::RVNGString();
xmlNodePtr node = nodeset->nodeTab[0];
xmlChar *prop = xmlGetProp(node, BAD_CAST(attribute.cstr()));
librevenge::RVNGString s((const char *)prop);
xmlFree(prop);
xmlXPathFreeObject(xpathobject);
return s;
}
/**
* Assert that xpath exists, and xpath returns exactly one node.
* xpath's attribute's value must equal to the expectedValue value.
*/
void assertXPath(xmlDocPtr doc, const librevenge::RVNGString &xpath, const librevenge::RVNGString &attribute, const librevenge::RVNGString &expectedValue)
{
librevenge::RVNGString actualValue = getXPath(doc, xpath, attribute);
librevenge::RVNGString message("Attribute '");
message.append(attribute);
message.append("' of '");
message.append(xpath);
message.append("': incorrect value.");
CPPUNIT_ASSERT_EQUAL_MESSAGE(message.cstr(), expectedValue, actualValue);
}
/// Assert that xpath exists, and does not contain the given attribute
void assertXPathNoAttribute(xmlDocPtr doc, const librevenge::RVNGString &xpath, const librevenge::RVNGString &attribute)
{
xmlXPathObjectPtr xpathobject = getXPathNode(doc, xpath);
xmlNodeSetPtr nodeset = xpathobject->nodesetval;
librevenge::RVNGString message1("In <");
message1.append(doc->name);
message1.append(">, XPath '");
message1.append(xpath);
message1.append("' number of nodes is incorrect");
CPPUNIT_ASSERT_EQUAL_MESSAGE(message1.cstr(), 1, xmlXPathNodeSetGetLength(nodeset));
xmlNodePtr node = nodeset->nodeTab[0];
librevenge::RVNGString message2("In <");
message2.append(doc->name);
message2.append(">, XPath '");
message2.append(xpath);
message2.append("' unexpected '");
message2.append(attribute);
message2.append("' attribute");
CPPUNIT_ASSERT_EQUAL_MESSAGE(message2.cstr(), static_cast<xmlChar *>(0), xmlGetProp(node, BAD_CAST(attribute.cstr())));
xmlXPathFreeObject(xpathobject);
}
#if 0 // keep for future use
/// Same as the assertXPathContent(), but don't assert: return the string instead.
librevenge::RVNGString getXPathContent(xmlDocPtr doc, const librevenge::RVNGString &xpath)
{
xmlXPathObjectPtr xpathObject = getXPathNode(doc, xpath);
xmlNodeSetPtr nodeset = xpathObject->nodesetval;
librevenge::RVNGString message("XPath '");
message.append(xpath);
message.append("': not found.");
CPPUNIT_ASSERT_MESSAGE(message.cstr(), xmlXPathNodeSetGetLength(nodeset) > 0);
xmlNodePtr node = nodeset->nodeTab[0];
librevenge::RVNGString s(reinterpret_cast<char *>((node->children[0]).content));
xmlXPathFreeObject(xpathObject);
return s;
}
/// Assert that xpath exists, and its content equals to content.
void assertXPathContent(xmlDocPtr doc, const librevenge::RVNGString &xpath, const librevenge::RVNGString &content)
{
librevenge::RVNGString message("XPath '");
message.append(xpath);
message.append("': contents of child does not match.");
CPPUNIT_ASSERT_EQUAL_MESSAGE(message.cstr(), content, getXPathContent(doc, xpath));
}
#endif
/// Paints an XML representation of filename into buffer, then returns the parsed buffer content.
xmlDocPtr parse(const char *filename, xmlBufferPtr buffer)
{
librevenge::RVNGString path(TDOC "/");
path.append(filename);
librevenge::RVNGFileStream input(path.cstr());
CPPUNIT_ASSERT(libvisio::VisioDocument::isSupported(&input));
xmlTextWriterPtr writer = xmlNewTextWriterMemory(buffer, 0);
CPPUNIT_ASSERT(writer);
xmlTextWriterStartDocument(writer, 0, 0, 0);
libvisio::XmlDrawingGenerator painter(writer);
CPPUNIT_ASSERT(libvisio::VisioDocument::parse(&input, &painter));
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
//std::cerr << "XML is '" << (const char *)xmlBufferContent(buffer) << "'" << std::endl;
return xmlParseMemory((const char *)xmlBufferContent(buffer), xmlBufferLength(buffer));
}
}
class ImportTest : public CPPUNIT_NS::TestFixture
{
// disable copying
ImportTest(const ImportTest &);
ImportTest &operator=(const ImportTest &);
CPPUNIT_TEST_SUITE(ImportTest);
CPPUNIT_TEST(testVsdxMetadataTitle);
CPPUNIT_TEST(testVsdMetadataTitleMs1252);
CPPUNIT_TEST(testVsdMetadataTitleUtf8);
CPPUNIT_TEST(testVsdUserDefinedMetadata);
CPPUNIT_TEST(testVsdxUserDefinedMetadata);
CPPUNIT_TEST(testVsdxImportBgColorFromTheme);
#if LIBXML_VERSION >= 20902
CPPUNIT_TEST(testVsdxCharBgColor);
#endif
CPPUNIT_TEST_SUITE_END();
void testVsdxMetadataTitle();
void testVsdMetadataTitleMs1252();
void testVsdMetadataTitleUtf8();
void testVsdUserDefinedMetadata();
void testVsdxUserDefinedMetadata();
void testVsdxImportBgColorFromTheme();
void testVsdxCharBgColor();
xmlBufferPtr m_buffer;
xmlDocPtr m_doc;
public:
ImportTest();
virtual void setUp();
virtual void tearDown();
};
ImportTest::ImportTest()
: m_buffer(0),
m_doc(0)
{
}
void ImportTest::setUp()
{
CPPUNIT_ASSERT(!m_buffer);
m_buffer = xmlBufferCreate();
CPPUNIT_ASSERT(m_buffer);
CPPUNIT_ASSERT(!m_doc);
}
void ImportTest::tearDown()
{
xmlFreeDoc(m_doc);
m_doc = 0;
xmlBufferFree(m_buffer);
m_buffer = 0;
}
void ImportTest::testVsdxMetadataTitle()
{
m_doc = parse("fdo86664.vsdx", m_buffer);
// The setDocumentMetaData() call was missing, so the node did not exist.
assertXPath(m_doc, "/document/setDocumentMetaData", "title", "mytitle");
assertXPath(m_doc, "/document/setDocumentMetaData", "subject", "mysubject");
assertXPath(m_doc, "/document/setDocumentMetaData", "initial-creator", "vmiklos creator");
// Test <dcterms:created> and <dcterms:modified>.
assertXPath(m_doc, "/document/setDocumentMetaData", "creation-date", "2014-11-24T10:35:17Z");
assertXPath(m_doc, "/document/setDocumentMetaData", "date", "2014-11-24T10:41:22Z");
assertXPath(m_doc, "/document/setDocumentMetaData", "keyword", "mytag");
assertXPath(m_doc, "/document/setDocumentMetaData", "description", "mycomment");
assertXPath(m_doc, "/document/setDocumentMetaData", "creator", "vmiklos modifier");
assertXPath(m_doc, "/document/setDocumentMetaData", "category", "mycategory");
}
void ImportTest::testVsdMetadataTitleMs1252()
{
m_doc = parse("fdo86729-ms1252.vsd", m_buffer);
// Test windows-1252 -> UTF-8 conversion, provided by ICU.
assertXPath(m_doc, "/document/setDocumentMetaData", "title", "mytitle\xC3\xA9\xC3\xA1");
assertXPath(m_doc, "/document/setDocumentMetaData", "subject", "mysubject");
assertXPath(m_doc, "/document/setDocumentMetaData", "initial-creator", "vmiklos creator");
// There is only one author/last-modifier field in the file, so make sure creator is the same as initial-creator.
assertXPath(m_doc, "/document/setDocumentMetaData", "creator", "vmiklos creator");
assertXPath(m_doc, "/document/setDocumentMetaData", "keyword", "mytag");
assertXPath(m_doc, "/document/setDocumentMetaData", "description", "mycomment");
}
void ImportTest::testVsdMetadataTitleUtf8()
{
m_doc = parse("fdo86729-utf8.vsd", m_buffer);
// Test the case when the string is UTF-8 encoded already in the file.
assertXPath(m_doc, "/document/setDocumentMetaData", "title", "mytitle\xC3\xA9\xC3\xA1\xC5\x91\xC5\xB1");
// Test <dcterms:created> and <dcterms:modified>.
assertXPath(m_doc, "/document/setDocumentMetaData", "creation-date", "2014-11-26T09:24:56Z");
assertXPath(m_doc, "/document/setDocumentMetaData", "date", "2014-11-26T09:24:56Z");
}
void ImportTest::testVsdUserDefinedMetadata()
{
m_doc = parse("dwg.vsd", m_buffer);
assertXPath(m_doc, "/document/setDocumentMetaData", "category", "Category test");
assertXPath(m_doc, "/document/setDocumentMetaData", "company", "Company test");
assertXPath(m_doc, "/document/setDocumentMetaData", "template", "BASICD_M.VSTX");
}
void ImportTest::testVsdxUserDefinedMetadata()
{
m_doc = parse("dwg.vsdx", m_buffer);
assertXPath(m_doc, "/document/setDocumentMetaData", "category", "Category test");
assertXPath(m_doc, "/document/setDocumentMetaData", "company", "Company test");
assertXPath(m_doc, "/document/setDocumentMetaData", "language", "en-US");
assertXPath(m_doc, "/document/setDocumentMetaData", "template", "BASICD_M.VSTX");
}
void ImportTest::testVsdxImportBgColorFromTheme()
{
m_doc = parse("color-boxes.vsdx", m_buffer);
assertXPath(m_doc, "/document/page/layer[1]//setStyle[2]", "fill-color", "#759fcc");
assertXPath(m_doc, "/document/page/layer[2]//setStyle[2]", "fill-color", "#70ad47");
assertXPath(m_doc, "/document/page/layer[3]//setStyle[2]", "fill-color", "#fec000");
assertXPath(m_doc, "/document/page/layer[4]//setStyle[2]", "fill-color", "#41719c");
assertXPath(m_doc, "/document/page/layer[5]//setStyle[2]", "fill-color", "#ed7d31");
assertXPath(m_doc, "/document/page/layer[6]//setStyle[2]", "fill-color", "#bdd0e9");
assertXPath(m_doc, "/document/page/layer[7]//setStyle[2]", "fill-color", "#5b9bd5");
}
void ImportTest::testVsdxCharBgColor()
{
m_doc = parse("bgcolor.vsdx", m_buffer);
assertXPathNoAttribute(m_doc, "/document/page/layer[1]/textObject/paragraph/span", "background-color");
assertXPath(m_doc, "/document/page/layer[2]/textObject/paragraph/span", "background-color", "#9dbb61");
assertXPath(m_doc, "/document/page/layer[3]/textObject/paragraph/span", "background-color", "#9dbb61");
}
CPPUNIT_TEST_SUITE_REGISTRATION(ImportTest);
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|