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_SW_QA_INC_SWMODELTESTBASE_HXX
11 #define INCLUDED_SW_QA_INC_SWMODELTESTBASE_HXX
14 #include <string_view>
16 #include <com/sun/star/beans/XPropertySet.hpp>
17 #include <com/sun/star/container/XNameAccess.hpp>
18 #include <com/sun/star/drawing/XShape.hpp>
19 #include <com/sun/star/style/XAutoStyleFamily.hpp>
20 #include <com/sun/star/text/XTextRange.hpp>
21 #include <com/sun/star/table/XCell.hpp>
22 #include <com/sun/star/table/BorderLine2.hpp>
23 #include <com/sun/star/xml/AttributeData.hpp>
25 #include "swqahelperdllapi.h"
26 #include <test/unoapixml_test.hxx>
27 #include <unotools/tempfile.hxx>
32 * Macro to declare a new test (with full round-trip. To test
33 * import only use the CPPUNIT_TEST_FIXTURE macro directly).
34 * In order to add a new test, one only needs to use this macro
35 * and then specify the test content, like this:
37 * DECLARE_SW_ROUNDTRIP_TEST(MyTest, "myfilename.docx", Test)
39 * CPPUNIT_ASSERT_EQUAL(blabla);
43 #define DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, password, BaseClass) \
44 class TestName : public BaseClass { \
46 virtual OUString getTestName() override { return #TestName; } \
48 CPPUNIT_TEST_SUITE(TestName); \
49 CPPUNIT_TEST(Load_Verify_Reload_Verify); \
50 CPPUNIT_TEST_SUITE_END(); \
52 void Load_Verify_Reload_Verify() {\
53 executeLoadVerifyReloadVerify(filename, password);\
55 void verify() override;\
57 CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
58 void TestName::verify()
60 #define DECLARE_OOXMLEXPORT_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, nullptr, Test)
61 #define DECLARE_RTFEXPORT_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, nullptr, Test)
62 #define DECLARE_ODFEXPORT_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, nullptr, Test)
63 #define DECLARE_FODFEXPORT_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, nullptr, Test)
64 #define DECLARE_WW8EXPORT_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, nullptr, Test)
66 #define DECLARE_SW_EXPORT_TEST(TestName, filename, password, BaseClass) \
67 class TestName : public BaseClass { \
69 virtual OUString getTestName() override { return #TestName; } \
71 CPPUNIT_TEST_SUITE(TestName); \
72 CPPUNIT_TEST(Import_Export); \
73 CPPUNIT_TEST_SUITE_END(); \
75 void Import_Export() {\
76 executeImportExport(filename, password);\
78 void verify() override;\
80 CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
81 void TestName::verify()
83 class SwXTextDocument
;
92 /// Base class for filter tests loading or roundtripping a document, then asserting the document model.
93 class SWQAHELPER_DLLPUBLIC SwModelTestBase
: public UnoApiXmlTest
96 bool mbExported
; ///< Does maTempFile already contain something useful?
99 xmlBufferPtr mpXmlBuffer
;
102 sal_uInt32 mnStartTime
;
104 virtual OUString
getTestName() { return OUString(); }
106 /// Copy&paste helper.
107 void paste(std::u16string_view aFilename
, OUString aInstance
, css::uno::Reference
<css::text::XTextRange
> const& xTextRange
);
110 SwModelTestBase(const OUString
& pTestDocumentPath
= OUString(), const OUString
& pFilter
= {});
114 * Helper func used by each unit test to test the 'import' code.
115 * (Loads the requested file and then calls 'verify' method)
117 void executeImportTest(const char* filename
, const char* pPassword
= nullptr);
120 * Helper func used by each unit test to test the 'export' code.
121 * (Loads the requested file, calls 'verify' function, save it to temp file, load the
122 * temp file and then calls 'verify' function again)
124 void executeLoadVerifyReloadVerify(const char* filename
, const char* pPassword
= nullptr);
127 * Helper func used by each unit test to test the 'export' code.
128 * (Loads the requested file, save it to temp file, load the
129 * temp file and then calls 'verify' method)
131 void executeLoadReloadVerify(const char* filename
, const char* pPassword
= nullptr);
134 * Helper func used by each unit test to test the 'export' code.
135 * (Loads the requested file for document base (this represents
136 * the initial document condition), exports with the desired
137 * export filter and then calls 'verify' method)
139 void executeImportExport(const char* filename
, const char* pPassword
);
142 * Function overridden by unit test. See DECLARE_SW_*_TEST macros
144 virtual void verify()
146 CPPUNIT_FAIL( "verify method must be overridden" );
150 * Override this function if some special filename-specific setup is needed
152 virtual std::unique_ptr
<Resetter
> preTest(const char* /*filename*/)
157 /// Override this function if some special file-specific setup is needed during export test: after load, but before save.
158 virtual void postLoad(const char* /*pFilename*/)
162 void dumpLayout(const css::uno::Reference
< css::lang::XComponent
> & rComponent
);
164 void discardDumpedLayout();
168 /// Get the length of the whole document. @deprecated why use this?
169 int getLength() const;
170 /// Get the body text of the whole document.
171 OUString
getBodyText() const;
173 /// Get a family of styles, see com.sun.star.style.StyleFamilies for possible values.
174 css::uno::Reference
<css::container::XNameAccess
> getStyles(const OUString
& aFamily
);
176 /// Get a family of auto styles, see com.sun.star.style.StyleFamilies for possible values.
177 css::uno::Reference
<css::style::XAutoStyleFamily
> getAutoStyles(const OUString
& aFamily
);
179 /// Similar to parseExport(), but this gives the xmlDocPtr of the layout dump.
180 xmlDocUniquePtr
parseLayoutDump();
183 * Extract a value from the layout dump using an XPath expression and an attribute name.
185 * If the attribute is omitted, the text of the node is returned.
187 OUString
parseDump(const OString
& aXPath
, const OString
& aAttribute
= OString());
189 template< typename T
>
190 T
getProperty( const css::uno::Any
& obj
, const OUString
& name
) const
192 css::uno::Reference
< css::beans::XPropertySet
> properties( obj
, uno::UNO_QUERY_THROW
);
194 if (!css::uno::fromAny(properties
->getPropertyValue(name
), &data
))
196 OString
const msg("the property is of unexpected type or void: "
197 + OUStringToOString(name
, RTL_TEXTENCODING_UTF8
));
198 CPPUNIT_FAIL(msg
.getStr());
203 template< typename T
>
204 T
getProperty( const css::uno::Reference
< css::uno::XInterface
>& obj
, const OUString
& name
) const
206 css::uno::Reference
< css::beans::XPropertySet
> properties( obj
, uno::UNO_QUERY_THROW
);
208 if (!(properties
->getPropertyValue(name
) >>= data
))
210 OString
const msg("the property is of unexpected type or void: "
211 + OUStringToOString(name
, RTL_TEXTENCODING_UTF8
));
212 CPPUNIT_FAIL(msg
.getStr());
217 bool isPropertyVoid(const css::uno::Reference
<css::uno::XInterface
>& object
, const OUString
& name
) const
219 if (!hasProperty(object
, name
))
222 css::uno::Reference
< css::beans::XPropertySet
> properties(object
, uno::UNO_QUERY_THROW
);
223 return !properties
->getPropertyValue(name
).hasValue();
226 bool hasProperty(const css::uno::Reference
<css::uno::XInterface
>& obj
, const OUString
& name
) const;
228 css::xml::AttributeData
getUserDefineAttribute(const css::uno::Any
& obj
, const OUString
& name
, const OUString
& rValue
) const;
230 int getParagraphs( css::uno::Reference
<text::XText
> const & xText
);
232 /// Get number of paragraphs of the document.
235 css::uno::Reference
<css::text::XTextContent
> getParagraphOrTable(int number
, css::uno::Reference
<css::text::XText
> const & xText
= css::uno::Reference
<css::text::XText
>()) const;
237 // Get paragraph (counted from 1), optionally check it contains the given text.
238 css::uno::Reference
< css::text::XTextRange
> getParagraph( int number
, const OUString
& content
= OUString() ) const;
240 sal_Int16
getNumberingTypeOfParagraph(int nPara
);
242 css::uno::Reference
<css::text::XTextRange
> getParagraphOfText(int number
, css::uno::Reference
<css::text::XText
> const & xText
, const OUString
& content
= OUString()) const;
244 /// get nth object/fly that is anchored AT paragraph
245 css::uno::Reference
<css::beans::XPropertySet
> getParagraphAnchoredObject(
246 int const index
, css::uno::Reference
<css::text::XTextRange
> const & xPara
) const;
248 /// Get run (counted from 1) of a paragraph, optionally check it contains the given text.
249 css::uno::Reference
<css::text::XTextRange
> getRun(uno::Reference
<css::text::XTextRange
> const & xParagraph
, int number
, const OUString
& content
= OUString()) const;
251 /// Get math formula string of a run.
252 OUString
getFormula(css::uno::Reference
<css::text::XTextRange
> const & xRun
) const;
254 /// get cell of a table; table can be retrieved with getParagraphOrTable
255 css::uno::Reference
<css::table::XCell
> getCell(
256 css::uno::Reference
<css::uno::XInterface
> const& xTableIfc
,
257 OUString
const& rCell
, OUString
const& rContent
= OUString());
259 /// Get shape (counted from 1)
260 css::uno::Reference
<css::drawing::XShape
> getShape(int number
);
262 /// Select shape (counted from 1)
263 void selectShape(int number
);
265 /// Get shape by name
266 css::uno::Reference
<css::drawing::XShape
> getShapeByName(std::u16string_view aName
);
268 /// Get TextFrame by name
269 css::uno::Reference
<css::drawing::XShape
> getTextFrameByName(const OUString
& aName
);
273 void saveAndReload(const OUString
& pFilter
, const char* pPassword
= nullptr);
275 /// Combines load() and save().
276 void loadAndSave(const char* pName
, const char* pPassword
= nullptr);
278 /// Combines load() and saveAndReload().
279 void loadAndReload(const char* pName
);
284 int getPages() const;
287 int getShapes() const;
290 * Returns an xml stream of an exported file.
291 * To be used when the exporter doesn't create zip archives, but single files
292 * (like Flat ODF Export)
294 xmlDocUniquePtr
parseExportedFile();
297 * Creates a new document to be used with the internal sw/ API.
301 * createSwDoc("test.fodt");
302 * createSwDoc("test.fodt", "test");
304 void createSwDoc(const char* pName
= nullptr, const char* pPassword
= nullptr);
307 * As createSwDoc except a Web Document in Browse Mode
309 void createSwWebDoc(const char* pName
= nullptr);
312 * As createSwDoc except a Global Document
314 void createSwGlobalDoc(const char* pName
= nullptr);
317 * Gets SwDoc from loaded component
322 * Gets SwDocShell from loaded component
324 SwDocShell
* getSwDocShell();
327 * Wraps a reqif-xhtml fragment into an XHTML file, and XML-parses it.
329 xmlDocUniquePtr
WrapReqifFromTempFile();
331 void WrapFromTempFile(SvMemoryStream
& rStream
);
333 bool isExported(){ return mbExported
; }
335 void emulateTyping(SwXTextDocument
& rTextDoc
, const std::u16string_view
& rStr
);
338 void loadURL(OUString
const& rURL
, const char* pPassword
= nullptr);
342 * Test whether the expected and actual borderline parameters are equal
345 * @param[in] rExpected expected borderline object
346 * @param[in] rActual actual borderline object
347 * @param[in] rSourceLine line from where the assertion is called
348 * Note: This method is the implementation of CPPUNIT_ASSERT_BORDER_EQUAL, so
349 * use that macro instead.
351 inline void assertBorderEqual(
352 const css::table::BorderLine2
& rExpected
, const css::table::BorderLine2
& rActual
,
353 const CppUnit::SourceLine
& rSourceLine
)
355 CPPUNIT_NS::assertEquals( rExpected
.Color
, rActual
.Color
, rSourceLine
, "different Color" );
356 CPPUNIT_NS::assertEquals( rExpected
.InnerLineWidth
, rActual
.InnerLineWidth
, rSourceLine
, "different InnerLineWidth" );
357 CPPUNIT_NS::assertEquals( rExpected
.OuterLineWidth
, rActual
.OuterLineWidth
, rSourceLine
, "different OuterLineWidth" );
358 CPPUNIT_NS::assertEquals( rExpected
.LineDistance
, rActual
.LineDistance
, rSourceLine
, "different LineDistance" );
359 CPPUNIT_NS::assertEquals( rExpected
.LineStyle
, rActual
.LineStyle
, rSourceLine
, "different LineStyle" );
360 CPPUNIT_NS::assertEquals( rExpected
.LineWidth
, rActual
.LineWidth
, rSourceLine
, "different LineWidth" );
363 #define CPPUNIT_ASSERT_BORDER_EQUAL(aExpected, aActual) \
364 assertBorderEqual( aExpected, aActual, CPPUNIT_SOURCELINE() ) \
366 #endif // INCLUDED_SW_QA_INC_SWMODELTESTBASE_HXX
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */