bump product version to 4.1.6.2
[LibreOffice.git] / bridges / test / testoffice.cxx
blob8c18fbf25a6db268a9248d09f759e4da896a9026
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 ::rtl;
49 using namespace ::cppu;
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::io;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::bridge;
54 using namespace ::com::sun::star::registry;
55 using namespace ::com::sun::star::connection;
56 using namespace ::com::sun::star::frame;
57 using namespace ::com::sun::star::text;
59 #include "testcomp.h"
61 #ifdef SAL_W32
62 #include <conio.h>
63 #endif
66 void mygetchar()
68 #ifdef SAL_W32
69 _getch();
70 #else
71 getchar();
72 #endif
76 void testPipe( const Reference < XMultiServiceFactory > & rSmgr )
78 Reference < XPipe > rPipe( Pipe::create(comphelper::getComponentContext(rSmgr)), UNO_QUERY_THROW );
81 Sequence < sal_Int8 > seq( 10 );
82 seq.getArray()[0] = 42;
83 rPipe->writeBytes( seq );
88 Sequence < sal_Int8 > seq;
89 if( ! ( rPipe->available() == 10) )
90 printf( "wrong bytes available\n" );
91 if( ! ( rPipe->readBytes( seq , 10 ) == 10 ) )
92 printf( "wrong bytes read\n" );
93 if( ! ( 42 == seq.getArray()[0] ) )
94 printf( "wrong element in sequence\n" );
96 // OSL_ASSERT( 0 );
99 #include<stdio.h>
100 #include<string.h>
102 void testWriter( const Reference < XComponent > & rCmp )
105 Reference< XTextDocument > rTextDoc( rCmp , UNO_QUERY );
107 Reference< XText > rText = rTextDoc->getText();
108 Reference< XTextCursor > rCursor = rText->createTextCursor();
109 Reference< XTextRange > rRange ( rCursor , UNO_QUERY );
111 char pcText[1024];
112 pcText[0] = 0;
113 printf( "pleast type any text\n" );
114 while( true )
116 scanf( "%s" , pcText );
118 if( !strcmp( pcText , "end" ) )
120 break;
123 if ( strlen( pcText ) < sizeof(pcText)-1 )
124 strcat( pcText , " " ); // #100211# - checked
126 rText->insertString( rRange , OUString::createFromAscii( pcText ) , sal_False );
130 void testDocument( const Reference < XMultiServiceFactory > & rSmgr )
132 Reference < XDesktop2 > rLoader = Desktop::create( comphelper::getComponentContext(rSmgr) );
134 sal_Char *urls[] = {
135 "private:factory/swriter",
136 "private:factory/scalc",
137 "private:factory/sdraw",
138 "http://www.heise.de",
139 "file://h|/remote_interfaces.sdw"
142 sal_Char *docu[]= {
143 "a new writer document ...\n",
144 "a new calc document ...\n",
145 "a new draw document ...\n",
146 "www.heise.de\n",
147 "the remote_interfaces.sdw doc\n"
150 sal_Int32 i;
151 for( i = 0 ; i < 1 ; i ++ )
153 printf( "press any key to open %s\n" , docu[i] );
154 mygetchar();
156 Reference< XComponent > rComponent =
157 rLoader->loadComponentFromURL(
158 OUString::createFromAscii( urls[i] ) ,
159 OUString("_blank"),
161 Sequence < ::com::sun::star::beans::PropertyValue >() );
163 testWriter( rComponent );
164 printf( "press any key to close the document\n" );
165 mygetchar();
166 rComponent->dispose();
171 void doSomething( const Reference < XInterface > &r )
173 Reference < XNamingService > rName( r, UNO_QUERY );
174 if( rName.is() )
176 printf( "got the remote naming service !\n" );
177 Reference < XInterface > rXsmgr = rName->getRegisteredObject(
178 OUString( "StarOffice.ServiceManager" ) );
180 Reference < XMultiServiceFactory > rSmgr( rXsmgr , UNO_QUERY );
181 if( rSmgr.is() )
183 printf( "got the remote service manager !\n" );
184 testPipe( rSmgr );
185 testDocument( rSmgr );
191 int main( int argc, char *argv[] )
193 if( argc < 2 )
195 printf( "usage : testclient host:port" );
196 return 0;
199 OUString sConnectionString;
200 OUString sProtocol;
201 sal_Bool bLatency = sal_False;
202 sal_Bool bReverse = sal_False;
203 parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
205 Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
206 OUString( "client.rdb" ) );
208 // just ensure that it is registered
210 Reference < XConnector > rConnector(
211 createComponent( OUString("com.sun.star.connection.Connector"),
212 OUString( "connector.uno" SAL_DLLEXTENSION),
213 rSMgr ),
214 UNO_QUERY );
216 createComponent( OUString("com.sun.star.bridge.Bridge.iiop"),
217 OUString( "remotebridge.uno" SAL_DLLEXTENSION),
218 rSMgr );
220 Reference < XBridgeFactory > rFactory(
221 createComponent( OUString("com.sun.star.bridge.BridgeFactory"),
222 OUString( "bridgefac.uno" SAL_DLLEXTENSION),
223 rSMgr ),
224 UNO_QUERY );
228 if( rFactory.is() && rConnector.is() )
230 Reference < XConnection > rConnection =
231 rConnector->connect( sConnectionString );
233 Reference < XBridge > rBridge = rFactory->createBridge(
234 OUString("bla blub"),
235 sProtocol,
236 rConnection,
237 Reference < XInstanceProvider > () );
239 Reference < XInterface > rInitialObject
240 = rBridge->getInstance( OUString("NamingService") );
242 if( rInitialObject.is() )
244 printf( "got the remote object\n" );
245 doSomething( rInitialObject );
247 TimeValue value={2,0};
248 osl_waitThread( &value );
251 catch (... ) {
252 printf( "Exception thrown\n" );
255 Reference < XComponent > rComp( rSMgr , UNO_QUERY );
256 rComp->dispose();
258 //_getch();
259 return 0;
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */