android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / qa / inc / swmodeltestbase.hxx
blob838622230877cc052d295df6935432d94f5ceb55
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #ifndef INCLUDED_SW_QA_INC_SWMODELTESTBASE_HXX
11 #define INCLUDED_SW_QA_INC_SWMODELTESTBASE_HXX
13 #include <memory>
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>
29 #include <doc.hxx>
31 /**
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)
38 * {
39 * CPPUNIT_ASSERT_EQUAL(blabla);
40 * }
43 #define DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, password, BaseClass) \
44 class TestName : public BaseClass { \
45 protected:\
46 virtual OUString getTestName() override { return #TestName; } \
47 public:\
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;\
56 }; \
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 { \
68 protected:\
69 virtual OUString getTestName() override { return #TestName; } \
70 public:\
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;\
79 }; \
80 CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \
81 void TestName::verify()
83 class SwXTextDocument;
84 namespace vcl
86 namespace pdf
88 class PDFiumDocument;
92 /// Base class for filter tests loading or roundtripping a document, then asserting the document model.
93 class SWQAHELPER_DLLPUBLIC SwModelTestBase : public UnoApiXmlTest
95 private:
96 bool mbExported; ///< Does maTempFile already contain something useful?
98 protected:
99 xmlBufferPtr mpXmlBuffer;
100 OUString mpFilter;
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);
109 public:
110 SwModelTestBase(const OUString& pTestDocumentPath = OUString(), const OUString& pFilter = {});
112 protected:
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*/)
154 return nullptr;
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();
166 void calcLayout();
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 );
193 T data;
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());
200 return data;
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 );
207 T data = T();
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());
214 return data;
217 bool isPropertyVoid(const css::uno::Reference<css::uno::XInterface>& object, const OUString& name) const
219 if (!hasProperty(object, name))
220 return false;
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.
233 int getParagraphs();
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);
271 void header();
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);
281 void finish();
283 /// Get page count.
284 int getPages() const;
286 /// Get shape count.
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.
299 * Examples:
300 * createSwDoc();
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
319 SwDoc* getSwDoc();
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);
337 private:
338 void loadURL(OUString const& rURL, const char* pPassword = nullptr);
342 * Test whether the expected and actual borderline parameters are equal
343 * and assert if not.
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: */