/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace com::sun::star; #define DEFAULT_STYLE "Default Style" /** * Macro to declare a new test (with full round-trip. To test * import only use the DECLARE_SW_IMPORT_TEST macro instead). * In order to add a new test, one only needs to use this macro * and then specify the test content, like this: * * DECLARE_SW_ROUNDTRIP_TEST(MyTest, "myfilename.docx", Test) * { * CPPUNIT_ASSERT_EQUAL(blabla); * } * */ #define DECLARE_SW_ROUNDTRIP_TEST(TestName, filename, BaseClass) \ class TestName : public BaseClass { \ public:\ CPPUNIT_TEST_SUITE(TestName); \ CPPUNIT_TEST(Import); \ CPPUNIT_TEST(Import_Export_Import); \ CPPUNIT_TEST_SUITE_END(); \ \ void Import() { \ executeImportTest(filename);\ }\ void Import_Export_Import() {\ executeImportExportImportTest(filename);\ }\ void verify();\ }; \ CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \ void TestName::verify() #define DECLARE_SW_IMPORT_TEST(TestName, filename, BaseClass) \ class TestName : public BaseClass { \ public:\ CPPUNIT_TEST_SUITE(TestName); \ CPPUNIT_TEST(Import); \ CPPUNIT_TEST_SUITE_END(); \ \ void Import() { \ executeImportTest(filename);\ }\ void verify();\ }; \ CPPUNIT_TEST_SUITE_REGISTRATION(TestName); \ void TestName::verify() /// Base class for filter tests loading or roundtriping a document, then asserting the document model. class SwModelTestBase : public test::BootstrapFixture, public unotest::MacrosTest { public: SwModelTestBase(const char* pTestDocumentPath = "", const char* pFilter = "") : mpXmlBuffer(0), mpTestDocumentPath(pTestDocumentPath), mpFilter(pFilter) { } ~SwModelTestBase() { } virtual void setUp() { test::BootstrapFixture::setUp(); mxDesktop.set( com::sun::star::frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory())) ); } virtual void tearDown() { if (mxComponent.is()) mxComponent->dispose(); test::BootstrapFixture::tearDown(); } protected: /** * Helper func used by each unit test to test the 'import' code. * (Loads the requested file and then calls 'verify' method) */ void executeImportTest(const char* filename) { // If the testcase is stored in some other format, it's pointless to test. if (mustTestImportOf(filename)) { header(); preTest(filename); load(mpTestDocumentPath, filename); postTest(filename); verify(); finish(); } } /** * Helper func used by each unit test to test the 'export' code. * (Loads the requested file, save it to temp file, load the * temp file and then calls 'verify' method) */ void executeImportExportImportTest(const char* filename) { header(); load(mpTestDocumentPath, filename); reload(mpFilter); verify(); finish(); } /** * Function overloaded by unit test. See DECLARE_SW_*_TEST macros */ virtual void verify() { CPPUNIT_FAIL( "verify method must be overriden" ); } /** * Override this function if interested in skipping import test for this file */ virtual bool mustTestImportOf(const char* /* filename */) const { return true; } /** * Override this function if some special filename-specific setup is needed */ virtual void preTest(const char* /*filename*/) { } /** * Override this function if some special filename-specific teardown is needed */ virtual void postTest(const char* /*filename*/) { } private: void dumpLayout() { // create the xml writer mpXmlBuffer = xmlBufferCreate(); xmlTextWriterPtr pXmlWriter = xmlNewTextWriterMemory(mpXmlBuffer, 0); xmlTextWriterStartDocument(pXmlWriter, NULL, NULL, NULL); // create the dump SwXTextDocument* pTxtDoc = dynamic_cast(mxComponent.get()); SwDoc* pDoc = pTxtDoc->GetDocShell()->GetDoc(); SwRootFrm* pLayout = pDoc->GetCurrentLayout(); pLayout->dumpAsXml(pXmlWriter); // delete xml writer xmlTextWriterEndDocument(pXmlWriter); xmlFreeTextWriter(pXmlWriter); } void calcLayout() { SwXTextDocument* pTxtDoc = dynamic_cast(mxComponent.get()); SwDoc* pDoc = pTxtDoc->GetDocShell()->GetDoc(); pDoc->GetCurrentViewShell()->CalcLayout(); } protected: /// Get the length of the whole document. int getLength() { uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); uno::Reference xParaEnumAccess(xTextDocument->getText(), uno::UNO_QUERY); uno::Reference xParaEnum = xParaEnumAccess->createEnumeration(); OUStringBuffer aBuf; while (xParaEnum->hasMoreElements()) { uno::Reference xRangeEnumAccess(xParaEnum->nextElement(), uno::UNO_QUERY); uno::Reference xRangeEnum = xRangeEnumAccess->createEnumeration(); while (xRangeEnum->hasMoreElements()) { uno::Reference xRange(xRangeEnum->nextElement(), uno::UNO_QUERY); aBuf.append(xRange->getString()); } } return aBuf.getLength(); } /// Get a family of styles, see com.sun.star.style.StyleFamilies for possible values. uno::Reference getStyles(OUString aFamily) { uno::Reference xStyleFamiliesSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xStyleFamilies(xStyleFamiliesSupplier->getStyleFamilies(), uno::UNO_QUERY); uno::Reference xStyleFamily(xStyleFamilies->getByName(aFamily), uno::UNO_QUERY); return xStyleFamily; } /// Get a family of auto styles, see com.sun.star.style.StyleFamilies for possible values. uno::Reference getAutoStyles(OUString aFamily) { uno::Reference< style::XAutoStylesSupplier > xAutoStylesSupplier(mxComponent, uno::UNO_QUERY); uno::Reference< style::XAutoStyles > xAutoStyles(xAutoStylesSupplier->getAutoStyles()); uno::Reference< style::XAutoStyleFamily > xAutoStyleFamily(xAutoStyles->getByName(aFamily), uno::UNO_QUERY); return xAutoStyleFamily; } /** * Extract a value from the layout dump using an XPath expression and an attribute name. * * If the attribute is omitted, the text of the node is returned. */ OUString parseDump(OString aXPath, OString aAttribute = OString()) { if (!mpXmlBuffer) dumpLayout(); xmlDocPtr pXmlDoc = xmlParseMemory((const char*)xmlBufferContent(mpXmlBuffer), xmlBufferLength(mpXmlBuffer));; xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc); xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(aXPath.getStr()), pXmlXpathCtx); xmlNodeSetPtr pXmlNodes = pXmlXpathObj->nodesetval; CPPUNIT_ASSERT_EQUAL(1, xmlXPathNodeSetGetLength(pXmlNodes)); xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; OUString aRet; if (aAttribute.getLength()) aRet = OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST(aAttribute.getStr()))); else aRet = OUString::createFromAscii((const char*)XML_GET_CONTENT(pXmlNode)); xmlFreeDoc(pXmlDoc); return aRet; } template< typename T > T getProperty( const uno::Any& obj, const OUString& name ) const { uno::Reference< beans::XPropertySet > properties( obj, uno::UNO_QUERY_THROW ); T data = T(); properties->getPropertyValue( name ) >>= data; return data; } template< typename T > T getProperty( const uno::Reference< uno::XInterface >& obj, const OUString& name ) const { uno::Reference< beans::XPropertySet > properties( obj, uno::UNO_QUERY_THROW ); T data = T(); properties->getPropertyValue( name ) >>= data; return data; } /// Get number of paragraphs of the document. int getParagraphs() { uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); uno::Reference xParaEnumAccess(xTextDocument->getText(), uno::UNO_QUERY); uno::Reference xParaEnum = xParaEnumAccess->createEnumeration(); int nRet = 0; while (xParaEnum->hasMoreElements()) { xParaEnum->nextElement(); nRet++; } return nRet; } uno::Reference getParagraphOrTable(int number, uno::Reference xText = uno::Reference()) const { uno::Reference paraEnumAccess; if (xText.is()) paraEnumAccess.set(xText, uno::UNO_QUERY); else { uno::Reference textDocument(mxComponent, uno::UNO_QUERY); paraEnumAccess.set(textDocument->getText(), uno::UNO_QUERY); } uno::Reference paraEnum = paraEnumAccess->createEnumeration(); for( int i = 1; i < number; ++i ) paraEnum->nextElement(); uno::Reference< text::XTextContent> const xElem(paraEnum->nextElement(), uno::UNO_QUERY_THROW); return xElem; } // Get paragraph (counted from 1), optionally check it contains the given text. uno::Reference< text::XTextRange > getParagraph( int number, OUString content = OUString() ) const { uno::Reference const xParagraph( getParagraphOrTable(number), uno::UNO_QUERY_THROW); if( !content.isEmpty()) CPPUNIT_ASSERT_EQUAL( content, xParagraph->getString()); return xParagraph; } uno::Reference getParagraphOfText(int number, uno::Reference xText) const { uno::Reference const xParagraph(getParagraphOrTable(number, xText), uno::UNO_QUERY_THROW); return xParagraph; } /// Get run (counted from 1) of a paragraph, optionally check it contains the given text. uno::Reference getRun(uno::Reference xParagraph, int number, OUString content = OUString()) const { uno::Reference xRunEnumAccess(xParagraph, uno::UNO_QUERY); uno::Reference xRunEnum = xRunEnumAccess->createEnumeration(); for (int i = 1; i < number; ++i) xRunEnum->nextElement(); uno::Reference xRun(xRunEnum->nextElement(), uno::UNO_QUERY); if( !content.isEmpty()) CPPUNIT_ASSERT_EQUAL( content, xRun->getString()); return xRun; } /// Get math formula string of a run. OUString getFormula(uno::Reference xRun) const { uno::Reference xContentEnumAccess(xRun, uno::UNO_QUERY); uno::Reference xContentEnum(xContentEnumAccess->createContentEnumeration(""), uno::UNO_QUERY); uno::Reference xFormula(xContentEnum->nextElement(), uno::UNO_QUERY); return getProperty(getProperty< uno::Reference >(xFormula, "Model"), "Formula"); } /// get cell of a table; table can be retrieved with getParagraphOrTable uno::Reference getCell( uno::Reference const& xTableIfc, OUString const& rCell, OUString const& rContent = OUString()) { uno::Reference const xTable(xTableIfc, uno::UNO_QUERY_THROW); uno::Reference const xCell( xTable->getCellByName(rCell), uno::UNO_SET_THROW); if (!rContent.isEmpty()) { uno::Reference const xCellText(xCell, uno::UNO_QUERY_THROW); CPPUNIT_ASSERT_EQUAL(rContent, xCellText->getString()); } return xCell; } /// Get shape (counted from 1) uno::Reference getShape(int number) { uno::Reference xDrawPageSupplier(mxComponent, uno::UNO_QUERY); uno::Reference xDrawPage = xDrawPageSupplier->getDrawPage(); uno::Reference xShape(xDrawPage->getByIndex(number - 1), uno::UNO_QUERY); return xShape; } void header() { std::cerr << "File tested,Execution Time (ms)" << std::endl; } void load(const char* pDir, const char* pName, bool bCalcLayout = true) { if (mxComponent.is()) mxComponent->dispose(); // Output name early, so in the case of a hang, the name of the hanging input file is visible. std::cerr << pName << ","; m_nStartTime = osl_getGlobalTimer(); mxComponent = loadFromDesktop(getURLFromSrc(pDir) + OUString::createFromAscii(pName), "com.sun.star.text.TextDocument"); if (bCalcLayout) calcLayout(); } void reload(const char* pFilter) { uno::Reference xStorable(mxComponent, uno::UNO_QUERY); uno::Sequence aArgs(1); aArgs[0].Name = "FilterName"; aArgs[0].Value <<= OUString::createFromAscii(pFilter); utl::TempFile aTempFile; aTempFile.EnableKillingFile(); xStorable->storeToURL(aTempFile.GetURL(), aArgs); uno::Reference xComponent(xStorable, uno::UNO_QUERY); xComponent->dispose(); mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument"); if (mpXmlBuffer) { xmlBufferFree(mpXmlBuffer); mpXmlBuffer = 0; } calcLayout(); } /// Save the loaded document to a tempfile. Can be used to check the resulting docx/odt directly as a ZIP file. void save(OUString aFilter, utl::TempFile& rTempFile) { rTempFile.EnableKillingFile(); uno::Reference xStorable(mxComponent, uno::UNO_QUERY); uno::Sequence aFilterArgs(1); aFilterArgs[0].Name = "FilterName"; aFilterArgs[0].Value <<= aFilter; xStorable->storeToURL(rTempFile.GetURL(), aFilterArgs); } void finish() { sal_uInt32 nEndTime = osl_getGlobalTimer(); std::cerr << (nEndTime - m_nStartTime) << std::endl; if (mpXmlBuffer) { xmlBufferFree(mpXmlBuffer); mpXmlBuffer = 0; } } /// Get page count. int getPages() { uno::Reference xModel(mxComponent, uno::UNO_QUERY); uno::Reference xTextViewCursorSupplier(xModel->getCurrentController(), uno::UNO_QUERY); uno::Reference xCursor(xTextViewCursorSupplier->getViewCursor(), uno::UNO_QUERY); xCursor->jumpToLastPage(); return xCursor->getPage(); } uno::Reference mxComponent; xmlBufferPtr mpXmlBuffer; const char* mpTestDocumentPath; const char* mpFilter; template< typename T > struct MethodEntry { const char* pName; void (T::*pMethod)(); }; sal_uInt32 m_nStartTime; }; /** * Test whether the expected and actual borderline parameters are equal * and assert if not. * * @param[in] rExpected expected borderline object * @param[in] rActual actual borderline object * @param[in] rSourceLine line from where the assertion is called * Note: This method is the implementatition of CPPUNIT_ASSERT_BORDER_EQUAL, so * use that macro instead. **/ inline void assertBorderEqual( const table::BorderLine2& rExpected, const table::BorderLine2& rActual, const CppUnit::SourceLine& rSourceLine ) { CPPUNIT_NS::assertEquals( rExpected.Color, rActual.Color, rSourceLine, "different Color" ); CPPUNIT_NS::assertEquals( rExpected.InnerLineWidth, rActual.InnerLineWidth, rSourceLine, "different InnerLineWidth" ); CPPUNIT_NS::assertEquals( rExpected.OuterLineWidth, rActual.OuterLineWidth, rSourceLine, "different OuterLineWidth" ); CPPUNIT_NS::assertEquals( rExpected.LineDistance, rActual.LineDistance, rSourceLine, "different LineDistance" ); CPPUNIT_NS::assertEquals( rExpected.LineStyle, rActual.LineStyle, rSourceLine, "different LineStyle" ); CPPUNIT_NS::assertEquals( rExpected.LineWidth, rActual.LineWidth, rSourceLine, "different LineWidth" ); } #define CPPUNIT_ASSERT_BORDER_EQUAL(aExpected, aActual) \ assertBorderEqual( aExpected, aActual, CPPUNIT_SOURCELINE() ) \ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */