1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 //------------------------------------------------------------------------
31 //------------------------------------------------------------------------
32 #include <com/sun/star/datatransfer/XTransferable.hpp>
33 #include <com/sun/star/datatransfer/clipboard/XClipboardManager.hpp>
34 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
35 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
36 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
37 #include <com/sun/star/lang/XComponent.hpp>
39 //------------------------------------------------------------------------
41 //------------------------------------------------------------------------
44 #include <cppuhelper/servicefactory.hxx>
45 #include <cppuhelper/implbase1.hxx>
46 #include <cppuhelper/implbase2.hxx>
47 #include <rtl/ustring.hxx>
48 #include <osl/diagnose.h>
54 //------------------------------------------------------------------------
56 //------------------------------------------------------------------------
59 #define PATH_SEPERATOR '/'
61 #define PATH_SEPERATOR '\\'
64 #define ENSURE( a, b ) if( !a ) { fprintf( stderr, b "\n" ); exit( -1 ); }
65 #define TEST( a, b ) fprintf( stderr, "Testing " a ); fprintf( stderr, b ? "passed\n" : "FAILED\n" )
66 #define PERFORM( a, b ) fprintf( stderr, "Performing " a); b; fprintf( stderr, "done\n" )
67 #define TRACE( a ) fprintf( stderr, a )
69 //------------------------------------------------------------------------
71 //------------------------------------------------------------------------
73 using namespace ::rtl
;
74 using namespace ::std
;
75 using namespace ::cppu
;
76 using namespace ::com::sun::star::container
;
77 using namespace ::com::sun::star::datatransfer
;
78 using namespace ::com::sun::star::datatransfer::clipboard
;
79 using namespace ::com::sun::star::uno
;
80 using namespace ::com::sun::star::io
;
81 using namespace ::com::sun::star::lang
;
83 //------------------------------------------------------------------------
85 //------------------------------------------------------------------------
87 const char * app
= NULL
;
89 //------------------------------------------------------------------------
91 //------------------------------------------------------------------------
93 class ClipboardOwner
: public WeakImplHelper1
< XClipboardOwner
>
95 Reference
< XClipboard
> m_xClipboard
;
96 Reference
< XTransferable
> m_xTransferable
;
98 sal_uInt32 m_nReceivedLostOwnerships
;
103 //--------------------------------------------------------------------
105 //--------------------------------------------------------------------
107 virtual void SAL_CALL
lostOwnership( const Reference
< XClipboard
>& xClipboard
, const Reference
< XTransferable
>& xTrans
) throw(RuntimeException
);
109 sal_uInt32
receivedLostOwnerships() { return m_nReceivedLostOwnerships
; };
110 Reference
< XClipboard
> lostOwnershipClipboardValue() { return m_xClipboard
; }
111 Reference
< XTransferable
> lostOwnershipTransferableValue() { return m_xTransferable
; };
114 //------------------------------------------------------------------------
116 //------------------------------------------------------------------------
118 ClipboardOwner::ClipboardOwner():
119 m_nReceivedLostOwnerships( 0 )
123 //------------------------------------------------------------------------
125 //------------------------------------------------------------------------
127 void SAL_CALL
ClipboardOwner::lostOwnership( const Reference
< XClipboard
>& xClipboard
, const Reference
< XTransferable
>& xTrans
)
128 throw(RuntimeException
)
130 m_nReceivedLostOwnerships
++;
131 m_xClipboard
= xClipboard
;
132 m_xTransferable
= xTrans
;
135 //------------------------------------------------------------------------
137 //------------------------------------------------------------------------
139 class ClipboardListener
: public WeakImplHelper1
< XClipboardListener
>
141 Reference
< XClipboard
> m_xClipboard
;
142 Reference
< XTransferable
> m_xTransferable
;
144 sal_uInt32 m_nReceivedChangedContentsEvents
;
149 //--------------------------------------------------------------------
151 //--------------------------------------------------------------------
153 virtual void SAL_CALL
changedContents( const ClipboardEvent
& event
) throw(RuntimeException
);
155 //--------------------------------------------------------------------
157 //--------------------------------------------------------------------
159 virtual void SAL_CALL
disposing( const EventObject
& event
) throw(RuntimeException
);
161 sal_uInt32
receivedChangedContentsEvents() { return m_nReceivedChangedContentsEvents
; };
162 Reference
< XClipboard
> changedContentsEventClipboardValue() { return m_xClipboard
; }
163 Reference
< XTransferable
> changedContentsEventTransferableValue() { return m_xTransferable
; };
166 //------------------------------------------------------------------------
168 //------------------------------------------------------------------------
170 ClipboardListener::ClipboardListener():
171 m_nReceivedChangedContentsEvents( 0 )
175 //------------------------------------------------------------------------
177 //------------------------------------------------------------------------
179 void SAL_CALL
ClipboardListener::changedContents( const ClipboardEvent
& event
)
180 throw(RuntimeException
)
182 m_nReceivedChangedContentsEvents
++;
183 m_xClipboard
= Reference
< XClipboard
> (event
.Source
, UNO_QUERY
);
184 m_xTransferable
= event
.Contents
;
187 //------------------------------------------------------------------------
189 //------------------------------------------------------------------------
191 void SAL_CALL
ClipboardListener::disposing( const EventObject
& event
)
192 throw(RuntimeException
)
196 //------------------------------------------------------------------------
197 // StringTransferable
198 //------------------------------------------------------------------------
200 class StringTransferable
: public WeakImplHelper2
< XClipboardOwner
, XTransferable
>
203 StringTransferable( );
205 //--------------------------------------------------------------------
207 //--------------------------------------------------------------------
209 virtual Any SAL_CALL
getTransferData( const DataFlavor
& aFlavor
) throw(UnsupportedFlavorException
, IOException
, RuntimeException
);
210 virtual Sequence
< DataFlavor
> SAL_CALL
getTransferDataFlavors( ) throw(RuntimeException
);
211 virtual sal_Bool SAL_CALL
isDataFlavorSupported( const DataFlavor
& aFlavor
) throw(RuntimeException
);
213 //--------------------------------------------------------------------
215 //--------------------------------------------------------------------
217 virtual void SAL_CALL
lostOwnership( const Reference
< XClipboard
>& xClipboard
, const Reference
< XTransferable
>& xTrans
) throw(RuntimeException
);
219 sal_Bool
receivedLostOwnership() { return m_receivedLostOwnership
; };
220 void clearReceivedLostOwnership() { m_receivedLostOwnership
= sal_False
; };
223 Sequence
< DataFlavor
> m_seqDFlv
;
225 sal_Bool m_receivedLostOwnership
;
228 //------------------------------------------------------------------------
230 //------------------------------------------------------------------------
232 StringTransferable::StringTransferable( ) :
234 m_receivedLostOwnership( sal_False
),
235 m_Data( OUString("clipboard test content") )
240 df.MimeType = L"text/plain; charset=unicode";
241 df.DataType = getCppuType( ( OUString* )0 );
246 //df.MimeType = L"text/plain; charset=windows1252";
247 df
.MimeType
= OUString("text/html");
248 df
.DataType
= getCppuType( ( Sequence
< sal_Int8
>* )0 );
253 //------------------------------------------------------------------------
255 //------------------------------------------------------------------------
257 Any SAL_CALL
StringTransferable::getTransferData( const DataFlavor
& aFlavor
)
258 throw(UnsupportedFlavorException
, IOException
, RuntimeException
)
262 /*if ( aFlavor == m_seqDFlv[0] )
264 anyData = makeAny( m_Data );
270 //------------------------------------------------------------------------
271 // getTransferDataFlavors
272 //------------------------------------------------------------------------
274 Sequence
< DataFlavor
> SAL_CALL
StringTransferable::getTransferDataFlavors( )
275 throw(RuntimeException
)
280 //------------------------------------------------------------------------
281 // isDataFlavorSupported
282 //------------------------------------------------------------------------
284 sal_Bool SAL_CALL
StringTransferable::isDataFlavorSupported( const DataFlavor
& aFlavor
)
285 throw(RuntimeException
)
287 sal_Int32 nLength
= m_seqDFlv
.getLength( );
288 sal_Bool bRet
= sal_False
;
290 // for ( sal_Int32 i = 0; i < nLength; ++i )
292 // if ( m_seqDFlv[i] == aFlavor )
302 //------------------------------------------------------------------------
304 //------------------------------------------------------------------------
306 void SAL_CALL
StringTransferable::lostOwnership( const Reference
< XClipboard
>& xClipboard
, const Reference
< XTransferable
>& xTrans
)
307 throw(RuntimeException
)
309 m_receivedLostOwnership
= sal_True
;
312 //------------------------------------------------------------------------
314 //------------------------------------------------------------------------
316 int SAL_CALL
main( int argc
, const char* argv
[] )
320 //------------------------------------------------------------------
321 // check command line parameters
322 //------------------------------------------------------------------
324 if ( NULL
== ( app
= strrchr( argv
[0], PATH_SEPERATOR
) ) )
329 for( int n
= 1; n
< argc
; n
++ )
331 if( strncmp( argv
[n
], "-r", 2 ) == 0 )
333 if( strlen( argv
[n
] ) > 2 )
334 aRegistry
= OUString::createFromAscii( argv
[n
] + 2 );
335 else if ( n
+ 1 < argc
)
336 aRegistry
= OUString::createFromAscii( argv
[++n
] );
340 if( aRegistry
.isEmpty( ) )
341 fprintf( stderr
, "Usage: %s -r full-path-to-applicat.rdb\n", app
);
343 //------------------------------------------------------------------
344 // create service manager
345 //------------------------------------------------------------------
346 Reference
< XMultiServiceFactory
> xServiceManager
;
350 xServiceManager
= createRegistryServiceFactory( aRegistry
, sal_True
);
351 ENSURE( xServiceManager
.is(), "*** ERROR *** service manager could not be created." );
353 //--------------------------------------------------------------
354 // create an instance of GenericClipboard service
355 //--------------------------------------------------------------
357 Sequence
< Any
> arguments(1);
358 arguments
[0] = makeAny( OUString("generic") );
360 Reference
< XClipboard
> xClipboard( xServiceManager
->createInstanceWithArguments(
361 OUString("com.sun.star.datatransfer.clipboard.GenericClipboard"),
362 arguments
), UNO_QUERY
);
364 ENSURE( xClipboard
.is(), "*** ERROR *** generic clipboard service could not be created." );
366 Reference
< XClipboardNotifier
> xClipboardNotifier( xClipboard
, UNO_QUERY
);
367 Reference
< XClipboardListener
> xClipboardListener
= new ClipboardListener();
368 ClipboardListener
* pListener
= (ClipboardListener
*) xClipboardListener
.get();
370 if( xClipboardNotifier
.is() )
371 xClipboardNotifier
->addClipboardListener( xClipboardListener
);
373 //--------------------------------------------------------------
374 // run various tests on clipboard implementation
375 //--------------------------------------------------------------
377 TRACE( "\n*** testing generic clipboard service ***\n" );
379 Reference
< XTransferable
> xContents
= new StringTransferable();
380 Reference
< XClipboardOwner
> xOwner
= new ClipboardOwner();
381 ClipboardOwner
*pOwner
= (ClipboardOwner
*) xOwner
.get();
383 TEST( "initial contents (none): ", xClipboard
->getContents().is() == sal_False
);
385 PERFORM( "update on contents with clipboard owner: ", xClipboard
->setContents( xContents
, xOwner
) );
386 TEST( "current clipboard contents: ", xContents
== xClipboard
->getContents() );
388 if( xClipboardNotifier
.is() )
390 TEST( "if received changedContents notifications: ", pListener
->receivedChangedContentsEvents() > 0 );
391 TEST( "if received exactly 1 changedContents notification: ", pListener
->receivedChangedContentsEvents() == 1 );
392 TEST( "if received changedContents notification for correct clipboard: ", pListener
->changedContentsEventClipboardValue() == xClipboard
);
393 TEST( "if received changedContents notification for correct clipboard: ", pListener
->changedContentsEventTransferableValue() == xContents
);
396 PERFORM( "update on contents without data (clear): ", xClipboard
->setContents( Reference
< XTransferable
>(), Reference
< XClipboardOwner
>() ) );
397 TEST( "if received lostOwnership message(s): ", pOwner
->receivedLostOwnerships() > 0 );
398 TEST( "if received exactly 1 lostOwnership message: ", pOwner
->receivedLostOwnerships() == 1 );
399 TEST( "if received lostOwnership message for the correct clipboard: ", pOwner
->lostOwnershipClipboardValue() == xClipboard
);
400 TEST( "if received lostOwnership message for the correct transferable: ", pOwner
->lostOwnershipTransferableValue() == xContents
);
401 TEST( "current clipboard contents (none): ", xClipboard
->getContents().is() == sal_False
);
403 if( xClipboardNotifier
.is() )
405 TEST( "if received changedContents notifications: ", pListener
->receivedChangedContentsEvents() > 1 );
406 TEST( "if received exactly 1 changedContents notification: ", pListener
->receivedChangedContentsEvents() == 2 );
407 TEST( "if received changedContents notification for correct clipboard: ", pListener
->changedContentsEventClipboardValue() == xClipboard
);
408 TEST( "if received changedContents notification for correct transferable: ", ! pListener
->changedContentsEventTransferableValue().is() );
411 PERFORM( "update on contents without clipboard owner: ", xClipboard
->setContents( xContents
, Reference
< XClipboardOwner
>() ) );
412 TEST( "that no further lostOwnership messages were received: ", pOwner
->receivedLostOwnerships() == 1 );
413 TEST( "current clipboard contents: ", xContents
== xClipboard
->getContents() );
415 if( xClipboardNotifier
.is() )
417 TEST( "if received changedContents notifications: ", pListener
->receivedChangedContentsEvents() > 2 );
418 TEST( "if received exactly 1 changedContents notification: ", pListener
->receivedChangedContentsEvents() == 3 );
419 TEST( "if received changedContents notification for correct clipboard: ", pListener
->changedContentsEventClipboardValue() == xClipboard
);
420 TEST( "if received changedContents notification for correct transferable: ", pListener
->changedContentsEventTransferableValue() == xContents
);
424 PERFORM( "update on contents without data (clear): ", xClipboard
->setContents( Reference
< XTransferable
>(), Reference
< XClipboardOwner
>() ) );
425 TEST( "that no further lostOwnership messages were received: ", pOwner
->receivedLostOwnerships() == 1 );
426 TEST( "current clipboard contents (none): ", xClipboard
->getContents().is() == sal_False
);
428 if( xClipboardNotifier
.is() )
430 TEST( "if received changedContents notifications: ", pListener
->receivedChangedContentsEvents() > 3 );
431 TEST( "if received exactly 1 changedContents notification: ", pListener
->receivedChangedContentsEvents() == 4 );
432 TEST( "if received changedContents notification for correct clipboard: ", pListener
->changedContentsEventClipboardValue() == xClipboard
);
433 TEST( "if received changedContents notification for correct transferable: ", ! pListener
->changedContentsEventTransferableValue().is() );
436 //--------------------------------------------------------------
437 // create an instance of ClipboardManager service
438 //--------------------------------------------------------------
440 Reference
< XClipboardManager
> xClipboardManager( xServiceManager
->createInstance(
441 OUString("com.sun.star.datatransfer.clipboard.ClipboardManager") ), UNO_QUERY
);
443 ENSURE( xClipboardManager
.is(), "*** ERROR *** clipboard manager service could not be created." );
445 //--------------------------------------------------------------
446 // run various tests on clipboard manager implementation
447 //--------------------------------------------------------------
449 TRACE( "\n*** testing clipboard manager service ***\n" );
451 TEST( "initial number of clipboards (0): ", xClipboardManager
->listClipboardNames().getLength() == 0 );
452 PERFORM( "insertion of generic clipboard: ", xClipboardManager
->addClipboard( xClipboard
) );
453 TEST( "number of inserted clipboards (1): ", xClipboardManager
->listClipboardNames().getLength() == 1 );
454 TEST( "name of inserted clipboard (generic): ", xClipboardManager
->listClipboardNames()[0] == OUString("generic") );
455 TEST( "inserted clipboard instance: ", xClipboardManager
->getClipboard( OUString("generic") ) == xClipboard
);
456 PERFORM( "removal of generic clipboard: ", xClipboardManager
->removeClipboard( OUString("generic") ) );
457 TEST( "number of inserted clipboards (0): ", xClipboardManager
->listClipboardNames().getLength() == 0 );
458 TRACE( "Testing inserted clipboard instance (none): " );
461 xClipboardManager
->getClipboard( OUString("generic") );
464 catch (const NoSuchElementException
&)
470 catch (const Exception
&)
472 ENSURE( sal_False
, "*** ERROR *** exception caught." );
475 //--------------------------------------------------------------------
476 // shutdown the service manager
477 //--------------------------------------------------------------------
479 // query XComponent interface
480 Reference
< XComponent
> xComponent( xServiceManager
, UNO_QUERY
);
482 ENSURE( xComponent
.is(), "*** ERROR *** service manager does not support XComponent." );
484 // Dispose and clear factory
485 xComponent
->dispose();
486 xServiceManager
.clear();
488 fprintf( stderr
, "Done.\n" );
493 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */