merge the formfield patch from ooo-build
[ooovba.git] / dtrans / source / test / test_dtrans.cxx
blob2753ac1e320a129554c30b0187194e30c8606502
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test_dtrans.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
35 //------------------------------------------------------------------------
36 // interface includes
37 //------------------------------------------------------------------------
38 #include <com/sun/star/datatransfer/XTransferable.hpp>
39 #include <com/sun/star/datatransfer/clipboard/XClipboardManager.hpp>
40 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
41 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
42 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
43 #include <com/sun/star/lang/XComponent.hpp>
45 //------------------------------------------------------------------------
46 // other includes
47 //------------------------------------------------------------------------
50 #include <cppuhelper/servicefactory.hxx>
51 #include <cppuhelper/implbase1.hxx>
52 #include <cppuhelper/implbase2.hxx>
53 #include <rtl/ustring.hxx>
54 #include <osl/diagnose.h>
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <string.h>
60 //#include <memory>
62 //#include <process.h>
64 //------------------------------------------------------------------------
65 // my defines
66 //------------------------------------------------------------------------
68 #ifdef UNX
69 #define PATH_SEPERATOR '/'
70 #else
71 #define PATH_SEPERATOR '\\'
72 #endif
74 #define ENSURE( a, b ) if( !a ) { fprintf( stderr, b "\n" ); exit( -1 ); }
75 #define TEST( a, b ) fprintf( stderr, "Testing " a ); fprintf( stderr, b ? "passed\n" : "FAILED\n" )
76 #define PERFORM( a, b ) fprintf( stderr, "Performing " a); b; fprintf( stderr, "done\n" )
77 #define TRACE( a ) fprintf( stderr, a )
79 //------------------------------------------------------------------------
80 // namespaces
81 //------------------------------------------------------------------------
83 using namespace ::rtl;
84 using namespace ::std;
85 using namespace ::cppu;
86 using namespace ::com::sun::star::container;
87 using namespace ::com::sun::star::datatransfer;
88 using namespace ::com::sun::star::datatransfer::clipboard;
89 using namespace ::com::sun::star::uno;
90 using namespace ::com::sun::star::io;
91 using namespace ::com::sun::star::lang;
93 //------------------------------------------------------------------------
94 // globals
95 //------------------------------------------------------------------------
97 const char * app = NULL;
99 //------------------------------------------------------------------------
100 // ClipboardOwner
101 //------------------------------------------------------------------------
103 class ClipboardOwner : public WeakImplHelper1< XClipboardOwner >
105 Reference< XClipboard > m_xClipboard;
106 Reference< XTransferable > m_xTransferable;
108 sal_uInt32 m_nReceivedLostOwnerships;
110 public:
111 ClipboardOwner();
113 //--------------------------------------------------------------------
114 // XClipboardOwner
115 //--------------------------------------------------------------------
117 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
119 sal_uInt32 receivedLostOwnerships() { return m_nReceivedLostOwnerships; };
120 Reference< XClipboard > lostOwnershipClipboardValue() { return m_xClipboard; }
121 Reference< XTransferable > lostOwnershipTransferableValue() { return m_xTransferable; };
124 //------------------------------------------------------------------------
125 // ctor
126 //------------------------------------------------------------------------
128 ClipboardOwner::ClipboardOwner():
129 m_nReceivedLostOwnerships( 0 )
133 //------------------------------------------------------------------------
134 // lostOwnership
135 //------------------------------------------------------------------------
137 void SAL_CALL ClipboardOwner::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
138 throw(RuntimeException)
140 m_nReceivedLostOwnerships++;
141 m_xClipboard = xClipboard;
142 m_xTransferable = xTrans;
145 //------------------------------------------------------------------------
146 // ClipboardListener
147 //------------------------------------------------------------------------
149 class ClipboardListener : public WeakImplHelper1< XClipboardListener >
151 Reference< XClipboard > m_xClipboard;
152 Reference< XTransferable > m_xTransferable;
154 sal_uInt32 m_nReceivedChangedContentsEvents;
156 public:
157 ClipboardListener();
159 //--------------------------------------------------------------------
160 // XClipboardOwner
161 //--------------------------------------------------------------------
163 virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw(RuntimeException);
165 //--------------------------------------------------------------------
166 // XEventListener
167 //--------------------------------------------------------------------
169 virtual void SAL_CALL disposing( const EventObject& event ) throw(RuntimeException);
171 sal_uInt32 receivedChangedContentsEvents() { return m_nReceivedChangedContentsEvents; };
172 Reference< XClipboard > changedContentsEventClipboardValue() { return m_xClipboard; }
173 Reference< XTransferable > changedContentsEventTransferableValue() { return m_xTransferable; };
176 //------------------------------------------------------------------------
177 // ctor
178 //------------------------------------------------------------------------
180 ClipboardListener::ClipboardListener():
181 m_nReceivedChangedContentsEvents( 0 )
185 //------------------------------------------------------------------------
186 // changedContents
187 //------------------------------------------------------------------------
189 void SAL_CALL ClipboardListener::changedContents( const ClipboardEvent& event )
190 throw(RuntimeException)
192 m_nReceivedChangedContentsEvents++;
193 m_xClipboard = Reference< XClipboard > (event.Source, UNO_QUERY);
194 m_xTransferable = event.Contents;
197 //------------------------------------------------------------------------
198 // disposing
199 //------------------------------------------------------------------------
201 void SAL_CALL ClipboardListener::disposing( const EventObject& event )
202 throw(RuntimeException)
206 //------------------------------------------------------------------------
207 // StringTransferable
208 //------------------------------------------------------------------------
210 class StringTransferable : public WeakImplHelper2< XClipboardOwner, XTransferable >
212 public:
213 StringTransferable( );
215 //--------------------------------------------------------------------
216 // XTransferable
217 //--------------------------------------------------------------------
219 virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
220 virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
221 virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
223 //--------------------------------------------------------------------
224 // XClipboardOwner
225 //--------------------------------------------------------------------
227 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
229 sal_Bool receivedLostOwnership() { return m_receivedLostOwnership; };
230 void clearReceivedLostOwnership() { m_receivedLostOwnership = sal_False; };
232 private:
233 Sequence< DataFlavor > m_seqDFlv;
234 OUString m_Data;
235 sal_Bool m_receivedLostOwnership;
238 //------------------------------------------------------------------------
239 // ctor
240 //------------------------------------------------------------------------
242 StringTransferable::StringTransferable( ) :
243 m_seqDFlv( 1 ),
244 m_receivedLostOwnership( sal_False ),
245 m_Data( OUString::createFromAscii("clipboard test content") )
247 DataFlavor df;
250 df.MimeType = L"text/plain; charset=unicode";
251 df.DataType = getCppuType( ( OUString* )0 );
253 m_seqDFlv[0] = df;
256 //df.MimeType = L"text/plain; charset=windows1252";
257 df.MimeType = OUString::createFromAscii( "text/html" );
258 df.DataType = getCppuType( ( Sequence< sal_Int8 >* )0 );
260 m_seqDFlv[0] = df;
263 //------------------------------------------------------------------------
264 // getTransferData
265 //------------------------------------------------------------------------
267 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& aFlavor )
268 throw(UnsupportedFlavorException, IOException, RuntimeException)
270 Any anyData;
272 /*if ( aFlavor == m_seqDFlv[0] )
274 anyData = makeAny( m_Data );
275 } */
276 #if 0
277 else if ( aFlavor == m_seqDFlv[0] )
279 OString aStr( m_Data.getStr( ), m_Data.getLength( ), 1252 );
280 Sequence< sal_Int8 > sOfChars( aStr.getLength( ) );
281 sal_Int32 lenStr = aStr.getLength( );
283 for ( sal_Int32 i = 0; i < lenStr; ++i )
284 sOfChars[i] = aStr[i];
286 anyData = makeAny( sOfChars );
288 #endif
290 return anyData;
293 //------------------------------------------------------------------------
294 // getTransferDataFlavors
295 //------------------------------------------------------------------------
297 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors( )
298 throw(RuntimeException)
300 return m_seqDFlv;
303 //------------------------------------------------------------------------
304 // isDataFlavorSupported
305 //------------------------------------------------------------------------
307 sal_Bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
308 throw(RuntimeException)
310 sal_Int32 nLength = m_seqDFlv.getLength( );
311 sal_Bool bRet = sal_False;
313 // for ( sal_Int32 i = 0; i < nLength; ++i )
314 // {
315 // if ( m_seqDFlv[i] == aFlavor )
316 // {
317 // bRet = sal_True;
318 // break;
319 // }
320 // }
322 return bRet;
325 //------------------------------------------------------------------------
326 // lostOwnership
327 //------------------------------------------------------------------------
329 void SAL_CALL StringTransferable::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
330 throw(RuntimeException)
332 m_receivedLostOwnership = sal_True;
335 //------------------------------------------------------------------------
336 // main
337 //------------------------------------------------------------------------
339 int SAL_CALL main( int argc, const char* argv[] )
341 OUString aRegistry;
343 //------------------------------------------------------------------
344 // check command line parameters
345 //------------------------------------------------------------------
347 if ( NULL == ( app = strrchr( argv[0], PATH_SEPERATOR ) ) )
348 app = argv[0];
349 else
350 app++;
352 for( int n = 1; n < argc; n++ )
354 if( strncmp( argv[n], "-r", 2 ) == 0 )
356 if( strlen( argv[n] ) > 2 )
357 aRegistry = OUString::createFromAscii( argv[n] + 2 );
358 else if ( n + 1 < argc )
359 aRegistry = OUString::createFromAscii( argv[++n] );
363 if( aRegistry.getLength() == 0 )
364 fprintf( stderr, "Usage: %s -r full-path-to-applicat.rdb\n", app );
366 //------------------------------------------------------------------
367 // create service manager
368 //------------------------------------------------------------------
369 Reference< XMultiServiceFactory > xServiceManager;
371 try
373 xServiceManager = createRegistryServiceFactory( aRegistry, sal_True );
374 ENSURE( xServiceManager.is(), "*** ERROR *** service manager could not be created." );
376 //--------------------------------------------------------------
377 // create an instance of GenericClipboard service
378 //--------------------------------------------------------------
380 Sequence< Any > arguments(1);
381 arguments[0] = makeAny( OUString::createFromAscii( "generic" ) );
383 Reference< XClipboard > xClipboard( xServiceManager->createInstanceWithArguments(
384 OUString::createFromAscii( "com.sun.star.datatransfer.clipboard.GenericClipboard" ),
385 arguments ), UNO_QUERY );
387 ENSURE( xClipboard.is(), "*** ERROR *** generic clipboard service could not be created." );
389 Reference< XClipboardNotifier > xClipboardNotifier( xClipboard, UNO_QUERY );
390 Reference< XClipboardListener > xClipboardListener = new ClipboardListener();
391 ClipboardListener * pListener = (ClipboardListener *) xClipboardListener.get();
393 if( xClipboardNotifier.is() )
394 xClipboardNotifier->addClipboardListener( xClipboardListener );
396 //--------------------------------------------------------------
397 // run various tests on clipboard implementation
398 //--------------------------------------------------------------
400 TRACE( "\n*** testing generic clipboard service ***\n" );
402 Reference< XTransferable > xContents = new StringTransferable();
403 Reference< XClipboardOwner > xOwner = new ClipboardOwner();
404 ClipboardOwner *pOwner = (ClipboardOwner *) xOwner.get();
406 TEST( "initial contents (none): ", xClipboard->getContents().is() == sal_False );
408 PERFORM( "update on contents with clipboard owner: ", xClipboard->setContents( xContents, xOwner ) );
409 TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
411 if( xClipboardNotifier.is() )
413 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 0 );
414 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 1 );
415 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
416 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventTransferableValue() == xContents );
419 PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
420 TEST( "if received lostOwnership message(s): ", pOwner->receivedLostOwnerships() > 0 );
421 TEST( "if received exactly 1 lostOwnership message: ", pOwner->receivedLostOwnerships() == 1 );
422 TEST( "if received lostOwnership message for the correct clipboard: ", pOwner->lostOwnershipClipboardValue() == xClipboard );
423 TEST( "if received lostOwnership message for the correct transferable: ", pOwner->lostOwnershipTransferableValue() == xContents );
424 TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
426 if( xClipboardNotifier.is() )
428 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 1 );
429 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 2 );
430 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
431 TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
434 PERFORM( "update on contents without clipboard owner: ", xClipboard->setContents( xContents, Reference< XClipboardOwner >() ) );
435 TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
436 TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
438 if( xClipboardNotifier.is() )
440 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 2 );
441 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 3 );
442 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
443 TEST( "if received changedContents notification for correct transferable: ", pListener->changedContentsEventTransferableValue() == xContents );
447 PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
448 TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
449 TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
451 if( xClipboardNotifier.is() )
453 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 3 );
454 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 4 );
455 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
456 TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
459 //--------------------------------------------------------------
460 // create an instance of ClipboardManager service
461 //--------------------------------------------------------------
463 Reference< XClipboardManager > xClipboardManager( xServiceManager->createInstance(
464 OUString::createFromAscii( "com.sun.star.datatransfer.clipboard.ClipboardManager" ) ), UNO_QUERY );
466 ENSURE( xClipboardManager.is(), "*** ERROR *** clipboard manager service could not be created." );
468 //--------------------------------------------------------------
469 // run various tests on clipboard manager implementation
470 //--------------------------------------------------------------
472 TRACE( "\n*** testing clipboard manager service ***\n" );
474 TEST( "initial number of clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
475 PERFORM( "insertion of generic clipboard: ", xClipboardManager->addClipboard( xClipboard ) );
476 TEST( "number of inserted clipboards (1): ", xClipboardManager->listClipboardNames().getLength() == 1 );
477 TEST( "name of inserted clipboard (generic): ", xClipboardManager->listClipboardNames()[0] == OUString::createFromAscii( "generic" ) );
478 TEST( "inserted clipboard instance: ", xClipboardManager->getClipboard( OUString::createFromAscii( "generic" ) ) == xClipboard );
479 PERFORM( "removal of generic clipboard: ", xClipboardManager->removeClipboard( OUString::createFromAscii( "generic" ) ) );
480 TEST( "number of inserted clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
481 TRACE( "Testing inserted clipboard instance (none): " );
484 xClipboardManager->getClipboard( OUString::createFromAscii( "generic" ) );
485 TRACE( "FAILED\n" );
487 catch( NoSuchElementException e )
489 TRACE( "passed\n" );
493 catch ( Exception aException )
495 ENSURE( sal_False, "*** ERROR *** exception caught." );
498 //--------------------------------------------------------------------
499 // shutdown the service manager
500 //--------------------------------------------------------------------
502 // query XComponent interface
503 Reference< XComponent > xComponent( xServiceManager, UNO_QUERY );
505 ENSURE( xComponent.is(), "*** ERROR *** service manager does not support XComponent." );
507 // Dispose and clear factory
508 xComponent->dispose();
509 xServiceManager.clear();
511 fprintf( stderr, "Done.\n" );
512 return 0;