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