1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef INCLUDED_TEST_XMLTESTTOOLS_HXX
11 #define INCLUDED_TEST_XMLTESTTOOLS_HXX
13 #include <sal/config.h>
14 #include <test/testdllapi.hxx>
16 #include <libxml/xmlwriter.h>
17 #include <libxml/tree.h>
18 #include <libxml/xpath.h>
19 #include <libxml/xpathInternals.h>
20 #include <libxml/parserInternals.h>
22 #include <rtl/string.hxx>
23 #include <rtl/ustring.hxx>
24 #include <unotools/tempfile.hxx>
25 #include <vcl/mtfxmldump.hxx>
26 #include <test/xmldocptr.hxx>
28 #include <cppunit/TestAssert.h>
30 #include <string_view>
33 class OOO_DLLPUBLIC_TEST XmlTestTools
36 /// Return xmlDocPtr representation of the XML stream read from pStream.
37 static xmlDocUniquePtr
parseXmlStream(SvStream
* pStream
);
39 static xmlDocUniquePtr
dumpAndParse(MetafileXmlDump
& rDumper
, const GDIMetaFile
& rGDIMetaFile
);
43 virtual ~XmlTestTools();
45 static xmlDocUniquePtr
parseXml(utl::TempFileNamed
const & aTempFile
);
47 virtual void registerNamespaces(xmlXPathContextPtr
& pXmlXpathCtx
);
49 // Caller must call xmlXPathFreeObject:
50 xmlXPathObjectPtr
getXPathNode(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
);
52 * Same as the assertXPath(), but don't assert: return the string instead.
54 OUString
getXPath(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, const char* pAttribute
);
55 OUString
getXPath(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
, const char* pAttribute
)
57 return getXPath(pXmlDoc
, sXPath
.getStr(), pAttribute
);
60 * Same as the assertXPathContent(), but don't assert: return the string instead.
62 OUString
getXPathContent(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
);
63 OUString
getXPathContent(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
)
65 return getXPathContent(pXmlDoc
, sXPath
.getStr());
68 * Get the position of the child named rName of the parent node specified by pXPath.
69 * Useful for checking relative order of elements.
71 int getXPathPosition(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, const char* pChildName
);
73 * Get the number of the nodes returned by the pXPath.
75 int countXPathNodes(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
);
76 int countXPathNodes(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
)
78 return countXPathNodes(pXmlDoc
, sXPath
.getStr());
81 * Assert that pXPath exists, returns exactly one node, and the pXPath's attribute's value
82 * equals to the rExpected value.
84 void assertXPath(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, const char* pAttribute
,
85 std::u16string_view rExpectedValue
);
86 void assertXPath(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
, const char* pAttribute
,
87 std::u16string_view rExpectedValue
)
89 assertXPath(pXmlDoc
, sXPath
.getStr(), pAttribute
, rExpectedValue
);
91 void assertXPathAttrs(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
,
92 const std::vector
<std::pair
<const char*, std::u16string_view
>>& aPairVector
);
93 void assertXPathAttrs(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
,
94 const std::vector
<std::pair
<const char*, std::u16string_view
>>& aPairVector
)
96 assertXPathAttrs(pXmlDoc
, sXPath
.getStr(), aPairVector
);
100 * Given a double for the rExpected value, assert that pXPath exists, returns exactly one node,
101 * and the pXPath's attribute's value matches the rExpected value within tolerance provided
102 * by delta. This is used to account for HiDPI scaling.
104 void assertXPathDoubleValue(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, const char* pAttribute
,
105 double expectedValue
, double delta
);
107 * Assert that pXPath exists, and returns exactly nNumberOfNodes nodes (1 by default).
108 * Also useful for checking that we do _not_ export some node (nNumberOfNodes == 0).
110 void assertXPath(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, int nNumberOfNodes
= 1);
111 void assertXPath(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
, int nNumberOfNodes
= 1)
113 assertXPath(pXmlDoc
, sXPath
.getStr(), nNumberOfNodes
);
116 * Assert that pXPath exists, and its content equals rContent.
118 void assertXPathContent(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, std::u16string_view rContent
);
119 void assertXPathContent(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
,
120 std::u16string_view rContent
)
122 assertXPathContent(pXmlDoc
, sXPath
.getStr(), rContent
);
125 * Assert that pXPath exists and it has an rNSPrefix=rNSHref namespace definition.
127 void assertXPathNSDef(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, std::string_view rNSPrefix
,
128 std::string_view rNSHref
);
130 * Assert that pXPath exists, and has exactly nNumberOfChildNodes child nodes.
131 * Useful for checking that we do have a no child nodes to a specific node (nNumberOfChildNodes == 0).
133 void assertXPathChildren(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, int nNumberOfChildNodes
);
134 void assertXPathChildren(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
,
135 int nNumberOfChildNodes
)
137 assertXPathChildren(pXmlDoc
, sXPath
.getStr(), nNumberOfChildNodes
);
140 * Assert that pXPath exists, has exactly 1 result set nodes and does *not* have an attribute named pAttribute.
142 void assertXPathNoAttribute(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, const char* pAttribute
);
143 void assertXPathNoAttribute(const xmlDocUniquePtr
& pXmlDoc
, const OString
& sXPath
,
144 const char* pAttribute
)
146 assertXPathNoAttribute(pXmlDoc
, sXPath
.getStr(), pAttribute
);
149 * Same as the assertXPathNoAttribute(), but don't assert: return the bool instead.
151 bool hasXPathAttribute(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, const char* pAttribute
);
152 // Assert that the node name of the single node returned by an XPath is as specified,
153 // e.g. to check order of elements, where getXPathPosition is unapplicable
154 void assertXPathNodeName(const xmlDocUniquePtr
& pXmlDoc
, const char* pXPath
, std::string_view rExpectedName
);
156 static void registerODFNamespaces(xmlXPathContextPtr
& pXmlXpathCtx
);
157 static void registerOOXMLNamespaces(xmlXPathContextPtr
& pXmlXpathCtx
);
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */