1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
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
)
62 void test_init_impl(bool bAssertOnDialog
, bool bNeedUCB
,
63 lang::XMultiServiceFactory
* pSFactory
)
66 ErrorHandler::RegisterDisplay( aBasicErrorFunc
);
68 // Make GraphicConverter work, normally done in desktop::Desktop::Main()
69 Application::SetFilterHdl(
70 LINK(0, test::BootstrapFixture
, ImplInitFilterHdl
));
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
)
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
);
105 if (test::isHeadless())
106 Application::EnableHeadlessMode(true);
108 test_init_impl(false, true, pFactory
);
110 catch (...) { abort(); }
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
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
);
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
);
153 void test::BootstrapFixture::validate(const OUString
& rPath
, test::ValidationFormat eFormat
)
158 #if HAVE_EXPORT_VALIDATION
160 if( eFormat
== test::OOXML
)
166 var
= "ODFVALIDATOR";
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());
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: ");
197 SAL_WARN("test", "no summary line");
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
);
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());
225 test::BootstrapFixture
, ImplInitFilterHdl
, ConvertData
*, pData
)
227 return GraphicFilter::GetGraphicFilter().GetFilterCallback().Call( pData
);
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */