cURL: follow redirects
[LibreOffice.git] / cppuhelper / test / testpropshlp.cxx
blobbde76c7dca67d5a45bfaef8b614c1eebbc1f6f1e
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 ::osl;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::lang;
45 static Property * getPropertyTable1()
47 static Property *pTable = 0;
49 if( ! pTable ) {
50 MutexGuard guard( Mutex::getGlobalMutex() );
51 if( ! pTable ) {
52 static Property aTable[] =
54 Property( OUString("a"), 0, cppu::UnoType<OUString>::get() ,
55 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //OUString
56 Property( OUString("b"), 1, cppu::UnoType<cppu::UnoCharType>::get() ,
57 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //Char
58 Property( OUString("c"), 2, cppu::UnoType<sal_Int32>::get(),
59 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //sal_Int32
60 Property( OUString("d"), 5, cppu::UnoType<double>::get() ,
61 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //double
62 Property( OUString("e"), 7, cppu::UnoType<bool>::get(),
63 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //BOOL
64 Property( OUString("f"), 8, cppu::UnoType<Any>::get() ,
65 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) //Any
67 pTable = aTable;
70 return pTable;
74 static Property * getPropertyTable2()
76 static Property *pTable = 0;
78 if( ! pTable ) {
79 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
80 if( ! pTable ) {
81 static Property aTable[] =
83 Property( OUString("f"), 8, cppu::UnoType<Any>::get() ,
84 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Any
85 Property( OUString("b"), 1, cppu::UnoType<cppu::UnoCharType>::get(),
86 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
87 Property( OUString("a"), 0, cppu::UnoType<OUString>::get(),
88 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // OUString
89 Property( OUString("d"), 5, cppu::UnoType<double>::get() ,
90 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Double
91 Property( OUString("c"), 2, cppu::UnoType<sal_Int32>::get(),
92 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // sal_Int32
93 Property( OUString("e"), 7, cppu::UnoType<bool>::get(),
94 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // Bool
96 pTable = aTable;
99 return pTable;
102 static Property * getPropertyTable3()
104 static Property *pTable = 0;
106 if( ! pTable ) {
107 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
108 if( ! pTable ) {
109 static Property aTable[] =
111 Property( OUString("b"), 1, cppu::UnoType<cppu::UnoCharType>::get(),
112 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
113 Property( OUString("f"), 8, cppu::UnoType<Any>::get() ,
114 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // any
115 Property( OUString("a"), 0, cppu::UnoType<OUString>::get(),
116 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // OUString
118 pTable = aTable;
121 return pTable;
125 static Property * getPropertyTable4()
127 static Property *pTable = 0;
129 if( ! pTable ) {
130 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
131 if( ! pTable ) {
132 static Property aTable[] =
134 Property( OUString("a"), 0, cppu::UnoType<OUString>::get(),
135 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // OUString
136 Property( OUString("b"), 1, cppu::UnoType<cppu::UnoCharType>::get(),
137 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
138 Property( OUString("f"), 2, cppu::UnoType<Any>::get() ,
139 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // Any
141 pTable = aTable;
144 return pTable;
148 /**********************
150 * Note : all Property names must be in the 127 ASCII subset !
152 **********************/
155 void test_PropertyArrayHelper()
157 // Test getProperties() and getCount()
159 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
160 OSL_ENSURE( 6 == a1.getCount(), "not all properties inserted" );
161 Sequence< Property > aProps = a1.getProperties();
162 Property * pP = aProps.getArray();
163 OSL_ENSURE( 6 == aProps.getLength(), "getProperties() gives not all properties" );
164 for( int i = 0; i < 6; i++ )
166 OSL_ENSURE( pP[i].Name == getPropertyTable1()[i].Name , "Name not correct" );
167 OSL_ENSURE( pP[i].Handle == getPropertyTable1()[i].Handle, "Handle not correct" );
168 OSL_ENSURE( pP[i].Attributes == getPropertyTable1()[i].Attributes, "Attributes not correct" );
169 OSL_ENSURE( pP[i].Type == getPropertyTable1()[i].Type, "Type not correct" );
173 // Test sorting
175 OPropertyArrayHelper a1( getPropertyTable2(), 6, sal_False );
176 Sequence< Property > aProps = a1.getProperties();
177 Property * pP = aProps.getArray();
178 OSL_ENSURE( 6 == aProps.getLength(), "getProperties() gives not all properties" );
180 // table to switch to sorted
181 int a[] = { 2 , 1 , 4, 3, 5, 0 };
182 for( int i = 0; i < 6; i++ )
184 OSL_ENSURE( pP[i].Name == getPropertyTable2()[a[i]].Name , "Name not correct" );
185 OSL_ENSURE( pP[i].Handle == getPropertyTable2()[a[i]].Handle, "Handle not correct" );
186 OSL_ENSURE( pP[i].Attributes == getPropertyTable2()[a[i]].Attributes, "Attributes not correct" );
187 OSL_ENSURE( pP[i].Type == getPropertyTable2()[a[i]].Type, "Type not correct" );
191 // Test sorting
193 OPropertyArrayHelper a1( getPropertyTable3(), 3, sal_False );
194 Sequence< Property > aProps = a1.getProperties();
195 Property * pP = aProps.getArray();
196 OSL_ENSURE( 3 == aProps.getLength(), "getProperties() gives not all properties" );
197 // table to switch to sorted
198 int a[] = { 2 , 0 , 1 };
199 for( int i = 0; i < 3; i++ )
201 OSL_ENSURE( pP[i].Name == getPropertyTable3()[a[i]].Name , "Name not correct" );
202 OSL_ENSURE( pP[i].Handle == getPropertyTable3()[a[i]].Handle, "Handle not correct" );
203 OSL_ENSURE( pP[i].Attributes == getPropertyTable3()[a[i]].Attributes, "Attributes not correct" );
204 OSL_ENSURE( pP[i].Type == getPropertyTable3()[a[i]].Type, "Type not correct" );
208 // Test getPropertyByName and hasPropertyByName
210 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
211 for( int i = 0; i < 6; i++ )
213 OSL_ENSURE( a1.hasPropertyByName( getPropertyTable1()[i].Name ), "hasPropertyByName not correct" );
214 Property aP = a1.getPropertyByName( getPropertyTable1()[i].Name );
215 OSL_ENSURE( aP.Name == getPropertyTable1()[i].Name , "Name not correct" );
216 OSL_ENSURE( aP.Handle == getPropertyTable1()[i].Handle, "Handle not correct" );
217 OSL_ENSURE( aP.Attributes == getPropertyTable1()[i].Attributes, "Attributes not correct" );
218 OSL_ENSURE( aP.Type == getPropertyTable1()[i].Type, "Type not correct" );
221 OSL_ENSURE( !a1.hasPropertyByName("never exist"), "hasPropertyByName not correct" );
224 a1.getPropertyByName( OUString("never exist") );
225 OSL_FAIL( "exception not thrown" );
227 catch( UnknownPropertyException & )
232 // Test getHandleByName
234 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
235 for( int i = 0; i < 6; i++ )
237 sal_Int32 Handle = a1.getHandleByName( getPropertyTable1()[i].Name );
238 OSL_ENSURE( Handle == getPropertyTable1()[i].Handle, "Handle not correct" );
240 sal_Int32 Handle = a1.getHandleByName( OUString("asdaf") );
241 OSL_ENSURE( Handle == -1, "Handle not correct" );
244 // Test fillPropertyMembersByHandle
246 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
247 int i;
248 for( i = 0; i < 6; i++ )
250 sal_Int16 nAttributes;
251 OUString aPropName;
252 sal_Bool b = a1.fillPropertyMembersByHandle( &aPropName, &nAttributes, getPropertyTable1()[i].Handle );
253 OSL_ENSURE( b, "fillPropertyMembersByHandle: handle not found" );
254 OSL_ENSURE( nAttributes == getPropertyTable1()[i].Attributes, "fillPropertyMembersByHandle: Attributes not correct" );
255 OSL_ENSURE( aPropName == getPropertyTable1()[i].Name , "fillPropertyMembersByHandle: Name not correct" );
257 OSL_ENSURE( !a1.fillPropertyMembersByHandle( NULL, NULL, 66666 ), "fillPropertyMembersByHandle: handle found" );
258 // optimized table
259 OPropertyArrayHelper a4( getPropertyTable4(), 3 );
260 for( i = 0; i < 3; i++ )
262 sal_Int16 nAttributes;
263 OUString aPropName;
264 sal_Bool b = a1.fillPropertyMembersByHandle( &aPropName, &nAttributes, getPropertyTable4()[i].Handle );
265 OSL_ENSURE( b, "fillPropertyMembersByHandle: handle not found" );
266 OSL_ENSURE( nAttributes == getPropertyTable1()[i].Attributes, "fillPropertyMembersByHandle: Attributes not correct" );
267 OSL_ENSURE( aPropName == getPropertyTable1()[i].Name , "fillPropertyMembersByHandle: Name not correct" );
269 OSL_ENSURE( !a4.fillPropertyMembersByHandle( NULL, NULL, 66666 ), "fillPropertyMembersByHandle: handle found" );
272 // Test fillHandles
274 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
275 Sequence< OUString > aS( 4 );
276 sal_Int32 Handles[4];
277 // muss sortiert sein
278 aS[0] = "a";
279 aS[1] = "d";
280 aS[2] = "f";
281 aS[3] = "t";
282 sal_Int32 nHitCount = a1.fillHandles( Handles, aS );
283 OSL_ENSURE( nHitCount == 3, "wrong number of hits " );
284 OSL_ENSURE( Handles[0] == getPropertyTable1()[0].Handle, "Handle not correct" );
285 OSL_ENSURE( Handles[1] == getPropertyTable1()[3].Handle, "Handle not correct" );
286 OSL_ENSURE( Handles[2] == getPropertyTable1()[5].Handle, "Handle not correct" );
287 OSL_ENSURE( Handles[3] == -1, "Handle not correct" );
292 // test_OPropertySetHelper
294 struct MutexContainer
296 Mutex aMutex;
298 class test_OPropertySetHelper :
299 public MutexContainer,
300 public OBroadcastHelper ,
301 public OPropertySetHelper,
302 public OWeakObject
304 public:
306 test_OPropertySetHelper( Property * p, sal_Int32 n )
307 : MutexContainer()
308 , OBroadcastHelper( ((MutexContainer *)this)->aMutex )
309 // , OPropertySetHelper( *(static_cast< OBroadcastHelper * >(this)))
310 // MSCI 4 bug ! :
311 // OBroadcastHelper == OBroadcastHelperVar<OMultiTypeInterfaceContainerHelper>
312 , OPropertySetHelper(
313 *(static_cast< OBroadcastHelper * >(this)))
314 , bBOOL( sal_False )
315 , nINT16( 0 )
316 , nINT32( 0 )
317 , pBasicProps( p )
318 , nPropCount( n )
323 ~test_OPropertySetHelper()
327 void dispose()
329 // see comphlp.cxx
330 sal_Bool bDoDispose = sal_False;
332 MutexGuard aGuard( rBHelper.rMutex );
333 if( !rBHelper.bDisposed && !rBHelper.bInDispose )
335 rBHelper.bInDispose = sal_True;
336 bDoDispose = sal_True;
339 if( bDoDispose )
341 disposing();
342 EventObject aEvt;
343 aEvt.Source.set( (static_cast< OWeakObject * >(this)) );
345 rBHelper.aLC.disposeAndClear( aEvt );
346 rBHelper.bDisposed = sal_True;
347 rBHelper.bInDispose = sal_False;
351 // XInterface
352 Any SAL_CALL queryInterface( const css::uno::Type & rType ) throw(RuntimeException)
354 Any aRet( OPropertySetHelper::queryInterface( rType ) );
355 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
357 void SAL_CALL acquire() throw()
358 { OWeakObject::acquire(); }
359 void SAL_CALL release() throw()
360 { OWeakObject::release(); }
362 // XPropertySet
363 Reference < XPropertySetInfo > SAL_CALL getPropertySetInfo()throw(RuntimeException);
365 using OPropertySetHelper::getFastPropertyValue;
367 sal_Bool bBOOL;
368 sal_Int16 nINT16;
369 sal_Int32 nINT32;
370 Property * pBasicProps;
371 sal_Int32 nPropCount;
372 protected:
373 IPropertyArrayHelper & SAL_CALL getInfoHelper() throw(RuntimeException);
374 sal_Bool SAL_CALL convertFastPropertyValue(
375 Any & rConvertedValue, Any & rOldValue,
376 sal_Int32 nHandle, const Any& rValue )
377 throw(IllegalArgumentException);
378 void SAL_CALL setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw(RuntimeException);
379 void SAL_CALL getFastPropertyValue( Any& rValue, sal_Int32 nHandle ) const throw(RuntimeException);
383 //------ The Properties of this implementation -------------------------
385 // Id must be the index into the array
386 #define PROPERTY_BOOL 0
387 #define PROPERTY_INT16 1
388 #define PROPERTY_INT32 2
390 // Max number of properties
391 #define PROPERTY_COUNT 4
392 // Names of Properties
394 * All Properties of this implementation. Must be sorted by name.
396 Property * getBasicProps()
398 static Property *pTable = 0;
400 if( ! pTable ) {
401 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
402 if( ! pTable ) {
404 static Property aBasicProps[PROPERTY_COUNT] =
406 Property( OUString("BOOL") , PROPERTY_BOOL , cppu::UnoType<bool>::get(), PropertyAttribute::READONLY ),
407 Property( OUString("INT16") , PROPERTY_INT16,
408 cppu::UnoType<sal_Int16>::get(), PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED ),
409 Property( OUString("INT32") , PROPERTY_INT32, cppu::UnoType<sal_Int32>::get(), PropertyAttribute::BOUND ),
410 Property( OUString("TEST") , 55 , cppu::UnoType<sal_Int32>::get(), PropertyAttribute::BOUND )
412 pTable = aBasicProps;
415 return pTable;
419 // test_OPropertySetHelper_Listener
421 class test_OPropertySetHelper_Listener
422 : public WeakImplHelper3< XPropertyChangeListener, XPropertiesChangeListener, XVetoableChangeListener >
424 public:
425 sal_Int32 nDisposing;
426 Mutex aMutex;
427 Any * pExceptedListenerValues;
428 sal_Int32 nCount;
429 sal_Int32 nCurrent;
431 test_OPropertySetHelper_Listener( Any * p = 0, sal_Int32 n = 0 )
432 : nDisposing( 0 )
433 , pExceptedListenerValues( p )
434 , nCount( n )
435 , nCurrent( 0 )
438 ~test_OPropertySetHelper_Listener()
442 sal_Int32 getRefCount() const
443 { return m_refCount; }
445 // XEventListener
446 void SAL_CALL disposing(const EventObject& /*evt*/) throw ( RuntimeException)
448 MutexGuard aGuard( aMutex );
449 nDisposing++;
452 // XPropertyChangeListener
453 void SAL_CALL propertyChange(const PropertyChangeEvent& evt) throw (RuntimeException)
455 if( !pExceptedListenerValues )
456 return;
457 MutexGuard aGuard( aMutex );
458 OSL_ENSURE( nCurrent +1 < nCount, "PropertySetHelper: too many listener calls" );
460 switch( evt.PropertyHandle )
462 case PROPERTY_BOOL:
464 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
465 OSL_ENSURE( evt.PropertyName == "BOOL", "PropertySetHelper: wrong name" );
467 break;
469 case PROPERTY_INT16:
471 OSL_ENSURE( evt.PropertyName == "INT16", "PropertySetHelper: wrong name" );
473 OSL_ENSURE( pExceptedListenerValues[nCurrent].getValueType().getTypeClass() == TypeClass_SHORT ,
474 "PropertySetHelper: wrong data type" );
476 sal_Int16 nInt16(0), nOldInt16(0);
477 pExceptedListenerValues[nCurrent] >>= nInt16;
478 evt.OldValue >>= nOldInt16;
479 OSL_ENSURE( nInt16 == nOldInt16, "PropertySetHelper: wrong old value" );
482 pExceptedListenerValues[nCurrent+1] >>= nInt16;
483 evt.NewValue >>= nOldInt16;
484 OSL_ENSURE( nInt16 == nOldInt16 , "PropertySetHelper: wrong new value" );
486 break;
488 case PROPERTY_INT32:
490 OSL_ENSURE( evt.PropertyName == "INT32", "PropertySetHelper: wrong name" );
492 sal_Int32 nInt32(0),nOldInt32(0);
494 pExceptedListenerValues[nCurrent] >>= nInt32;
495 evt.OldValue >>= nOldInt32;
496 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong old value" );
498 pExceptedListenerValues[nCurrent+1] >>= nInt32;
499 evt.NewValue >>= nOldInt32;
500 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong new value" );
502 break;
504 default:
505 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
507 nCurrent += 2;
510 // XVetoableChangeListener
511 void SAL_CALL vetoableChange(const PropertyChangeEvent& evt) throw (PropertyVetoException, RuntimeException)
513 if( !pExceptedListenerValues )
514 return;
515 MutexGuard aGuard( aMutex );
516 OSL_ENSURE( nCurrent +1 < nCount, "PropertySetHelper: too many listener calls" );
518 switch( evt.PropertyHandle )
520 case PROPERTY_BOOL:
522 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
523 OSL_ENSURE( evt.PropertyName == "BOOL", "PropertySetHelper: wrong name" );
525 break;
527 case PROPERTY_INT16:
529 OSL_ENSURE( evt.PropertyName == "INT16", "PropertySetHelper: wrong name" );
531 sal_Int16 nInt16(0), nOldInt16(0);
532 pExceptedListenerValues[nCurrent] >>= nInt16;
533 evt.OldValue >>= nOldInt16;
535 OSL_ENSURE( nInt16 == nOldInt16,"PropertySetHelper: wrong old value" );
537 pExceptedListenerValues[nCurrent+1] >>= nInt16;
538 evt.NewValue >>= nOldInt16;
539 OSL_ENSURE( nInt16 == nOldInt16 , "PropertySetHelper: wrong new value" );
541 if( nOldInt16 == 100 )
543 nCurrent += 2;
544 throw PropertyVetoException();
547 break;
549 case PROPERTY_INT32:
551 OSL_ENSURE( evt.PropertyName == "INT32", "PropertySetHelper: wrong name" );
553 sal_Int32 nInt32(0),nOldInt32(0);
554 pExceptedListenerValues[nCurrent] >>= nInt32;
555 evt.OldValue >>= nOldInt32;
556 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong old value" );
558 pExceptedListenerValues[nCurrent+1] >>= nInt32;
559 evt.NewValue >>= nOldInt32;
560 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong new value" );
562 break;
564 default:
565 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
567 nCurrent += 2;
570 // XPropertiesChangeListener
571 void SAL_CALL propertiesChange(const Sequence< PropertyChangeEvent >& evtSeq) throw (RuntimeException)
573 if( !pExceptedListenerValues )
574 return;
575 MutexGuard aGuard( aMutex );
576 for( sal_Int32 i = 0; i < evtSeq.getLength(); i++ )
578 const PropertyChangeEvent & evt = evtSeq.getConstArray()[i];
579 OSL_ENSURE( nCurrent +1 < nCount, "PropertySetHelper: too many listener calls" );
581 switch( evt.PropertyHandle )
583 case PROPERTY_BOOL:
585 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
586 OSL_ENSURE( evt.PropertyName == "BOOL", "PropertySetHelper: wrong name" );
588 break;
590 case PROPERTY_INT16:
592 OSL_ENSURE( evt.PropertyName == "INT16", "PropertySetHelper: wrong name" );
594 sal_Int16 nInt16(0), nOldInt16(0);
595 pExceptedListenerValues[nCurrent] >>= nInt16;
596 evt.OldValue >>= nOldInt16;
597 OSL_ENSURE( nInt16 == nOldInt16 , "PropertySetHelper: wrong old value" );
600 pExceptedListenerValues[nCurrent+1] >>= nInt16;
601 evt.NewValue >>= nOldInt16;
602 OSL_ENSURE( nInt16 == nOldInt16 , "PropertySetHelper: wrong new value" );
604 break;
606 case PROPERTY_INT32:
608 OSL_ENSURE( evt.PropertyName == "INT32", "PropertySetHelper: wrong name" );
611 sal_Int32 nInt32(0),nOldInt32(0);
612 pExceptedListenerValues[nCurrent] >>= nInt32;
613 evt.OldValue >>= nOldInt32;
614 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong old value" );
616 pExceptedListenerValues[nCurrent+1] >>= nInt32;
617 evt.NewValue >>= nOldInt32;
618 OSL_ENSURE( nInt32 == nOldInt32 , "PropertySetHelper: wrong new value" );
620 break;
622 default:
623 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
625 nCurrent += 2;
631 * Create a table that map names to index values.
633 IPropertyArrayHelper & test_OPropertySetHelper::getInfoHelper() throw(RuntimeException)
635 // no multi thread protection
636 static OPropertyArrayHelper aInfo( pBasicProps, nPropCount );
637 return aInfo;
640 // XPropertySet
641 Reference < XPropertySetInfo > test_OPropertySetHelper::getPropertySetInfo()
642 throw(RuntimeException)
644 // no multi thread protection
645 static Reference < XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
646 return xInfo;
649 // Return sal_True, value changed
650 sal_Bool test_OPropertySetHelper::convertFastPropertyValue
652 Any & rConvertedValue,
653 Any & rOldValue,
654 sal_Int32 nHandle,
655 const Any& rValue
656 )throw(IllegalArgumentException)
658 switch( nHandle )
660 case PROPERTY_BOOL:
662 sal_Bool b;
663 convertPropertyValue( b , rValue );
664 if( b != bBOOL )
667 rConvertedValue.setValue( &b , cppu::UnoType<bool>::get() );
668 rOldValue.setValue( & bBOOL , cppu::UnoType<bool>::get() );
669 return sal_True;
671 else
672 return sal_False;
675 case PROPERTY_INT16:
677 sal_Int16 n16;
678 convertPropertyValue( n16 , rValue );
680 if( n16 != nINT16 )
682 rConvertedValue <<= n16;
683 rOldValue <<= nINT16;
684 return sal_True;
686 else
687 return sal_False;
690 case PROPERTY_INT32:
692 sal_Int32 n32;
693 convertPropertyValue( n32 , rValue );
694 if( n32 != nINT32 )
696 rConvertedValue <<= n32;
697 rOldValue <<= nINT32;
698 return sal_True;
700 else
701 return sal_False;
704 default:
705 OSL_ENSURE( nHandle == -1, "invalid property handle" );
706 return sal_False;
711 * only set the value.
713 void test_OPropertySetHelper::setFastPropertyValue_NoBroadcast
715 sal_Int32 nHandle,
716 const Any& rValue
717 )throw(RuntimeException)
719 switch( nHandle )
721 case PROPERTY_BOOL:
722 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "invalid type" );
723 bBOOL = *((sal_Bool*)rValue.getValue());
724 break;
726 case PROPERTY_INT16:
727 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_SHORT, "invalid type" );
728 rValue >>= nINT16;
729 break;
731 case PROPERTY_INT32:
732 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_LONG, "invalid type" );
733 rValue >>= nINT32;
734 break;
736 default:
737 OSL_ENSURE( nHandle == -1, "invalid property handle" );
742 void test_OPropertySetHelper::getFastPropertyValue( Any & rRet, sal_Int32 nHandle ) const
743 throw(RuntimeException)
745 switch( nHandle )
747 case PROPERTY_BOOL:
748 rRet.setValue( &bBOOL , cppu::UnoType<bool>::get() );
749 break;
751 case PROPERTY_INT16:
752 rRet <<= nINT16;
753 break;
755 case PROPERTY_INT32:
756 rRet <<= nINT32;
757 break;
759 default:
760 OSL_ENSURE( nHandle == -1, "invalid property handle" );
765 void test_PropertySetHelper()
767 test_PropertyArrayHelper();
769 test_OPropertySetHelper * pPS;
771 Reference < XPropertySet > xPS;
772 Reference < XPropertyChangeListener > xPS_L;
773 test_OPropertySetHelper_Listener * pPS_L;
775 Reference < XInterface > x;
777 for( int z = 0; z < 2; z++ )
779 // first test aBasicProps Handles are { 0, 1, 2, 55 }
780 // first test getBasicProps() Handles are { 0, 1, 2 }
781 xPS = pPS = new test_OPropertySetHelper( getBasicProps(), PROPERTY_COUNT - z );
782 xPS_L = static_cast< XPropertyChangeListener * >( pPS_L = new test_OPropertySetHelper_Listener() );
784 // Test queryInterface
785 Reference < XPropertySet > rProp( xPS , UNO_QUERY );
786 OSL_ENSURE( rProp.is() , "PropertySetHelper: XPropertySet nor supported" );
788 Reference < XMultiPropertySet > rMulti( xPS , UNO_QUERY );
789 OSL_ENSURE( rMulti.is() , "PropertySetHelper: XMultiPropertySet nor supported" );
791 Reference < XFastPropertySet > rFast( xPS , UNO_QUERY );
792 OSL_ENSURE( rFast.is() , "PropertySetHelper: XFastPropertySet nor supported" );
794 x.clear();
796 // Test add-remove listener
798 Reference < XPropertiesChangeListener > x1( xPS_L, UNO_QUERY );
799 Reference < XVetoableChangeListener > x2( xPS_L, UNO_QUERY );
801 xPS->addPropertyChangeListener( OUString("INT16"), xPS_L );
802 Sequence<OUString> szPN( 3 );
803 szPN[0] = "BOOL";
804 szPN[1] = "INT32";
805 szPN[2] = "Does not exist"; // must ne ignored by the addPropertiesChangeListener method
806 pPS->addPropertiesChangeListener( szPN, x1 );
808 szPN = Sequence<OUString>();
809 pPS->addPropertiesChangeListener( szPN, x1 );
810 pPS->addVetoableChangeListener( OUString("INT16"), x2 );
812 xPS->removePropertyChangeListener( OUString("INT16"), xPS_L );
813 pPS->removePropertiesChangeListener( x1 );
814 pPS->removePropertiesChangeListener( x1 );
815 pPS->removeVetoableChangeListener( OUString("INT16"), x2 );
817 // this exception must thrown
820 xPS->addPropertyChangeListener( OUString("Does not exist"), xPS_L );
821 OSL_FAIL( "PropertySetHelper: exception not thrown" );
823 catch( UnknownPropertyException & /*e*/ )
830 xPS->addVetoableChangeListener( OUString("Does not exist"), x2 );
831 OSL_FAIL( "PropertySetHelper: exception not thrown" );
833 catch( UnknownPropertyException & /*e*/ )
839 OSL_ENSURE( pPS_L->getRefCount() == 1, "PropertySetHelper: wrong reference count" );
841 // Test disposing
843 Reference < XPropertiesChangeListener > x1( xPS_L, UNO_QUERY );
844 Reference < XVetoableChangeListener > x2( xPS_L, UNO_QUERY );
846 xPS->addPropertyChangeListener( OUString("INT16"), xPS_L );
847 Sequence<OUString> szPN( 2 );
848 szPN[0] = "BOOL";
849 szPN[1] = "INT32";
850 pPS->addPropertiesChangeListener( szPN, x1 );
851 szPN = Sequence<OUString>();
852 pPS->addPropertiesChangeListener( szPN, x1 );
853 pPS->addVetoableChangeListener( OUString("INT16"), x2 );
854 pPS->dispose();
856 OSL_ENSURE( pPS_L->nDisposing == 4 , "PropertySetHelper: wrong disposing count" );
857 OSL_ENSURE( pPS_L->getRefCount() == 1 , "PropertySetHelper: wrong reference count" );
858 pPS_L->nDisposing = 0;
859 xPS = pPS = new test_OPropertySetHelper( getBasicProps(), PROPERTY_COUNT - z );
861 // Test set- and get- (Fast) propertyValue
863 // set read only property
866 // Readonly raises a vetoable exception
867 sal_Bool b = sal_True;
868 Any aBool;
869 aBool.setValue( &b , cppu::UnoType<bool>::get() );
870 xPS->setPropertyValue("BOOL", aBool );
871 OSL_FAIL( "PropertySetHelper: exception not thrown" );
873 catch( PropertyVetoException & /*e*/ )
879 // Readonly raises a vetoable exception
880 sal_Bool b = sal_True;
881 Any aBool;
882 aBool.setValue( &b , cppu::UnoType<bool>::get() );
883 // BOOL i s0
884 pPS->setFastPropertyValue( PROPERTY_BOOL, aBool );
885 OSL_FAIL( "PropertySetHelper: exception not thrown" );
887 catch( PropertyVetoException & /*e*/ )
891 // set unknown property
894 sal_Bool b = sal_True;
895 Any aBool;
896 aBool.setValue( &b , cppu::UnoType<bool>::get() );
897 xPS->setPropertyValue("Does not exist", aBool );
898 OSL_FAIL( "PropertySetHelper: exception not thrown" );
900 catch( UnknownPropertyException & /*e*/ )
906 sal_Bool b = sal_True;
907 Any aBool;
908 aBool.setValue( &b , cppu::UnoType<bool>::get() );
909 pPS->setFastPropertyValue( 3, aBool );
910 OSL_FAIL( "PropertySetHelper: exception not thrown" );
912 catch( UnknownPropertyException & /*e*/ )
916 // get unknown property
919 Any aBool;
920 aBool = xPS->getPropertyValue("Does not exist");
921 OSL_FAIL( "PropertySetHelper: exception not thrown" );
923 catch( UnknownPropertyException & /*e*/ )
929 Any aBool;
930 aBool = ((XFastPropertySet *)pPS)->getFastPropertyValue( 3 );
931 OSL_FAIL( "PropertySetHelper: exception not thrown" );
933 catch( UnknownPropertyException & /*e*/ )
937 // set property with invalid type
940 Any aBool;
941 xPS->setPropertyValue("INT32", aBool );
942 OSL_FAIL( "PropertySetHelper: exception not thrown" );
944 catch( IllegalArgumentException & /*e*/ )
950 Any aBool;
951 pPS->setFastPropertyValue( PROPERTY_INT32, aBool );
952 OSL_FAIL( "PropertySetHelper: exception not thrown" );
954 catch( IllegalArgumentException & /*e*/ )
958 // narrowing conversion is not allowed!
961 Any aINT32;
962 aINT32 <<= (sal_Int32 ) 16;
963 xPS->setPropertyValue("INT16", aINT32 );
964 OSL_FAIL( "PropertySetHelper: exception not thrown" );
966 catch( IllegalArgumentException & /*e*/ )
973 Any aINT32;
974 aINT32 <<= (sal_Int32) 16;
975 pPS->setFastPropertyValue( PROPERTY_INT16, aINT32 );
976 OSL_FAIL( "PropertySetHelper: exception not thrown" );
978 catch( IllegalArgumentException & /*e*/ )
983 Any aValue;
984 aValue = xPS->getPropertyValue("BOOL");
985 sal_Bool b = *( (sal_Bool*)aValue.getValue());
986 OSL_ENSURE( ! b, "PropertySetHelper: wrong BOOL value" );
987 aValue = ((XFastPropertySet *)pPS)->getFastPropertyValue( PROPERTY_BOOL );
989 b = *((sal_Bool*)aValue.getValue());
990 OSL_ENSURE( !b, "PropertySetHelper: wrong BOOL value" );
992 sal_Int16 n16(0);
993 aValue <<=(sal_Int16)22;
994 xPS->setPropertyValue("INT16", aValue );
995 aValue = xPS->getPropertyValue("INT16");
996 aValue >>= n16;
997 OSL_ENSURE( 22 == n16 , "PropertySetHelper: wrong INT16 value" );
998 aValue <<= (sal_Int16)44;
999 ((XFastPropertySet *)pPS)->setFastPropertyValue( PROPERTY_INT16, aValue );
1001 aValue = ((XFastPropertySet *)pPS)->getFastPropertyValue( PROPERTY_INT16 );
1002 aValue >>= n16;
1003 OSL_ENSURE( 44 == n16, "PropertySetHelper: wrong INT16 value" );
1005 // widening conversion
1006 aValue <<= (sal_Int16)55;
1007 xPS->setPropertyValue("INT32", aValue );
1008 aValue = xPS->getPropertyValue("INT32");
1009 sal_Int32 n32(0);
1010 aValue >>= n32;
1011 OSL_ENSURE( 55 == n32 , "PropertySetHelper: wrong INT32 value" );
1012 aValue <<= (sal_Int16)66;
1013 ((XFastPropertySet *)pPS)->setFastPropertyValue( PROPERTY_INT32, aValue );
1014 aValue = ((XFastPropertySet *)pPS)->getFastPropertyValue( PROPERTY_INT32 );
1015 aValue >>= n32;
1016 OSL_ENSURE( 66 == n32, "PropertySetHelper: wrong INT32 value" );
1018 Sequence< OUString >valueNames = Sequence<OUString>( 3 );
1019 valueNames[0] = "BOOL";
1020 valueNames[1] = "INT16";
1021 valueNames[2] = "INT32";
1022 Sequence< Any > aValues = pPS->getPropertyValues( valueNames );
1024 b = *((sal_Bool*)aValues.getConstArray()[0].getValue());
1025 aValues.getConstArray()[1] >>= n16;
1026 aValues.getConstArray()[2] >>= n32;
1028 OSL_ENSURE( !b, "PropertySetHelper: wrong BOOL value" );
1029 OSL_ENSURE( 44 == n16, "PropertySetHelper: wrong INT16 value" );
1030 OSL_ENSURE( 66 == n32, "PropertySetHelper: wrong INT32 value" );
1032 pPS->nINT32 = 0;
1033 pPS->nINT16 = 0;
1035 // Test add-remove listener
1037 Reference < XVetoableChangeListener > x2( xPS_L, UNO_QUERY );
1039 xPS->addPropertyChangeListener( OUString("INT16"), xPS_L );
1040 pPS->addVetoableChangeListener( OUString("INT16"), x2 );
1042 pPS_L->nCount = 10;
1043 Sequence< Any > aSeq( pPS_L->nCount );
1044 pPS_L->nCurrent = 0;
1046 pPS_L->pExceptedListenerValues = aSeq.getArray();
1048 pPS_L->pExceptedListenerValues[0] <<= (sal_Int16) 0; // old value vetoable
1049 pPS_L->pExceptedListenerValues[1] <<= (sal_Int16) 22; // new value vetoable
1050 pPS_L->pExceptedListenerValues[2] <<= (sal_Int16) 0; // old value bound
1051 pPS_L->pExceptedListenerValues[3] <<= (sal_Int16) 22; // new value bound
1052 pPS_L->pExceptedListenerValues[4] <<= (sal_Int16) 22; // old value vetoable
1053 pPS_L->pExceptedListenerValues[5] <<= (sal_Int16) 44; // new value vetoable
1054 pPS_L->pExceptedListenerValues[6] <<= (sal_Int16) 22; // old value bound
1055 pPS_L->pExceptedListenerValues[7] <<= (sal_Int16) 44; // new value bound
1056 pPS_L->pExceptedListenerValues[8] <<= (sal_Int16) 44; // old value vetoable
1057 pPS_L->pExceptedListenerValues[9] <<= (sal_Int16) 100; // new value vetoable exception
1059 Any aValue;
1060 aValue <<= (sal_Int16)22;
1061 xPS->setPropertyValue("INT16", aValue );
1062 aValue <<= (sal_Int16) 44;
1063 ((XFastPropertySet *)pPS)->setFastPropertyValue( PROPERTY_INT16, aValue );
1064 aValue <<= (sal_Int16)100;// exception
1068 ((XFastPropertySet *)pPS)->setFastPropertyValue( PROPERTY_INT16, aValue );
1069 OSL_FAIL( "PropertySetHelper: exception not thrown" );
1071 catch( PropertyVetoException & /*e*/ )
1075 OSL_ENSURE( pPS_L->nCount == pPS_L->nCurrent, "not all listeners called" );
1076 pPS->nINT32 = 0;
1077 pPS->nINT16 = 0;
1078 pPS_L->nCount = 0;
1079 pPS_L->nCurrent = 0;
1080 pPS_L->pExceptedListenerValues = NULL;
1081 xPS->removePropertyChangeListener( OUString("INT16"), xPS_L );
1082 pPS->removeVetoableChangeListener( OUString("INT16"), x2 );
1085 // Test multi property set listener
1087 Reference < XPropertiesChangeListener > x1( xPS_L, UNO_QUERY );
1088 Reference < XVetoableChangeListener > x2( xPS_L, UNO_QUERY );
1090 pPS->addVetoableChangeListener( OUString("INT16") , x2 );
1091 Sequence<OUString> szPN( 4 );
1092 szPN[0] = "BOOL";
1093 szPN[1] = "INT32";
1094 szPN[2] = "Does not exist"; // must ne ignored by the addPropertiesChangeListener method
1095 szPN[3] = "INT16";
1096 pPS->addPropertiesChangeListener( szPN, x1 );
1098 pPS_L->nCount = 6;
1099 Sequence< Any > aSeq( pPS_L->nCount );
1100 pPS_L->nCurrent = 0;
1101 pPS_L->pExceptedListenerValues = aSeq.getArray();
1102 pPS_L->pExceptedListenerValues[0] <<= (sal_Int16) 0; // old value vetoable
1103 pPS_L->pExceptedListenerValues[1] <<= (sal_Int16 ) 22; // new value vetoable
1104 // INT32 is not constrained
1105 pPS_L->pExceptedListenerValues[2] <<= (sal_Int16) 0; // old value bound
1106 pPS_L->pExceptedListenerValues[3] <<= (sal_Int16) 22; // new value bound
1107 pPS_L->pExceptedListenerValues[4] <<= (sal_Int32) 0; // old value bound
1108 pPS_L->pExceptedListenerValues[5] <<= (sal_Int32) 44; // new value bound
1110 szPN = Sequence<OUString>( 2 );
1111 szPN[0] = "INT16";
1112 szPN[1] = "INT32";
1113 Sequence< Any > aValues( 2 );
1114 aValues.getArray()[0] <<= (sal_Int16) 22;
1115 aValues.getArray()[1] <<= (sal_Int16) 44;
1116 pPS->setPropertyValues( szPN, aValues );
1117 OSL_ENSURE( pPS_L->nCount == pPS_L->nCurrent, "not all listeners called" );
1119 //firePropertiesChangeEvent
1120 pPS->nINT16 = 8;
1121 pPS->nINT32 = 5;
1122 pPS_L->nCount = 4;
1123 pPS_L->nCurrent = 0;
1124 pPS_L->pExceptedListenerValues[0] <<= (sal_Int16) 8; // old value
1125 pPS_L->pExceptedListenerValues[1] <<= (sal_Int16) 8; // new value
1126 pPS_L->pExceptedListenerValues[2] <<= (sal_Int32) 5; // old value
1127 pPS_L->pExceptedListenerValues[3] <<= (sal_Int32) 5; // new value
1128 pPS->firePropertiesChangeEvent( szPN, pPS_L );
1129 OSL_ENSURE( pPS_L->nCount == pPS_L->nCurrent, "not all listeners called" );
1132 //vetoable exception with multiple
1133 szPN[0] = "INT16";
1134 szPN[1] = "INT16";
1135 pPS->nINT32 = 0;
1136 pPS->nINT16 = 0;
1137 pPS_L->nCount = 4;
1138 pPS_L->nCurrent = 0;
1139 pPS_L->pExceptedListenerValues[0] <<= (sal_Int16) 0; // old value vetoable
1140 pPS_L->pExceptedListenerValues[1] <<= (sal_Int16) 44; // new value vetoable
1141 pPS_L->pExceptedListenerValues[2] <<= (sal_Int16) 0; // old value vetoable
1142 pPS_L->pExceptedListenerValues[3] <<= (sal_Int16) 100; // new value vetoable
1146 aValues.getArray()[0] <<= (sal_Int16)44;
1147 aValues.getArray()[1] <<= (sal_Int16)100;
1148 pPS->setPropertyValues( szPN, aValues );
1149 OSL_FAIL( "PropertySetHelper: exception not thrown" );
1151 catch ( PropertyVetoException & /*e*/ )
1155 OSL_ENSURE( pPS_L->nCount == pPS_L->nCurrent, "not all listeners called" );
1156 pPS->removePropertiesChangeListener( x1 );
1157 pPS->removeVetoableChangeListener( OUString("INT16"), x2 );
1162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */