nss: upgrade to release 3.73
[LibreOffice.git] / test / source / bootstrapfixture.cxx
blob4048858c77be5314bd6d5535ccdf00eb6fa52caa
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 <vcl/errinf.hxx>
14 #include <sal/log.hxx>
15 #include <comphelper/processfactory.hxx>
17 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
18 #include <com/sun/star/ucb/XContentProvider.hpp>
19 #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
21 #include <vcl/svapp.hxx>
22 #include <tools/link.hxx>
23 #include <vcl/graphicfilter.hxx>
24 #include <osl/file.hxx>
25 #include <osl/process.h>
26 #include <unotools/tempfile.hxx>
27 #include <vcl/salgtype.hxx>
28 #include <vcl/scheduler.hxx>
29 #include <vcl/virdev.hxx>
31 #include <memory>
32 #include <cstring>
34 #include "setupvcl.hxx"
36 using namespace ::com::sun::star;
38 static void aBasicErrorFunc( const OUString &rErr, const OUString &rAction )
40 OString aErr = "Unexpected dialog: " +
41 OUStringToOString( rAction, RTL_TEXTENCODING_ASCII_US ) +
42 " Error: " +
43 OUStringToOString( rErr, RTL_TEXTENCODING_ASCII_US );
44 CPPUNIT_ASSERT_MESSAGE( aErr.getStr(), false);
47 // NB. this constructor is called before any tests are run, once for each
48 // test function in a rather non-intuitive way. This is why all the 'real'
49 // heavy lifting is deferred until setUp. setUp and tearDown are interleaved
50 // between the tests as you might expect.
51 test::BootstrapFixture::BootstrapFixture( bool bAssertOnDialog, bool bNeedUCB )
52 : m_bNeedUCB( bNeedUCB )
53 , m_bAssertOnDialog( bAssertOnDialog )
57 extern "C"
60 static void test_init_impl(bool bAssertOnDialog, bool bNeedUCB,
61 lang::XMultiServiceFactory * pSFactory)
63 if (bAssertOnDialog)
64 ErrorRegistry::RegisterDisplay( aBasicErrorFunc );
66 // Make GraphicConverter work, normally done in desktop::Desktop::Main()
67 Application::SetFilterHdl(
68 LINK(nullptr, test::BootstrapFixture, ImplInitFilterHdl));
70 if (bNeedUCB)
72 // initialise unconfigured UCB:
73 uno::Reference<ucb::XUniversalContentBroker> xUcb(pSFactory->createInstance("com.sun.star.ucb.UniversalContentBroker"), uno::UNO_QUERY_THROW);
74 uno::Reference<ucb::XContentProvider> xFileProvider(pSFactory->createInstance("com.sun.star.ucb.FileContentProvider"), uno::UNO_QUERY_THROW);
75 xUcb->registerContentProvider(xFileProvider, "file", true);
76 uno::Reference<ucb::XContentProvider> xTdocProvider(pSFactory->createInstance("com.sun.star.ucb.TransientDocumentsContentProvider"), uno::UNO_QUERY);
77 if (xTdocProvider.is())
79 xUcb->registerContentProvider(xTdocProvider, "vnd.sun.star.tdoc", true);
84 // this is called from pyuno
85 SAL_DLLPUBLIC_EXPORT void test_init(lang::XMultiServiceFactory *pFactory)
87 try
89 ::comphelper::setProcessServiceFactory(pFactory);
90 test::setUpVcl(true); // hard-code python tests to headless
91 test_init_impl(false, true, pFactory);
93 catch (...) { abort(); }
96 // this is called from pyuno
97 SAL_DLLPUBLIC_EXPORT void test_deinit()
99 DeInitVCL();
102 } // extern "C"
104 void test::BootstrapFixture::setUp()
106 test::BootstrapFixtureBase::setUp();
108 test_init_impl(m_bAssertOnDialog, m_bNeedUCB, m_xSFactory.get());
110 #if OSL_DEBUG_LEVEL > 0
111 Scheduler::ProcessEventsToIdle();
112 #endif
114 mxComponentContext.set(comphelper::getComponentContext(getMultiServiceFactory()));
117 test::BootstrapFixture::~BootstrapFixture()
121 #if HAVE_EXPORT_VALIDATION
122 namespace {
124 OString loadFile(const OUString& rURL)
126 osl::File aFile(rURL);
127 osl::FileBase::RC eStatus = aFile.open(osl_File_OpenFlag_Read);
128 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, eStatus);
129 sal_uInt64 nSize;
130 aFile.getSize(nSize);
131 std::unique_ptr<char[]> aBytes(new char[nSize]);
132 sal_uInt64 nBytesRead;
133 aFile.read(aBytes.get(), nSize, nBytesRead);
134 CPPUNIT_ASSERT_EQUAL(nSize, nBytesRead);
135 OString aContent(aBytes.get(), nBytesRead);
137 return aContent;
141 #endif
143 void test::BootstrapFixture::validate(const OUString& rPath, test::ValidationFormat eFormat) const
145 #if HAVE_EXPORT_VALIDATION
146 OUString var;
147 if( eFormat == test::OOXML )
149 var = "OFFICEOTRON";
151 else if ( eFormat == test::ODF )
153 var = "ODFVALIDATOR";
155 else if ( eFormat == test::MSBINARY )
157 #if HAVE_BFFVALIDATOR
158 var = "BFFVALIDATOR";
159 #else
160 // Binary Format Validator is disabled
161 return;
162 #endif
164 OUString aValidator;
165 oslProcessError e = osl_getEnvironment(var.pData, &aValidator.pData);
166 CPPUNIT_ASSERT_EQUAL_MESSAGE(
167 OUString("cannot get env var " + var).toUtf8().getStr(),
168 osl_Process_E_None, e);
169 CPPUNIT_ASSERT_MESSAGE(
170 OUString("empty get env var " + var).toUtf8().getStr(),
171 !aValidator.isEmpty());
173 if (eFormat == test::ODF)
175 // invoke without -e so that we know when something new is written
176 // in loext namespace that isn't yet in the custom schema
177 aValidator += " -M "
178 + m_directories.getPathFromSrc("/schema/libreoffice/OpenDocument-manifest-schema-v1.3+libreoffice.rng")
179 + " -D "
180 + m_directories.getPathFromSrc("/schema/libreoffice/OpenDocument-dsig-schema-v1.3+libreoffice.rng")
181 + " -O "
182 + m_directories.getPathFromSrc("/schema/libreoffice/OpenDocument-schema-v1.3+libreoffice.rng")
183 + " -m "
184 + m_directories.getPathFromSrc("/schema/mathml2/mathml2.xsd");
187 utl::TempFile aOutput;
188 aOutput.EnableKillingFile();
189 OUString aOutputFile = aOutput.GetFileName();
190 OUString aCommand = aValidator + " " + rPath + " > " + aOutputFile;
192 SAL_INFO("test", "BootstrapFixture::validate: executing '" << aCommand << "'");
193 int returnValue = system(OUStringToOString(aCommand, RTL_TEXTENCODING_UTF8).getStr());
195 OString aContentString = loadFile(aOutput.GetURL());
196 OUString aContentOUString = OStringToOUString(aContentString, RTL_TEXTENCODING_UTF8);
198 if( eFormat == test::OOXML && !aContentOUString.isEmpty() )
200 // check for validation errors here
201 sal_Int32 nIndex = aContentOUString.lastIndexOf("Grand total of errors in submitted package: ");
202 if(nIndex == -1)
204 SAL_WARN("test", "no summary line");
206 else
208 sal_Int32 nStartOfNumber = nIndex + std::strlen("Grand total of errors in submitted package: ");
209 OUString aNumber = aContentOUString.copy(nStartOfNumber);
210 sal_Int32 nErrors = aNumber.toInt32();
211 OString aMsg = "validation error in OOXML export: Errors: " + OString::number(nErrors);
212 if(nErrors)
214 SAL_WARN("test", aContentOUString);
216 CPPUNIT_ASSERT_EQUAL_MESSAGE(aMsg.getStr(), sal_Int32(0), nErrors);
219 else if( eFormat == test::ODF && !aContentOUString.isEmpty() )
221 if( aContentOUString.indexOf("Error") != -1 )
223 SAL_WARN("test", aContentOUString);
224 CPPUNIT_FAIL(aContentString.getStr());
227 CPPUNIT_ASSERT_EQUAL_MESSAGE(
228 OUStringToOString("failed to execute: " + aCommand + "\n" + aContentOUString,
229 RTL_TEXTENCODING_UTF8).getStr(), 0, returnValue);
230 #else
231 (void)rPath;
232 (void)eFormat;
233 #endif
236 IMPL_STATIC_LINK(
237 test::BootstrapFixture, ImplInitFilterHdl, ConvertData&, rData, bool)
239 return GraphicFilter::GetGraphicFilter().GetFilterCallback().Call( rData );
242 sal_uInt16 test::BootstrapFixture::getDefaultDeviceBitCount()
244 ScopedVclPtr<VirtualDevice> device
245 = VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT);
246 return device->GetBitCount();
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */