Branch libreoffice-5-0-4
[LibreOffice.git] / test / source / vclbootstrapprotector.cxx
blob2aad56303df6644eba4d97af68e3c7e200d52033
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 <sal/config.h>
12 #include <boost/noncopyable.hpp>
13 #include <com/sun/star/configuration/theDefaultProvider.hpp>
14 #include <com/sun/star/lang/XComponent.hpp>
15 #include <com/sun/star/util/XFlushable.hpp>
16 #include <comphelper/processfactory.hxx>
17 #include <cppunit/Protector.h>
18 #include <cppunittester/protectorfactory.hxx>
19 #include <i18nlangtag/lang.h>
20 #include <i18nlangtag/languagetag.hxx>
21 #include <i18nlangtag/mslangid.hxx>
22 #include <sal/types.h>
23 #include <tools/resmgr.hxx>
24 #include <unotools/configmgr.hxx>
25 #include <unotools/syslocaleoptions.hxx>
26 #include <vcl/svapp.hxx>
28 #include <isheadless.hxx>
30 namespace {
32 class Protector: public CppUnit::Protector, private boost::noncopyable {
33 public:
34 Protector() {
35 // Force locale (and resource files loaded) to en-US:
36 ResMgr::SetDefaultLocale(LanguageTag("en-US"));
37 SvtSysLocaleOptions localOptions;
38 localOptions.SetLocaleConfigString("en-US");
39 localOptions.SetUILocaleConfigString("en-US");
40 MsLangId::setConfiguredSystemUILanguage(LANGUAGE_ENGLISH_US);
41 LanguageTag::setConfiguredSystemLanguage(LANGUAGE_ENGLISH_US);
42 InitVCL();
43 if (test::isHeadless()) {
44 Application::EnableHeadlessMode(true);
46 Application::setDeInitHook(LINK(this, Protector, deinitHook));
49 private:
50 virtual ~Protector() {
51 DeInitVCL();
54 virtual bool protect(
55 CppUnit::Functor const & functor, CppUnit::ProtectorContext const &)
56 SAL_OVERRIDE
57 { return functor(); }
59 DECL_STATIC_LINK(Protector, deinitHook, void *);
62 // HACK so that defaultBootstrap_InitialComponentContext (in
63 // unobootstrapprotector) is called before InitVCL (above), but component
64 // context is disposed (redundantly again in unobootstrapprotector) from within
65 // DeInitVCL (cf. Desktop::DeInit, desktop/source/app/app.cxx):
66 IMPL_STATIC_LINK_NOARG(Protector, deinitHook) {
67 css::uno::Reference<css::uno::XComponentContext> context;
68 try {
69 context = comphelper::getProcessComponentContext();
70 } catch (css::uno::RuntimeException &) {}
71 if (context.is()) {
72 css::uno::Reference<css::lang::XMultiServiceFactory> config;
73 try {
74 config = css::configuration::theDefaultProvider::get(context);
75 } catch (css::uno::DeploymentException &) {}
76 if (config.is()) {
77 utl::ConfigManager::storeConfigItems();
78 css::uno::Reference<css::util::XFlushable>(
79 config, css::uno::UNO_QUERY_THROW)->flush();
81 css::uno::Reference<css::lang::XComponent>(
82 context, css::uno::UNO_QUERY_THROW)->dispose();
83 comphelper::setProcessServiceFactory(0);
85 return 0;
90 extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * SAL_CALL
91 vclbootstrapprotector() {
92 return new Protector;
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */