kill copy&paste by introducing SwModelTestBase::parseDump()

Change-Id: I7cc3e05e48fc9850fbe04c6511f5e9f18a680ec7
This commit is contained in:
Miklos Vajna
2012-06-21 17:03:23 +02:00
parent 4cc2c691a9
commit 8616f227c7
3 changed files with 61 additions and 66 deletions

View File

@@ -45,14 +45,6 @@
#include <vcl/svapp.hxx>
#include <unotxdoc.hxx>
#include <docsh.hxx>
#include <doc.hxx>
#include <rootfrm.hxx>
#include <libxml/xmlwriter.h>
#include <libxml/xpath.h>
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<SwXTextDocument *>(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);

View File

@@ -32,12 +32,31 @@
#include <unotest/macros_test.hxx>
#include <rtl/ustrbuf.hxx>
#include <unotxdoc.hxx>
#include <docsh.hxx>
#include <doc.hxx>
#include <rootfrm.hxx>
#include <libxml/xmlwriter.h>
#include <libxml/xpath.h>
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<SwXTextDocument *>(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<lang::XComponent> mxComponent;
xmlBufferPtr mpXmlBuffer;
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

View File

@@ -36,14 +36,6 @@
#include <vcl/svapp.hxx>
#include <unotxdoc.hxx>
#include <docsh.hxx>
#include <doc.hxx>
#include <rootfrm.hxx>
#include <libxml/xmlwriter.h>
#include <libxml/xpath.h>
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<SwXTextDocument *>(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);