fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / stoc / test / testsmgr_cpnt.cxx
blobb8e100f189527cdf646a0c0614007039661eb10b
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <rtl/alloc.h>
21 #include <osl/security.h>
22 #include <osl/thread.h>
23 #include <osl/mutex.hxx>
24 #include <cppuhelper/queryinterface.hxx>
25 #include <cppuhelper/weak.hxx>
26 #include <uno/mapping.hxx>
29 #include <cppuhelper/factory.hxx>
30 #include <cppuhelper/servicefactory.hxx>
31 #include <cppuhelper/implbase1.hxx>
32 #include <registry/registry.hxx>
34 #include <com/sun/star/registry/XSimpleRegistry.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <com/sun/star/registry/XImplementationRegistration.hpp>
38 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
41 #include <com/sun/star/container/XEnumerationAccess.hpp>
42 #include <com/sun/star/container/XEnumeration.hpp>
44 #if defined ( UNX )
45 #include <limits.h>
46 #define _MAX_PATH PATH_MAX
47 #endif
50 #define IMPLEMENTATION_NAME "com.sun.star.DummyService.V10"
51 #define SERVICE_NAME "com.sun.star.ts.TestManagerImpl"
54 using namespace com::sun::star::uno;
55 using namespace com::sun::star::registry;
56 using namespace com::sun::star::lang;
57 using namespace com::sun::star::container;
58 using namespace osl;
59 using namespace cppu;
63 Reference<XMultiServiceFactory> getProcessServiceManager()
65 Reference<XMultiServiceFactory > s_x;
66 if (! s_x.is())
68 MutexGuard aGuard( Mutex::getGlobalMutex() );
69 if (! s_x.is())
70 s_x = createRegistryServiceFactory( OUString("stoctest.rdb"), sal_False );
72 return s_x;
75 Reference< XMultiServiceFactory > createRegistryServiceManager( const OUString& registryName )
77 return createRegistryServiceFactory( registryName );
81 /**********************************
82 * The service, that is used to test the Service manager
86 *************************************/
87 static sal_uInt32 nInstanceCount = 0;
88 class Test_Manager_Impl : public WeakImplHelper1< XServiceInfo >
90 public:
91 Test_Manager_Impl(){ nInstanceCount++;}
92 ~Test_Manager_Impl();
94 // XServiceInfo
95 OUString SAL_CALL getImplementationName() throw();
96 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
97 Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
98 static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(void) throw();
100 private:
101 // static XIdlClassRef getStaticIdlClass();
104 Test_Manager_Impl::~Test_Manager_Impl()
106 nInstanceCount--;
110 // old, is no longer needed by the new Mimic
111 Reference< XInterface > SAL_CALL Test_Manager_Impl_CreateInstance_Impl()
113 return (OWeakObject *)new Test_Manager_Impl();
117 //*************************************************************************
118 // Test_Manager_Impl_CreateInstance()
120 Reference < XInterface > SAL_CALL Test_Manager_Impl_CreateInstance(
121 const Reference< XMultiServiceFactory > & /*rSMgr*/ ) throw (Exception)
123 Reference < XInterface > xService = (XWeak *)(OWeakObject *)new Test_Manager_Impl( );
125 return xService;
129 //*************************************************************************
130 // Test_Manager_Impl::getImplementationName
132 OUString Test_Manager_Impl::getImplementationName() throw()
134 return OUString(IMPLEMENTATION_NAME);
137 //*************************************************************************
138 // Test_Manager_Impl::supportsService
140 sal_Bool Test_Manager_Impl::supportsService( const OUString& ServiceName ) throw()
142 Sequence< OUString > aSNL = getSupportedServiceNames();
143 const OUString * pArray = aSNL.getConstArray();
144 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
145 if( pArray[i] == ServiceName )
146 return sal_True;
147 return sal_False;
150 //*************************************************************************
151 // Test_Manager_Impl::getSupportedServiceNames
153 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames(void) throw ()
155 return getSupportedServiceNames_Static();
158 //*************************************************************************
159 // Test_Manager_Impl::getSupportedServiceNames_Static
161 Sequence< OUString > Test_Manager_Impl::getSupportedServiceNames_Static(void) throw ()
163 Sequence< OUString > aSNS( 2 );
164 aSNS.getArray()[0] = OUString(SERVICE_NAME);
165 aSNS.getArray()[1] = OUString("com.sun.star.bridge.Bridge");
166 return aSNS;
172 /****
175 * This routine performs the test of the process service manager ( getProcessServiceManager is called )
179 ****/
181 #include <stdio.h>
183 extern "C" void SAL_CALL test_ServiceManager()
185 #if ! defined SAL_DLLPREFIX
186 #define SAL_DLLPREFIX ""
187 #endif
188 OUString atUModule2 = OUString( SAL_DLLPREFIX "testsmgr_component" SAL_DLLEXTENSION );
190 // expand shared library name
191 OString atModule2( OUStringToOString(atUModule2, RTL_TEXTENCODING_ASCII_US) );
193 // get the process servicemanager
194 Reference <XMultiServiceFactory> xSMgr = getProcessServiceManager();
196 OSL_ENSURE( xSMgr.is() , "query on XServiceManager failed" );
198 Reference<XContentEnumerationAccess> xContEnum(xSMgr, UNO_QUERY);
199 OSL_ENSURE( xContEnum.is() , "query on XContentEnumerationAccess failed" );
200 Reference<XEnumeration > xEnum(xContEnum->createContentEnumeration(OUString("com.sun.star.registry.SimpleRegistry")));
201 OSL_ENSURE( xEnum.is() , "createContentEnumeration failed" );
202 sal_Int32 nLen = 0;
203 while( xEnum->hasMoreElements() )
205 nLen++;
206 xEnum->nextElement();
208 OSL_ENSURE( nLen == 1, "more than one implementation for SimpleRegistry" );
210 Reference<XEnumerationAccess> xImplEnum(xSMgr, UNO_QUERY);
211 OSL_ENSURE( xImplEnum.is() , "query on XEnumeration failed" );
212 xEnum = Reference<XEnumeration >(xImplEnum->createEnumeration());
213 OSL_ENSURE( xEnum.is() , "createEnumeration failed" );
214 nLen = 0;
215 while( xEnum->hasMoreElements() )
217 nLen++;
218 Reference< XServiceInfo > sf( xEnum->nextElement(), UNO_QUERY );
219 OString str( OUStringToOString( sf->getImplementationName(), RTL_TEXTENCODING_ASCII_US ) );
220 ::fprintf( stderr, "> implementation name: %s\n", str.getStr() );
222 OSL_ENSURE( nLen == 8, "more than 6 factories" );
224 // try to get an instance for a unknown service
225 OSL_VERIFY( !xSMgr->createInstance(OUString("bla.blup.Q")).is() );
228 // First test : register service via the internal function of the component itself
231 Reference< XImplementationRegistration >
232 xInst( xSMgr->createInstance(OUString("com.sun.star.registry.ImplementationRegistration")), UNO_QUERY );
233 OSL_ENSURE( xInst.is(), "no ImplementationRegistration" );
235 try {
236 // register the services via writeComponentRegInfo (see at end of this file)
237 xInst->registerImplementation(OUString("com.sun.star.loader.SharedLibrary"), atUModule2, Reference< XSimpleRegistry >() );
239 catch(const CannotRegisterImplementationException &) {
240 OSL_ENSURE( 0, "register implementation failed" );
243 // getImplementations() check
244 Sequence<OUString> seqImpl = xInst->getImplementations(OUString("com.sun.star.loader.SharedLibrary"), atUModule2);
245 OSL_ENSURE( seqImpl.getLength() == 1, "count of implementantions is wrong" );
246 OSL_ENSURE( seqImpl.getConstArray()[0] == OUString("com.sun.star.DummyService.V10"), "implementation name is not equal" );
249 // tests, if a service provider can be instantiated.
251 Reference< XInterface > xIFace(xSMgr->createInstance(OUString("com.sun.star.ts.TestManagerImpl")));
252 OSL_ENSURE( xIFace.is(), "loadable service not found" );
254 // remove the service
255 OSL_VERIFY( xInst->revokeImplementation(atUModule2, Reference< XSimpleRegistry > ()) );
258 Reference<XComponent> xComp(xSMgr, UNO_QUERY);
259 xComp->dispose();
261 xComp.clear();
262 xSMgr.clear();
267 extern "C"
269 //==================================================================================================
270 sal_Bool SAL_CALL component_writeInfo(
271 void * /*pServiceManager*/, void * pRegistryKey )
273 if (pRegistryKey)
277 Reference< XRegistryKey > xNewKey(
278 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
279 OUString( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
281 const Sequence< OUString > & rSNL =
282 Test_Manager_Impl::getSupportedServiceNames_Static();
283 const OUString * pArray = rSNL.getConstArray();
284 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
285 xNewKey->createKey( pArray[nPos] );
287 return sal_True;
289 catch (InvalidRegistryException &)
291 OSL_FAIL( "### InvalidRegistryException!" );
294 return sal_False;
296 //==================================================================================================
297 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
298 const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
300 void * pRet = 0;
302 if (rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
304 Reference< XSingleServiceFactory > xFactory( createSingleFactory(
305 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
306 OUString( IMPLEMENTATION_NAME ),
307 Test_Manager_Impl_CreateInstance,
308 Test_Manager_Impl::getSupportedServiceNames_Static() ) );
310 if (xFactory.is())
312 xFactory->acquire();
313 pRet = xFactory.get();
317 return pRet;
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */