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

Generated by: LCOV version 1.11