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 OString
& rXPath
);
52 * Same as the assertXPath(), but don't assert: return the string instead.
54 OUString
getXPath(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, const OString
& rAttribute
);
56 * Same as the assertXPathContent(), but don't assert: return the string instead.
58 OUString
getXPathContent(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
);
60 * Get the position of the child named rName of the parent node specified by rXPath.
61 * Useful for checking relative order of elements.
63 int getXPathPosition(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, std::string_view rChildName
);
65 * Get the number of the nodes returned by the rXPath.
67 int countXPathNodes(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
);
69 * Assert that rXPath exists, returns exactly one node, and the rXPath's attribute's value
70 * equals to the rExpected value.
72 void assertXPath(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, const OString
& rAttribute
,
73 const OUString
& rExpectedValue
);
74 void assertXPathAttrs(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
,
75 const std::vector
<std::pair
<OString
, OUString
>>& aPairVector
);
78 * Given a double for the rExpected value, assert that rXPath exists, returns exactly one node,
79 * and the rXPath's attribute's value matches the rExpected value within tolerance provided
80 * by delta. This is used to account for HiDPI scaling.
82 void assertXPathDoubleValue(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, const OString
& rAttribute
,
83 double expectedValue
, double delta
);
85 * Assert that rXPath exists, and returns exactly nNumberOfNodes nodes (1 by default).
86 * Also useful for checking that we do _not_ export some node (nNumberOfNodes == 0).
88 void assertXPath(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, int nNumberOfNodes
= 1);
90 * Assert that rXPath exists, and its content equals rContent.
92 void assertXPathContent(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, const OUString
& rContent
);
94 * Assert that rXPath exists and it has an rNSPrefix=rNSHref namespace definition.
96 void assertXPathNSDef(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, std::u16string_view rNSPrefix
,
97 std::u16string_view rNSHref
);
99 * Assert that rXPath exists, and has exactly nNumberOfChildNodes child nodes.
100 * Useful for checking that we do have a no child nodes to a specific node (nNumberOfChildNodes == 0).
102 void assertXPathChildren(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, int nNumberOfChildNodes
);
104 * Assert that rXPath exists, has exactly 1 result set nodes and does *not* have an attribute named rAttribute.
106 void assertXPathNoAttribute(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, const OString
& rAttribute
);
107 // Assert that the node name of the single node returned by an XPath is as specified,
108 // e.g. to check order of elements, where getXPathPosition is unapplicable
109 void assertXPathNodeName(const xmlDocUniquePtr
& pXmlDoc
, const OString
& rXPath
, const OString
& rExpectedName
);
111 static void registerODFNamespaces(xmlXPathContextPtr
& pXmlXpathCtx
);
112 static void registerOOXMLNamespaces(xmlXPathContextPtr
& pXmlXpathCtx
);
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */