LCOV - code coverage report
Current view: top level - test/source - bootstrapfixture.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 43 52 82.7 %
Date: 2014-11-03 Functions: 11 13 84.6 %
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 <config_features.h>
      11             : 
      12             : #include <test/bootstrapfixture.hxx>
      13             : #include <tools/errinf.hxx>
      14             : #include <rtl/strbuf.hxx>
      15             : #include <rtl/bootstrap.hxx>
      16             : #include <cppuhelper/bootstrap.hxx>
      17             : #include <comphelper/processfactory.hxx>
      18             : 
      19             : #include <com/sun/star/lang/Locale.hpp>
      20             : #include <com/sun/star/lang/XComponent.hpp>
      21             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      22             : #include <com/sun/star/ucb/XContentProvider.hpp>
      23             : #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
      24             : 
      25             : #include <i18nlangtag/mslangid.hxx>
      26             : #include <vcl/svapp.hxx>
      27             : #include <tools/resmgr.hxx>
      28             : #include <vcl/graphicfilter.hxx>
      29             : #include <unotools/syslocaleoptions.hxx>
      30             : #include <osl/file.hxx>
      31             : #include <unotools/tempfile.hxx>
      32             : 
      33             : #include <boost/scoped_array.hpp>
      34             : #include <cstring>
      35             : 
      36             : using namespace ::com::sun::star;
      37             : 
      38           0 : static void aBasicErrorFunc( const OUString &rErr, const OUString &rAction )
      39             : {
      40           0 :     OStringBuffer aErr( "Unexpected dialog: " );
      41           0 :     aErr.append( OUStringToOString( rAction, RTL_TEXTENCODING_ASCII_US ) );
      42           0 :     aErr.append( " Error: " );
      43           0 :     aErr.append( OUStringToOString( rErr, RTL_TEXTENCODING_ASCII_US ) );
      44           0 :     CPPUNIT_ASSERT_MESSAGE( aErr.getStr(), false);
      45           0 : }
      46             : 
      47             : // NB. this constructor is called before any tests are run, once for each
      48             : // test function in a rather non-intuitive way. This is why all the 'real'
      49             : // heavy lifting is deferred until setUp. setUp and tearDown are interleaved
      50             : // between the tests as you might expect.
      51        4624 : test::BootstrapFixture::BootstrapFixture( bool bAssertOnDialog, bool bNeedUCB )
      52             :     : m_bNeedUCB( bNeedUCB )
      53        4624 :     , m_bAssertOnDialog( bAssertOnDialog )
      54             : {
      55        4624 : }
      56             : 
      57             : extern "C"
      58             : {
      59             : 
      60        4638 : void test_init_impl(bool bAssertOnDialog, bool bNeedUCB,
      61             :         lang::XMultiServiceFactory * pSFactory)
      62             : {
      63        4638 :     if (bAssertOnDialog)
      64        4590 :         ErrorHandler::RegisterDisplay( aBasicErrorFunc );
      65             : 
      66             :     // Make GraphicConverter work, normally done in desktop::Desktop::Main()
      67             :     Application::SetFilterHdl(
      68        4638 :             STATIC_LINK(0, test::BootstrapFixture, ImplInitFilterHdl));
      69             : 
      70        4638 :     if (bNeedUCB)
      71             :     {
      72             :         // initialise unconfigured UCB:
      73        4522 :         uno::Reference<ucb::XUniversalContentBroker> xUcb(pSFactory->createInstance("com.sun.star.ucb.UniversalContentBroker"), uno::UNO_QUERY_THROW);
      74        9044 :         uno::Reference<ucb::XContentProvider> xFileProvider(pSFactory->createInstance("com.sun.star.ucb.FileContentProvider"), uno::UNO_QUERY_THROW);
      75        4522 :         xUcb->registerContentProvider(xFileProvider, "file", sal_True);
      76        9044 :         uno::Reference<ucb::XContentProvider> xTdocProvider(pSFactory->createInstance("com.sun.star.ucb.TransientDocumentsContentProvider"), uno::UNO_QUERY);
      77        4522 :         if (xTdocProvider.is())
      78             :         {
      79        1198 :             xUcb->registerContentProvider(xTdocProvider, "vnd.sun.star.tdoc", sal_True);
      80        4522 :         }
      81             :     }
      82        4638 : }
      83             : 
      84             : // this is called from pyuno
      85          14 : SAL_DLLPUBLIC_EXPORT void test_init(lang::XMultiServiceFactory *pFactory)
      86             : {
      87             :     try
      88             :     {
      89          14 :         ::comphelper::setProcessServiceFactory(pFactory);
      90             : 
      91             :         // force locale (and resource files loaded) to en-US
      92          14 :         OUString aLangISO( "en-US" );
      93          14 :         ResMgr::SetDefaultLocale( LanguageTag( aLangISO) );
      94             : 
      95          28 :         SvtSysLocaleOptions aLocalOptions;
      96          14 :         aLocalOptions.SetLocaleConfigString( aLangISO );
      97          14 :         aLocalOptions.SetUILocaleConfigString( aLangISO );
      98             : 
      99          14 :         MsLangId::setConfiguredSystemUILanguage(LANGUAGE_ENGLISH_US);
     100          14 :         LanguageTag::setConfiguredSystemLanguage(LANGUAGE_ENGLISH_US);
     101             : 
     102          14 :         InitVCL();
     103          14 :         if (Application::IsHeadlessModeRequested())
     104           0 :             Application::EnableHeadlessMode(true);
     105             : 
     106          28 :         test_init_impl(false, true, pFactory);
     107             :     }
     108           0 :     catch (...) { abort(); }
     109          14 : }
     110             : 
     111             : } // extern "C"
     112             : 
     113        4624 : void test::BootstrapFixture::setUp()
     114             : {
     115        4624 :     test::BootstrapFixtureBase::setUp();
     116             : 
     117        4624 :     test_init_impl(m_bAssertOnDialog, m_bNeedUCB, m_xSFactory.get());
     118        4624 : }
     119             : 
     120        4624 : void test::BootstrapFixture::tearDown()
     121             : {
     122        4624 :     test::BootstrapFixtureBase::tearDown();
     123        4624 : }
     124             : 
     125        4624 : test::BootstrapFixture::~BootstrapFixture()
     126             : {
     127        4624 : }
     128             : 
     129             : #if HAVE_EXPORT_VALIDATION
     130             : namespace {
     131             : 
     132             : OString loadFile(const OUString& rURL)
     133             : {
     134             :     osl::File aFile(rURL);
     135             :     osl::FileBase::RC eStatus = aFile.open(osl_File_OpenFlag_Read);
     136             :     CPPUNIT_ASSERT_EQUAL(eStatus, osl::FileBase::E_None);
     137             :     sal_uInt64 nSize;
     138             :     aFile.getSize(nSize);
     139             :     boost::scoped_array<char> aBytes(new char[nSize]);
     140             :     sal_uInt64 nBytesRead;
     141             :     aFile.read(aBytes.get(), nSize, nBytesRead);
     142             :     CPPUNIT_ASSERT_EQUAL(nSize, nBytesRead);
     143             :     OString aContent(aBytes.get(), nBytesRead);
     144             : 
     145             :     return aContent;
     146             : }
     147             : 
     148             : }
     149             : #endif
     150             : 
     151         106 : void test::BootstrapFixture::validate(const OUString& rPath, test::ValidationFormat eFormat )
     152             : {
     153             :     (void)rPath;
     154             :     (void)eFormat;
     155             : 
     156             : #if HAVE_EXPORT_VALIDATION
     157             :     OUString aValidator;
     158             :     if( eFormat == test::OOXML )
     159             :     {
     160             :         aValidator = "officeotron ";
     161             :     }
     162             :     else
     163             :     {
     164             :         aValidator = "odfvalidator ";
     165             :     }
     166             : 
     167             :     utl::TempFile aOutput;
     168             :     aOutput.EnableKillingFile();
     169             :     OUString aOutputFile = aOutput.GetFileName();
     170             :     OUString aCommand = aValidator + rPath + " > " + aOutputFile;
     171             : 
     172             :     int returnValue = system(OUStringToOString(aCommand, RTL_TEXTENCODING_UTF8).getStr());
     173             :     CPPUNIT_ASSERT_EQUAL_MESSAGE(
     174             :         OUStringToOString(OUString("failed to execute: ") + aCommand,
     175             :             RTL_TEXTENCODING_UTF8).getStr(), 0, returnValue);
     176             : 
     177             :     OString aContentString = loadFile(aOutput.GetURL());
     178             :     OUString aContentOUString = OStringToOUString(aContentString, RTL_TEXTENCODING_UTF8);
     179             : 
     180             :     if( eFormat == test::OOXML && !aContentOUString.isEmpty() )
     181             :     {
     182             :         // check for validation errors here
     183             :         sal_Int32 nIndex = aContentOUString.lastIndexOf("Grand total of errors in submitted package: ");
     184             :         if(nIndex == -1)
     185             :         {
     186             :             SAL_WARN("test", "no summary line");
     187             :         }
     188             :         else
     189             :         {
     190             :             sal_Int32 nStartOfNumber = nIndex + std::strlen("Grand total of errors in submitted package: ");
     191             :             OUString aNumber = aContentOUString.copy(nStartOfNumber);
     192             :             sal_Int32 nErrors = aNumber.toInt32();
     193             :             OString aMsg("validation error in OOXML export: Errors: ");
     194             :             aMsg = aMsg + OString::number(nErrors);
     195             :             if(nErrors)
     196             :             {
     197             :                 SAL_WARN("test", aContentOUString);
     198             :             }
     199             :             CPPUNIT_ASSERT_EQUAL_MESSAGE(aMsg.getStr(), sal_Int32(0), nErrors);
     200             :         }
     201             :     }
     202             :     else if( eFormat == test::ODF && !aContentOUString.isEmpty() )
     203             :     {
     204             :         if( aContentOUString.indexOf("Error") != -1 )
     205             :         {
     206             :             SAL_WARN("test", aContentOUString);
     207             :             CPPUNIT_FAIL(aContentString.getStr());
     208             :         }
     209             :     }
     210             : #endif
     211         106 : }
     212             : 
     213         812 : IMPL_STATIC_LINK_NOINSTANCE(
     214             :         test::BootstrapFixture, ImplInitFilterHdl, ConvertData*, pData)
     215             : {
     216         406 :     return GraphicFilter::GetGraphicFilter().GetFilterCallback().Call( pData );
     217         642 : }
     218             : 
     219             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10