LCOV - code coverage report
Current view: top level - libreoffice/sc/qa/unit - subsequent_export-test.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 142 144 98.6 %
Date: 2012-12-17 Functions: 21 22 95.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             : #include <sal/config.h>
      11             : #include <unotest/filters-test.hxx>
      12             : #include <test/bootstrapfixture.hxx>
      13             : #include <rtl/strbuf.hxx>
      14             : #include <osl/file.hxx>
      15             : 
      16             : #include <sfx2/app.hxx>
      17             : #include <sfx2/docfilt.hxx>
      18             : #include <sfx2/docfile.hxx>
      19             : #include <sfx2/frame.hxx>
      20             : #include <sfx2/sfxmodelfactory.hxx>
      21             : #include <svl/stritem.hxx>
      22             : 
      23             : #include <unotools/tempfile.hxx>
      24             : #include <comphelper/storagehelper.hxx>
      25             : 
      26             : #define CALC_DEBUG_OUTPUT 0
      27             : #define TEST_BUG_FILES 0
      28             : 
      29             : #include "helper/qahelper.hxx"
      30             : 
      31             : #include "docsh.hxx"
      32             : #include "postit.hxx"
      33             : #include "patattr.hxx"
      34             : #include "scitems.hxx"
      35             : #include "document.hxx"
      36             : #include "cellform.hxx"
      37             : 
      38             : #define ODS_FORMAT_TYPE 50331943
      39             : #define XLS_FORMAT_TYPE 318767171
      40             : #define XLSX_FORMAT_TYPE 268959811
      41             : #define LOTUS123_FORMAT_TYPE 268435649
      42             : 
      43             : #define ODS     0
      44             : #define XLS     1
      45             : #define XLSX    2
      46             : #define LOTUS123 3
      47             : 
      48             : using namespace ::com::sun::star;
      49             : using namespace ::com::sun::star::uno;
      50             : 
      51             : namespace {
      52             : 
      53             : struct FileFormat {
      54             :     const char* pName; const char* pFilterName; const char* pTypeName; unsigned int nFormatType;
      55             : };
      56             : 
      57             : FileFormat aFileFormats[] = {
      58             :     { "ods" , "calc8", "", ODS_FORMAT_TYPE },
      59             :     { "xls" , "MS Excel 97", "calc_MS_EXCEL_97", XLS_FORMAT_TYPE },
      60             :     { "xlsx", "Calc MS Excel 2007 XML" , "MS Excel 2007 XML", XLSX_FORMAT_TYPE },
      61             :     { "123" , "Lotus", "calc_Lotus", LOTUS123_FORMAT_TYPE }
      62             : };
      63             : 
      64             : }
      65             : 
      66          12 : class ScExportTest : public test::BootstrapFixture
      67             : {
      68             : public:
      69             :     ScExportTest();
      70             : 
      71             :     virtual void setUp();
      72             :     virtual void tearDown();
      73             : 
      74             :     ScDocShellRef saveAndReload( ScDocShell*, const rtl::OUString&, const rtl::OUString&, const rtl::OUString&, sal_uLong );
      75             :     ScDocShellRef saveAndReloadPassword( ScDocShell*, const rtl::OUString&, const rtl::OUString&, const rtl::OUString&, sal_uLong );
      76             : 
      77             :     void test();
      78             :     void testPasswordExport();
      79             :     void testConditionalFormatExportXLSX();
      80             : 
      81           4 :     CPPUNIT_TEST_SUITE(ScExportTest);
      82           2 :     CPPUNIT_TEST(test);
      83             : #if !defined(MACOSX) && !defined(DRAGONFLY) && !defined(WNT)
      84           2 :     CPPUNIT_TEST(testPasswordExport);
      85             : #endif
      86           2 :     CPPUNIT_TEST(testConditionalFormatExportXLSX);
      87           4 :     CPPUNIT_TEST_SUITE_END();
      88             : 
      89             : private:
      90             :     rtl::OUString m_aBaseString;
      91             :     uno::Reference<uno::XInterface> m_xCalcComponent;
      92             : 
      93             :     ScDocShellRef load(
      94             :         const OUString& rURL, const OUString& rFilter, const OUString &rUserData,
      95             :         const OUString& rTypeName, sal_Int32 nFormat, sal_uLong nFormatType,
      96             :         const OUString* pPassword = NULL );
      97             : 
      98             :     ScDocShellRef saveAndReload( ScDocShell* pShell, sal_Int32 nFormat );
      99             :     ScDocShellRef loadDocument( const rtl::OUString& rFileNameBase, sal_Int32 nFormat );
     100             :     void createFileURL( const rtl::OUString& aFileBase, const rtl::OUString& rFileExtension, rtl::OUString& rFilePath);
     101             :     void createCSVPath(const rtl::OUString& rFileBase, rtl::OUString& rCSVPath);
     102             : };
     103             : 
     104           2 : void ScExportTest::createFileURL(const rtl::OUString& aFileBase, const rtl::OUString& aFileExtension, rtl::OUString& rFilePath)
     105             : {
     106           2 :     rtl::OUString aSep("/");
     107           2 :     rtl::OUStringBuffer aBuffer( getSrcRootURL() );
     108           2 :     aBuffer.append(m_aBaseString).append(aSep).append(aFileExtension);
     109           2 :     aBuffer.append(aSep).append(aFileBase).append(aFileExtension);
     110           2 :     rFilePath = aBuffer.makeStringAndClear();
     111           2 : }
     112             : 
     113           2 : void ScExportTest::createCSVPath(const rtl::OUString& aFileBase, rtl::OUString& rCSVPath)
     114             : {
     115           2 :     rtl::OUStringBuffer aBuffer(getSrcRootPath());
     116           2 :     aBuffer.append(m_aBaseString).append(rtl::OUString("/contentCSV/"));
     117           2 :     aBuffer.append(aFileBase).append(rtl::OUString("csv"));
     118           2 :     rCSVPath = aBuffer.makeStringAndClear();
     119           2 : }
     120             : 
     121           2 : ScDocShellRef ScExportTest::saveAndReloadPassword(ScDocShell* pShell, const rtl::OUString &rFilter,
     122             :     const rtl::OUString &rUserData, const rtl::OUString& rTypeName, sal_uLong nFormatType)
     123             : {
     124           2 :     utl::TempFile aTempFile;
     125           2 :     aTempFile.EnableKillingFile();
     126           2 :     SfxMedium aStoreMedium( aTempFile.GetURL(), STREAM_STD_WRITE );
     127           2 :     sal_uInt32 nExportFormat = 0;
     128           2 :     if (nFormatType)
     129           2 :         nExportFormat = SFX_FILTER_EXPORT | SFX_FILTER_USESOPTIONS;
     130             :     SfxFilter* pExportFilter = new SfxFilter(
     131             :         rFilter,
     132             :         rtl::OUString(), nFormatType, nExportFormat, rTypeName, 0, rtl::OUString(),
     133           2 :         rUserData, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("private:factory/scalc*")) );
     134           2 :     pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
     135           2 :     aStoreMedium.SetFilter(pExportFilter);
     136           2 :     SfxItemSet* pExportSet = aStoreMedium.GetItemSet();
     137           2 :     uno::Sequence< beans::NamedValue > aEncryptionData = comphelper::OStorageHelper::CreatePackageEncryptionData( rtl::OUString("test") );
     138           2 :     uno::Any xEncryptionData;
     139           2 :     xEncryptionData <<= aEncryptionData;
     140           2 :     pExportSet->Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA, xEncryptionData));
     141             : 
     142           2 :     uno::Reference< embed::XStorage > xMedStorage = aStoreMedium.GetStorage();
     143           2 :     ::comphelper::OStorageHelper::SetCommonStorageEncryptionData( xMedStorage, aEncryptionData );
     144             : 
     145           2 :     pShell->DoSaveAs( aStoreMedium );
     146           2 :     pShell->DoClose();
     147             : 
     148             :     //std::cout << "File: " << aTempFile.GetURL() << std::endl;
     149             : 
     150           2 :     sal_uInt32 nFormat = 0;
     151           2 :     if (nFormatType)
     152           2 :         nFormat = SFX_FILTER_IMPORT | SFX_FILTER_USESOPTIONS;
     153             : 
     154           2 :     OUString aPass("test");
     155           2 :     return load(aTempFile.GetURL(), rFilter, rUserData, rTypeName, nFormat, nFormatType, &aPass);
     156             : }
     157             : 
     158           4 : ScDocShellRef ScExportTest::saveAndReload(ScDocShell* pShell, const rtl::OUString &rFilter,
     159             :     const rtl::OUString &rUserData, const rtl::OUString& rTypeName, sal_uLong nFormatType)
     160             : {
     161             : 
     162           4 :     utl::TempFile aTempFile;
     163           4 :     aTempFile.EnableKillingFile();
     164           4 :     SfxMedium aStoreMedium( aTempFile.GetURL(), STREAM_STD_WRITE );
     165           4 :     sal_uInt32 nExportFormat = 0;
     166           4 :     if (nFormatType)
     167           4 :         nExportFormat = SFX_FILTER_EXPORT | SFX_FILTER_USESOPTIONS;
     168             :     SfxFilter* pExportFilter = new SfxFilter(
     169             :         rFilter,
     170             :         rtl::OUString(), nFormatType, nExportFormat, rTypeName, 0, rtl::OUString(),
     171           4 :         rUserData, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("private:factory/scalc*")) );
     172           4 :     pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
     173           4 :     aStoreMedium.SetFilter(pExportFilter);
     174           4 :     pShell->DoSaveAs( aStoreMedium );
     175           4 :     pShell->DoClose();
     176             : 
     177             :     //std::cout << "File: " << aTempFile.GetURL() << std::endl;
     178             : 
     179           4 :     sal_uInt32 nFormat = 0;
     180           4 :     if (nFormatType)
     181           4 :         nFormat = SFX_FILTER_IMPORT | SFX_FILTER_USESOPTIONS;
     182             : 
     183           4 :     return load(aTempFile.GetURL(), rFilter, rUserData, rTypeName, nFormat, nFormatType);
     184             : }
     185             : 
     186           8 : ScDocShellRef ScExportTest::load(
     187             :     const OUString& rURL, const OUString& rFilter, const OUString &rUserData,
     188             :     const OUString& rTypeName, sal_Int32 nFormat, sal_uLong nFormatType, const OUString* pPassword )
     189             : {
     190             :     SfxFilter* pFilter = new SfxFilter(
     191             :         rFilter,
     192             :         rtl::OUString(), nFormatType, nFormat, rTypeName, 0, rtl::OUString(),
     193           8 :         rUserData, OUString("private:factory/scalc*"));
     194           8 :     pFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
     195             : 
     196           8 :     ScDocShellRef xDocShRef = new ScDocShell;
     197           8 :     xDocShRef->GetDocument()->EnableUserInteraction(false);
     198           8 :     SfxMedium* pSrcMed = new SfxMedium(rURL, STREAM_STD_READ);
     199           8 :     pSrcMed->SetFilter(pFilter);
     200           8 :     pSrcMed->UseInteractionHandler(false);
     201           8 :     if (pPassword)
     202             :     {
     203           2 :         SfxItemSet* pSet = pSrcMed->GetItemSet();
     204           2 :         pSet->Put(SfxStringItem(SID_PASSWORD, *pPassword));
     205             :     }
     206           8 :     if (!xDocShRef->DoLoad(pSrcMed))
     207             :     {
     208           0 :         xDocShRef->DoClose();
     209             :         // load failed.
     210           0 :         xDocShRef.Clear();
     211             :     }
     212             : 
     213           8 :     return xDocShRef;
     214             : }
     215             : 
     216           4 : ScDocShellRef ScExportTest::saveAndReload( ScDocShell* pShell, sal_Int32 nFormat )
     217             : {
     218           4 :     rtl::OUString aFileExtension(aFileFormats[nFormat].pName, strlen(aFileFormats[nFormat].pName), RTL_TEXTENCODING_UTF8 );
     219           4 :     rtl::OUString aFilterName(aFileFormats[nFormat].pFilterName, strlen(aFileFormats[nFormat].pFilterName), RTL_TEXTENCODING_UTF8) ;
     220           4 :     rtl::OUString aFilterType(aFileFormats[nFormat].pTypeName, strlen(aFileFormats[nFormat].pTypeName), RTL_TEXTENCODING_UTF8);
     221           4 :     ScDocShellRef xDocSh = saveAndReload(pShell, aFilterName, rtl::OUString(), aFilterType, aFileFormats[nFormat].nFormatType);
     222             : 
     223           4 :     CPPUNIT_ASSERT(xDocSh.Is());
     224           4 :     return xDocSh;
     225             : }
     226             : 
     227           2 : ScDocShellRef ScExportTest::loadDocument(const rtl::OUString& rFileName, sal_Int32 nFormat)
     228             : {
     229           2 :     rtl::OUString aFileExtension(aFileFormats[nFormat].pName, strlen(aFileFormats[nFormat].pName), RTL_TEXTENCODING_UTF8 );
     230           2 :     rtl::OUString aFilterName(aFileFormats[nFormat].pFilterName, strlen(aFileFormats[nFormat].pFilterName), RTL_TEXTENCODING_UTF8) ;
     231           2 :     rtl::OUString aFileName;
     232           2 :     createFileURL( rFileName, aFileExtension, aFileName );
     233           2 :     rtl::OUString aFilterType(aFileFormats[nFormat].pTypeName, strlen(aFileFormats[nFormat].pTypeName), RTL_TEXTENCODING_UTF8);
     234           2 :     unsigned int nFormatType = aFileFormats[nFormat].nFormatType;
     235           2 :     unsigned int nClipboardId = nFormatType ? SFX_FILTER_IMPORT | SFX_FILTER_USESOPTIONS : 0;
     236             : 
     237           2 :     return load(aFileName, aFilterName, OUString(), aFilterType, nClipboardId, nFormatType);
     238             : }
     239             : 
     240           2 : void ScExportTest::test()
     241             : {
     242             :     ScDocShell* pShell = new ScDocShell(
     243             :         SFXMODEL_STANDARD |
     244             :         SFXMODEL_DISABLE_EMBEDDED_SCRIPTS |
     245           2 :         SFXMODEL_DISABLE_DOCUMENT_RECOVERY);
     246           2 :     pShell->DoInitNew();
     247             : 
     248           2 :     ScDocument* pDoc = pShell->GetDocument();
     249             : 
     250           2 :     pDoc->SetValue(0,0,0, 1.0);
     251           2 :     CPPUNIT_ASSERT(pDoc);
     252             : 
     253           2 :     ScDocShellRef xDocSh = saveAndReload( pShell, ODS );
     254             : 
     255           2 :     CPPUNIT_ASSERT(xDocSh.Is());
     256           2 :     ScDocument* pLoadedDoc = xDocSh->GetDocument();
     257           2 :     double aVal = pLoadedDoc->GetValue(0,0,0);
     258           2 :     CPPUNIT_ASSERT_DOUBLES_EQUAL(aVal, 1.0, 1e-8);
     259           2 : }
     260             : 
     261           2 : void ScExportTest::testPasswordExport()
     262             : {
     263             :     ScDocShell* pShell = new ScDocShell(
     264             :         SFXMODEL_STANDARD |
     265             :         SFXMODEL_DISABLE_EMBEDDED_SCRIPTS |
     266           2 :         SFXMODEL_DISABLE_DOCUMENT_RECOVERY);
     267           2 :     pShell->DoInitNew();
     268             : 
     269           2 :     ScDocument* pDoc = pShell->GetDocument();
     270             : 
     271           2 :     pDoc->SetValue(0,0,0, 1.0);
     272           2 :     CPPUNIT_ASSERT(pDoc);
     273             : 
     274           2 :     sal_Int32 nFormat = ODS;
     275           2 :     rtl::OUString aFileExtension(aFileFormats[nFormat].pName, strlen(aFileFormats[nFormat].pName), RTL_TEXTENCODING_UTF8 );
     276           2 :     rtl::OUString aFilterName(aFileFormats[nFormat].pFilterName, strlen(aFileFormats[nFormat].pFilterName), RTL_TEXTENCODING_UTF8) ;
     277           2 :     rtl::OUString aFilterType(aFileFormats[nFormat].pTypeName, strlen(aFileFormats[nFormat].pTypeName), RTL_TEXTENCODING_UTF8);
     278           2 :     ScDocShellRef xDocSh = saveAndReloadPassword(pShell, aFilterName, rtl::OUString(), aFilterType, aFileFormats[nFormat].nFormatType);
     279             : 
     280           2 :     CPPUNIT_ASSERT(xDocSh.Is());
     281           2 :     ScDocument* pLoadedDoc = xDocSh->GetDocument();
     282           2 :     double aVal = pLoadedDoc->GetValue(0,0,0);
     283           2 :     CPPUNIT_ASSERT_DOUBLES_EQUAL(aVal, 1.0, 1e-8);
     284           2 : }
     285             : 
     286           2 : void ScExportTest::testConditionalFormatExportXLSX()
     287             : {
     288           2 :     ScDocShellRef xShell = loadDocument("new_cond_format_test.", XLSX);
     289           2 :     CPPUNIT_ASSERT(xShell.Is());
     290             : 
     291           2 :     ScDocShellRef xDocSh = saveAndReload(&(*xShell), XLSX);
     292           2 :     CPPUNIT_ASSERT(xDocSh.Is());
     293           2 :     ScDocument* pDoc = xDocSh->GetDocument();
     294           2 :     rtl::OUString aCSVFile("new_cond_format_test.");
     295           2 :     rtl::OUString aCSVPath;
     296           2 :     createCSVPath( aCSVFile, aCSVPath );
     297           2 :     testCondFile(aCSVPath, pDoc, 0);
     298           2 : }
     299             : 
     300           6 : ScExportTest::ScExportTest()
     301           6 :       : m_aBaseString(RTL_CONSTASCII_USTRINGPARAM("/sc/qa/unit/data"))
     302             : {
     303           6 : }
     304             : 
     305           6 : void ScExportTest::setUp()
     306             : {
     307           6 :     test::BootstrapFixture::setUp();
     308             : 
     309             :     // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
     310             :     // which is a private symbol to us, gets called
     311             :     m_xCalcComponent =
     312          12 :         getMultiServiceFactory()->createInstance(rtl::OUString(
     313           6 :         RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.SpreadsheetDocument")));
     314           6 :     CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xCalcComponent.is());
     315           6 : }
     316             : 
     317           6 : void ScExportTest::tearDown()
     318             : {
     319           6 :     uno::Reference< lang::XComponent >( m_xCalcComponent, UNO_QUERY_THROW )->dispose();
     320           6 :     test::BootstrapFixture::tearDown();
     321           6 : }
     322             : 
     323           2 : CPPUNIT_TEST_SUITE_REGISTRATION(ScExportTest);
     324             : 
     325           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     326             : 
     327             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10