Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / dtrans / source / test / test_dtrans.cxx
blobf531f12fd11974194599519ca4a90df2d751cc9b
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/implbase.hxx>
29 #include <rtl/ustring.hxx>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
35 // my defines
37 #ifdef UNX
38 #define PATH_SEPARATOR '/'
39 #else
40 #define PATH_SEPARATOR '\\'
41 #endif
43 #define ENSURE( a, b ) if( !a ) { fprintf( stderr, b "\n" ); exit( -1 ); }
44 #define TEST( a, b ) fprintf( stderr, "Testing " a ); fprintf( stderr, b ? "passed\n" : "FAILED\n" )
45 #define PERFORM( a, b ) fprintf( stderr, "Performing " a); b; fprintf( stderr, "done\n" )
46 #define TRACE( a ) fprintf( stderr, a )
48 // namespaces
50 using namespace ::std;
51 using namespace ::cppu;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::datatransfer;
54 using namespace ::com::sun::star::datatransfer::clipboard;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::io;
57 using namespace ::com::sun::star::lang;
59 // globals
61 const char * app = NULL;
63 // ClipboardOwner
65 class ClipboardOwner : public WeakImplHelper< XClipboardOwner >
67 Reference< XClipboard > m_xClipboard;
68 Reference< XTransferable > m_xTransferable;
70 sal_uInt32 m_nReceivedLostOwnerships;
72 public:
73 ClipboardOwner();
75 // XClipboardOwner
77 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
79 sal_uInt32 receivedLostOwnerships() { return m_nReceivedLostOwnerships; };
80 Reference< XClipboard > lostOwnershipClipboardValue() { return m_xClipboard; }
81 Reference< XTransferable > lostOwnershipTransferableValue() { return m_xTransferable; };
84 // ctor
86 ClipboardOwner::ClipboardOwner():
87 m_nReceivedLostOwnerships( 0 )
91 // lostOwnership
93 void SAL_CALL ClipboardOwner::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
94 throw(RuntimeException)
96 m_nReceivedLostOwnerships++;
97 m_xClipboard = xClipboard;
98 m_xTransferable = xTrans;
101 // ClipboardListener
103 class ClipboardListener : public WeakImplHelper< XClipboardListener >
105 Reference< XClipboard > m_xClipboard;
106 Reference< XTransferable > m_xTransferable;
108 sal_uInt32 m_nReceivedChangedContentsEvents;
110 public:
111 ClipboardListener();
113 // XClipboardOwner
115 virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw(RuntimeException);
117 // XEventListener
119 virtual void SAL_CALL disposing( const EventObject& event ) throw(RuntimeException);
121 sal_uInt32 receivedChangedContentsEvents() { return m_nReceivedChangedContentsEvents; };
122 Reference< XClipboard > changedContentsEventClipboardValue() { return m_xClipboard; }
123 Reference< XTransferable > changedContentsEventTransferableValue() { return m_xTransferable; };
126 // ctor
128 ClipboardListener::ClipboardListener():
129 m_nReceivedChangedContentsEvents( 0 )
133 // changedContents
135 void SAL_CALL ClipboardListener::changedContents( const ClipboardEvent& event )
136 throw(RuntimeException)
138 m_nReceivedChangedContentsEvents++;
139 m_xClipboard.set(event.Source, UNO_QUERY);
140 m_xTransferable = event.Contents;
143 // disposing
145 void SAL_CALL ClipboardListener::disposing( const EventObject& event )
146 throw(RuntimeException)
150 // StringTransferable
152 class StringTransferable : public WeakImplHelper< XClipboardOwner, XTransferable >
154 public:
155 StringTransferable( );
157 // XTransferable
159 virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
160 virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
161 virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
163 // XClipboardOwner
165 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
167 sal_Bool receivedLostOwnership() { return m_receivedLostOwnership; };
168 void clearReceivedLostOwnership() { m_receivedLostOwnership = sal_False; };
170 private:
171 Sequence< DataFlavor > m_seqDFlv;
172 OUString m_Data;
173 sal_Bool m_receivedLostOwnership;
176 // ctor
178 StringTransferable::StringTransferable( ) :
179 m_seqDFlv( 1 ),
180 m_receivedLostOwnership( sal_False ),
181 m_Data( OUString("clipboard test content") )
183 DataFlavor df;
186 df.MimeType = L"text/plain; charset=unicode";
187 df.DataType = cppu::UnoType<OUString>::get();
189 m_seqDFlv[0] = df;
192 //df.MimeType = L"text/plain; charset=windows1252";
193 df.MimeType = "text/html";
194 df.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
196 m_seqDFlv[0] = df;
199 // getTransferData
201 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& aFlavor )
202 throw(UnsupportedFlavorException, IOException, RuntimeException)
204 Any anyData;
206 /*if ( aFlavor == m_seqDFlv[0] )
208 anyData = makeAny( m_Data );
209 } */
211 return anyData;
214 // getTransferDataFlavors
216 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors( )
217 throw(RuntimeException)
219 return m_seqDFlv;
222 // isDataFlavorSupported
224 sal_Bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
225 throw(RuntimeException)
227 sal_Int32 nLength = m_seqDFlv.getLength( );
228 sal_Bool bRet = sal_False;
230 // for ( sal_Int32 i = 0; i < nLength; ++i )
231 // {
232 // if ( m_seqDFlv[i] == aFlavor )
233 // {
234 // bRet = sal_True;
235 // break;
236 // }
237 // }
239 return bRet;
242 // lostOwnership
244 void SAL_CALL StringTransferable::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
245 throw(RuntimeException)
247 m_receivedLostOwnership = sal_True;
250 // main
252 int SAL_CALL main( int argc, const char* argv[] )
254 OUString aRegistry;
256 // check command line parameters
258 if ( NULL == ( app = strrchr( argv[0], PATH_SEPARATOR ) ) )
259 app = argv[0];
260 else
261 app++;
263 for( int n = 1; n < argc; n++ )
265 if( strncmp( argv[n], "-r", 2 ) == 0 )
267 if( strlen( argv[n] ) > 2 )
268 aRegistry = OUString::createFromAscii( argv[n] + 2 );
269 else if ( n + 1 < argc )
270 aRegistry = OUString::createFromAscii( argv[++n] );
274 if( aRegistry.isEmpty( ) )
275 fprintf( stderr, "Usage: %s -r full-path-to-applicat.rdb\n", app );
277 // create service manager
279 Reference< XMultiServiceFactory > xServiceManager;
283 xServiceManager = createRegistryServiceFactory( aRegistry, sal_True );
284 ENSURE( xServiceManager.is(), "*** ERROR *** service manager could not be created." );
286 // create an instance of GenericClipboard service
288 Sequence< Any > arguments(1);
289 arguments[0] = makeAny( OUString("generic") );
291 Reference< XClipboard > xClipboard( xServiceManager->createInstanceWithArguments(
292 "com.sun.star.datatransfer.clipboard.GenericClipboard",
293 arguments ), UNO_QUERY );
295 ENSURE( xClipboard.is(), "*** ERROR *** generic clipboard service could not be created." );
297 Reference< XClipboardNotifier > xClipboardNotifier( xClipboard, UNO_QUERY );
298 Reference< XClipboardListener > xClipboardListener = new ClipboardListener();
299 ClipboardListener * pListener = (ClipboardListener *) xClipboardListener.get();
301 if( xClipboardNotifier.is() )
302 xClipboardNotifier->addClipboardListener( xClipboardListener );
304 // run various tests on clipboard implementation
306 TRACE( "\n*** testing generic clipboard service ***\n" );
308 Reference< XTransferable > xContents = new StringTransferable();
309 Reference< XClipboardOwner > xOwner = new ClipboardOwner();
310 ClipboardOwner *pOwner = (ClipboardOwner *) xOwner.get();
312 TEST( "initial contents (none): ", xClipboard->getContents().is() == sal_False );
314 PERFORM( "update on contents with clipboard owner: ", xClipboard->setContents( xContents, xOwner ) );
315 TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
317 if( xClipboardNotifier.is() )
319 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 0 );
320 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 1 );
321 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
322 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventTransferableValue() == xContents );
325 PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
326 TEST( "if received lostOwnership message(s): ", pOwner->receivedLostOwnerships() > 0 );
327 TEST( "if received exactly 1 lostOwnership message: ", pOwner->receivedLostOwnerships() == 1 );
328 TEST( "if received lostOwnership message for the correct clipboard: ", pOwner->lostOwnershipClipboardValue() == xClipboard );
329 TEST( "if received lostOwnership message for the correct transferable: ", pOwner->lostOwnershipTransferableValue() == xContents );
330 TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
332 if( xClipboardNotifier.is() )
334 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 1 );
335 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 2 );
336 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
337 TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
340 PERFORM( "update on contents without clipboard owner: ", xClipboard->setContents( xContents, Reference< XClipboardOwner >() ) );
341 TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
342 TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
344 if( xClipboardNotifier.is() )
346 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 2 );
347 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 3 );
348 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
349 TEST( "if received changedContents notification for correct transferable: ", pListener->changedContentsEventTransferableValue() == xContents );
352 PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
353 TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
354 TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
356 if( xClipboardNotifier.is() )
358 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 3 );
359 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 4 );
360 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
361 TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
364 // create an instance of ClipboardManager service
366 Reference< XClipboardManager > xClipboardManager( xServiceManager->createInstance(
367 "com.sun.star.datatransfer.clipboard.ClipboardManager" ), UNO_QUERY );
369 ENSURE( xClipboardManager.is(), "*** ERROR *** clipboard manager service could not be created." );
371 // run various tests on clipboard manager implementation
373 TRACE( "\n*** testing clipboard manager service ***\n" );
375 TEST( "initial number of clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
376 PERFORM( "insertion of generic clipboard: ", xClipboardManager->addClipboard( xClipboard ) );
377 TEST( "number of inserted clipboards (1): ", xClipboardManager->listClipboardNames().getLength() == 1 );
378 TEST( "name of inserted clipboard (generic): ", xClipboardManager->listClipboardNames()[0] == "generic" );
379 TEST( "inserted clipboard instance: ", xClipboardManager->getClipboard( OUString("generic") ) == xClipboard );
380 PERFORM( "removal of generic clipboard: ", xClipboardManager->removeClipboard( OUString("generic") ) );
381 TEST( "number of inserted clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
382 TRACE( "Testing inserted clipboard instance (none): " );
385 xClipboardManager->getClipboard( OUString("generic") );
386 TRACE( "FAILED\n" );
388 catch (const NoSuchElementException&)
390 TRACE( "passed\n" );
394 catch (const Exception&)
396 ENSURE( sal_False, "*** ERROR *** exception caught." );
399 // shutdown the service manager
401 // query XComponent interface
402 Reference< XComponent > xComponent( xServiceManager, UNO_QUERY );
404 ENSURE( xComponent.is(), "*** ERROR *** service manager does not support XComponent." );
406 // Dispose and clear factory
407 xComponent->dispose();
408 xServiceManager.clear();
410 fprintf( stderr, "Done.\n" );
411 return 0;
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */