summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMichael Meeks <michael.meeks@suse.com>2011-09-28 13:48:34 +0100
committerMichael Meeks <michael.meeks@suse.com>2011-09-28 15:09:47 +0100
commit950090c5d5be46d40a5f09e8e8295e36215d1dd8 (patch)
treeafa74d1e612eda85e041c88461d79a0ec577c7f4 /test
parent3ff029819c578ef942ae9c7b03308f54204aabed (diff)
add new bootstrapfixture to share and simplify unit test code
Diffstat (limited to 'test')
-rw-r--r--test/inc/test/bootstrapfixture.hxx80
-rw-r--r--test/prj/build.lst2
-rw-r--r--test/prj/d.lst1
-rw-r--r--test/source/cpp/bootstrapfixture.cxx115
-rw-r--r--test/source/cpp/makefile.mk6
5 files changed, 203 insertions, 1 deletions
diff --git a/test/inc/test/bootstrapfixture.hxx b/test/inc/test/bootstrapfixture.hxx
new file mode 100644
index 000000000000..791710c4cce4
--- /dev/null
+++ b/test/inc/test/bootstrapfixture.hxx
@@ -0,0 +1,80 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * Version: MPL 1.1 / GPLv3+ / LGPLv3+
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License or as specified alternatively below. You may obtain a copy of
+ * the License at http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * Major Contributor(s):
+ * Copyright (C) 2011 Michael Meeks <michael.meeks@suse.com>
+ *
+ * All Rights Reserved.
+ *
+ * For minor contributions see the git repository.
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
+ * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
+ * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
+ * instead of those above.
+ */
+#ifndef INCLUDED_TEST_BOOTSTRAPFIXTURE_HXX
+#define INCLUDED_TEST_BOOTSTRAPFIXTURE_HXX
+
+#include <sal/config.h>
+
+#include <rtl/string.hxx>
+#include <com/sun/star/uno/XComponentContext.hpp>
+#include <com/sun/star/lang/XMultiServiceFactory.hpp>
+#include <com/sun/star/lang/XMultiComponentFactory.hpp>
+
+#include "sal/precppunit.hxx"
+#include "cppunit/TestAssert.h"
+#include "cppunit/TestFixture.h"
+#include "cppunit/extensions/HelperMacros.h"
+#include "cppunit/plugin/TestPlugIn.h"
+#include "test/detail/testdllapi.hxx"
+
+namespace test {
+
+// Class to do lots of heavy-lifting UNO & environment
+// bootstrapping for unit tests, such that we can use
+// almost an entire LibreOffice during compile - so
+// that we can get pieces of code alone to beat them up.
+class OOO_DLLPUBLIC_TEST BootstrapFixture : public CppUnit::TestFixture
+{
+ ::rtl::OUString m_aSrcRootURL;
+ ::rtl::OUString m_aSrcRootPath;
+
+ com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> m_xContext;
+ com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory> m_xSFactory;
+ com::sun::star::uno::Reference<com::sun::star::lang::XMultiComponentFactory> m_xFactory;
+
+public:
+ BootstrapFixture( bool bAssertOnDialog = true );
+ virtual ~BootstrapFixture();
+
+ com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext>
+ getComponentContext() { return m_xContext; }
+ com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory>
+ getMultiServiceFactory() { return m_xSFactory; }
+
+ ::rtl::OUString getSrcRootURL() { return m_aSrcRootURL; }
+ ::rtl::OUString getSrcRootPath() { return m_aSrcRootPath; }
+
+ // return a URL to a given c-str path from the source directory
+ ::rtl::OUString getURLFromSrc( const char *pPath );
+};
+
+}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/test/prj/build.lst b/test/prj/build.lst
index b223c324edef..655170facea6 100644
--- a/test/prj/build.lst
+++ b/test/prj/build.lst
@@ -1,4 +1,4 @@
-te test : BOOST:boost cppu cppuhelper CPPUNIT:cppunit javaunohelper offapi ridljar sal solenv stlport unoil qadevOOo NULL
+te test : BOOST:boost cppu cppuhelper CPPUNIT:cppunit javaunohelper offapi ridljar sal solenv stlport unoil qadevOOo vcl NULL
te test\inc nmake - all inc NULL
te test\source\cpp nmake - all source_cpp inc NULL
te test\source\cpp\unoexceptionprotector nmake - all source_cpp_unoexceptionprotector inc NULL
diff --git a/test/prj/d.lst b/test/prj/d.lst
index 24ec21cc4dea..bf4af6d239d7 100644
--- a/test/prj/d.lst
+++ b/test/prj/d.lst
@@ -11,6 +11,7 @@ mkdir: %_DEST%\inc\test\detail
..\inc\test\detail\testdllapi.hxx %_DEST%\inc\test\detail\testdllapi.hxx
..\inc\test\gettestargument.hxx %_DEST%\inc\test\gettestargument.hxx
..\inc\test\officeconnection.hxx %_DEST%\inc\test\officeconnection.hxx
+..\inc\test\bootstrapfixture.hxx %_DEST%\inc\test\bootstrapfixture.hxx
..\inc\test\oustringostreaminserter.hxx %_DEST%\inc\test\oustringostreaminserter.hxx
..\inc\test\toabsolutefileurl.hxx %_DEST%\inc\test\toabsolutefileurl.hxx
..\inc\test\uniquepipename.hxx %_DEST%\inc\test\uniquepipename.hxx
diff --git a/test/source/cpp/bootstrapfixture.cxx b/test/source/cpp/bootstrapfixture.cxx
new file mode 100644
index 000000000000..d46cf4d11230
--- /dev/null
+++ b/test/source/cpp/bootstrapfixture.cxx
@@ -0,0 +1,115 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * Version: MPL 1.1 / GPLv3+ / LGPLv3+
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License or as specified alternatively below. You may obtain a copy of
+ * the License at http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * Major Contributor(s):
+ * Copyright (C) 2011 Michael Meeks <michael.meeks@suse.com>
+ * Caolán McNamara <caolanm@redhat.com>
+ *
+ * All Rights Reserved.
+ *
+ * For minor contributions see the git repository.
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
+ * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
+ * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
+ * instead of those above.
+ */
+#include <test/bootstrapfixture.hxx>
+#include <tools/errinf.hxx>
+#include <rtl/strbuf.hxx>
+#include <cppuhelper/bootstrap.hxx>
+#include <ucbhelper/contentbroker.hxx>
+#include <comphelper/processfactory.hxx>
+
+#include <com/sun/star/lang/Locale.hpp>
+#include <com/sun/star/lang/XComponent.hpp>
+#include <com/sun/star/lang/XMultiServiceFactory.hpp>
+
+#include <vcl/svapp.hxx>
+#include <tools/resmgr.hxx>
+#include <unotools/syslocaleoptions.hxx>
+
+using namespace ::com::sun::star;
+
+static void aBasicErrorFunc( const String &rErr, const String &rAction )
+{
+ rtl::OStringBuffer aErr( "Unexpected dialog: " );
+ aErr.append( rtl::OUStringToOString( rAction, RTL_TEXTENCODING_ASCII_US ) );
+ aErr.append( " Error: " );
+ aErr.append( rtl::OUStringToOString( rErr, RTL_TEXTENCODING_ASCII_US ) );
+ CPPUNIT_ASSERT_MESSAGE( aErr.getStr(), false);
+}
+
+test::BootstrapFixture::BootstrapFixture( bool bAssertOnDialog )
+ : m_aSrcRootURL(RTL_CONSTASCII_USTRINGPARAM("file://"))
+{
+ m_xContext = cppu::defaultBootstrap_InitialComponentContext();
+ m_xFactory = m_xContext->getServiceManager();
+ m_xSFactory = uno::Reference<lang::XMultiServiceFactory> (m_xFactory, uno::UNO_QUERY_THROW);
+
+ //Without this we're crashing because callees are using
+ //getProcessServiceFactory. In general those should be removed in favour
+ //of retaining references to the root ServiceFactory as its passed around
+ comphelper::setProcessServiceFactory(m_xSFactory);
+
+ // initialise UCB-Broker
+ uno::Sequence<uno::Any> aUcbInitSequence(2);
+ aUcbInitSequence[0] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local"));
+ aUcbInitSequence[1] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office"));
+ bool bInitUcb = ucbhelper::ContentBroker::initialize(m_xSFactory, aUcbInitSequence);
+ CPPUNIT_ASSERT_MESSAGE("Should be able to initialize UCB", bInitUcb);
+
+ uno::Reference<ucb::XContentProviderManager> xUcb =
+ ucbhelper::ContentBroker::get()->getContentProviderManagerInterface();
+ uno::Reference<ucb::XContentProvider> xFileProvider(m_xSFactory->createInstance(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.FileContentProvider"))), uno::UNO_QUERY);
+ xUcb->registerContentProvider(xFileProvider, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file")), sal_True);
+
+ // force locale (and resource files loaded) to en-US
+ rtl::OUString aLang( RTL_CONSTASCII_USTRINGPARAM( "en" ) );
+ rtl::OUString aCountry( RTL_CONSTASCII_USTRINGPARAM( "US" ) );
+ rtl::OUString aISO( RTL_CONSTASCII_USTRINGPARAM( "en-US" ) );
+ lang::Locale aLocale(aLang, aCountry, rtl::OUString());
+ ResMgr::SetDefaultLocale( aLocale );
+ SvtSysLocaleOptions aLocalOptions;
+ aLocalOptions.SetUILocaleConfigString( aISO );
+
+ InitVCL(m_xSFactory);
+
+ if( bAssertOnDialog )
+ ErrorHandler::RegisterDisplay( aBasicErrorFunc );
+
+ const char* pSrcRoot = getenv( "SRC_ROOT" );
+ CPPUNIT_ASSERT_MESSAGE("SRC_ROOT env variable not set", pSrcRoot != NULL && pSrcRoot[0] != 0);
+
+#ifdef WNT
+ if (pSrcRoot[1] == ':')
+ m_aSrcRootURL += rtl::OUString::createFromAscii( "/" );
+#endif
+ m_aSrcRootPath = rtl::OUString::createFromAscii( pSrcRoot );
+ m_aSrcRootURL += m_aSrcRootPath;
+}
+
+test::BootstrapFixture::~BootstrapFixture()
+{
+ uno::Reference< lang::XComponent >(m_xContext, uno::UNO_QUERY_THROW)->dispose();
+}
+
+::rtl::OUString test::BootstrapFixture::getURLFromSrc( const char *pPath )
+{
+ return m_aSrcRootURL + rtl::OUString::createFromAscii( pPath );
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/test/source/cpp/makefile.mk b/test/source/cpp/makefile.mk
index fd0b2bb42bbb..68bca023a58c 100644
--- a/test/source/cpp/makefile.mk
+++ b/test/source/cpp/makefile.mk
@@ -40,6 +40,7 @@ CFLAGSCXX += $(CPPUNIT_CFLAGS)
SLOFILES = \
$(SLO)/getargument.obj \
$(SLO)/gettestargument.obj \
+ $(SLO)/bootstrapfixture.obj \
$(SLO)/officeconnection.obj \
$(SLO)/toabsolutefileurl.obj \
$(SLO)/uniquepipename.obj
@@ -52,7 +53,12 @@ SHL1IMPLIB = i$(SHL1TARGET)
SHL1OBJS = $(SLOFILES)
SHL1RPATH = NONE
SHL1STDLIBS = \
+ $(VCLLIB) \
+ $(TOOLSLIB) \
+ $(UCBHELPERLIB) \
+ $(COMPHELPERLIB) \
$(CPPUHELPERLIB) \
+ $(UNOTOOLSLIB) \
$(CPPULIB) \
$(CPPUNITLIB) \
$(SALLIB)