LCOV - code coverage report
Current view: top level - sd/qa/unit - sdmodeltestbase.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 75 81 92.6 %
Date: 2014-04-11 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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             :  */
       9             : 
      10             : #ifndef INCLUDED_SD_QA_UNIT_SDMODELTESTBASE_HXX
      11             : #define INCLUDED_SD_QA_UNIT_SDMODELTESTBASE_HXX
      12             : 
      13             : #include <test/bootstrapfixture.hxx>
      14             : #include <test/xmldiff.hxx>
      15             : 
      16             : #include <unotest/filters-test.hxx>
      17             : #include <unotest/macros_test.hxx>
      18             : 
      19             : #include "drawdoc.hxx"
      20             : #include "../source/ui/inc/DrawDocShell.hxx"
      21             : #include "unotools/tempfile.hxx"
      22             : 
      23             : #include <rtl/strbuf.hxx>
      24             : #include <sfx2/docfile.hxx>
      25             : #include <sfx2/docfilt.hxx>
      26             : 
      27             : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
      28             : #include <drawinglayer/XShapeDumper.hxx>
      29             : 
      30             : using namespace ::com::sun::star;
      31             : 
      32             : struct FileFormat {
      33             :     const char* pName; const char* pFilterName; const char* pTypeName; const char* pUserData; sal_uLong nFormatType;
      34             : };
      35             : 
      36             : // These values are taken from "Flags" in filter/source/config/fragments/filters/*
      37             : #define ODP_FORMAT_TYPE  ( SFX_FILTER_IMPORT | SFX_FILTER_EXPORT | SFX_FILTER_TEMPLATE | SFX_FILTER_OWN | SFX_FILTER_DEFAULT | SFX_FILTER_ENCRYPTION | SFX_FILTER_PREFERED )
      38             : #define PPT_FORMAT_TYPE  ( SFX_FILTER_IMPORT | SFX_FILTER_EXPORT | SFX_FILTER_ALIEN )
      39             : #define PPTX_FORMAT_TYPE ( SFX_FILTER_IMPORT | SFX_FILTER_EXPORT | SFX_FILTER_ALIEN | SFX_FILTER_STARONEFILTER | SFX_FILTER_PREFERED )
      40             : 
      41             : /** List of file formats we support in Impress unit tests.
      42             : 
      43             : Taken from filter/source/config/fragments/filters/ too:
      44             : pName: The file extension.
      45             : pFilterName: <node oor:Name="...">
      46             : pTypeName: <prop oor:Name="UIName">...</prop>
      47             : nFormatType: <prop oor:name="Flags">...</prop>
      48             : */
      49             : FileFormat aFileFormats[] = {
      50             :     { "odp",  "impress8", "impress8", "", ODP_FORMAT_TYPE },
      51             :     { "ppt",  "MS PowerPoint 97", "Microsoft PowerPoint 97/2000/XP/2003", "sdfilt", PPT_FORMAT_TYPE },
      52             :     { "pptx", "Impress MS PowerPoint 2007 XML", "MS PowerPoint 2007 XML", "", PPTX_FORMAT_TYPE },
      53             :     { 0, 0, 0, 0, 0 }
      54             : };
      55             : 
      56             : #define ODP 0
      57             : #define PPT 1
      58             : #define PPTX 2
      59             : 
      60             : /// Base class for filter tests loading or roundtriping a document, and asserting the document model.
      61          21 : class SdModelTestBase : public test::BootstrapFixture, public unotest::MacrosTest
      62             : {
      63             : public:
      64          21 :     SdModelTestBase()
      65          21 :     {
      66          21 :     }
      67             : 
      68          21 :     virtual void setUp() SAL_OVERRIDE
      69             :     {
      70          21 :         test::BootstrapFixture::setUp();
      71             : 
      72             :         // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
      73             :         // which is a private symbol to us, gets called
      74          21 :         m_xDrawComponent = getMultiServiceFactory()->createInstance("com.sun.star.comp.Draw.PresentationDocument");
      75          21 :         CPPUNIT_ASSERT_MESSAGE("no impress component!", m_xDrawComponent.is());
      76          21 :     }
      77             : 
      78          21 :     virtual void tearDown() SAL_OVERRIDE
      79             :     {
      80          21 :         uno::Reference< lang::XComponent >( m_xDrawComponent, uno::UNO_QUERY_THROW )->dispose();
      81          21 :         test::BootstrapFixture::tearDown();
      82          21 :     }
      83             : 
      84             : protected:
      85             :     /// Load the document.
      86          35 :     ::sd::DrawDocShellRef loadURL( const OUString &rURL )
      87             :     {
      88          35 :         FileFormat *pFmt(0);
      89             : 
      90          84 :         for (size_t i = 0; i < SAL_N_ELEMENTS (aFileFormats); i++)
      91             :         {
      92          84 :             pFmt = aFileFormats + i;
      93          84 :             if (pFmt->pName &&  rURL.endsWithIgnoreAsciiCaseAsciiL (pFmt->pName, strlen (pFmt->pName)))
      94          35 :                 break;
      95             :         }
      96          35 :         CPPUNIT_ASSERT_MESSAGE( "missing filter info", pFmt->pName != NULL );
      97             : 
      98          35 :         sal_uInt32 nFormat = 0;
      99          35 :         if (pFmt->nFormatType)
     100          35 :             nFormat = SFX_FILTER_IMPORT | SFX_FILTER_USESOPTIONS;
     101             :         SfxFilter* aFilter = new SfxFilter(
     102             :             OUString::createFromAscii( pFmt->pFilterName ),
     103             :             OUString(), pFmt->nFormatType, nFormat,
     104             :             OUString::createFromAscii( pFmt->pTypeName ),
     105             :             0, OUString(),
     106             :             OUString::createFromAscii( pFmt->pUserData ),
     107          35 :             OUString("private:factory/simpress*") );
     108          35 :         aFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
     109             : 
     110          35 :         ::sd::DrawDocShellRef xDocShRef = new ::sd::DrawDocShell();
     111          35 :         SfxMedium* pSrcMed = new SfxMedium(rURL, STREAM_STD_READ);
     112          35 :         pSrcMed->SetFilter(aFilter);
     113          35 :         if ( !xDocShRef->DoLoad(pSrcMed) || !xDocShRef.Is() )
     114             :         {
     115           0 :             if (xDocShRef.Is())
     116           0 :                 xDocShRef->DoClose();
     117           0 :             CPPUNIT_ASSERT_MESSAGE( OUStringToOString( "failed to load " + rURL, RTL_TEXTENCODING_UTF8 ).getStr(), false );
     118             :         }
     119          35 :         CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
     120             : 
     121          35 :         return xDocShRef;
     122             :     }
     123             : 
     124           6 :     ::sd::DrawDocShellRef saveAndReload( ::sd::DrawDocShell *pShell, sal_Int32 nExportType )
     125             :     {
     126           6 :         FileFormat *pFmt = &aFileFormats[0];
     127           6 :         if( ( (sal_uInt32) nExportType ) < SAL_N_ELEMENTS( aFileFormats ) )
     128           6 :             pFmt = &aFileFormats[ nExportType ];
     129           6 :         OUString aExt = OUString( "." ) + OUString::createFromAscii( pFmt->pName );
     130          12 :         utl::TempFile aTempFile( OUString(), &aExt );
     131           6 :         aTempFile.EnableKillingFile();
     132          12 :         SfxMedium aStoreMedium( aTempFile.GetURL(), STREAM_STD_WRITE );
     133           6 :         sal_uInt32 nExportFormat = 0;
     134           6 :         if( pFmt->nFormatType == ODP_FORMAT_TYPE )
     135           2 :             nExportFormat = SFX_FILTER_EXPORT | SFX_FILTER_USESOPTIONS;
     136             :         SfxFilter* pExportFilter = new SfxFilter(
     137             :                 OUString::createFromAscii( pFmt->pFilterName ),
     138             :                 OUString(), pFmt->nFormatType, nExportFormat,
     139             :                 OUString::createFromAscii( pFmt->pTypeName ),
     140             :                 0, OUString(),
     141             :                 OUString::createFromAscii( pFmt->pUserData ),
     142           6 :                 OUString("private:factory/simpress*") );
     143           6 :         pExportFilter->SetVersion( SOFFICE_FILEFORMAT_CURRENT );
     144           6 :         aStoreMedium.SetFilter( pExportFilter );
     145           6 :         pShell->DoSaveAs( aStoreMedium );
     146           6 :         pShell->DoClose();
     147             : 
     148          12 :         return loadURL( aTempFile.GetURL() );
     149             :     }
     150             : 
     151             :     /** Dump shapes in xDocShRef, and compare the dump against content of pShapesDumpFileNameBase<number>.xml.
     152             : 
     153             :         @param bCreate Instead of comparing to the reference file(s), create it/them.
     154             :     */
     155           9 :     void compareWithShapesDump( ::sd::DrawDocShellRef xDocShRef, const OUString &rShapesDumpFileNameBase, bool bCreate = false )
     156             :     {
     157           9 :         CPPUNIT_ASSERT_MESSAGE( "failed to load", xDocShRef.Is() );
     158           9 :         CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
     159             : 
     160           9 :         uno::Reference<frame::XModel> xTempModel(xDocShRef->GetDoc()->getUnoModel(), uno::UNO_QUERY_THROW);
     161           9 :         CPPUNIT_ASSERT(xTempModel.is());
     162          18 :         uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier (xTempModel, uno::UNO_QUERY_THROW);
     163           9 :         CPPUNIT_ASSERT(xDrawPagesSupplier.is());
     164          18 :         uno::Reference< drawing::XDrawPages > xDrawPages = xDrawPagesSupplier->getDrawPages();
     165           9 :         CPPUNIT_ASSERT(xDrawPages.is());
     166             : 
     167           9 :         XShapeDumper xShapeDumper;
     168           9 :         sal_Int32 nLength = xDrawPages->getCount();
     169          27 :         for (sal_Int32 i = 0; i < nLength; ++i)
     170             :         {
     171          18 :             uno::Reference<drawing::XDrawPage> xDrawPage;
     172          36 :             uno::Any aAny = xDrawPages->getByIndex(i);
     173          18 :             aAny >>= xDrawPage;
     174          36 :             uno::Reference< drawing::XShapes > xShapes(xDrawPage, uno::UNO_QUERY_THROW);
     175          36 :             OUString aString = xShapeDumper.dump(xShapes);
     176             : 
     177          36 :             OStringBuffer aFileNameBuf( OUStringToOString( rShapesDumpFileNameBase, RTL_TEXTENCODING_UTF8 ) );
     178          18 :             aFileNameBuf.append(i);
     179          18 :             aFileNameBuf.append(".xml");
     180             : 
     181          36 :             OString aFileName = aFileNameBuf.makeStringAndClear();
     182             : 
     183          18 :             if ( bCreate )
     184             :             {
     185           0 :                 std::ofstream aStream( aFileName.getStr(), std::ofstream::out );
     186           0 :                 aStream << aString;
     187           0 :                 aStream.close();
     188             :             }
     189             :             else
     190             :             {
     191             :                 doXMLDiff(aFileName.getStr(),
     192             :                         OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr(),
     193          18 :                         static_cast<int>(aString.getLength()),
     194             :                         OUStringToOString(
     195             :                             getPathFromSrc("/sd/qa/unit/data/tolerance.xml"),
     196          36 :                             RTL_TEXTENCODING_UTF8).getStr());
     197             :             }
     198          18 :         }
     199          18 :         xDocShRef->DoClose();
     200           9 :     }
     201             : 
     202             : 
     203             : private:
     204             :     uno::Reference<uno::XInterface> m_xDrawComponent;
     205             : };
     206             : 
     207             : #endif
     208             : 
     209             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10