xref: /aoo41x/main/dtrans/source/test/test_dtrans.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dtrans.hxx"
30 
31 
32 //------------------------------------------------------------------------
33 // interface includes
34 //------------------------------------------------------------------------
35 #include <com/sun/star/datatransfer/XTransferable.hpp>
36 #include <com/sun/star/datatransfer/clipboard/XClipboardManager.hpp>
37 #include <com/sun/star/datatransfer/clipboard/XClipboardOwner.hpp>
38 #include <com/sun/star/datatransfer/clipboard/XClipboardNotifier.hpp>
39 #include <com/sun/star/datatransfer/clipboard/XClipboardEx.hpp>
40 #include <com/sun/star/lang/XComponent.hpp>
41 
42 //------------------------------------------------------------------------
43 // other includes
44 //------------------------------------------------------------------------
45 
46 
47 #include <cppuhelper/servicefactory.hxx>
48 #include <cppuhelper/implbase1.hxx>
49 #include <cppuhelper/implbase2.hxx>
50 #include <rtl/ustring.hxx>
51 #include <osl/diagnose.h>
52 
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <string.h>
56 
57 //#include <memory>
58 
59 //#include <process.h>
60 
61 //------------------------------------------------------------------------
62 // my defines
63 //------------------------------------------------------------------------
64 
65 #ifdef UNX
66 #define PATH_SEPERATOR '/'
67 #else
68 #define PATH_SEPERATOR '\\'
69 #endif
70 
71 #define ENSURE( a, b ) if( !a ) { fprintf( stderr, b "\n" ); exit( -1 ); }
72 #define TEST( a, b ) fprintf( stderr, "Testing " a ); fprintf( stderr, b ? "passed\n" : "FAILED\n" )
73 #define PERFORM( a, b ) fprintf( stderr, "Performing " a); b; fprintf( stderr, "done\n" )
74 #define TRACE( a ) fprintf( stderr, a )
75 
76 //------------------------------------------------------------------------
77 //	namespaces
78 //------------------------------------------------------------------------
79 
80 using namespace	::rtl;
81 using namespace ::std;
82 using namespace ::cppu;
83 using namespace ::com::sun::star::container;
84 using namespace ::com::sun::star::datatransfer;
85 using namespace ::com::sun::star::datatransfer::clipboard;
86 using namespace ::com::sun::star::uno;
87 using namespace ::com::sun::star::io;
88 using namespace	::com::sun::star::lang;
89 
90 //------------------------------------------------------------------------
91 //	globals
92 //------------------------------------------------------------------------
93 
94 const char * app = NULL;
95 
96 //------------------------------------------------------------------------
97 //	ClipboardOwner
98 //------------------------------------------------------------------------
99 
100 class ClipboardOwner : public WeakImplHelper1< XClipboardOwner >
101 {
102 	Reference< XClipboard >    m_xClipboard;
103 	Reference< XTransferable > m_xTransferable;
104 
105 	sal_uInt32 m_nReceivedLostOwnerships;
106 
107 public:
108 	ClipboardOwner();
109 
110 	//--------------------------------------------------------------------
111 	// XClipboardOwner
112 	//--------------------------------------------------------------------
113 
114 	virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
115 
116 	sal_uInt32 receivedLostOwnerships() { return m_nReceivedLostOwnerships; };
117 	Reference< XClipboard >    lostOwnershipClipboardValue() { return m_xClipboard; }
118 	Reference< XTransferable > lostOwnershipTransferableValue() { return m_xTransferable; };
119 };
120 
121 //------------------------------------------------------------------------
122 //	ctor
123 //------------------------------------------------------------------------
124 
125 ClipboardOwner::ClipboardOwner():
126 	m_nReceivedLostOwnerships( 0 )
127 {
128 }
129 
130 //------------------------------------------------------------------------
131 //	lostOwnership
132 //------------------------------------------------------------------------
133 
134 void SAL_CALL ClipboardOwner::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
135 	throw(RuntimeException)
136 {
137 	m_nReceivedLostOwnerships++;
138 	m_xClipboard = xClipboard;
139 	m_xTransferable = xTrans;
140 }
141 
142 //------------------------------------------------------------------------
143 //	ClipboardListener
144 //------------------------------------------------------------------------
145 
146 class ClipboardListener : public WeakImplHelper1< XClipboardListener >
147 {
148 	Reference< XClipboard >    m_xClipboard;
149 	Reference< XTransferable > m_xTransferable;
150 
151 	sal_uInt32 m_nReceivedChangedContentsEvents;
152 
153 public:
154 	ClipboardListener();
155 
156 	//--------------------------------------------------------------------
157 	// XClipboardOwner
158 	//--------------------------------------------------------------------
159 
160 	virtual void SAL_CALL changedContents( const ClipboardEvent& event ) throw(RuntimeException);
161 
162 	//--------------------------------------------------------------------
163 	// XEventListener
164 	//--------------------------------------------------------------------
165 
166 	virtual void SAL_CALL disposing( const EventObject& event ) throw(RuntimeException);
167 
168 	sal_uInt32 receivedChangedContentsEvents() { return m_nReceivedChangedContentsEvents; };
169 	Reference< XClipboard >    changedContentsEventClipboardValue() { return m_xClipboard; }
170 	Reference< XTransferable > changedContentsEventTransferableValue() { return m_xTransferable; };
171 };
172 
173 //------------------------------------------------------------------------
174 //	ctor
175 //------------------------------------------------------------------------
176 
177 ClipboardListener::ClipboardListener():
178 	m_nReceivedChangedContentsEvents( 0 )
179 {
180 }
181 
182 //------------------------------------------------------------------------
183 //	changedContents
184 //------------------------------------------------------------------------
185 
186 void SAL_CALL ClipboardListener::changedContents( const ClipboardEvent& event )
187 	throw(RuntimeException)
188 {
189 	m_nReceivedChangedContentsEvents++;
190 	m_xClipboard = Reference< XClipboard > (event.Source, UNO_QUERY);
191 	m_xTransferable = event.Contents;
192 }
193 
194 //------------------------------------------------------------------------
195 //	disposing
196 //------------------------------------------------------------------------
197 
198 void SAL_CALL ClipboardListener::disposing( const EventObject& event )
199 	throw(RuntimeException)
200 {
201 }
202 
203 //------------------------------------------------------------------------
204 //	StringTransferable
205 //------------------------------------------------------------------------
206 
207 class StringTransferable : public WeakImplHelper2< XClipboardOwner, XTransferable >
208 {
209 public:
210 	StringTransferable( );
211 
212 	//--------------------------------------------------------------------
213 	// XTransferable
214 	//--------------------------------------------------------------------
215 
216 	virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
217     virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors(  ) throw(RuntimeException);
218 	virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
219 
220 	//--------------------------------------------------------------------
221 	// XClipboardOwner
222 	//--------------------------------------------------------------------
223 
224 	virtual void SAL_CALL lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans ) throw(RuntimeException);
225 
226 	sal_Bool receivedLostOwnership() { return m_receivedLostOwnership; };
227 	void clearReceivedLostOwnership() { m_receivedLostOwnership = sal_False; };
228 
229 private:
230 	Sequence< DataFlavor > m_seqDFlv;
231 	OUString               m_Data;
232 	sal_Bool               m_receivedLostOwnership;
233 };
234 
235 //------------------------------------------------------------------------
236 //	ctor
237 //------------------------------------------------------------------------
238 
239 StringTransferable::StringTransferable( ) :
240 	m_seqDFlv( 1 ),
241 	m_receivedLostOwnership( sal_False ),
242 	m_Data( OUString::createFromAscii("clipboard test content") )
243 {
244 	DataFlavor df;
245 
246 	/*
247 	df.MimeType = L"text/plain; charset=unicode";
248 	df.DataType = getCppuType( ( OUString* )0 );
249 
250 	m_seqDFlv[0] = df;
251 	*/
252 
253 	//df.MimeType = L"text/plain; charset=windows1252";
254 	df.MimeType = OUString::createFromAscii( "text/html" );
255 	df.DataType = getCppuType( ( Sequence< sal_Int8 >* )0 );
256 
257 	m_seqDFlv[0] = df;
258 }
259 
260 //------------------------------------------------------------------------
261 //	getTransferData
262 //------------------------------------------------------------------------
263 
264 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& aFlavor )
265 	throw(UnsupportedFlavorException, IOException, RuntimeException)
266 {
267 	Any anyData;
268 
269 	/*if ( aFlavor == m_seqDFlv[0] )
270 	{
271 		anyData = makeAny( m_Data );
272 	} */
273 #if 0
274 	else if ( aFlavor == m_seqDFlv[0] )
275 	{
276 		OString aStr( m_Data.getStr( ), m_Data.getLength( ), 1252 );
277 		Sequence< sal_Int8 > sOfChars( aStr.getLength( ) );
278 		sal_Int32 lenStr = aStr.getLength( );
279 
280 		for ( sal_Int32 i = 0; i < lenStr; ++i )
281 			sOfChars[i] = aStr[i];
282 
283 		anyData = makeAny( sOfChars );
284 	}
285 #endif
286 
287 	return anyData;
288 }
289 
290 //------------------------------------------------------------------------
291 //	getTransferDataFlavors
292 //------------------------------------------------------------------------
293 
294 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors(  )
295 	throw(RuntimeException)
296 {
297 	return m_seqDFlv;
298 }
299 
300 //------------------------------------------------------------------------
301 //	isDataFlavorSupported
302 //------------------------------------------------------------------------
303 
304 sal_Bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
305 	throw(RuntimeException)
306 {
307 	sal_Int32 nLength = m_seqDFlv.getLength( );
308 	sal_Bool bRet     = sal_False;
309 
310 //	for ( sal_Int32 i = 0; i < nLength; ++i )
311 //	{
312 //		if ( m_seqDFlv[i] == aFlavor )
313 //		{
314 //			bRet = sal_True;
315 //			break;
316 //		}
317 //	}
318 
319 	return bRet;
320 }
321 
322 //------------------------------------------------------------------------
323 //	lostOwnership
324 //------------------------------------------------------------------------
325 
326 void SAL_CALL StringTransferable::lostOwnership( const Reference< XClipboard >& xClipboard, const Reference< XTransferable >& xTrans )
327 	throw(RuntimeException)
328 {
329 	m_receivedLostOwnership = sal_True;
330 }
331 
332 //------------------------------------------------------------------------
333 //	main
334 //------------------------------------------------------------------------
335 
336 int SAL_CALL main( int argc, const char* argv[] )
337 {
338 	OUString aRegistry;
339 
340 	//------------------------------------------------------------------
341 	// check command line parameters
342 	//------------------------------------------------------------------
343 
344 	if ( NULL == ( app = strrchr( argv[0], PATH_SEPERATOR ) ) )
345 		app = argv[0];
346 	else
347 		app++;
348 
349 	for( int n = 1; n < argc; n++ )
350 	{
351 		if( strncmp( argv[n], "-r", 2 ) == 0 )
352 		{
353 			if( strlen( argv[n] ) > 2 )
354 				aRegistry = OUString::createFromAscii( argv[n] + 2 );
355 			else if ( n + 1 < argc )
356 				aRegistry = OUString::createFromAscii( argv[++n] );
357 		}
358 	}
359 
360 	if( aRegistry.getLength() == 0 )
361 		fprintf( stderr, "Usage: %s -r full-path-to-applicat.rdb\n", app );
362 
363 	//------------------------------------------------------------------
364 	// create service manager
365 	//------------------------------------------------------------------
366 	Reference< XMultiServiceFactory > xServiceManager;
367 
368 	try
369 	{
370 		xServiceManager = createRegistryServiceFactory( aRegistry, sal_True );
371 		ENSURE( xServiceManager.is(), "*** ERROR *** service manager could not be created." );
372 
373 		//--------------------------------------------------------------
374 		// create an instance of GenericClipboard service
375 		//--------------------------------------------------------------
376 
377 		Sequence< Any > arguments(1);
378 		arguments[0] = makeAny( OUString::createFromAscii( "generic" ) );
379 
380 		Reference< XClipboard > xClipboard( xServiceManager->createInstanceWithArguments(
381 			OUString::createFromAscii( "com.sun.star.datatransfer.clipboard.GenericClipboard" ),
382 			arguments ), UNO_QUERY );
383 
384 		ENSURE( xClipboard.is(), "*** ERROR *** generic clipboard service could not be created." );
385 
386 		Reference< XClipboardNotifier > xClipboardNotifier( xClipboard, UNO_QUERY );
387 		Reference< XClipboardListener > xClipboardListener = new ClipboardListener();
388 		ClipboardListener * pListener = (ClipboardListener *) xClipboardListener.get();
389 
390 		if( xClipboardNotifier.is() )
391 			xClipboardNotifier->addClipboardListener( xClipboardListener );
392 
393 		//--------------------------------------------------------------
394 		// run various tests on clipboard implementation
395 		//--------------------------------------------------------------
396 
397 		TRACE( "\n*** testing generic clipboard service ***\n" );
398 
399 		Reference< XTransferable > xContents = new StringTransferable();
400 		Reference< XClipboardOwner > xOwner  = new ClipboardOwner();
401 		ClipboardOwner *pOwner = (ClipboardOwner *) xOwner.get();
402 
403 		TEST( "initial contents (none): ", xClipboard->getContents().is() == sal_False );
404 
405 		PERFORM( "update on contents with clipboard owner: ", xClipboard->setContents( xContents, xOwner ) );
406 		TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
407 
408 		if( xClipboardNotifier.is() )
409 		{
410 			TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 0 );
411 			TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 1 );
412 			TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
413 			TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventTransferableValue() == xContents );
414 		}
415 
416 		PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
417 		TEST( "if received lostOwnership message(s): ", pOwner->receivedLostOwnerships() > 0 );
418 		TEST( "if received exactly 1 lostOwnership message: ", pOwner->receivedLostOwnerships() == 1 );
419 		TEST( "if received lostOwnership message for the correct clipboard: ", pOwner->lostOwnershipClipboardValue() == xClipboard );
420 		TEST( "if received lostOwnership message for the correct transferable: ", pOwner->lostOwnershipTransferableValue() == xContents );
421 		TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
422 
423 		if( xClipboardNotifier.is() )
424 		{
425 			TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 1 );
426 			TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 2 );
427 			TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
428 			TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
429 		}
430 
431 		PERFORM( "update on contents without clipboard owner: ", xClipboard->setContents( xContents, Reference< XClipboardOwner >() ) );
432 		TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
433 		TEST( "current clipboard contents: ", xContents == xClipboard->getContents() );
434 
435 		if( xClipboardNotifier.is() )
436 		{
437 			TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 2 );
438 			TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 3 );
439 			TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
440 			TEST( "if received changedContents notification for correct transferable: ", pListener->changedContentsEventTransferableValue() == xContents );
441 		}
442 
443 
444 		PERFORM( "update on contents without data (clear): ", xClipboard->setContents( Reference< XTransferable >(), Reference< XClipboardOwner >() ) );
445 		TEST( "that no further lostOwnership messages were received: ", pOwner->receivedLostOwnerships() == 1 );
446 		TEST( "current clipboard contents (none): ", xClipboard->getContents().is() == sal_False );
447 
448 		if( xClipboardNotifier.is() )
449 		{
450 			TEST( "if received changedContents notifications: ", pListener->receivedChangedContentsEvents() > 3 );
451 			TEST( "if received exactly 1 changedContents notification: ", pListener->receivedChangedContentsEvents() == 4 );
452 			TEST( "if received changedContents notification for correct clipboard: ", pListener->changedContentsEventClipboardValue() == xClipboard );
453 			TEST( "if received changedContents notification for correct transferable: ", ! pListener->changedContentsEventTransferableValue().is() );
454 		}
455 
456 		//--------------------------------------------------------------
457 		// create an instance of ClipboardManager service
458 		//--------------------------------------------------------------
459 
460 		Reference< XClipboardManager > xClipboardManager( xServiceManager->createInstance(
461 			OUString::createFromAscii( "com.sun.star.datatransfer.clipboard.ClipboardManager" ) ), UNO_QUERY );
462 
463 		ENSURE( xClipboardManager.is(), "*** ERROR *** clipboard manager service could not be created." );
464 
465 		//--------------------------------------------------------------
466 		// run various tests on clipboard manager implementation
467 		//--------------------------------------------------------------
468 
469 		TRACE( "\n*** testing clipboard manager service ***\n" );
470 
471 		TEST( "initial number of clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
472 		PERFORM( "insertion of generic clipboard: ", xClipboardManager->addClipboard( xClipboard ) );
473 		TEST( "number of inserted clipboards (1): ", xClipboardManager->listClipboardNames().getLength() == 1 );
474 		TEST( "name of inserted clipboard (generic): ", xClipboardManager->listClipboardNames()[0] == OUString::createFromAscii( "generic" ) );
475 		TEST( "inserted clipboard instance: ", xClipboardManager->getClipboard( OUString::createFromAscii( "generic" ) ) == xClipboard );
476 		PERFORM( "removal of generic clipboard: ", xClipboardManager->removeClipboard( OUString::createFromAscii( "generic" ) ) );
477 		TEST( "number of inserted clipboards (0): ", xClipboardManager->listClipboardNames().getLength() == 0 );
478 		TRACE( "Testing inserted clipboard instance (none): " );
479 		try
480 		{
481 			xClipboardManager->getClipboard( OUString::createFromAscii( "generic" ) );
482 			TRACE( "FAILED\n" );
483 		}
484 		catch( NoSuchElementException e )
485 		{
486 			TRACE( "passed\n" );
487 		}
488 	}
489 
490 	catch ( Exception aException )
491 	{
492 		ENSURE( sal_False, "*** ERROR *** exception caught." );
493 	}
494 
495 	//--------------------------------------------------------------------
496 	// shutdown the service manager
497 	//--------------------------------------------------------------------
498 
499 	// query XComponent interface
500 	Reference< XComponent > xComponent( xServiceManager, UNO_QUERY );
501 
502 	ENSURE( xComponent.is(), "*** ERROR *** service manager does not support XComponent." );
503 
504 	// Dispose and clear factory
505 	xComponent->dispose();
506 	xServiceManager.clear();
507 
508 	fprintf( stderr, "Done.\n" );
509 	return 0;
510 }
511 
512 
513