From 8616f227c722affcedff7632ba97644d04427c94 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Thu, 21 Jun 2012 17:03:23 +0200 Subject: [PATCH] kill copy&paste by introducing SwModelTestBase::parseDump() Change-Id: I7cc3e05e48fc9850fbe04c6511f5e9f18a680ec7 --- sw/qa/extras/ooxmltok/ooxmltok.cxx | 34 +---------------- sw/qa/extras/swmodeltestbase.hxx | 59 ++++++++++++++++++++++++++++++ sw/qa/extras/ww8tok/ww8tok.cxx | 34 +---------------- 3 files changed, 61 insertions(+), 66 deletions(-) diff --git a/sw/qa/extras/ooxmltok/ooxmltok.cxx b/sw/qa/extras/ooxmltok/ooxmltok.cxx index a34dd392f473..59e3aab1327e 100644 --- a/sw/qa/extras/ooxmltok/ooxmltok.cxx +++ b/sw/qa/extras/ooxmltok/ooxmltok.cxx @@ -45,14 +45,6 @@ #include -#include -#include -#include -#include - -#include -#include - using rtl::OString; using rtl::OUString; using rtl::OUStringBuffer; @@ -568,34 +560,10 @@ void Test::testN758883() * to the numbering. This is easier to test using a layout dump. */ - // create xml writer - xmlBufferPtr pXmlBuffer = xmlBufferCreate(); - xmlTextWriterPtr pXmlWriter = xmlNewTextWriterMemory(pXmlBuffer, 0); - xmlTextWriterStartDocument(pXmlWriter, NULL, NULL, NULL); - - // create dump load("n758883.docx"); - 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); - - // parse the dump - xmlDocPtr pXmlDoc = xmlParseMemory((const char*)xmlBufferContent(pXmlBuffer), xmlBufferLength(pXmlBuffer));; - xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc); - xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST("/root/page/body/txt/Special"), pXmlXpathCtx); - xmlNodeSetPtr pXmlNodes = pXmlXpathObj->nodesetval; - xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; - OUString aHeight = OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST("nHeight"))); + OUString aHeight = parseDump("/root/page/body/txt/Special", "nHeight"); CPPUNIT_ASSERT_EQUAL(sal_Int32(220), aHeight.toInt32()); // It was 280 - - // delete dump - xmlFreeDoc(pXmlDoc); - xmlBufferFree(pXmlBuffer); } CPPUNIT_TEST_SUITE_REGISTRATION(Test); diff --git a/sw/qa/extras/swmodeltestbase.hxx b/sw/qa/extras/swmodeltestbase.hxx index 0ddd2266b830..669e559f8143 100644 --- a/sw/qa/extras/swmodeltestbase.hxx +++ b/sw/qa/extras/swmodeltestbase.hxx @@ -32,12 +32,31 @@ #include #include +#include +#include +#include +#include + +#include +#include + using namespace com::sun::star; /// 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() + : mpXmlBuffer(0) + { + } + + ~SwModelTestBase() + { + if (mpXmlBuffer) + xmlBufferFree(mpXmlBuffer); + } + virtual void setUp() { test::BootstrapFixture::setUp(); @@ -54,6 +73,26 @@ public: test::BootstrapFixture::tearDown(); } +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); + } + + protected: /// Get the length of the whole document. int getLength() @@ -84,7 +123,27 @@ protected: return xStyleFamily; } + /// Extract a value from the layout dump using an XPath expression and an attribute name. + rtl::OUString parseDump(rtl::OString aXPath, rtl::OString aAttribute) + { + 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; + xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; + rtl::OUString aRet = rtl::OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST(aAttribute.getStr()))); + + xmlFreeDoc(pXmlDoc); + + return aRet; + } + uno::Reference mxComponent; + xmlBufferPtr mpXmlBuffer; }; /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/qa/extras/ww8tok/ww8tok.cxx b/sw/qa/extras/ww8tok/ww8tok.cxx index a63ff1437147..53f90301e157 100644 --- a/sw/qa/extras/ww8tok/ww8tok.cxx +++ b/sw/qa/extras/ww8tok/ww8tok.cxx @@ -36,14 +36,6 @@ #include -#include -#include -#include -#include - -#include -#include - using rtl::OString; using rtl::OUString; using rtl::OUStringBuffer; @@ -225,34 +217,10 @@ void Test::testN757905() // paragraph height. When in Word-compat mode, we should take the max of // the two, not just the height of the fly. - // create xml writer - xmlBufferPtr pXmlBuffer = xmlBufferCreate(); - xmlTextWriterPtr pXmlWriter = xmlNewTextWriterMemory(pXmlBuffer, 0); - xmlTextWriterStartDocument(pXmlWriter, NULL, NULL, NULL); - - // create dump load("n757905.doc"); - 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); - - // parse the dump - xmlDocPtr pXmlDoc = xmlParseMemory((const char*)xmlBufferContent(pXmlBuffer), xmlBufferLength(pXmlBuffer));; - xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc); - xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST("/root/page/body/txt/infos/bounds"), pXmlXpathCtx); - xmlNodeSetPtr pXmlNodes = pXmlXpathObj->nodesetval; - xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0]; - OUString aHeight = OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST("height"))); + OUString aHeight = parseDump("/root/page/body/txt/infos/bounds", "height"); CPPUNIT_ASSERT(sal_Int32(31) < aHeight.toInt32()); - - // delete dump - xmlFreeDoc(pXmlDoc); - xmlBufferFree(pXmlBuffer); } CPPUNIT_TEST_SUITE_REGISTRATION(Test);