build fix
[LibreOffice.git] / dtrans / source / test / test_dtrans.cxx
blobb81b3b6c8272a845fd5bacfb8045d74388440011
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>
30 #include <osl/diagnose.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
36 // my defines
38 #ifdef UNX
39 #define PATH_SEPARATOR '/'
40 #else
41 #define PATH_SEPARATOR '\\'
42 #endif
44 #define ENSURE( a, b ) if( !a ) { fprintf( stderr, b "\n" ); exit( -1 ); }
45 #define TEST( a, b ) fprintf( stderr, "Testing " a ); fprintf( stderr, b ? "passed\n" : "FAILED\n" )
46 #define PERFORM( a, b ) fprintf( stderr, "Performing " a); b; fprintf( stderr, "done\n" )
47 #define TRACE( a ) fprintf( stderr, a )
49 // namespaces
51 using namespace ::std;
52 using namespace ::cppu;
53 using namespace ::com::sun::star::container;
54 using namespace ::com::sun::star::datatransfer;
55 using namespace ::com::sun::star::datatransfer::clipboard;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::io;
58 using namespace ::com::sun::star::lang;
60 // globals
62 const char * app = NULL;
64 // ClipboardOwner
66 class ClipboardOwner : public WeakImplHelper< XClipboardOwner >
68 Reference< XClipboard > m_xClipboard;
69 Reference< XTransferable > m_xTransferable;
71 sal_uInt32 m_nReceivedLostOwnerships;
73 public:
74 ClipboardOwner();
76 // XClipboardOwner
78 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
80 sal_uInt32 receivedLostOwnerships() { return m_nReceivedLostOwnerships; };
81 Reference< XClipboard > lostOwnershipClipboardValue() { return m_xClipboard; }
82 Reference< XTransferable > lostOwnershipTransferableValue() { return m_xTransferable; };
85 // ctor
87 ClipboardOwner::ClipboardOwner():
88 m_nReceivedLostOwnerships( 0 )
92 // lostOwnership
94 void SAL_CALL ClipboardOwner::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
95 throw(RuntimeException)
97 m_nReceivedLostOwnerships++;
98 m_xClipboard = xClipboard;
99 m_xTransferable = xTrans;
102 // ClipboardListener
104 class ClipboardListener : public WeakImplHelper< XClipboardListener >
106 Reference< XClipboard > m_xClipboard;
107 Reference< XTransferable > m_xTransferable;
109 sal_uInt32 m_nReceivedChangedContentsEvents;
111 public:
112 ClipboardListener();
114 // XClipboardOwner
116 virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw(RuntimeException);
118 // XEventListener
120 virtual void SAL_CALL disposing( const EventObject& event ) throw(RuntimeException);
122 sal_uInt32 receivedChangedContentsEvents() { return m_nReceivedChangedContentsEvents; };
123 Reference< XClipboard > changedContentsEventClipboardValue() { return m_xClipboard; }
124 Reference< XTransferable > changedContentsEventTransferableValue() { return m_xTransferable; };
127 // ctor
129 ClipboardListener::ClipboardListener():
130 m_nReceivedChangedContentsEvents( 0 )
134 // changedContents
136 void SAL_CALL ClipboardListener::changedContents( const ClipboardEvent& event )
137 throw(RuntimeException)
139 m_nReceivedChangedContentsEvents++;
140 m_xClipboard.set(event.Source, UNO_QUERY);
141 m_xTransferable = event.Contents;
144 // disposing
146 void SAL_CALL ClipboardListener::disposing( const EventObject& event )
147 throw(RuntimeException)
151 // StringTransferable
153 class StringTransferable : public WeakImplHelper< XClipboardOwner, XTransferable >
155 public:
156 StringTransferable( );
158 // XTransferable
160 virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
161 virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
162 virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
164 // XClipboardOwner
166 virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
168 sal_Bool receivedLostOwnership() { return m_receivedLostOwnership; };
169 void clearReceivedLostOwnership() { m_receivedLostOwnership = sal_False; };
171 private:
172 Sequence< DataFlavor > m_seqDFlv;
173 OUString m_Data;
174 sal_Bool m_receivedLostOwnership;
177 // ctor
179 StringTransferable::StringTransferable( ) :
180 m_seqDFlv( 1 ),
181 m_receivedLostOwnership( sal_False ),
182 m_Data( OUString("clipboard test content") )
184 DataFlavor df;
187 df.MimeType = L"text/plain; charset=unicode";
188 df.DataType = cppu::UnoType<OUString>::get();
190 m_seqDFlv[0] = df;
193 //df.MimeType = L"text/plain; charset=windows1252";
194 df.MimeType = "text/html";
195 df.DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
197 m_seqDFlv[0] = df;
200 // getTransferData
202 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& aFlavor )
203 throw(UnsupportedFlavorException, IOException, RuntimeException)
205 Any anyData;
207 /*if ( aFlavor == m_seqDFlv[0] )
209 anyData = makeAny( m_Data );
210 } */
212 return anyData;
215 // getTransferDataFlavors
217 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors( )
218 throw(RuntimeException)
220 return m_seqDFlv;
223 // isDataFlavorSupported
225 sal_Bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
226 throw(RuntimeException)
228 sal_Int32 nLength = m_seqDFlv.getLength( );
229 sal_Bool bRet = sal_False;
231 // for ( sal_Int32 i = 0; i < nLength; ++i )
232 // {
233 // if ( m_seqDFlv[i] == aFlavor )
234 // {
235 // bRet = sal_True;
236 // break;
237 // }
238 // }
240 return bRet;
243 // lostOwnership
245 void SAL_CALL StringTransferable::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
246 throw(RuntimeException)
248 m_receivedLostOwnership = sal_True;
251 // main
253 int SAL_CALL main( int argc, const char* argv[] )
255 OUString aRegistry;
257 // check command line parameters
259 if ( NULL == ( app = strrchr( argv[0], PATH_SEPARATOR ) ) )
260 app = argv[0];
261 else
262 app++;
264 for( int n = 1; n < argc; n++ )
266 if( strncmp( argv[n], "-r", 2 ) == 0 )
268 if( strlen( argv[n] ) > 2 )
269 aRegistry = OUString::createFromAscii( argv[n] + 2 );
270 else if ( n + 1 < argc )
271 aRegistry = OUString::createFromAscii( argv[++n] );
275 if( aRegistry.isEmpty( ) )
276 fprintf( stderr, "Usage: %s -r full-path-to-applicat.rdb\n", app );
278 // create service manager
280 Reference< XMultiServiceFactory > xServiceManager;
284 xServiceManager = createRegistryServiceFactory( aRegistry, sal_True );
285 ENSURE( xServiceManager.is(), "*** ERROR *** service manager could not be created." );
287 // create an instance of GenericClipboard service
289 Sequence< Any > arguments(1);
290 arguments[0] = makeAny( OUString("generic") );
292 Reference< XClipboard > xClipboard( xServiceManager->createInstanceWithArguments(
293 "com.sun.star.datatransfer.clipboard.GenericClipboard",
294 arguments ), UNO_QUERY );
296 ENSURE( xClipboard.is(), "*** ERROR *** generic clipboard service could not be created." );
298 Reference< XClipboardNotifier > xClipboardNotifier( xClipboard, UNO_QUERY );
299 Reference< XClipboardListener > xClipboardListener = new ClipboardListener();
300 ClipboardListener * pListener = (ClipboardListener *) xClipboardListener.get();
302 if( xClipboardNotifier.is() )
303 xClipboardNotifier->addClipboardListener( xClipboardListener );
305 // run various tests on clipboard implementation
307 TRACE( "\n*** testing generic clipboard service ***\n" );
309 Reference< XTransferable > xContents = new StringTransferable();
310 Reference< XClipboardOwner > xOwner = new ClipboardOwner();
311 ClipboardOwner *pOwner = (ClipboardOwner *) xOwner.get();
313 TEST( "initial contents (none): ", xClipboard->getContents().is() == sal_False );
315 PERFORM( "update on contents with clipboard owner: ", xClipboard->setContents( xContents, xOwner ) );
316 TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
318 if( xClipboardNotifier.is() )
320 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 0 );
321 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 1 );
322 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
323 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventTransferableValue() == xContents );
326 PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
327 TEST( "if received lostOwnership message(s): ", pOwner->receivedLostOwnerships() > 0 );
328 TEST( "if received exactly 1 lostOwnership message: ", pOwner->receivedLostOwnerships() == 1 );
329 TEST( "if received lostOwnership message for the correct clipboard: ", pOwner->lostOwnershipClipboardValue() == xClipboard );
330 TEST( "if received lostOwnership message for the correct transferable: ", pOwner->lostOwnershipTransferableValue() == xContents );
331 TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
333 if( xClipboardNotifier.is() )
335 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 1 );
336 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 2 );
337 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
338 TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
341 PERFORM( "update on contents without clipboard owner: ", xClipboard->setContents( xContents, Reference< XClipboardOwner >() ) );
342 TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
343 TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
345 if( xClipboardNotifier.is() )
347 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 2 );
348 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 3 );
349 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
350 TEST( "if received changedContents notification for correct transferable: ", pListener->changedContentsEventTransferableValue() == xContents );
353 PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
354 TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
355 TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
357 if( xClipboardNotifier.is() )
359 TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 3 );
360 TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 4 );
361 TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
362 TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
365 // create an instance of ClipboardManager service
367 Reference< XClipboardManager > xClipboardManager( xServiceManager->createInstance(
368 "com.sun.star.datatransfer.clipboard.ClipboardManager" ), UNO_QUERY );
370 ENSURE( xClipboardManager.is(), "*** ERROR *** clipboard manager service could not be created." );
372 // run various tests on clipboard manager implementation
374 TRACE( "\n*** testing clipboard manager service ***\n" );
376 TEST( "initial number of clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
377 PERFORM( "insertion of generic clipboard: ", xClipboardManager->addClipboard( xClipboard ) );
378 TEST( "number of inserted clipboards (1): ", xClipboardManager->listClipboardNames().getLength() == 1 );
379 TEST( "name of inserted clipboard (generic): ", xClipboardManager->listClipboardNames()[0] == "generic" );
380 TEST( "inserted clipboard instance: ", xClipboardManager->getClipboard( OUString("generic") ) == xClipboard );
381 PERFORM( "removal of generic clipboard: ", xClipboardManager->removeClipboard( OUString("generic") ) );
382 TEST( "number of inserted clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
383 TRACE( "Testing inserted clipboard instance (none): " );
386 xClipboardManager->getClipboard( OUString("generic") );
387 TRACE( "FAILED\n" );
389 catch (const NoSuchElementException&)
391 TRACE( "passed\n" );
395 catch (const Exception&)
397 ENSURE( sal_False, "*** ERROR *** exception caught." );
400 // shutdown the service manager
402 // query XComponent interface
403 Reference< XComponent > xComponent( xServiceManager, UNO_QUERY );
405 ENSURE( xComponent.is(), "*** ERROR *** service manager does not support XComponent." );
407 // Dispose and clear factory
408 xComponent->dispose();
409 xServiceManager.clear();
411 fprintf( stderr, "Done.\n" );
412 return 0;
415 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */