bump product version to 4.1.6.2
[LibreOffice.git] / cppuhelper / test / testpropshlp.cxx
blobc67c1ecbf43e4a01973f8300365a9e5151b18676
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 #if !defined(OSL_DEBUG_LEVEL) || OSL_DEBUG_LEVEL == 0
21 # undef OSL_DEBUG_LEVEL
22 # define OSL_DEBUG_LEVEL 2
23 #endif
26 #include <osl/mutex.hxx>
27 #include <osl/diagnose.h>
29 #include <cppuhelper/propshlp.hxx>
30 #include <cppuhelper/weak.hxx>
32 #include <cppuhelper/proptypehlp.hxx>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <cppuhelper/implbase3.hxx>
39 using namespace ::cppu;
40 using namespace ::rtl;
41 using namespace ::osl;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::lang;
46 static Property * getPropertyTable1()
48 static Property *pTable = 0;
50 if( ! pTable ) {
51 MutexGuard guard( Mutex::getGlobalMutex() );
52 if( ! pTable ) {
53 static Property aTable[] =
55 Property( OUString("a"), 0, getCppuType( (OUString *)0) ,
56 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //OUString
57 Property( OUString("b"), 1, getCppuCharType( ) ,
58 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //Char
59 Property( OUString("c"), 2, getCppuType( (sal_Int32*)0) ,
60 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //sal_Int32
61 Property( OUString("d"), 5, getCppuType( (double*)0) ,
62 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //double
63 Property( OUString("e"), 7, getCppuBooleanType() ,
64 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //BOOL
65 Property( OUString("f"), 8, getCppuType( (Any*)0) ,
66 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) //Any
68 pTable = aTable;
71 return pTable;
75 static Property * getPropertyTable2()
77 static Property *pTable = 0;
79 if( ! pTable ) {
80 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
81 if( ! pTable ) {
82 static Property aTable[] =
84 Property( OUString("f"), 8, getCppuType( (Any *)0) ,
85 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Any
86 Property( OUString("b"), 1, getCppuCharType( ),
87 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
88 Property( OUString("a"), 0, getCppuType( (OUString*)0),
89 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // OUString
90 Property( OUString("d"), 5, getCppuType( (double*)0) ,
91 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Double
92 Property( OUString("c"), 2, getCppuType( (sal_Int32*)0),
93 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // sal_Int32
94 Property( OUString("e"), 7, getCppuBooleanType() ,
95 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // Bool
97 pTable = aTable;
100 return pTable;
103 static Property * getPropertyTable3()
105 static Property *pTable = 0;
107 if( ! pTable ) {
108 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
109 if( ! pTable ) {
110 static Property aTable[] =
112 Property( OUString("b"), 1, getCppuCharType( ),
113 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
114 Property( OUString("f"), 8, getCppuType( (Any *)0) ,
115 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // any
116 Property( OUString("a"), 0, getCppuType( (OUString*)0),
117 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // OUString
119 pTable = aTable;
122 return pTable;
126 static Property * getPropertyTable4()
128 static Property *pTable = 0;
130 if( ! pTable ) {
131 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
132 if( ! pTable ) {
133 static Property aTable[] =
135 Property( OUString("a"), 0, getCppuType( (OUString*)0),
136 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // OUString
137 Property( OUString("b"), 1, getCppuCharType( ),
138 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
139 Property( OUString("f"), 2, getCppuType( (Any *)0) ,
140 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // Any
142 pTable = aTable;
145 return pTable;
150 /**********************
152 * Note : all Property names must be in the 127 ASCII subset !
154 **********************/
157 void test_PropertyArrayHelper()
159 // Test getProperties() and getCount()
161 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
162 OSL_ENSURE( 6 == a1.getCount(), "not all properties inserted" );
163 Sequence< Property > aProps = a1.getProperties();
164 Property * pP = aProps.getArray();
165 OSL_ENSURE( 6 == aProps.getLength(), "getProperties() gives not all properties" );
166 for( int i = 0; i < 6; i++ )
168 OSL_ENSURE( pP[i].Name == getPropertyTable1()[i].Name , "Name not correct" );
169 OSL_ENSURE( pP[i].Handle == getPropertyTable1()[i].Handle, "Handle not correct" );
170 OSL_ENSURE( pP[i].Attributes == getPropertyTable1()[i].Attributes, "Attributes not correct" );
171 OSL_ENSURE( pP[i].Type == getPropertyTable1()[i].Type, "Type not correct" );
175 // Test sorting
177 OPropertyArrayHelper a1( getPropertyTable2(), 6, sal_False );
178 Sequence< Property > aProps = a1.getProperties();
179 Property * pP = aProps.getArray();
180 OSL_ENSURE( 6 == aProps.getLength(), "getProperties() gives not all properties" );
182 // table to switch to sorted
183 int a[] = { 2 , 1 , 4, 3, 5, 0 };
184 for( int i = 0; i < 6; i++ )
186 OSL_ENSURE( pP[i].Name == getPropertyTable2()[a[i]].Name , "Name not correct" );
187 OSL_ENSURE( pP[i].Handle == getPropertyTable2()[a[i]].Handle, "Handle not correct" );
188 OSL_ENSURE( pP[i].Attributes == getPropertyTable2()[a[i]].Attributes, "Attributes not correct" );
189 OSL_ENSURE( pP[i].Type == getPropertyTable2()[a[i]].Type, "Type not correct" );
193 // Test sorting
195 OPropertyArrayHelper a1( getPropertyTable3(), 3, sal_False );
196 Sequence< Property > aProps = a1.getProperties();
197 Property * pP = aProps.getArray();
198 OSL_ENSURE( 3 == aProps.getLength(), "getProperties() gives not all properties" );
199 // table to switch to sorted
200 int a[] = { 2 , 0 , 1 };
201 for( int i = 0; i < 3; i++ )
203 OSL_ENSURE( pP[i].Name == getPropertyTable3()[a[i]].Name , "Name not correct" );
204 OSL_ENSURE( pP[i].Handle == getPropertyTable3()[a[i]].Handle, "Handle not correct" );
205 OSL_ENSURE( pP[i].Attributes == getPropertyTable3()[a[i]].Attributes, "Attributes not correct" );
206 OSL_ENSURE( pP[i].Type == getPropertyTable3()[a[i]].Type, "Type not correct" );
210 // Test getPropertyByName and hasPropertyByName
212 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
213 for( int i = 0; i < 6; i++ )
215 OSL_ENSURE( a1.hasPropertyByName( getPropertyTable1()[i].Name ), "hasPropertyByName not correct" );
216 Property aP = a1.getPropertyByName( getPropertyTable1()[i].Name );
217 OSL_ENSURE( aP.Name == getPropertyTable1()[i].Name , "Name not correct" );
218 OSL_ENSURE( aP.Handle == getPropertyTable1()[i].Handle, "Handle not correct" );
219 OSL_ENSURE( aP.Attributes == getPropertyTable1()[i].Attributes, "Attributes not correct" );
220 OSL_ENSURE( aP.Type == getPropertyTable1()[i].Type, "Type not correct" );
223 OSL_ENSURE( !a1.hasPropertyByName( OUString("never exist") ), "hasPropertyByName not correct" );
226 a1.getPropertyByName( OUString("never exist") );
227 OSL_FAIL( "exeption not thrown" );
229 catch( UnknownPropertyException & )
234 // Test getHandleByName
236 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
237 for( int i = 0; i < 6; i++ )
239 sal_Int32 Handle = a1.getHandleByName( getPropertyTable1()[i].Name );
240 OSL_ENSURE( Handle == getPropertyTable1()[i].Handle, "Handle not correct" );
242 sal_Int32 Handle = a1.getHandleByName( OUString("asdaf") );
243 OSL_ENSURE( Handle == -1, "Handle not correct" );
246 // Test fillPropertyMembersByHandle
248 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
249 int i;
250 for( i = 0; i < 6; i++ )
252 sal_Int16 nAttributes;
253 OUString aPropName;
254 sal_Bool b = a1.fillPropertyMembersByHandle( &aPropName, &nAttributes, getPropertyTable1()[i].Handle );
255 OSL_ENSURE( b, "fillPropertyMembersByHandle: handle not found" );
256 OSL_ENSURE( nAttributes == getPropertyTable1()[i].Attributes, "fillPropertyMembersByHandle: Attributes not correct" );
257 OSL_ENSURE( aPropName == getPropertyTable1()[i].Name , "fillPropertyMembersByHandle: Name not correct" );
259 OSL_ENSURE( !a1.fillPropertyMembersByHandle( NULL, NULL, 66666 ), "fillPropertyMembersByHandle: handle found" );
260 // optimized table
261 OPropertyArrayHelper a4( getPropertyTable4(), 3 );
262 for( i = 0; i < 3; i++ )
264 sal_Int16 nAttributes;
265 OUString aPropName;
266 sal_Bool b = a1.fillPropertyMembersByHandle( &aPropName, &nAttributes, getPropertyTable4()[i].Handle );
267 OSL_ENSURE( b, "fillPropertyMembersByHandle: handle not found" );
268 OSL_ENSURE( nAttributes == getPropertyTable1()[i].Attributes, "fillPropertyMembersByHandle: Attributes not correct" );
269 OSL_ENSURE( aPropName == getPropertyTable1()[i].Name , "fillPropertyMembersByHandle: Name not correct" );
271 OSL_ENSURE( !a4.fillPropertyMembersByHandle( NULL, NULL, 66666 ), "fillPropertyMembersByHandle: handle found" );
274 // Test fillHandles
276 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
277 Sequence< OUString > aS( 4 );
278 sal_Int32 Handles[4];
279 // muss sortiert sein
280 aS.getArray()[0] = OUString("a");
281 aS.getArray()[1] = OUString("d");
282 aS.getArray()[2] = OUString("f");
283 aS.getArray()[3] = OUString("t");
284 sal_Int32 nHitCount = a1.fillHandles( Handles, aS );
285 OSL_ENSURE( nHitCount == 3, "wrong number of hits " );
286 OSL_ENSURE( Handles[0] == getPropertyTable1()[0].Handle, "Handle not correct" );
287 OSL_ENSURE( Handles[1] == getPropertyTable1()[3].Handle, "Handle not correct" );
288 OSL_ENSURE( Handles[2] == getPropertyTable1()[5].Handle, "Handle not correct" );
289 OSL_ENSURE( Handles[3] == -1, "Handle not correct" );
296 //----------------------------------------------------
297 // test_OPropertySetHelper
298 //----------------------------------------------------
299 struct MutexContainer
301 Mutex aMutex;
303 class test_OPropertySetHelper :
304 public MutexContainer,
305 public OBroadcastHelper ,
306 public OPropertySetHelper,
307 public OWeakObject
309 public:
311 test_OPropertySetHelper( Property * p, sal_Int32 n )
312 : MutexContainer()
313 , OBroadcastHelper( ((MutexContainer *)this)->aMutex )
314 // , OPropertySetHelper( *(static_cast< OBroadcastHelper * >(this)))
315 // MSCI 4 bug ! :
316 // OBroadcastHelper == OBroadcastHelperVar<OMultiTypeInterfaceContainerHelper>
317 , OPropertySetHelper(
318 *(static_cast< OBroadcastHelper * >(this)))
319 , bBOOL( sal_False )
320 , nINT16( 0 )
321 , nINT32( 0 )
322 , pBasicProps( p )
323 , nPropCount( n )
328 ~test_OPropertySetHelper()
332 void dispose()
334 // see comphlp.cxx
335 sal_Bool bDoDispose = sal_False;
337 MutexGuard aGuard( rBHelper.rMutex );
338 if( !rBHelper.bDisposed && !rBHelper.bInDispose )
340 rBHelper.bInDispose = sal_True;
341 bDoDispose = sal_True;
344 if( bDoDispose )
346 disposing();
347 EventObject aEvt;
348 aEvt.Source = Reference < XInterface > ( (static_cast< OWeakObject * >(this)) );
350 rBHelper.aLC.disposeAndClear( aEvt );
351 rBHelper.bDisposed = sal_True;
352 rBHelper.bInDispose = sal_False;
356 // XInterface
357 Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(RuntimeException)
359 Any aRet( OPropertySetHelper::queryInterface( rType ) );
360 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
362 void SAL_CALL acquire() throw()
363 { OWeakObject::acquire(); }
364 void SAL_CALL release() throw()
365 { OWeakObject::release(); }
367 // XPropertySet
368 Reference < XPropertySetInfo > SAL_CALL getPropertySetInfo()throw(RuntimeException);
370 using OPropertySetHelper::getFastPropertyValue;
372 sal_Bool bBOOL;
373 sal_Int16 nINT16;
374 sal_Int32 nINT32;
375 Property * pBasicProps;
376 sal_Int32 nPropCount;
377 protected:
378 IPropertyArrayHelper & SAL_CALL getInfoHelper() throw(RuntimeException);
379 sal_Bool SAL_CALL convertFastPropertyValue(
380 Any & rConvertedValue, Any & rOldValue,
381 sal_Int32 nHandle, const Any& rValue )
382 throw(IllegalArgumentException);
383 void SAL_CALL setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw(RuntimeException);
384 void SAL_CALL getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const throw(RuntimeException);
387 //----------------------------------------------------------------------
388 //------ The Properties of this implementation -------------------------
389 //----------------------------------------------------------------------
390 // Id must be the index into the array
391 #define PROPERTY_BOOL 0
392 #define PROPERTY_INT16 1
393 #define PROPERTY_INT32 2
395 // Max number of properties
396 #define PROPERTY_COUNT 4
397 // Names of Properties
399 * All Properties of this implementation. Must be sorted by name.
401 Property * getBasicProps()
403 static Property *pTable = 0;
405 if( ! pTable ) {
406 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
407 if( ! pTable ) {
409 static Property aBasicProps[PROPERTY_COUNT] =
411 Property( OUString("BOOL") , PROPERTY_BOOL , getCppuBooleanType(), PropertyAttribute::READONLY ),
412 Property( OUString("INT16") , PROPERTY_INT16,
413 getCppuType( (sal_Int16*)0 ), PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED ),
414 Property( OUString("INT32") , PROPERTY_INT32, getCppuType( (sal_Int32*)0 ), PropertyAttribute::BOUND ),
415 Property( OUString("TEST") , 55 , getCppuType( (sal_Int32*)0), PropertyAttribute::BOUND )
417 pTable = aBasicProps;
420 return pTable;
424 //----------------------------------------------------
425 // test_OPropertySetHelper_Listener
426 //----------------------------------------------------
427 class test_OPropertySetHelper_Listener
428 : public WeakImplHelper3< XPropertyChangeListener, XPropertiesChangeListener, XVetoableChangeListener >
430 public:
431 sal_Int32 nDisposing;
432 Mutex aMutex;
433 Any * pExceptedListenerValues;
434 sal_Int32 nCount;
435 sal_Int32 nCurrent;
437 test_OPropertySetHelper_Listener( Any * p = 0, sal_Int32 n = 0 )
438 : nDisposing( 0 )
439 , pExceptedListenerValues( p )
440 , nCount( n )
441 , nCurrent( 0 )
444 ~test_OPropertySetHelper_Listener()
448 sal_Int32 getRefCount() const
449 { return m_refCount; }
451 // XEventListener
452 void SAL_CALL disposing(const EventObject& /*evt*/) throw ( RuntimeException)
454 MutexGuard aGuard( aMutex );
455 nDisposing++;
458 // XPropertyChangeListener
459 void SAL_CALL propertyChange(const PropertyChangeEvent& evt) throw (RuntimeException)
461 if( !pExceptedListenerValues )
462 return;
463 MutexGuard aGuard( aMutex );
464 OSL_ENSURE( nCurrent +1 < nCount, "PropertySetHelper: too many listener calls" );
466 switch( evt.PropertyHandle )
468 case PROPERTY_BOOL:
470 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
471 OSL_ENSURE( evt.PropertyName == OUString("BOOL"), "PropertySetHelper: wrong name" );
473 break;
475 case PROPERTY_INT16:
477 OSL_ENSURE( evt.PropertyName == OUString("INT16"), "PropertySetHelper: wrong name" );
479 OSL_ENSURE( pExceptedListenerValues[nCurrent].getValueType().getTypeClass() == TypeClass_SHORT ,
480 "PropertySetHelper: wrong data type" );
482 sal_Int16 nInt16(0), nOldInt16(0);
483 pExceptedListenerValues[nCurrent] >>= nInt16;
484 evt.OldValue >>= nOldInt16;
485 OSL_ENSURE( nInt16 == nOldInt16, "PropertySetHelper: wrong old value" );
488 pExceptedListenerValues[nCurrent+1] >>= nInt16;
489 evt.NewValue >>= nOldInt16;
490 OSL_ENSURE( nInt16 == nOldInt16 , "PropertySetHelper: wrong new value" );
492 break;
494 case PROPERTY_INT32:
496 OSL_ENSURE( evt.PropertyName == OUString("INT32"), "PropertySetHelper: wrong name" );
498 sal_Int32 nInt32(0),nOldInt32(0);
500 pExceptedListenerValues[nCurrent] >>= nInt32;
501 evt.OldValue >>= nOldInt32;
502 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong old value" );
504 pExceptedListenerValues[nCurrent+1] >>= nInt32;
505 evt.NewValue >>= nOldInt32;
506 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong new value" );
508 break;
510 default:
511 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
513 nCurrent += 2;
516 // XVetoableChangeListener
517 void SAL_CALL vetoableChange(const PropertyChangeEvent& evt) throw (PropertyVetoException, RuntimeException)
519 if( !pExceptedListenerValues )
520 return;
521 MutexGuard aGuard( aMutex );
522 OSL_ENSURE( nCurrent +1 < nCount, "PropertySetHelper: too many listener calls" );
524 switch( evt.PropertyHandle )
526 case PROPERTY_BOOL:
528 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
529 OSL_ENSURE( evt.PropertyName == OUString("BOOL"), "PropertySetHelper: wrong name" );
531 break;
533 case PROPERTY_INT16:
535 OSL_ENSURE( evt.PropertyName == OUString("INT16"), "PropertySetHelper: wrong name" );
537 sal_Int16 nInt16(0), nOldInt16(0);
538 pExceptedListenerValues[nCurrent] >>= nInt16;
539 evt.OldValue >>= nOldInt16;
541 OSL_ENSURE( nInt16 == nOldInt16,"PropertySetHelper: wrong old value" );
543 pExceptedListenerValues[nCurrent+1] >>= nInt16;
544 evt.NewValue >>= nOldInt16;
545 OSL_ENSURE( nInt16 == nOldInt16 , "PropertySetHelper: wrong new value" );
547 if( nOldInt16 == 100 )
549 nCurrent += 2;
550 throw PropertyVetoException();
553 break;
555 case PROPERTY_INT32:
557 OSL_ENSURE( evt.PropertyName == OUString("INT32"), "PropertySetHelper: wrong name" );
559 sal_Int32 nInt32(0),nOldInt32(0);
560 pExceptedListenerValues[nCurrent] >>= nInt32;
561 evt.OldValue >>= nOldInt32;
562 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong old value" );
564 pExceptedListenerValues[nCurrent+1] >>= nInt32;
565 evt.NewValue >>= nOldInt32;
566 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong new value" );
568 break;
570 default:
571 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
573 nCurrent += 2;
576 // XPropertiesChangeListener
577 void SAL_CALL propertiesChange(const Sequence< PropertyChangeEvent >& evtSeq) throw (RuntimeException)
579 if( !pExceptedListenerValues )
580 return;
581 MutexGuard aGuard( aMutex );
582 for( sal_Int32 i = 0; i < evtSeq.getLength(); i++ )
584 const PropertyChangeEvent & evt = evtSeq.getConstArray()[i];
585 OSL_ENSURE( nCurrent +1 < nCount, "PropertySetHelper: too many listener calls" );
587 switch( evt.PropertyHandle )
589 case PROPERTY_BOOL:
591 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
592 OSL_ENSURE( evt.PropertyName == OUString("BOOL"), "PropertySetHelper: wrong name" );
594 break;
596 case PROPERTY_INT16:
598 OSL_ENSURE( evt.PropertyName == OUString("INT16"), "PropertySetHelper: wrong name" );
600 sal_Int16 nInt16(0), nOldInt16(0);
601 pExceptedListenerValues[nCurrent] >>= nInt16;
602 evt.OldValue >>= nOldInt16;
603 OSL_ENSURE( nInt16 == nOldInt16 , "PropertySetHelper: wrong old value" );
606 pExceptedListenerValues[nCurrent+1] >>= nInt16;
607 evt.NewValue >>= nOldInt16;
608 OSL_ENSURE( nInt16 == nOldInt16 , "PropertySetHelper: wrong new value" );
610 break;
612 case PROPERTY_INT32:
614 OSL_ENSURE( evt.PropertyName == OUString("INT32"), "PropertySetHelper: wrong name" );
617 sal_Int32 nInt32(0),nOldInt32(0);
618 pExceptedListenerValues[nCurrent] >>= nInt32;
619 evt.OldValue >>= nOldInt32;
620 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong old value" );
622 pExceptedListenerValues[nCurrent+1] >>= nInt32;
623 evt.NewValue >>= nOldInt32;
624 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong new value" );
626 break;
628 default:
629 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
631 nCurrent += 2;
637 * Create a table that map names to index values.
639 IPropertyArrayHelper & test_OPropertySetHelper::getInfoHelper() throw(RuntimeException)
641 // no multi thread protection
642 static OPropertyArrayHelper aInfo( pBasicProps, nPropCount );
643 return aInfo;
646 // XPropertySet
647 Reference < XPropertySetInfo > test_OPropertySetHelper::getPropertySetInfo()
648 throw(RuntimeException)
650 // no multi thread protection
651 static Reference < XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
652 return xInfo;
655 // Return sal_True, value changed
656 sal_Bool test_OPropertySetHelper::convertFastPropertyValue
658 Any & rConvertedValue,
659 Any & rOldValue,
660 sal_Int32 nHandle,
661 const Any& rValue
662 )throw(IllegalArgumentException)
664 switch( nHandle )
666 case PROPERTY_BOOL:
668 sal_Bool b;
669 convertPropertyValue( b , rValue );
670 if( b != bBOOL )
673 rConvertedValue.setValue( &b , ::getCppuBooleanType() );
674 rOldValue.setValue( & bBOOL , ::getCppuBooleanType() );
675 return sal_True;
677 else
678 return sal_False;
681 case PROPERTY_INT16:
683 sal_Int16 n16;
684 convertPropertyValue( n16 , rValue );
686 if( n16 != nINT16 )
688 rConvertedValue <<= n16;
689 rOldValue <<= nINT16;
690 return sal_True;
692 else
693 return sal_False;
696 case PROPERTY_INT32:
698 sal_Int32 n32;
699 convertPropertyValue( n32 , rValue );
700 if( n32 != nINT32 )
702 rConvertedValue <<= n32;
703 rOldValue <<= nINT32;
704 return sal_True;
706 else
707 return sal_False;
710 default:
711 OSL_ENSURE( nHandle == -1, "invalid property handle" );
712 return sal_False;
717 * only set the value.
719 void test_OPropertySetHelper::setFastPropertyValue_NoBroadcast
721 sal_Int32 nHandle,
722 const Any& rValue
723 )throw(RuntimeException)
725 switch( nHandle )
727 case PROPERTY_BOOL:
728 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "invalid type" );
729 bBOOL = *((sal_Bool*)rValue.getValue());
730 break;
732 case PROPERTY_INT16:
733 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_SHORT, "invalid type" );
734 rValue >>= nINT16;
735 break;
737 case PROPERTY_INT32:
738 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_LONG, "invalid type" );
739 rValue >>= nINT32;
740 break;
742 default:
743 OSL_ENSURE( nHandle == -1, "invalid property handle" );
747 //--------------------------
748 void test_OPropertySetHelper::getFastPropertyValue( Any & rRet, sal_Int32 nHandle ) const
749 throw(RuntimeException)
751 switch( nHandle )
753 case PROPERTY_BOOL:
754 rRet.setValue( &bBOOL , getCppuBooleanType() );
755 break;
757 case PROPERTY_INT16:
758 rRet <<= nINT16;
759 break;
761 case PROPERTY_INT32:
762 rRet <<= nINT32;
763 break;
765 default:
766 OSL_ENSURE( nHandle == -1, "invalid property handle" );
771 void test_PropertySetHelper()
773 test_PropertyArrayHelper();
775 test_OPropertySetHelper * pPS;
777 Reference < XPropertySet > xPS;
778 Reference < XPropertyChangeListener > xPS_L;
779 test_OPropertySetHelper_Listener * pPS_L;
781 Reference < XInterface > x;
783 for( int z = 0; z < 2; z++ )
785 // first test aBasicProps Handles are { 0, 1, 2, 55 }
786 // first test getBasicProps() Handles are { 0, 1, 2 }
787 xPS = pPS = new test_OPropertySetHelper( getBasicProps(), PROPERTY_COUNT - z );
788 xPS_L = static_cast< XPropertyChangeListener * >( pPS_L = new test_OPropertySetHelper_Listener() );
790 // Test queryInterface
791 Reference < XPropertySet > rProp( xPS , UNO_QUERY );
792 OSL_ENSURE( rProp.is() , "PropertySetHelper: XPropertySet nor supported" );
794 Reference < XMultiPropertySet > rMulti( xPS , UNO_QUERY );
795 OSL_ENSURE( rMulti.is() , "PropertySetHelper: XMultiPropertySet nor supported" );
797 Reference < XFastPropertySet > rFast( xPS , UNO_QUERY );
798 OSL_ENSURE( rFast.is() , "PropertySetHelper: XFastPropertySet nor supported" );
800 x = Reference < XInterface > ();
802 // Test add-remove listener
804 Reference < XPropertiesChangeListener > x1( xPS_L, UNO_QUERY );
805 Reference < XVetoableChangeListener > x2( xPS_L, UNO_QUERY );
807 xPS->addPropertyChangeListener( OUString("INT16"), xPS_L );
808 Sequence<OUString> szPN( 3 );
809 szPN.getArray()[0] = OUString("BOOL");
810 szPN.getArray()[1] = OUString("INT32");
811 szPN.getArray()[2] = OUString("Does not exist"); // must ne ignored by the addPropertiesChangeListener method
812 pPS->addPropertiesChangeListener( szPN, x1 );
814 szPN = Sequence<OUString>();
815 pPS->addPropertiesChangeListener( szPN, x1 );
816 pPS->addVetoableChangeListener( OUString("INT16"), x2 );
818 xPS->removePropertyChangeListener( OUString("INT16"), xPS_L );
819 pPS->removePropertiesChangeListener( x1 );
820 pPS->removePropertiesChangeListener( x1 );
821 pPS->removeVetoableChangeListener( OUString("INT16"), x2 );
823 // this exception must thrown
826 xPS->addPropertyChangeListener( OUString("Does not exist"), xPS_L );
827 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
829 catch( UnknownPropertyException & /*e*/ )
836 xPS->addVetoableChangeListener( OUString("Does not exist"), x2 );
837 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
839 catch( UnknownPropertyException & /*e*/ )
845 OSL_ENSURE( pPS_L->getRefCount() == 1, "PropertySetHelper: wrong reference count" );
847 // Test disposing
849 Reference < XPropertiesChangeListener > x1( xPS_L, UNO_QUERY );
850 Reference < XVetoableChangeListener > x2( xPS_L, UNO_QUERY );
852 xPS->addPropertyChangeListener( OUString("INT16"), xPS_L );
853 Sequence<OUString> szPN( 2 );
854 szPN.getArray()[0] = OUString("BOOL");
855 szPN.getArray()[1] = OUString("INT32");
856 pPS->addPropertiesChangeListener( szPN, x1 );
857 szPN = Sequence<OUString>();
858 pPS->addPropertiesChangeListener( szPN, x1 );
859 pPS->addVetoableChangeListener( OUString("INT16"), x2 );
860 pPS->dispose();
862 OSL_ENSURE( pPS_L->nDisposing == 4 , "PropertySetHelper: wrong disposing count" );
863 OSL_ENSURE( pPS_L->getRefCount() == 1 , "PropertySetHelper: wrong reference count" );
864 pPS_L->nDisposing = 0;
865 xPS = pPS = new test_OPropertySetHelper( getBasicProps(), PROPERTY_COUNT - z );
867 // Test set- and get- (Fast) propertyValue
869 // set read only property
872 // Readonly raises a vetoable exception
873 sal_Bool b = sal_True;
874 Any aBool;
875 aBool.setValue( &b , getCppuBooleanType() );
876 xPS->setPropertyValue( OUString("BOOL"), aBool );
877 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
879 catch( PropertyVetoException & /*e*/ )
885 // Readonly raises a vetoable exception
886 sal_Bool b = sal_True;
887 Any aBool;
888 aBool.setValue( &b , getCppuBooleanType() );
889 // BOOL i s0
890 pPS->setFastPropertyValue( PROPERTY_BOOL, aBool );
891 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
893 catch( PropertyVetoException & /*e*/ )
897 // set unknown property
900 sal_Bool b = sal_True;
901 Any aBool;
902 aBool.setValue( &b , getCppuBooleanType() );
903 xPS->setPropertyValue( OUString("Does not exist"), aBool );
904 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
906 catch( UnknownPropertyException & /*e*/ )
912 sal_Bool b = sal_True;
913 Any aBool;
914 aBool.setValue( &b , getCppuBooleanType() );
915 pPS->setFastPropertyValue( 3, aBool );
916 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
918 catch( UnknownPropertyException & /*e*/ )
922 // get unknown property
925 Any aBool;
926 aBool = xPS->getPropertyValue( OUString("Does not exist") );
927 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
929 catch( UnknownPropertyException & /*e*/ )
935 Any aBool;
936 aBool = ((XFastPropertySet *)pPS)->getFastPropertyValue( 3 );
937 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
939 catch( UnknownPropertyException & /*e*/ )
943 // set property with invalid type
946 Any aBool;
947 xPS->setPropertyValue( OUString("INT32"), aBool );
948 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
950 catch( IllegalArgumentException & /*e*/ )
956 Any aBool;
957 pPS->setFastPropertyValue( PROPERTY_INT32, aBool );
958 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
960 catch( IllegalArgumentException & /*e*/ )
964 // narrowing conversion is not allowed!
967 Any aINT32;
968 aINT32 <<= (sal_Int32 ) 16;
969 xPS->setPropertyValue( OUString("INT16"), aINT32 );
970 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
972 catch( IllegalArgumentException & /*e*/ )
979 Any aINT32;
980 aINT32 <<= (sal_Int32) 16;
981 pPS->setFastPropertyValue( PROPERTY_INT16, aINT32 );
982 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
984 catch( IllegalArgumentException & /*e*/ )
989 Any aValue;
990 aValue = xPS->getPropertyValue( OUString("BOOL") );
991 sal_Bool b = *( (sal_Bool*)aValue.getValue());
992 OSL_ENSURE( ! b, "PropertySetHelper: wrong BOOL value" );
993 aValue = ((XFastPropertySet *)pPS)->getFastPropertyValue( PROPERTY_BOOL );
995 b = *((sal_Bool*)aValue.getValue());
996 OSL_ENSURE( !b, "PropertySetHelper: wrong BOOL value" );
998 sal_Int16 n16(0);
999 aValue <<=(sal_Int16)22;
1000 xPS->setPropertyValue( OUString("INT16"), aValue );
1001 aValue = xPS->getPropertyValue( OUString("INT16") );
1002 aValue >>= n16;
1003 OSL_ENSURE( 22 == n16 , "PropertySetHelper: wrong INT16 value" );
1004 aValue <<= (sal_Int16)44;
1005 ((XFastPropertySet *)pPS)->setFastPropertyValue( PROPERTY_INT16, aValue );
1007 aValue = ((XFastPropertySet *)pPS)->getFastPropertyValue( PROPERTY_INT16 );
1008 aValue >>= n16;
1009 OSL_ENSURE( 44 == n16, "PropertySetHelper: wrong INT16 value" );
1011 // widening conversion
1012 aValue <<= (sal_Int16)55;
1013 xPS->setPropertyValue( OUString("INT32"), aValue );
1014 aValue = xPS->getPropertyValue( OUString("INT32") );
1015 sal_Int32 n32(0);
1016 aValue >>= n32;
1017 OSL_ENSURE( 55 == n32 , "PropertySetHelper: wrong INT32 value" );
1018 aValue <<= (sal_Int16)66;
1019 ((XFastPropertySet *)pPS)->setFastPropertyValue( PROPERTY_INT32, aValue );
1020 aValue = ((XFastPropertySet *)pPS)->getFastPropertyValue( PROPERTY_INT32 );
1021 aValue >>= n32;
1022 OSL_ENSURE( 66 == n32, "PropertySetHelper: wrong INT32 value" );
1024 Sequence< OUString >valueNames = Sequence<OUString>( 3 );
1025 valueNames.getArray()[0] = OUString("BOOL");
1026 valueNames.getArray()[1] = OUString("INT16");
1027 valueNames.getArray()[2] = OUString("INT32");
1028 Sequence< Any > aValues = pPS->getPropertyValues( valueNames );
1030 b = *((sal_Bool*)aValues.getConstArray()[0].getValue());
1031 aValues.getConstArray()[1] >>= n16;
1032 aValues.getConstArray()[2] >>= n32;
1034 OSL_ENSURE( !b, "PropertySetHelper: wrong BOOL value" );
1035 OSL_ENSURE( 44 == n16, "PropertySetHelper: wrong INT16 value" );
1036 OSL_ENSURE( 66 == n32, "PropertySetHelper: wrong INT32 value" );
1038 pPS->nINT32 = 0;
1039 pPS->nINT16 = 0;
1041 // Test add-remove listener
1043 Reference < XVetoableChangeListener > x2( xPS_L, UNO_QUERY );
1045 xPS->addPropertyChangeListener( OUString("INT16"), xPS_L );
1046 pPS->addVetoableChangeListener( OUString("INT16"), x2 );
1048 pPS_L->nCount = 10;
1049 Sequence< Any > aSeq( pPS_L->nCount );
1050 pPS_L->nCurrent = 0;
1052 pPS_L->pExceptedListenerValues = aSeq.getArray();
1054 pPS_L->pExceptedListenerValues[0] <<= (sal_Int16) 0; // old value vetoable
1055 pPS_L->pExceptedListenerValues[1] <<= (sal_Int16) 22; // new value vetoable
1056 pPS_L->pExceptedListenerValues[2] <<= (sal_Int16) 0; // old value bound
1057 pPS_L->pExceptedListenerValues[3] <<= (sal_Int16) 22; // new value bound
1058 pPS_L->pExceptedListenerValues[4] <<= (sal_Int16) 22; // old value vetoable
1059 pPS_L->pExceptedListenerValues[5] <<= (sal_Int16) 44; // new value vetoable
1060 pPS_L->pExceptedListenerValues[6] <<= (sal_Int16) 22; // old value bound
1061 pPS_L->pExceptedListenerValues[7] <<= (sal_Int16) 44; // new value bound
1062 pPS_L->pExceptedListenerValues[8] <<= (sal_Int16) 44; // old value vetoable
1063 pPS_L->pExceptedListenerValues[9] <<= (sal_Int16) 100; // new value vetoable exception
1065 Any aValue;
1066 aValue <<= (sal_Int16)22;
1067 xPS->setPropertyValue( OUString("INT16"), aValue );
1068 aValue <<= (sal_Int16) 44;
1069 ((XFastPropertySet *)pPS)->setFastPropertyValue( PROPERTY_INT16, aValue );
1070 aValue <<= (sal_Int16)100;// exception
1074 ((XFastPropertySet *)pPS)->setFastPropertyValue( PROPERTY_INT16, aValue );
1075 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
1077 catch( PropertyVetoException & /*e*/ )
1081 OSL_ENSURE( pPS_L->nCount == pPS_L->nCurrent, "not all listeners called" );
1082 pPS->nINT32 = 0;
1083 pPS->nINT16 = 0;
1084 pPS_L->nCount = 0;
1085 pPS_L->nCurrent = 0;
1086 pPS_L->pExceptedListenerValues = NULL;
1087 xPS->removePropertyChangeListener( OUString("INT16"), xPS_L );
1088 pPS->removeVetoableChangeListener( OUString("INT16"), x2 );
1091 // Test multi property set listener
1093 Reference < XPropertiesChangeListener > x1( xPS_L, UNO_QUERY );
1094 Reference < XVetoableChangeListener > x2( xPS_L, UNO_QUERY );
1096 pPS->addVetoableChangeListener( OUString("INT16") , x2 );
1097 Sequence<OUString> szPN( 4 );
1098 szPN.getArray()[0] = OUString("BOOL");
1099 szPN.getArray()[1] = OUString("INT32");
1100 szPN.getArray()[2] = OUString("Does not exist"); // must ne ignored by the addPropertiesChangeListener method
1101 szPN.getArray()[3] = OUString("INT16");
1102 pPS->addPropertiesChangeListener( szPN, x1 );
1104 pPS_L->nCount = 6;
1105 Sequence< Any > aSeq( pPS_L->nCount );
1106 pPS_L->nCurrent = 0;
1107 pPS_L->pExceptedListenerValues = aSeq.getArray();
1108 pPS_L->pExceptedListenerValues[0] <<= (sal_Int16) 0; // old value vetoable
1109 pPS_L->pExceptedListenerValues[1] <<= (sal_Int16 ) 22; // new value vetoable
1110 // INT32 is not constrained
1111 pPS_L->pExceptedListenerValues[2] <<= (sal_Int16) 0; // old value bound
1112 pPS_L->pExceptedListenerValues[3] <<= (sal_Int16) 22; // new value bound
1113 pPS_L->pExceptedListenerValues[4] <<= (sal_Int32) 0; // old value bound
1114 pPS_L->pExceptedListenerValues[5] <<= (sal_Int32) 44; // new value bound
1116 szPN = Sequence<OUString>( 2 );
1117 szPN.getArray()[0] = OUString("INT16");
1118 szPN.getArray()[1] = OUString("INT32");
1119 Sequence< Any > aValues( 2 );
1120 aValues.getArray()[0] <<= (sal_Int16) 22;
1121 aValues.getArray()[1] <<= (sal_Int16) 44;
1122 pPS->setPropertyValues( szPN, aValues );
1123 OSL_ENSURE( pPS_L->nCount == pPS_L->nCurrent, "not all listeners called" );
1125 //firePropertiesChangeEvent
1126 pPS->nINT16 = 8;
1127 pPS->nINT32 = 5;
1128 pPS_L->nCount = 4;
1129 pPS_L->nCurrent = 0;
1130 pPS_L->pExceptedListenerValues[0] <<= (sal_Int16) 8; // old value
1131 pPS_L->pExceptedListenerValues[1] <<= (sal_Int16) 8; // new value
1132 pPS_L->pExceptedListenerValues[2] <<= (sal_Int32) 5; // old value
1133 pPS_L->pExceptedListenerValues[3] <<= (sal_Int32) 5; // new value
1134 pPS->firePropertiesChangeEvent( szPN, pPS_L );
1135 OSL_ENSURE( pPS_L->nCount == pPS_L->nCurrent, "not all listeners called" );
1138 //vetoable exception with multible
1139 szPN.getArray()[0] = OUString("INT16");
1140 szPN.getArray()[1] = OUString("INT16");
1141 pPS->nINT32 = 0;
1142 pPS->nINT16 = 0;
1143 pPS_L->nCount = 4;
1144 pPS_L->nCurrent = 0;
1145 pPS_L->pExceptedListenerValues[0] <<= (sal_Int16) 0; // old value vetoable
1146 pPS_L->pExceptedListenerValues[1] <<= (sal_Int16) 44; // new value vetoable
1147 pPS_L->pExceptedListenerValues[2] <<= (sal_Int16) 0; // old value vetoable
1148 pPS_L->pExceptedListenerValues[3] <<= (sal_Int16) 100; // new value vetoable
1152 aValues.getArray()[0] <<= (sal_Int16)44;
1153 aValues.getArray()[1] <<= (sal_Int16)100;
1154 pPS->setPropertyValues( szPN, aValues );
1155 OSL_FAIL( "PropertySetHelper: exeption not thrown" );
1157 catch ( PropertyVetoException & /*e*/ )
1161 OSL_ENSURE( pPS_L->nCount == pPS_L->nCurrent, "not all listeners called" );
1162 pPS->removePropertiesChangeListener( x1 );
1163 pPS->removeVetoableChangeListener( OUString("INT16"), x2 );
1168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */