LCOV - code coverage report
Current view: top level - sd/qa/unit - sdmodeltestbase.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 92 99 92.9 %
Date: 2015-06-13 12:38:46 Functions: 10 11 90.9 %
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             : #include <svl/itemset.hxx>
      27             : 
      28             : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
      29             : #include <drawinglayer/XShapeDumper.hxx>
      30             : 
      31             : using namespace ::com::sun::star;
      32             : 
      33             : struct FileFormat
      34             : {
      35             :     const char* pName;
      36             :     const char* pFilterName;
      37             :     const char* pTypeName;
      38             :     const char* pUserData;
      39             :     SfxFilterFlags nFormatType;
      40             : };
      41             : 
      42             : // These values are taken from "Flags" in filter/source/config/fragments/filters/*
      43             : // You need to turn value of oor:name="Flags" to SfxFilterFlags::*, see
      44             : // include/comphelper/documentconstants.hxx for the possible values.
      45             : // Note: 3RDPARTYFILTER == SfxFilterFlags::STARONEFILTER
      46             : #define ODP_FORMAT_TYPE  ( SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::TEMPLATE | SfxFilterFlags::OWN | SfxFilterFlags::DEFAULT | SfxFilterFlags::ENCRYPTION | SfxFilterFlags::PREFERED )
      47             : #define PPT_FORMAT_TYPE  ( SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN )
      48             : #define PPTX_FORMAT_TYPE ( SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::STARONEFILTER | SfxFilterFlags::PREFERED )
      49             : #define HTML_FORMAT_TYPE ( SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN )
      50             : #define PDF_FORMAT_TYPE  ( SfxFilterFlags::STARONEFILTER | SfxFilterFlags::ALIEN | SfxFilterFlags::IMPORT | SfxFilterFlags::PREFERED )
      51             : 
      52             : /** List of file formats we support in Impress unit tests.
      53             : 
      54             : Taken from filter/source/config/fragments/filters/ too:
      55             : pName: The file extension.
      56             : pFilterName: <node oor:Name="...">
      57             : pTypeName: <prop oor:Name="UIName">...</prop>
      58             : nFormatType: <prop oor:name="Flags">...</prop>
      59             : */
      60             : FileFormat aFileFormats[] =
      61             : {
      62           6 :     { "odp",  "impress8", "impress8", "", ODP_FORMAT_TYPE },
      63           6 :     { "ppt",  "MS PowerPoint 97", "Microsoft PowerPoint 97/2000/XP/2003", "sdfilt", PPT_FORMAT_TYPE },
      64           6 :     { "pptx", "Impress Office Open XML", "Office Open XML Presentation", "", PPTX_FORMAT_TYPE },
      65           6 :     { "html", "graphic_HTML", "graphic_HTML", "", HTML_FORMAT_TYPE },
      66           6 :     { "pdf",  "draw_pdf_import", "pdf_Portable_Document_Format", "", PDF_FORMAT_TYPE },
      67             :     { 0, 0, 0, 0, SfxFilterFlags::NONE }
      68          15 : };
      69             : 
      70             : #define ODP  0
      71             : #define PPT  1
      72             : #define PPTX 2
      73             : #define HTML 3
      74             : #define PDF  4
      75             : 
      76             : /// Base class for filter tests loading or roundtriping a document, and asserting the document model.
      77          63 : class SdModelTestBase : public test::BootstrapFixture, public unotest::MacrosTest
      78             : {
      79             : private:
      80             :     uno::Reference<uno::XInterface> mxDrawComponent;
      81             : 
      82             : public:
      83          63 :     SdModelTestBase()
      84          63 :     {}
      85             : 
      86          63 :     virtual void setUp() SAL_OVERRIDE
      87             :     {
      88          63 :         test::BootstrapFixture::setUp();
      89             : 
      90             :         // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
      91             :         // which is a private symbol to us, gets called
      92          63 :         mxDrawComponent = getMultiServiceFactory()->createInstance("com.sun.star.comp.Draw.PresentationDocument");
      93          63 :         CPPUNIT_ASSERT_MESSAGE("no impress component!", mxDrawComponent.is());
      94          63 :     }
      95             : 
      96          63 :     virtual void tearDown() SAL_OVERRIDE
      97             :     {
      98          63 :         uno::Reference<lang::XComponent>(mxDrawComponent, uno::UNO_QUERY_THROW)->dispose();
      99          63 :         test::BootstrapFixture::tearDown();
     100          63 :     }
     101             : 
     102             : protected:
     103             :     /// Load the document.
     104         114 :     sd::DrawDocShellRef loadURL( const OUString &rURL, sal_Int32 nFormat, SfxAllItemSet *pParams = 0 )
     105             :     {
     106         114 :         FileFormat *pFmt = getFormat(nFormat);
     107         114 :         CPPUNIT_ASSERT_MESSAGE( "missing filter info", pFmt->pName != NULL );
     108             : 
     109         114 :         SotClipboardFormatId nOptions = SotClipboardFormatId::NONE;
     110         114 :         if (pFmt->nFormatType != SfxFilterFlags::NONE)
     111         114 :             nOptions = SotClipboardFormatId::STARCALC_8;
     112             :         SfxFilter* aFilter = new SfxFilter(
     113             :             OUString::createFromAscii( pFmt->pFilterName ),
     114             :             OUString(), pFmt->nFormatType, nOptions,
     115             :             OUString::createFromAscii( pFmt->pTypeName ),
     116             :             0, OUString(),
     117             :             OUString::createFromAscii( pFmt->pUserData ),
     118         114 :             OUString("private:factory/simpress*") );
     119         114 :         aFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
     120             : 
     121         114 :         ::sd::DrawDocShellRef xDocShRef = new ::sd::DrawDocShell();
     122         114 :         SfxMedium* pSrcMed = new SfxMedium(rURL, STREAM_STD_READ, aFilter, pParams);
     123         114 :         if ( !xDocShRef->DoLoad(pSrcMed) || !xDocShRef.Is() )
     124             :         {
     125           0 :             if (xDocShRef.Is())
     126           0 :                 xDocShRef->DoClose();
     127           0 :             CPPUNIT_ASSERT_MESSAGE( OUStringToOString( "failed to load " + rURL, RTL_TEXTENCODING_UTF8 ).getStr(), false );
     128             :         }
     129         114 :         CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
     130             : 
     131         114 :         return xDocShRef;
     132             :     }
     133             : 
     134         141 :     FileFormat* getFormat(sal_Int32 nExportType)
     135             :     {
     136         141 :         FileFormat* pFormat = &aFileFormats[0];
     137         141 :         if (((sal_uInt32) nExportType) < SAL_N_ELEMENTS(aFileFormats))
     138         141 :             pFormat = &aFileFormats[nExportType];
     139         141 :         return pFormat;
     140             :     }
     141             : 
     142           1 :     void exportTo(sd::DrawDocShell* pShell, FileFormat* pFormat, utl::TempFile& rTempFile)
     143             :     {
     144           1 :         SfxMedium aStoreMedium(rTempFile.GetURL(), STREAM_STD_WRITE);
     145           1 :         SotClipboardFormatId nExportFormat = SotClipboardFormatId::NONE;
     146           1 :         if (pFormat->nFormatType == ODP_FORMAT_TYPE)
     147           0 :             nExportFormat = SotClipboardFormatId::STARCALC_8;
     148             :         SfxFilter* pExportFilter = new SfxFilter(
     149             :                                         OUString::createFromAscii(pFormat->pFilterName),
     150             :                                         OUString(), pFormat->nFormatType, nExportFormat,
     151             :                                         OUString::createFromAscii(pFormat->pTypeName),
     152             :                                         0, OUString(),
     153             :                                         OUString::createFromAscii(pFormat->pUserData),
     154           1 :                                         OUString("private:factory/simpress*") );
     155           1 :         pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
     156           1 :         aStoreMedium.SetFilter(pExportFilter);
     157           1 :         pShell->ConvertTo(aStoreMedium);
     158           1 :         pShell->DoClose();
     159           1 :     }
     160             : 
     161          26 :     void save(sd::DrawDocShell* pShell, FileFormat* pFormat, utl::TempFile& rTempFile)
     162             :     {
     163          26 :         SfxMedium aStoreMedium(rTempFile.GetURL(), STREAM_STD_WRITE);
     164          26 :         SotClipboardFormatId nExportFormat = SotClipboardFormatId::NONE;
     165          26 :         if (pFormat->nFormatType == ODP_FORMAT_TYPE)
     166           3 :             nExportFormat = SotClipboardFormatId::STARCHART_8;
     167             :         SfxFilter* pExportFilter = new SfxFilter(
     168             :                                         OUString::createFromAscii(pFormat->pFilterName),
     169             :                                         OUString(), pFormat->nFormatType, nExportFormat,
     170             :                                         OUString::createFromAscii(pFormat->pTypeName),
     171             :                                         0, OUString(),
     172             :                                         OUString::createFromAscii(pFormat->pUserData),
     173          26 :                                         OUString("private:factory/simpress*") );
     174          26 :         pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
     175          26 :         aStoreMedium.SetFilter(pExportFilter);
     176          26 :         pShell->DoSaveAs(aStoreMedium);
     177          26 :         pShell->DoClose();
     178          26 :     }
     179             : 
     180          26 :     sd::DrawDocShellRef saveAndReload(sd::DrawDocShell *pShell, sal_Int32 nExportType)
     181             :     {
     182          26 :         FileFormat* pFormat = getFormat(nExportType);
     183          26 :         utl::TempFile aTempFile;
     184          26 :         save(pShell, pFormat, aTempFile);
     185          26 :         if(nExportType == ODP)
     186             :         {
     187             :             // BootstrapFixture::validate(aTempFile.GetFileName(), test::ODF);
     188             :         }
     189          23 :         else if(nExportType == PPTX)
     190             :         {
     191          23 :             BootstrapFixture::validate(aTempFile.GetFileName(), test::OOXML);
     192             :         }
     193          26 :         aTempFile.EnableKillingFile();
     194          26 :         return loadURL(aTempFile.GetURL(), nExportType);
     195             :     }
     196             : 
     197             :     /** Dump shapes in xDocShRef, and compare the dump against content of pShapesDumpFileNameBase<number>.xml.
     198             : 
     199             :         @param bCreate Instead of comparing to the reference file(s), create it/them.
     200             :     */
     201          12 :     void compareWithShapesDump( ::sd::DrawDocShellRef xDocShRef, const OUString &rShapesDumpFileNameBase, bool bCreate = false )
     202             :     {
     203          12 :         CPPUNIT_ASSERT_MESSAGE( "failed to load", xDocShRef.Is() );
     204          12 :         CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
     205             : 
     206          12 :         uno::Reference<frame::XModel> xTempModel(xDocShRef->GetDoc()->getUnoModel(), uno::UNO_QUERY_THROW);
     207          12 :         CPPUNIT_ASSERT(xTempModel.is());
     208          24 :         uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier (xTempModel, uno::UNO_QUERY_THROW);
     209          12 :         CPPUNIT_ASSERT(xDrawPagesSupplier.is());
     210          24 :         uno::Reference< drawing::XDrawPages > xDrawPages = xDrawPagesSupplier->getDrawPages();
     211          12 :         CPPUNIT_ASSERT(xDrawPages.is());
     212             : 
     213          12 :         XShapeDumper xShapeDumper;
     214          12 :         sal_Int32 nLength = xDrawPages->getCount();
     215          33 :         for (sal_Int32 i = 0; i < nLength; ++i)
     216             :         {
     217          21 :             uno::Reference<drawing::XDrawPage> xDrawPage;
     218          42 :             uno::Any aAny = xDrawPages->getByIndex(i);
     219          21 :             aAny >>= xDrawPage;
     220          42 :             uno::Reference< drawing::XShapes > xShapes(xDrawPage, uno::UNO_QUERY_THROW);
     221          42 :             OUString aString = XShapeDumper::dump(xShapes);
     222             : 
     223          42 :             OStringBuffer aFileNameBuf( OUStringToOString( rShapesDumpFileNameBase, RTL_TEXTENCODING_UTF8 ) );
     224          21 :             aFileNameBuf.append(i);
     225          21 :             aFileNameBuf.append(".xml");
     226             : 
     227          42 :             OString aFileName = aFileNameBuf.makeStringAndClear();
     228             : 
     229          21 :             if ( bCreate )
     230             :             {
     231           0 :                 std::ofstream aStream( aFileName.getStr(), std::ofstream::out );
     232           0 :                 aStream << aString;
     233           0 :                 aStream.close();
     234             :             }
     235             :             else
     236             :             {
     237             :                 doXMLDiff(aFileName.getStr(),
     238             :                         OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr(),
     239             :                         static_cast<int>(aString.getLength()),
     240             :                         OUStringToOString(
     241             :                             getPathFromSrc("/sd/qa/unit/data/tolerance.xml"),
     242          21 :                             RTL_TEXTENCODING_UTF8).getStr());
     243             :             }
     244          21 :         }
     245          24 :         xDocShRef->DoClose();
     246          12 :     }
     247             : };
     248             : 
     249             : #endif
     250             : 
     251             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11