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