Branch libreoffice-5-0-4
[LibreOffice.git] / bridges / test / testoffice.cxx
blob5d17a0045fa207f919cee7caf4d4e7f7c52187f3
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 <osl/time.h>
22 #include <osl/mutex.hxx>
23 #include <osl/thread.h>
25 #include <cppuhelper/servicefactory.hxx>
27 #include <com/sun/star/connection/XConnector.hpp>
29 #include <com/sun/star/bridge/XBridgeFactory.hpp>
31 #include <com/sun/star/uno/XNamingService.hpp>
32 #include <com/sun/star/io/Pipe.hpp>
34 #include <com/sun/star/text/XTextDocument.hpp>
36 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/frame/Desktop.hpp>
41 #include <com/sun/star/frame/XComponentLoader.hpp>
43 #include <cppuhelper/weak.hxx>
45 #include <test/XTestFactory.hpp>
47 using namespace ::test;
48 using namespace ::cppu;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::io;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::bridge;
53 using namespace ::com::sun::star::registry;
54 using namespace ::com::sun::star::connection;
55 using namespace ::com::sun::star::frame;
56 using namespace ::com::sun::star::text;
58 #include "testcomp.h"
60 #ifdef SAL_W32
61 #include <conio.h>
62 #endif
65 void mygetchar()
67 #ifdef SAL_W32
68 _getch();
69 #else
70 getchar();
71 #endif
75 void testPipe( const Reference < XMultiServiceFactory > & rSmgr )
77 Reference < XPipe > rPipe( Pipe::create(comphelper::getComponentContext(rSmgr)), UNO_QUERY_THROW );
80 Sequence < sal_Int8 > seq( 10 );
81 seq.getArray()[0] = 42;
82 rPipe->writeBytes( seq );
87 Sequence < sal_Int8 > seq;
88 if( ! ( rPipe->available() == 10) )
89 printf( "wrong bytes available\n" );
90 if( ! ( rPipe->readBytes( seq , 10 ) == 10 ) )
91 printf( "wrong bytes read\n" );
92 if( ! ( 42 == seq.getArray()[0] ) )
93 printf( "wrong element in sequence\n" );
95 // OSL_ASSERT( 0 );
98 #include <stdio.h>
99 #include <string.h>
101 void testWriter( const Reference < XComponent > & rCmp )
104 Reference< XTextDocument > rTextDoc( rCmp , UNO_QUERY );
106 Reference< XText > rText = rTextDoc->getText();
107 Reference< XTextCursor > rCursor = rText->createTextCursor();
108 Reference< XTextRange > rRange ( rCursor , UNO_QUERY );
110 char pcText[1024];
111 pcText[0] = 0;
112 printf( "pleast type any text\n" );
113 while( true )
115 scanf( "%s" , pcText );
117 if( !strcmp( pcText , "end" ) )
119 break;
122 if ( strlen( pcText ) < sizeof(pcText)-1 )
123 strcat( pcText , " " ); // #100211# - checked
125 rText->insertString( rRange , OUString::createFromAscii( pcText ) , sal_False );
129 void testDocument( const Reference < XMultiServiceFactory > & rSmgr )
131 Reference < XDesktop2 > rLoader = Desktop::create( comphelper::getComponentContext(rSmgr) );
133 sal_Char *urls[] = {
134 "private:factory/swriter",
135 "private:factory/scalc",
136 "private:factory/sdraw",
137 "http://www.heise.de",
138 "file://h|/remote_interfaces.sdw"
141 sal_Char *docu[]= {
142 "a new writer document ...\n",
143 "a new calc document ...\n",
144 "a new draw document ...\n",
145 "www.heise.de\n",
146 "the remote_interfaces.sdw doc\n"
149 sal_Int32 i;
150 for( i = 0 ; i < 1 ; i ++ )
152 printf( "press any key to open %s\n" , docu[i] );
153 mygetchar();
155 Reference< XComponent > rComponent =
156 rLoader->loadComponentFromURL(
157 OUString::createFromAscii( urls[i] ) ,
158 OUString("_blank"),
160 Sequence < ::com::sun::star::beans::PropertyValue >() );
162 testWriter( rComponent );
163 printf( "press any key to close the document\n" );
164 mygetchar();
165 rComponent->dispose();
170 void doSomething( const Reference < XInterface > &r )
172 Reference < XNamingService > rName( r, UNO_QUERY );
173 if( rName.is() )
175 printf( "got the remote naming service !\n" );
176 Reference < XInterface > rXsmgr = rName->getRegisteredObject(
177 OUString( "StarOffice.ServiceManager" ) );
179 Reference < XMultiServiceFactory > rSmgr( rXsmgr , UNO_QUERY );
180 if( rSmgr.is() )
182 printf( "got the remote service manager !\n" );
183 testPipe( rSmgr );
184 testDocument( rSmgr );
190 int main( int argc, char *argv[] )
192 if( argc < 2 )
194 printf( "usage : testclient host:port" );
195 return 0;
198 OUString sConnectionString;
199 OUString sProtocol;
200 sal_Bool bLatency = sal_False;
201 sal_Bool bReverse = sal_False;
202 parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
204 Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
205 OUString( "client.rdb" ) );
207 // just ensure that it is registered
209 Reference < XConnector > rConnector(
210 createComponent( OUString("com.sun.star.connection.Connector"),
211 OUString( "connector.uno" SAL_DLLEXTENSION),
212 rSMgr ),
213 UNO_QUERY );
215 createComponent( OUString("com.sun.star.bridge.Bridge.iiop"),
216 OUString( "remotebridge.uno" SAL_DLLEXTENSION),
217 rSMgr );
219 Reference < XBridgeFactory > rFactory(
220 createComponent( OUString("com.sun.star.bridge.BridgeFactory"),
221 OUString( "bridgefac.uno" SAL_DLLEXTENSION),
222 rSMgr ),
223 UNO_QUERY );
227 if( rFactory.is() && rConnector.is() )
229 Reference < XConnection > rConnection =
230 rConnector->connect( sConnectionString );
232 Reference < XBridge > rBridge = rFactory->createBridge(
233 OUString("bla blub"),
234 sProtocol,
235 rConnection,
236 Reference < XInstanceProvider > () );
238 Reference < XInterface > rInitialObject
239 = rBridge->getInstance( OUString("NamingService") );
241 if( rInitialObject.is() )
243 printf( "got the remote object\n" );
244 doSomething( rInitialObject );
246 TimeValue value={2,0};
247 osl_waitThread( &value );
250 catch (... ) {
251 printf( "Exception thrown\n" );
254 Reference < XComponent > rComp( rSMgr , UNO_QUERY );
255 rComp->dispose();
257 //_getch();
258 return 0;
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */