LCOV - code coverage report
Current view: top level - sw/qa/core - filters-test.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 62 62 100.0 %
Date: 2015-06-13 12:38:46 Functions: 17 18 94.4 %
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             : #include <unotest/filters-test.hxx>
      11             : #include <test/bootstrapfixture.hxx>
      12             : 
      13             : #include <cppuhelper/implbase1.hxx>
      14             : 
      15             : #include <comphelper/processfactory.hxx>
      16             : 
      17             : #include <sfx2/app.hxx>
      18             : #include <sfx2/docfilt.hxx>
      19             : #include <sfx2/docfile.hxx>
      20             : #include <sfx2/sfxmodelfactory.hxx>
      21             : #include <sfx2/sfxsids.hrc>
      22             : 
      23             : #include <svl/stritem.hxx>
      24             : #include <unotools/tempfile.hxx>
      25             : 
      26             : #include "init.hxx"
      27             : #include "iodetect.hxx"
      28             : #include "swtypes.hxx"
      29             : #include "doc.hxx"
      30             : #include "docsh.hxx"
      31             : #include "shellres.hxx"
      32             : #include "docufld.hxx"
      33             : 
      34             : typedef tools::SvRef<SwDocShell> SwDocShellRef;
      35             : 
      36             : using namespace ::com::sun::star;
      37             : 
      38             : /* Implementation of Filters test */
      39             : 
      40           3 : class SwFiltersTest
      41             :     : public test::FiltersTest
      42             :     , public test::BootstrapFixture
      43             : {
      44             : public:
      45             :     virtual bool load( const OUString &rFilter, const OUString &rURL,
      46             :         const OUString &rUserData, SfxFilterFlags nFilterFlags,
      47             :         SotClipboardFormatId nClipboardID, unsigned int nFilterVersion) SAL_OVERRIDE;
      48             :     virtual bool save( const OUString &rFilter, const OUString &rURL,
      49             :         const OUString &rUserData, SfxFilterFlags nFilterFlags,
      50             :         SotClipboardFormatId nClipboardID, unsigned int nFilterVersion) SAL_OVERRIDE;
      51             :     virtual void setUp() SAL_OVERRIDE;
      52             : 
      53             :     // Ensure CVEs remain unbroken
      54             :     void testCVEs();
      55             : 
      56           2 :     CPPUNIT_TEST_SUITE(SwFiltersTest);
      57           1 :     CPPUNIT_TEST(testCVEs);
      58           5 :     CPPUNIT_TEST_SUITE_END();
      59             : 
      60             : private:
      61             :     bool filter( const OUString &rFilter, const OUString &rURL,
      62             :         const OUString &rUserData, SfxFilterFlags nFilterFlags,
      63             :         SotClipboardFormatId nClipboardID, unsigned int nFilterVersion, bool bExport);
      64             :     uno::Reference<uno::XInterface> m_xWriterComponent;
      65             : };
      66             : 
      67          76 : bool SwFiltersTest::load(const OUString &rFilter, const OUString &rURL,
      68             :     const OUString &rUserData, SfxFilterFlags nFilterFlags,
      69             :         SotClipboardFormatId nClipboardID, unsigned int nFilterVersion)
      70             : {
      71          76 :     return filter(rFilter, rURL, rUserData, nFilterFlags, nClipboardID, nFilterVersion, false);
      72             : }
      73             : 
      74          12 : bool SwFiltersTest::save(const OUString &rFilter, const OUString &rURL,
      75             :     const OUString &rUserData, SfxFilterFlags nFilterFlags,
      76             :         SotClipboardFormatId nClipboardID, unsigned int nFilterVersion)
      77             : {
      78          12 :     return filter(rFilter, rURL, rUserData, nFilterFlags, nClipboardID, nFilterVersion, true);
      79             : }
      80             : 
      81          88 : bool SwFiltersTest::filter(const OUString &rFilter, const OUString &rURL,
      82             :     const OUString &rUserData, SfxFilterFlags nFilterFlags,
      83             :         SotClipboardFormatId nClipboardID, unsigned int nFilterVersion, bool bExport)
      84             : {
      85             :     SfxFilter* pFilter = new SfxFilter(
      86             :         rFilter, OUString(), nFilterFlags,
      87             :         nClipboardID, OUString(), 0, OUString(),
      88          88 :         rUserData, OUString());
      89          88 :     pFilter->SetVersion(nFilterVersion);
      90             : 
      91          88 :     SwDocShellRef xDocShRef = new SwDocShell;
      92          88 :     SfxMedium* pSrcMed = new SfxMedium(rURL, STREAM_STD_READ);
      93             : 
      94          88 :     const SfxFilter* pImportFilter = 0;
      95          88 :     SfxFilter* pExportFilter = 0;
      96          88 :     if (bExport)
      97             :     {
      98          12 :         SfxGetpApp()->GetFilterMatcher().GuessFilter(*pSrcMed, &pImportFilter, SfxFilterFlags::IMPORT, SfxFilterFlags::NONE);
      99          12 :         pExportFilter = pFilter;
     100             :     }
     101             :     else
     102          76 :         pImportFilter = pFilter;
     103             : 
     104          88 :     pSrcMed->SetFilter(pImportFilter);
     105             : 
     106          88 :     if (rUserData == FILTER_TEXT_DLG)
     107             :     {
     108             :         pSrcMed->GetItemSet()->Put(
     109           3 :             SfxStringItem(SID_FILE_FILTEROPTIONS, OUString("UTF8,LF,Liberation Mono,en-US")));
     110             :     }
     111             : 
     112          88 :     bool bLoaded = xDocShRef->DoLoad(pSrcMed);
     113          88 :     if (!bExport)
     114             :     {
     115          76 :         if (xDocShRef.Is())
     116          76 :             xDocShRef->DoClose();
     117          76 :         return bLoaded;
     118             :     }
     119             : 
     120             :     // How come an error may be set, and still DoLoad() returns success? Strange...
     121          12 :     if (bLoaded)
     122          12 :         xDocShRef->ResetError();
     123             : 
     124          24 :     utl::TempFile aTempFile;
     125          12 :     aTempFile.EnableKillingFile();
     126          24 :     SfxMedium aDstMed(aTempFile.GetURL(), STREAM_STD_WRITE);
     127          12 :     aDstMed.SetFilter(pExportFilter);
     128          12 :     bool bSaved = xDocShRef->DoSaveAs(aDstMed);
     129          12 :     if (xDocShRef.Is())
     130          12 :         xDocShRef->DoClose();
     131         100 :     return bSaved;
     132             : }
     133             : 
     134             : #define isstorage SotClipboardFormatId::STRING
     135             : 
     136           1 : void SwFiltersTest::testCVEs()
     137             : {
     138             :     testDir(OUString("StarOffice XML (Writer)"),
     139             :             getURLFromSrc("/sw/qa/core/data/xml/"),
     140             :             OUString(FILTER_XML),
     141           2 :             SfxFilterFlags::IMPORT | SfxFilterFlags::OWN | SfxFilterFlags::DEFAULT,
     142           1 :             isstorage, SOFFICE_FILEFORMAT_CURRENT);
     143             : 
     144             :     testDir(OUString("writer8"),
     145             :             getURLFromSrc("/sw/qa/core/data/odt/"),
     146             :             OUString(FILTER_XML),
     147           2 :             SfxFilterFlags::IMPORT | SfxFilterFlags::OWN | SfxFilterFlags::DEFAULT,
     148           1 :             isstorage, SOFFICE_FILEFORMAT_CURRENT);
     149             : 
     150             :     testDir(OUString("MS Word 97"),
     151             :             getURLFromSrc("/sw/qa/core/data/ww8/"),
     152           1 :             OUString(FILTER_WW8));
     153             : 
     154             :     testDir(OUString("MS WinWord 5"),
     155             :             getURLFromSrc("/sw/qa/core/data/ww5/"),
     156           1 :             OUString(sWW5));
     157             : 
     158             :     testDir(OUString("Text (encoded)"),
     159             :             getURLFromSrc("/sw/qa/core/data/txt/"),
     160           1 :             OUString(FILTER_TEXT_DLG));
     161             : 
     162             :     testDir(OUString("MS Word 2007 XML"),
     163             :             getURLFromSrc("/sw/qa/core/data/ooxml/"),
     164             :             OUString(),
     165           1 :             SfxFilterFlags::STARONEFILTER);
     166             : 
     167             :     testDir(OUString("Rich Text Format"),
     168             :             getURLFromSrc("/sw/qa/core/data/rtf/"),
     169             :             OUString(),
     170           1 :             SfxFilterFlags::STARONEFILTER);
     171             : 
     172             :     testDir(OUString("HTML"),
     173             :             getURLFromSrc("/sw/qa/core/data/html/"),
     174           1 :             OUString(sHTML));
     175             : 
     176             :     testDir("T602Document",
     177             :             getURLFromSrc("/sw/qa/core/data/602/"),
     178             :             OUString(),
     179           1 :             SfxFilterFlags::STARONEFILTER);
     180             : 
     181             :     testDir("Rich Text Format",
     182             :             getURLFromSrc("/sw/qa/core/exportdata/rtf/"),
     183             :             OUString(),
     184             :             SfxFilterFlags::STARONEFILTER,
     185             :             SotClipboardFormatId::NONE,
     186             :             0,
     187           1 :             /*bExport=*/true);
     188             : 
     189             :     testDir("HTML",
     190             :             getURLFromSrc("/sw/qa/core/exportdata/html/"),
     191             :             OUString(sHTML),
     192             :             SfxFilterFlags::NONE,
     193             :             SotClipboardFormatId::NONE,
     194             :             0,
     195           1 :             /*bExport=*/true);
     196             : 
     197             :     testDir("MS Word 2007 XML",
     198             :             getURLFromSrc("/sw/qa/core/exportdata/ooxml/"),
     199             :             OUString(),
     200             :             SfxFilterFlags::STARONEFILTER,
     201             :             SotClipboardFormatId::NONE,
     202             :             0,
     203           1 :             /*bExport=*/true);
     204             : 
     205           1 : }
     206             : 
     207           1 : void SwFiltersTest::setUp()
     208             : {
     209           1 :     test::BootstrapFixture::setUp();
     210             : 
     211             :     //This is a bit of a fudge, we do this to ensure that SwGlobals::ensure,
     212             :     //which is a private symbol to us, gets called
     213           2 :     m_xWriterComponent =
     214           2 :         getMultiServiceFactory()->createInstance(OUString(
     215           2 :         "com.sun.star.comp.Writer.TextDocument"));
     216           1 :     CPPUNIT_ASSERT_MESSAGE("no writer component!", m_xWriterComponent.is());
     217           1 : }
     218             : 
     219           1 : CPPUNIT_TEST_SUITE_REGISTRATION(SwFiltersTest);
     220             : 
     221           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     222             : 
     223             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11