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>
31 #define DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, password, BaseClass) \
32 class TestName : public BaseClass { \
34 CPPUNIT_TEST_SUITE(TestName); \
35 CPPUNIT_TEST(Load_Verify_Reload_Verify); \
36 CPPUNIT_TEST_SUITE_END(); \
38 void Load_Verify_Reload_Verify() {\
39 executeLoadVerifyReloadVerify(filename, password);\
41 void verify() override;\
43 CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
44 void TestName::verify()
46 #define DECLARE_OOXMLEXPORT_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, nullptr, Test)
47 #define DECLARE_ODFEXPORT_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, nullptr, Test)
48 #define DECLARE_WW8EXPORT_TEST(TestName, filename) DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, nullptr, Test)
50 class SwXTextDocument
;
53 class ConfigurationChanges
;
63 /// Temporarily enables the ExportFormFields setting.
64 class SWQAHELPER_DLLPUBLIC SwExportFormFieldsGuard
66 std::shared_ptr
<comphelper::ConfigurationChanges
> m_pBatch
;
69 SwExportFormFieldsGuard();
70 ~SwExportFormFieldsGuard();
73 /// Base class for filter tests loading or roundtripping a document, then asserting the document model.
74 class SWQAHELPER_DLLPUBLIC SwModelTestBase
: public UnoApiXmlTest
77 xmlBufferPtr mpXmlBuffer
;
80 /// Copy&paste helper.
81 void paste(std::u16string_view aFilename
, const OUString
& aInstance
, css::uno::Reference
<css::text::XTextRange
> const& xTextRange
);
84 SwModelTestBase(const OUString
& pTestDocumentPath
= OUString(), const OUString
& pFilter
= {});
88 * Helper func used by each unit test to test the 'export' code.
89 * (Loads the requested file, calls 'verify' function, save it to temp file, load the
90 * temp file and then calls 'verify' function again)
92 void executeLoadVerifyReloadVerify(const char* filename
, const char* pPassword
= nullptr);
95 * Function overridden by unit test. See DECLARE_SW_*_TEST macros
99 CPPUNIT_FAIL( "verify method must be overridden" );
104 /// Get the body text of the whole document.
105 OUString
getBodyText() const;
107 /// Get a family of styles, see com.sun.star.style.StyleFamilies for possible values.
108 css::uno::Reference
<css::container::XNameAccess
> getStyles(const OUString
& aFamily
);
110 /// Get a family of auto styles, see com.sun.star.style.StyleFamilies for possible values.
111 css::uno::Reference
<css::style::XAutoStyleFamily
> getAutoStyles(const OUString
& aFamily
);
113 /// Similar to parseExport(), but this gives the xmlDocPtr of the layout dump.
114 xmlDocUniquePtr
parseLayoutDump(const css::uno::Reference
< css::lang::XComponent
>& xComponent
= nullptr);
116 template< typename T
>
117 T
getProperty( const css::uno::Any
& obj
, const OUString
& name
) const
119 css::uno::Reference
< css::beans::XPropertySet
> properties( obj
, uno::UNO_QUERY_THROW
);
121 if (!css::uno::fromAny(properties
->getPropertyValue(name
), &data
))
123 OString
const msg("the property is of unexpected type or void: "
124 + OUStringToOString(name
, RTL_TEXTENCODING_UTF8
));
125 CPPUNIT_FAIL(msg
.getStr());
130 template< typename T
>
131 T
getProperty( const css::uno::Reference
< css::uno::XInterface
>& obj
, const OUString
& name
) const
133 css::uno::Reference
< css::beans::XPropertySet
> properties( obj
, uno::UNO_QUERY_THROW
);
135 if (!(properties
->getPropertyValue(name
) >>= data
))
137 OString
const msg("the property is of unexpected type or void: "
138 + OUStringToOString(name
, RTL_TEXTENCODING_UTF8
));
139 CPPUNIT_FAIL(msg
.getStr());
144 bool isPropertyVoid(const css::uno::Reference
<css::uno::XInterface
>& object
, const OUString
& name
) const
146 if (!hasProperty(object
, name
))
149 css::uno::Reference
< css::beans::XPropertySet
> properties(object
, uno::UNO_QUERY_THROW
);
150 return !properties
->getPropertyValue(name
).hasValue();
153 bool hasProperty(const css::uno::Reference
<css::uno::XInterface
>& obj
, const OUString
& name
) const;
155 css::xml::AttributeData
getUserDefineAttribute(const css::uno::Any
& obj
, const OUString
& name
, const OUString
& rValue
) const;
157 int getParagraphs( css::uno::Reference
<text::XText
> const & xText
);
159 /// Get number of paragraphs of the document.
162 css::uno::Reference
<css::text::XTextContent
> getParagraphOrTable(int number
, css::uno::Reference
<css::text::XText
> const & xText
= css::uno::Reference
<css::text::XText
>()) const;
164 // Get paragraph (counted from 1), optionally check it contains the given text.
165 css::uno::Reference
< css::text::XTextRange
> getParagraph( int number
, const OUString
& content
= OUString() ) const;
167 sal_Int16
getNumberingTypeOfParagraph(int nPara
);
169 css::uno::Reference
<css::text::XTextRange
> getParagraphOfText(int number
, css::uno::Reference
<css::text::XText
> const & xText
, const OUString
& content
= OUString()) const;
171 /// get nth object/fly that is anchored AT paragraph
172 css::uno::Reference
<css::beans::XPropertySet
> getParagraphAnchoredObject(
173 int const index
, css::uno::Reference
<css::text::XTextRange
> const & xPara
) const;
175 /// Get run (counted from 1) of a paragraph, optionally check it contains the given text.
176 css::uno::Reference
<css::text::XTextRange
> getRun(uno::Reference
<css::text::XTextRange
> const & xParagraph
, int number
, const OUString
& content
= OUString()) const;
178 /// Get math formula string of a run.
179 OUString
getFormula(css::uno::Reference
<css::text::XTextRange
> const & xRun
) const;
181 /// get cell of a table; table can be retrieved with getParagraphOrTable
182 css::uno::Reference
<css::table::XCell
> getCell(
183 css::uno::Reference
<css::uno::XInterface
> const& xTableIfc
,
184 OUString
const& rCell
, OUString
const& rContent
= OUString());
186 /// Get shape (counted from 1)
187 css::uno::Reference
<css::drawing::XShape
> getShape(int number
);
189 /// Select shape (counted from 1)
190 void selectShape(int number
);
192 /// Get shape by name
193 css::uno::Reference
<css::drawing::XShape
> getShapeByName(std::u16string_view aName
);
195 /// Get TextFrame by name
196 css::uno::Reference
<css::drawing::XShape
> getTextFrameByName(const OUString
& aName
);
200 void saveAndReload(const OUString
& pFilter
, const char* pPassword
= nullptr);
202 /// Combines load() and save().
203 void loadAndSave(const char* pName
, const char* pPassword
= nullptr);
205 /// Combines load() and saveAndReload().
206 void loadAndReload(const char* pName
);
209 int getPages() const;
212 int getShapes() const;
215 * Creates a new document to be used with the internal sw/ API.
219 * createSwDoc("test.fodt");
220 * createSwDoc("test.fodt", "test");
222 void createSwDoc(const char* pName
= nullptr, const char* pPassword
= nullptr);
225 * As createSwDoc except a Web Document in Browse Mode
227 void createSwWebDoc(const char* pName
= nullptr);
230 * As createSwDoc except a Global Document
232 void createSwGlobalDoc(const char* pName
= nullptr);
235 * Gets SwDoc from loaded component
240 * Gets SwDocShell from loaded component
242 SwDocShell
* getSwDocShell();
245 * Gets SwXTextDocument from loaded component
247 SwXTextDocument
* getSwTextDoc();
250 * Wraps a reqif-xhtml fragment into an XHTML file, and XML-parses it.
252 xmlDocUniquePtr
WrapReqifFromTempFile();
254 void emulateTyping(std::u16string_view rStr
);
257 void loadURL(OUString
const& rURL
, const char* pPassword
= nullptr);
259 void dumpLayout(SwDoc
* pDoc
);
263 * Test whether the expected and actual borderline parameters are equal
266 * @param[in] rExpected expected borderline object
267 * @param[in] rActual actual borderline object
268 * @param[in] rSourceLine line from where the assertion is called
269 * Note: This method is the implementation of CPPUNIT_ASSERT_BORDER_EQUAL, so
270 * use that macro instead.
272 inline void assertBorderEqual(
273 const css::table::BorderLine2
& rExpected
, const css::table::BorderLine2
& rActual
,
274 const CppUnit::SourceLine
& rSourceLine
)
276 CPPUNIT_NS::assertEquals( rExpected
.Color
, rActual
.Color
, rSourceLine
, "different Color" );
277 CPPUNIT_NS::assertEquals( rExpected
.InnerLineWidth
, rActual
.InnerLineWidth
, rSourceLine
, "different InnerLineWidth" );
278 CPPUNIT_NS::assertEquals( rExpected
.OuterLineWidth
, rActual
.OuterLineWidth
, rSourceLine
, "different OuterLineWidth" );
279 CPPUNIT_NS::assertEquals( rExpected
.LineDistance
, rActual
.LineDistance
, rSourceLine
, "different LineDistance" );
280 CPPUNIT_NS::assertEquals( rExpected
.LineStyle
, rActual
.LineStyle
, rSourceLine
, "different LineStyle" );
281 CPPUNIT_NS::assertEquals( rExpected
.LineWidth
, rActual
.LineWidth
, rSourceLine
, "different LineWidth" );
284 #define CPPUNIT_ASSERT_BORDER_EQUAL(aExpected, aActual) \
285 assertBorderEqual( aExpected, aActual, CPPUNIT_SOURCELINE() ) \
287 #endif // INCLUDED_SW_QA_INC_SWMODELTESTBASE_HXX
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */