Branch libreoffice-5-0-4
[LibreOffice.git] / test / source / bootstrapfixture.cxx
blob40a68ce1f6947874239d1c39ae55a8e09da80d59
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <config_features.h>
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>
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>
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>
33 #include <isheadless.hxx>
35 #include <boost/scoped_array.hpp>
36 #include <cstring>
38 using namespace ::com::sun::star;
40 static void aBasicErrorFunc( const OUString &rErr, const OUString &rAction )
42 OStringBuffer aErr( "Unexpected dialog: " );
43 aErr.append( OUStringToOString( rAction, RTL_TEXTENCODING_ASCII_US ) );
44 aErr.append( " Error: " );
45 aErr.append( OUStringToOString( rErr, RTL_TEXTENCODING_ASCII_US ) );
46 CPPUNIT_ASSERT_MESSAGE( aErr.getStr(), false);
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 test::BootstrapFixture::BootstrapFixture( bool bAssertOnDialog, bool bNeedUCB )
54 : m_bNeedUCB( bNeedUCB )
55 , m_bAssertOnDialog( bAssertOnDialog )
59 extern "C"
62 void test_init_impl(bool bAssertOnDialog, bool bNeedUCB,
63 lang::XMultiServiceFactory * pSFactory)
65 if (bAssertOnDialog)
66 ErrorHandler::RegisterDisplay( aBasicErrorFunc );
68 // Make GraphicConverter work, normally done in desktop::Desktop::Main()
69 Application::SetFilterHdl(
70 LINK(0, test::BootstrapFixture, ImplInitFilterHdl));
72 if (bNeedUCB)
74 // initialise unconfigured UCB:
75 uno::Reference<ucb::XUniversalContentBroker> xUcb(pSFactory->createInstance("com.sun.star.ucb.UniversalContentBroker"), uno::UNO_QUERY_THROW);
76 uno::Reference<ucb::XContentProvider> xFileProvider(pSFactory->createInstance("com.sun.star.ucb.FileContentProvider"), uno::UNO_QUERY_THROW);
77 xUcb->registerContentProvider(xFileProvider, "file", sal_True);
78 uno::Reference<ucb::XContentProvider> xTdocProvider(pSFactory->createInstance("com.sun.star.ucb.TransientDocumentsContentProvider"), uno::UNO_QUERY);
79 if (xTdocProvider.is())
81 xUcb->registerContentProvider(xTdocProvider, "vnd.sun.star.tdoc", sal_True);
86 // this is called from pyuno
87 SAL_DLLPUBLIC_EXPORT void test_init(lang::XMultiServiceFactory *pFactory)
89 try
91 ::comphelper::setProcessServiceFactory(pFactory);
93 // force locale (and resource files loaded) to en-US
94 OUString aLangISO( "en-US" );
95 ResMgr::SetDefaultLocale( LanguageTag( aLangISO) );
97 SvtSysLocaleOptions aLocalOptions;
98 aLocalOptions.SetLocaleConfigString( aLangISO );
99 aLocalOptions.SetUILocaleConfigString( aLangISO );
101 MsLangId::setConfiguredSystemUILanguage(LANGUAGE_ENGLISH_US);
102 LanguageTag::setConfiguredSystemLanguage(LANGUAGE_ENGLISH_US);
104 InitVCL();
105 if (test::isHeadless())
106 Application::EnableHeadlessMode(true);
108 test_init_impl(false, true, pFactory);
110 catch (...) { abort(); }
113 } // extern "C"
115 void test::BootstrapFixture::setUp()
117 test::BootstrapFixtureBase::setUp();
119 test_init_impl(m_bAssertOnDialog, m_bNeedUCB, m_xSFactory.get());
122 void test::BootstrapFixture::tearDown()
124 test::BootstrapFixtureBase::tearDown();
127 test::BootstrapFixture::~BootstrapFixture()
131 #if HAVE_EXPORT_VALIDATION
132 namespace {
134 OString loadFile(const OUString& rURL)
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);
147 return aContent;
151 #endif
153 void test::BootstrapFixture::validate(const OUString& rPath, test::ValidationFormat eFormat )
155 (void)rPath;
156 (void)eFormat;
158 #if HAVE_EXPORT_VALIDATION
159 OUString var;
160 if( eFormat == test::OOXML )
162 var = "OFFICEOTRON";
164 else
166 var = "ODFVALIDATOR";
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 += " ";
178 utl::TempFile aOutput;
179 aOutput.EnableKillingFile();
180 OUString aOutputFile = aOutput.GetFileName();
181 OUString aCommand = aValidator + rPath + " > " + aOutputFile;
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);
188 OString aContentString = loadFile(aOutput.GetURL());
189 OUString aContentOUString = OStringToOUString(aContentString, RTL_TEXTENCODING_UTF8);
191 if( eFormat == test::OOXML && !aContentOUString.isEmpty() )
193 // check for validation errors here
194 sal_Int32 nIndex = aContentOUString.lastIndexOf("Grand total of errors in submitted package: ");
195 if(nIndex == -1)
197 SAL_WARN("test", "no summary line");
199 else
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)
208 SAL_WARN("test", aContentOUString);
210 CPPUNIT_ASSERT_EQUAL_MESSAGE(aMsg.getStr(), sal_Int32(0), nErrors);
213 else if( eFormat == test::ODF && !aContentOUString.isEmpty() )
215 if( aContentOUString.indexOf("Error") != -1 )
217 SAL_WARN("test", aContentOUString);
218 CPPUNIT_FAIL(aContentString.getStr());
221 #endif
224 IMPL_STATIC_LINK(
225 test::BootstrapFixture, ImplInitFilterHdl, ConvertData*, pData)
227 return GraphicFilter::GetGraphicFilter().GetFilterCallback().Call( pData );
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */