update credits
[LibreOffice.git] / dtrans / source / test / test_dtrans.cxx
blobb0226c83f1a43612c848338015579cd50b3f8fc8
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 <com/sun/star/datatransfer/XTransferable.hpp>
21 #include <com/sun/star/datatransfer/clipboard/XClipboardManager.hpp>
22 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
23 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
24 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
25 #include <com/sun/star/lang/XComponent.hpp>
27 #include <cppuhelper/servicefactory.hxx>
28 #include <cppuhelper/implbase1.hxx>
29 #include <cppuhelper/implbase2.hxx>
30 #include <rtl/ustring.hxx>
31 #include <osl/diagnose.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
37 //------------------------------------------------------------------------
38 // my defines
39 //------------------------------------------------------------------------
41 #ifdef UNX
42 #define PATH_SEPARATOR '/'
43 #else
44 #define PATH_SEPARATOR '\\'
45 #endif
47 #define ENSURE( a, b ) if( !a ) { fprintf( stderr, b "\n" ); exit( -1 ); }
48 #define TEST( a, b ) fprintf( stderr, "Testing " a ); fprintf( stderr, b ? "passed\n" : "FAILED\n" )
49 #define PERFORM( a, b ) fprintf( stderr, "Performing " a); b; fprintf( stderr, "done\n" )
50 #define TRACE( a ) fprintf( stderr, a )
52 //------------------------------------------------------------------------
53 // namespaces
54 //------------------------------------------------------------------------
56 using namespace ::rtl;
57 using namespace ::std;
58 using namespace ::cppu;
59 using namespace ::com::sun::star::container;
60 using namespace ::com::sun::star::datatransfer;
61 using namespace ::com::sun::star::datatransfer::clipboard;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::io;
64 using namespace ::com::sun::star::lang;
66 //------------------------------------------------------------------------
67 // globals
68 //------------------------------------------------------------------------
70 const char * app = NULL;
72 //------------------------------------------------------------------------
73 // ClipboardOwner
74 //------------------------------------------------------------------------
76 class ClipboardOwner : public WeakImplHelper1< XClipboardOwner >
78 Reference< XClipboard > m_xClipboard;
79 Reference< XTransferable > m_xTransferable;
81 sal_uInt32 m_nReceivedLostOwnerships;
83 public:
84 ClipboardOwner();
86 //--------------------------------------------------------------------
87 // XClipboardOwner
88 //--------------------------------------------------------------------
90 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
92 sal_uInt32 receivedLostOwnerships() { return m_nReceivedLostOwnerships; };
93 Reference< XClipboard > lostOwnershipClipboardValue() { return m_xClipboard; }
94 Reference< XTransferable > lostOwnershipTransferableValue() { return m_xTransferable; };
97 //------------------------------------------------------------------------
98 // ctor
99 //------------------------------------------------------------------------
101 ClipboardOwner::ClipboardOwner():
102 m_nReceivedLostOwnerships( 0 )
106 //------------------------------------------------------------------------
107 // lostOwnership
108 //------------------------------------------------------------------------
110 void SAL_CALL ClipboardOwner::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
111 throw(RuntimeException)
113 m_nReceivedLostOwnerships++;
114 m_xClipboard = xClipboard;
115 m_xTransferable = xTrans;
118 //------------------------------------------------------------------------
119 // ClipboardListener
120 //------------------------------------------------------------------------
122 class ClipboardListener : public WeakImplHelper1< XClipboardListener >
124 Reference< XClipboard > m_xClipboard;
125 Reference< XTransferable > m_xTransferable;
127 sal_uInt32 m_nReceivedChangedContentsEvents;
129 public:
130 ClipboardListener();
132 //--------------------------------------------------------------------
133 // XClipboardOwner
134 //--------------------------------------------------------------------
136 virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw(RuntimeException);
138 //--------------------------------------------------------------------
139 // XEventListener
140 //--------------------------------------------------------------------
142 virtual void SAL_CALL disposing( const EventObject& event ) throw(RuntimeException);
144 sal_uInt32 receivedChangedContentsEvents() { return m_nReceivedChangedContentsEvents; };
145 Reference< XClipboard > changedContentsEventClipboardValue() { return m_xClipboard; }
146 Reference< XTransferable > changedContentsEventTransferableValue() { return m_xTransferable; };
149 //------------------------------------------------------------------------
150 // ctor
151 //------------------------------------------------------------------------
153 ClipboardListener::ClipboardListener():
154 m_nReceivedChangedContentsEvents( 0 )
158 //------------------------------------------------------------------------
159 // changedContents
160 //------------------------------------------------------------------------
162 void SAL_CALL ClipboardListener::changedContents( const ClipboardEvent& event )
163 throw(RuntimeException)
165 m_nReceivedChangedContentsEvents++;
166 m_xClipboard = Reference< XClipboard > (event.Source, UNO_QUERY);
167 m_xTransferable = event.Contents;
170 //------------------------------------------------------------------------
171 // disposing
172 //------------------------------------------------------------------------
174 void SAL_CALL ClipboardListener::disposing( const EventObject& event )
175 throw(RuntimeException)
179 //------------------------------------------------------------------------
180 // StringTransferable
181 //------------------------------------------------------------------------
183 class StringTransferable : public WeakImplHelper2< XClipboardOwner, XTransferable >
185 public:
186 StringTransferable( );
188 //--------------------------------------------------------------------
189 // XTransferable
190 //--------------------------------------------------------------------
192 virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
193 virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
194 virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
196 //--------------------------------------------------------------------
197 // XClipboardOwner
198 //--------------------------------------------------------------------
200 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
202 sal_Bool receivedLostOwnership() { return m_receivedLostOwnership; };
203 void clearReceivedLostOwnership() { m_receivedLostOwnership = sal_False; };
205 private:
206 Sequence< DataFlavor > m_seqDFlv;
207 OUString m_Data;
208 sal_Bool m_receivedLostOwnership;
211 //------------------------------------------------------------------------
212 // ctor
213 //------------------------------------------------------------------------
215 StringTransferable::StringTransferable( ) :
216 m_seqDFlv( 1 ),
217 m_receivedLostOwnership( sal_False ),
218 m_Data( OUString("clipboard test content") )
220 DataFlavor df;
223 df.MimeType = L"text/plain; charset=unicode";
224 df.DataType = getCppuType( ( OUString* )0 );
226 m_seqDFlv[0] = df;
229 //df.MimeType = L"text/plain; charset=windows1252";
230 df.MimeType = OUString("text/html");
231 df.DataType = getCppuType( ( Sequence< sal_Int8 >* )0 );
233 m_seqDFlv[0] = df;
236 //------------------------------------------------------------------------
237 // getTransferData
238 //------------------------------------------------------------------------
240 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& aFlavor )
241 throw(UnsupportedFlavorException, IOException, RuntimeException)
243 Any anyData;
245 /*if ( aFlavor == m_seqDFlv[0] )
247 anyData = makeAny( m_Data );
248 } */
250 return anyData;
253 //------------------------------------------------------------------------
254 // getTransferDataFlavors
255 //------------------------------------------------------------------------
257 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors( )
258 throw(RuntimeException)
260 return m_seqDFlv;
263 //------------------------------------------------------------------------
264 // isDataFlavorSupported
265 //------------------------------------------------------------------------
267 sal_Bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
268 throw(RuntimeException)
270 sal_Int32 nLength = m_seqDFlv.getLength( );
271 sal_Bool bRet = sal_False;
273 // for ( sal_Int32 i = 0; i < nLength; ++i )
274 // {
275 // if ( m_seqDFlv[i] == aFlavor )
276 // {
277 // bRet = sal_True;
278 // break;
279 // }
280 // }
282 return bRet;
285 //------------------------------------------------------------------------
286 // lostOwnership
287 //------------------------------------------------------------------------
289 void SAL_CALL StringTransferable::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
290 throw(RuntimeException)
292 m_receivedLostOwnership = sal_True;
295 //------------------------------------------------------------------------
296 // main
297 //------------------------------------------------------------------------
299 int SAL_CALL main( int argc, const char* argv[] )
301 OUString aRegistry;
303 //------------------------------------------------------------------
304 // check command line parameters
305 //------------------------------------------------------------------
307 if ( NULL == ( app = strrchr( argv[0], PATH_SEPARATOR ) ) )
308 app = argv[0];
309 else
310 app++;
312 for( int n = 1; n < argc; n++ )
314 if( strncmp( argv[n], "-r", 2 ) == 0 )
316 if( strlen( argv[n] ) > 2 )
317 aRegistry = OUString::createFromAscii( argv[n] + 2 );
318 else if ( n + 1 < argc )
319 aRegistry = OUString::createFromAscii( argv[++n] );
323 if( aRegistry.isEmpty( ) )
324 fprintf( stderr, "Usage: %s -r full-path-to-applicat.rdb\n", app );
326 //------------------------------------------------------------------
327 // create service manager
328 //------------------------------------------------------------------
329 Reference< XMultiServiceFactory > xServiceManager;
333 xServiceManager = createRegistryServiceFactory( aRegistry, sal_True );
334 ENSURE( xServiceManager.is(), "*** ERROR *** service manager could not be created." );
336 //--------------------------------------------------------------
337 // create an instance of GenericClipboard service
338 //--------------------------------------------------------------
340 Sequence< Any > arguments(1);
341 arguments[0] = makeAny( OUString("generic") );
343 Reference< XClipboard > xClipboard( xServiceManager->createInstanceWithArguments(
344 OUString("com.sun.star.datatransfer.clipboard.GenericClipboard"),
345 arguments ), UNO_QUERY );
347 ENSURE( xClipboard.is(), "*** ERROR *** generic clipboard service could not be created." );
349 Reference< XClipboardNotifier > xClipboardNotifier( xClipboard, UNO_QUERY );
350 Reference< XClipboardListener > xClipboardListener = new ClipboardListener();
351 ClipboardListener * pListener = (ClipboardListener *) xClipboardListener.get();
353 if( xClipboardNotifier.is() )
354 xClipboardNotifier->addClipboardListener( xClipboardListener );
356 //--------------------------------------------------------------
357 // run various tests on clipboard implementation
358 //--------------------------------------------------------------
360 TRACE( "\n*** testing generic clipboard service ***\n" );
362 Reference< XTransferable > xContents = new StringTransferable();
363 Reference< XClipboardOwner > xOwner = new ClipboardOwner();
364 ClipboardOwner *pOwner = (ClipboardOwner *) xOwner.get();
366 TEST( "initial contents (none): ", xClipboard->getContents().is() == sal_False );
368 PERFORM( "update on contents with clipboard owner: ", xClipboard->setContents( xContents, xOwner ) );
369 TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
371 if( xClipboardNotifier.is() )
373 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 0 );
374 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 1 );
375 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
376 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventTransferableValue() == xContents );
379 PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
380 TEST( "if received lostOwnership message(s): ", pOwner->receivedLostOwnerships() > 0 );
381 TEST( "if received exactly 1 lostOwnership message: ", pOwner->receivedLostOwnerships() == 1 );
382 TEST( "if received lostOwnership message for the correct clipboard: ", pOwner->lostOwnershipClipboardValue() == xClipboard );
383 TEST( "if received lostOwnership message for the correct transferable: ", pOwner->lostOwnershipTransferableValue() == xContents );
384 TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
386 if( xClipboardNotifier.is() )
388 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 1 );
389 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 2 );
390 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
391 TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
394 PERFORM( "update on contents without clipboard owner: ", xClipboard->setContents( xContents, Reference< XClipboardOwner >() ) );
395 TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
396 TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
398 if( xClipboardNotifier.is() )
400 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 2 );
401 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 3 );
402 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
403 TEST( "if received changedContents notification for correct transferable: ", pListener->changedContentsEventTransferableValue() == xContents );
407 PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
408 TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
409 TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
411 if( xClipboardNotifier.is() )
413 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 3 );
414 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 4 );
415 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
416 TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
419 //--------------------------------------------------------------
420 // create an instance of ClipboardManager service
421 //--------------------------------------------------------------
423 Reference< XClipboardManager > xClipboardManager( xServiceManager->createInstance(
424 OUString("com.sun.star.datatransfer.clipboard.ClipboardManager") ), UNO_QUERY );
426 ENSURE( xClipboardManager.is(), "*** ERROR *** clipboard manager service could not be created." );
428 //--------------------------------------------------------------
429 // run various tests on clipboard manager implementation
430 //--------------------------------------------------------------
432 TRACE( "\n*** testing clipboard manager service ***\n" );
434 TEST( "initial number of clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
435 PERFORM( "insertion of generic clipboard: ", xClipboardManager->addClipboard( xClipboard ) );
436 TEST( "number of inserted clipboards (1): ", xClipboardManager->listClipboardNames().getLength() == 1 );
437 TEST( "name of inserted clipboard (generic): ", xClipboardManager->listClipboardNames()[0] == OUString("generic") );
438 TEST( "inserted clipboard instance: ", xClipboardManager->getClipboard( OUString("generic") ) == xClipboard );
439 PERFORM( "removal of generic clipboard: ", xClipboardManager->removeClipboard( OUString("generic") ) );
440 TEST( "number of inserted clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
441 TRACE( "Testing inserted clipboard instance (none): " );
444 xClipboardManager->getClipboard( OUString("generic") );
445 TRACE( "FAILED\n" );
447 catch (const NoSuchElementException&)
449 TRACE( "passed\n" );
453 catch (const Exception&)
455 ENSURE( sal_False, "*** ERROR *** exception caught." );
458 //--------------------------------------------------------------------
459 // shutdown the service manager
460 //--------------------------------------------------------------------
462 // query XComponent interface
463 Reference< XComponent > xComponent( xServiceManager, UNO_QUERY );
465 ENSURE( xComponent.is(), "*** ERROR *** service manager does not support XComponent." );
467 // Dispose and clear factory
468 xComponent->dispose();
469 xServiceManager.clear();
471 fprintf( stderr, "Done.\n" );
472 return 0;
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */