summaryrefslogtreecommitdiff
path: root/test/source/vclbootstrapprotector.cxx
blob: 2aad56303df6644eba4d97af68e3c7e200d52033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <sal/config.h>

#include <boost/noncopyable.hpp>
#include <com/sun/star/configuration/theDefaultProvider.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/util/XFlushable.hpp>
#include <comphelper/processfactory.hxx>
#include <cppunit/Protector.h>
#include <cppunittester/protectorfactory.hxx>
#include <i18nlangtag/lang.h>
#include <i18nlangtag/languagetag.hxx>
#include <i18nlangtag/mslangid.hxx>
#include <sal/types.h>
#include <tools/resmgr.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/syslocaleoptions.hxx>
#include <vcl/svapp.hxx>

#include <isheadless.hxx>

namespace {

class Protector: public CppUnit::Protector, private boost::noncopyable {
public:
    Protector() {
        // Force locale (and resource files loaded) to en-US:
        ResMgr::SetDefaultLocale(LanguageTag("en-US"));
        SvtSysLocaleOptions localOptions;
        localOptions.SetLocaleConfigString("en-US");
        localOptions.SetUILocaleConfigString("en-US");
        MsLangId::setConfiguredSystemUILanguage(LANGUAGE_ENGLISH_US);
        LanguageTag::setConfiguredSystemLanguage(LANGUAGE_ENGLISH_US);
        InitVCL();
        if (test::isHeadless()) {
            Application::EnableHeadlessMode(true);
        }
        Application::setDeInitHook(LINK(this, Protector, deinitHook));
    }

private:
    virtual ~Protector() {
        DeInitVCL();
    }

    virtual bool protect(
        CppUnit::Functor const & functor, CppUnit::ProtectorContext const &)
        SAL_OVERRIDE
    { return functor(); }

    DECL_STATIC_LINK(Protector, deinitHook, void *);
};

// HACK so that defaultBootstrap_InitialComponentContext (in
// unobootstrapprotector) is called before InitVCL (above), but component
// context is disposed (redundantly again in unobootstrapprotector) from within
// DeInitVCL (cf. Desktop::DeInit, desktop/source/app/app.cxx):
IMPL_STATIC_LINK_NOARG(Protector, deinitHook) {
    css::uno::Reference<css::uno::XComponentContext> context;
    try {
        context = comphelper::getProcessComponentContext();
    } catch (css::uno::RuntimeException &) {}
    if (context.is()) {
        css::uno::Reference<css::lang::XMultiServiceFactory> config;
        try {
            config = css::configuration::theDefaultProvider::get(context);
        } catch (css::uno::DeploymentException &) {}
        if (config.is()) {
            utl::ConfigManager::storeConfigItems();
            css::uno::Reference<css::util::XFlushable>(
                config, css::uno::UNO_QUERY_THROW)->flush();
        }
        css::uno::Reference<css::lang::XComponent>(
            context, css::uno::UNO_QUERY_THROW)->dispose();
        comphelper::setProcessServiceFactory(0);
    }
    return 0;
}

}

extern "C" SAL_DLLPUBLIC_EXPORT CppUnit::Protector * SAL_CALL
vclbootstrapprotector() {
    return new Protector;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */