LCOV - code coverage report
Current view: top level - sw/qa/extras - swmodeltestbase.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 102 102 100.0 %
Date: 2012-08-25 Functions: 23 24 95.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 145 274 52.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       3                 :            :  *
       4                 :            :  * The contents of this file are subject to the Mozilla Public License Version
       5                 :            :  * 1.1 (the "License"); you may not use this file except in compliance with
       6                 :            :  * the License. You may obtain a copy of the License at
       7                 :            :  * http://www.mozilla.org/MPL/
       8                 :            :  *
       9                 :            :  * Software distributed under the License is distributed on an "AS IS" basis,
      10                 :            :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      11                 :            :  * for the specific language governing rights and limitations under the
      12                 :            :  * License.
      13                 :            :  *
      14                 :            :  * The Initial Developer of the Original Code is
      15                 :            :  *       Miklos Vajna <vmiklos@suse.cz> (SUSE, Inc.)
      16                 :            :  * Portions created by the Initial Developer are Copyright (C) 2012 the
      17                 :            :  * Initial Developer. All Rights Reserved.
      18                 :            :  *
      19                 :            :  * Contributor(s):
      20                 :            :  *
      21                 :            :  * Alternatively, the contents of this file may be used under the terms of
      22                 :            :  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      23                 :            :  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
      24                 :            :  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
      25                 :            :  * instead of those above.
      26                 :            :  */
      27                 :            : 
      28                 :            : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
      29                 :            : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
      30                 :            : #include <com/sun/star/text/XTextDocument.hpp>
      31                 :            : #include <com/sun/star/text/XTextRange.hpp>
      32                 :            : 
      33                 :            : #include <test/bootstrapfixture.hxx>
      34                 :            : #include <unotest/macros_test.hxx>
      35                 :            : #include <rtl/ustrbuf.hxx>
      36                 :            : 
      37                 :            : #include <unotxdoc.hxx>
      38                 :            : #include <docsh.hxx>
      39                 :            : #include <doc.hxx>
      40                 :            : #include <rootfrm.hxx>
      41                 :            : 
      42                 :            : #include <libxml/xmlwriter.h>
      43                 :            : #include <libxml/xpath.h>
      44                 :            : 
      45                 :            : using namespace com::sun::star;
      46                 :            : 
      47                 :            : /// Base class for filter tests loading or roundtriping a document, then asserting the document model.
      48                 :            : class SwModelTestBase : public test::BootstrapFixture, public unotest::MacrosTest
      49                 :            : {
      50                 :            : public:
      51                 :        384 :     SwModelTestBase()
      52         [ +  - ]:        384 :         : mpXmlBuffer(0)
      53                 :            :     {
      54                 :        384 :     }
      55                 :            : 
      56                 :        384 :     ~SwModelTestBase()
      57         [ +  - ]:        384 :     {
      58         [ +  + ]:        384 :         if (mpXmlBuffer)
      59         [ +  - ]:          6 :             xmlBufferFree(mpXmlBuffer);
      60         [ -  + ]:        384 :     }
      61                 :            : 
      62                 :        384 :     virtual void setUp()
      63                 :            :     {
      64                 :        384 :         test::BootstrapFixture::setUp();
      65                 :            : 
      66 [ +  - ][ +  - ]:        384 :         mxDesktop.set(getMultiServiceFactory()->createInstance("com.sun.star.frame.Desktop"), uno::UNO_QUERY);
                 [ +  - ]
      67 [ +  - ][ +  - ]:        384 :         CPPUNIT_ASSERT(mxDesktop.is());
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
      68                 :        384 :     }
      69                 :            : 
      70                 :        384 :     virtual void tearDown()
      71                 :            :     {
      72         [ +  - ]:        384 :         if (mxComponent.is())
      73                 :        384 :             mxComponent->dispose();
      74                 :            : 
      75                 :        384 :         test::BootstrapFixture::tearDown();
      76                 :        384 :     }
      77                 :            : 
      78                 :            : private:
      79                 :          6 :     void dumpLayout()
      80                 :            :     {
      81                 :            :         // create the xml writer
      82                 :          6 :         mpXmlBuffer = xmlBufferCreate();
      83                 :          6 :         xmlTextWriterPtr pXmlWriter = xmlNewTextWriterMemory(mpXmlBuffer, 0);
      84                 :          6 :         xmlTextWriterStartDocument(pXmlWriter, NULL, NULL, NULL);
      85                 :            : 
      86                 :            :         // create the dump
      87         [ -  + ]:          6 :         SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get());
      88                 :          6 :         SwDoc* pDoc = pTxtDoc->GetDocShell()->GetDoc();
      89                 :          6 :         SwRootFrm* pLayout = pDoc->GetCurrentLayout();
      90                 :          6 :         pLayout->dumpAsXml(pXmlWriter);
      91                 :            : 
      92                 :            :         // delete xml writer
      93                 :          6 :         xmlTextWriterEndDocument(pXmlWriter);
      94                 :          6 :         xmlFreeTextWriter(pXmlWriter);
      95                 :          6 :     }
      96                 :            : 
      97                 :            : 
      98                 :            : protected:
      99                 :            :     /// Get the length of the whole document.
     100                 :         18 :     int getLength()
     101                 :            :     {
     102         [ +  - ]:         18 :         uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
     103 [ +  - ][ +  - ]:         18 :         uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(), uno::UNO_QUERY);
                 [ +  - ]
     104 [ +  - ][ +  - ]:         18 :         uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
     105                 :         18 :         rtl::OUStringBuffer aBuf;
     106 [ +  - ][ +  - ]:         36 :         while (xParaEnum->hasMoreElements())
                 [ +  + ]
     107                 :            :         {
     108 [ +  - ][ +  - ]:         18 :             uno::Reference<container::XEnumerationAccess> xRangeEnumAccess(xParaEnum->nextElement(), uno::UNO_QUERY);
                 [ +  - ]
     109 [ +  - ][ +  - ]:         18 :             uno::Reference<container::XEnumeration> xRangeEnum = xRangeEnumAccess->createEnumeration();
     110 [ +  - ][ +  - ]:         48 :             while (xRangeEnum->hasMoreElements())
                 [ +  + ]
     111                 :            :             {
     112 [ +  - ][ +  - ]:         30 :                 uno::Reference<text::XTextRange> xRange(xRangeEnum->nextElement(), uno::UNO_QUERY);
                 [ +  - ]
     113 [ +  - ][ +  - ]:         30 :                 aBuf.append(xRange->getString());
                 [ +  - ]
     114                 :         30 :             }
     115                 :         18 :         }
     116                 :         18 :         return aBuf.getLength();
     117                 :            :     }
     118                 :            : 
     119                 :            :     /// Get a family of styles, see com.sun.star.style.StyleFamilies for possible values.
     120                 :         33 :     uno::Reference<container::XNameAccess> getStyles(rtl::OUString aFamily)
     121                 :            :     {
     122         [ +  - ]:         33 :         uno::Reference<style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(mxComponent, uno::UNO_QUERY);
     123 [ +  - ][ +  - ]:         33 :         uno::Reference<container::XNameAccess> xStyleFamilies(xStyleFamiliesSupplier->getStyleFamilies(), uno::UNO_QUERY);
                 [ +  - ]
     124 [ +  - ][ +  - ]:         33 :         uno::Reference<container::XNameAccess> xStyleFamily(xStyleFamilies->getByName(aFamily), uno::UNO_QUERY);
                 [ +  - ]
     125                 :         33 :         return xStyleFamily;
     126                 :            :     }
     127                 :            : 
     128                 :            :     /// Extract a value from the layout dump using an XPath expression and an attribute name.
     129                 :          6 :     rtl::OUString parseDump(rtl::OString aXPath, rtl::OString aAttribute)
     130                 :            :     {
     131         [ +  - ]:          6 :         if (!mpXmlBuffer)
     132                 :          6 :             dumpLayout();
     133                 :            : 
     134                 :          6 :         xmlDocPtr pXmlDoc = xmlParseMemory((const char*)xmlBufferContent(mpXmlBuffer), xmlBufferLength(mpXmlBuffer));;
     135                 :            : 
     136                 :          6 :         xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pXmlDoc);
     137                 :          6 :         xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(aXPath.getStr()), pXmlXpathCtx);
     138                 :          6 :         xmlNodeSetPtr pXmlNodes = pXmlXpathObj->nodesetval;
     139                 :          6 :         xmlNodePtr pXmlNode = pXmlNodes->nodeTab[0];
     140                 :          6 :         rtl::OUString aRet = rtl::OUString::createFromAscii((const char*)xmlGetProp(pXmlNode, BAD_CAST(aAttribute.getStr())));
     141                 :            : 
     142         [ +  - ]:          6 :         xmlFreeDoc(pXmlDoc);
     143                 :            : 
     144                 :          6 :         return aRet;
     145                 :            :     }
     146                 :            : 
     147                 :            :     template< typename T >
     148                 :         60 :     T getProperty( uno::Any obj, const rtl::OUString& name ) const
     149                 :            :     {
     150 [ +  - ][ +  - ]:         60 :         uno::Reference< beans::XPropertySet > properties( obj, uno::UNO_QUERY );
                 [ +  - ]
     151         [ +  - ]:         45 :         T data = T();
     152   [ +  -  +  - ]:         60 :         properties->getPropertyValue( name ) >>= data;
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     153                 :         60 :         return data;
     154                 :            :     }
     155                 :            : 
     156                 :            :     template< typename T >
     157                 :        501 :     T getProperty( uno::Reference< uno::XInterface > obj, const rtl::OUString& name ) const
     158                 :            :     {
     159 [ +  - ][ +  - ]:        501 :         uno::Reference< beans::XPropertySet > properties( obj, uno::UNO_QUERY );
                 [ +  - ]
     160                 :        474 :         T data = T();
     161   [ +  -  +  - ]:        501 :         properties->getPropertyValue( name ) >>= data;
           [ +  -  +  -  
                   +  - ]
           [ +  -  +  - ]
           [ +  -  +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     162                 :        501 :         return data;
     163                 :            :     }
     164                 :            : 
     165                 :            :     /// Get number of paragraphs of the document.
     166                 :          3 :     int getParagraphs()
     167                 :            :     {
     168         [ +  - ]:          3 :         uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
     169 [ +  - ][ +  - ]:          3 :         uno::Reference<container::XEnumerationAccess> xParaEnumAccess(xTextDocument->getText(), uno::UNO_QUERY);
                 [ +  - ]
     170 [ +  - ][ +  - ]:          3 :         uno::Reference<container::XEnumeration> xParaEnum = xParaEnumAccess->createEnumeration();
     171                 :          3 :         int nRet = 0;
     172 [ +  - ][ +  - ]:          9 :         while (xParaEnum->hasMoreElements())
                 [ +  + ]
     173                 :            :         {
     174 [ +  - ][ +  - ]:          6 :             xParaEnum->nextElement();
     175                 :          6 :             nRet++;
     176                 :            :         }
     177                 :          3 :         return nRet;
     178                 :            :     }
     179                 :            : 
     180                 :            :     // Get paragraph (counted from 1), optionally check it contains the given text.
     181                 :        243 :     uno::Reference< text::XTextRange > getParagraph( int number, OUString content = OUString() ) const
     182                 :            :     {
     183         [ +  - ]:        243 :         uno::Reference<text::XTextDocument> textDocument(mxComponent, uno::UNO_QUERY);
     184 [ +  - ][ +  - ]:        243 :         uno::Reference<container::XEnumerationAccess> paraEnumAccess(textDocument->getText(), uno::UNO_QUERY);
                 [ +  - ]
     185 [ +  - ][ +  - ]:        243 :         uno::Reference<container::XEnumeration> paraEnum = paraEnumAccess->createEnumeration();
     186         [ +  + ]:        510 :         for( int i = 1;
     187                 :            :              i < number;
     188                 :            :              ++i )
     189 [ +  - ][ +  - ]:        267 :             paraEnum->nextElement();
     190 [ +  - ][ +  - ]:        243 :         uno::Reference< text::XTextRange > paragraph( paraEnum->nextElement(), uno::UNO_QUERY );
                 [ +  - ]
     191         [ +  + ]:        243 :         if( !content.isEmpty())
     192 [ +  - ][ +  - ]:         21 :             CPPUNIT_ASSERT_EQUAL( content, paragraph->getString());
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     193                 :        243 :         return paragraph;
     194                 :            :     }
     195                 :            : 
     196                 :            :     /// Get run (counted from 1) of a paragraph, optionally check it contains the given text.
     197                 :        225 :     uno::Reference<text::XTextRange> getRun(uno::Reference<text::XTextRange> xParagraph, int number, OUString content = OUString()) const
     198                 :            :     {
     199         [ +  - ]:        225 :         uno::Reference<container::XEnumerationAccess> xRunEnumAccess(xParagraph, uno::UNO_QUERY);
     200 [ +  - ][ +  - ]:        225 :         uno::Reference<container::XEnumeration> xRunEnum = xRunEnumAccess->createEnumeration();
     201         [ +  + ]:        405 :         for (int i = 1; i < number; ++i)
     202 [ +  - ][ +  - ]:        180 :             xRunEnum->nextElement();
     203 [ +  - ][ +  - ]:        225 :         uno::Reference<text::XTextRange> xRun(xRunEnum->nextElement(), uno::UNO_QUERY);
                 [ +  - ]
     204         [ +  + ]:        225 :         if( !content.isEmpty())
     205 [ +  - ][ +  - ]:          9 :             CPPUNIT_ASSERT_EQUAL( content, xRun->getString());
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     206                 :        225 :         return xRun;
     207                 :            :     }
     208                 :            : 
     209                 :            :     /// Get math formula string of a run.
     210                 :        207 :     OUString getFormula(uno::Reference<text::XTextRange> xRun) const
     211                 :            :     {
     212         [ +  - ]:        207 :         uno::Reference<container::XContentEnumerationAccess> xContentEnumAccess(xRun, uno::UNO_QUERY);
     213 [ +  - ][ +  - ]:        207 :         uno::Reference<container::XEnumeration> xContentEnum(xContentEnumAccess->createContentEnumeration(""), uno::UNO_QUERY);
                 [ +  - ]
     214 [ +  - ][ +  - ]:        207 :         uno::Reference<beans::XPropertySet> xFormula(xContentEnum->nextElement(), uno::UNO_QUERY);
                 [ +  - ]
     215 [ +  - ][ +  - ]:        207 :         return getProperty<OUString>(getProperty< uno::Reference<beans::XPropertySet> >(xFormula, "Model"), "Formula");
     216                 :            :     }
     217                 :            : 
     218                 :            :     uno::Reference<lang::XComponent> mxComponent;
     219                 :            :     xmlBufferPtr mpXmlBuffer;
     220                 :            : };
     221                 :            : 
     222                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10