1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testpropshlp.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #if !defined(OSL_DEBUG_LEVEL) || OSL_DEBUG_LEVEL == 0
32 # undef OSL_DEBUG_LEVEL
33 # define OSL_DEBUG_LEVEL 2
37 // MARKER(update_precomp.py): autogen include statement, do not remove
38 #include "precompiled_cppuhelper.hxx"
39 #include <osl/mutex.hxx>
40 #include <osl/diagnose.h>
42 #include <cppuhelper/propshlp.hxx>
43 #include <cppuhelper/weak.hxx>
45 #include <cppuhelper/proptypehlp.hxx>
47 #include <com/sun/star/uno/Any.hxx>
48 #include <com/sun/star/beans/PropertyAttribute.hpp>
50 #include <cppuhelper/implbase3.hxx>
52 using namespace ::cppu
;
53 using namespace ::rtl
;
54 using namespace ::osl
;
55 using namespace ::com::sun::star::uno
;
56 using namespace ::com::sun::star::beans
;
57 using namespace ::com::sun::star::lang
;
61 /**********************
63 **********************/
64 static Property
* getPropertyTable1()
66 static Property
*pTable
= 0;
69 MutexGuard
guard( Mutex::getGlobalMutex() );
71 static Property aTable
[] =
73 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("a") ), 0, getCppuType( (OUString
*)0) ,
74 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), //OUString
75 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("b") ), 1, getCppuCharType( ) ,
76 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), //Char
77 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("c") ), 2, getCppuType( (sal_Int32
*)0) ,
78 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), //sal_Int32
79 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("d") ), 5, getCppuType( (double*)0) ,
80 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), //double
81 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("e") ), 7, getCppuBooleanType() ,
82 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), //BOOL
83 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("f") ), 8, getCppuType( (Any
*)0) ,
84 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
) //Any
93 static Property
* getPropertyTable2()
95 static Property
*pTable
= 0;
98 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
100 static Property aTable
[] =
102 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("f") ), 8, getCppuType( (Any
*)0) ,
103 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // Any
104 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("b") ), 1, getCppuCharType( ),
105 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // Char
106 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("a") ), 0, getCppuType( (OUString
*)0),
107 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // OUString
108 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("d") ), 5, getCppuType( (double*)0) ,
109 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // Double
110 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("c") ), 2, getCppuType( (sal_Int32
*)0),
111 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // sal_Int32
112 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("e") ), 7, getCppuBooleanType() ,
113 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
) // Bool
121 static Property
* getPropertyTable3()
123 static Property
*pTable
= 0;
126 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
128 static Property aTable
[] =
130 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("b") ), 1, getCppuCharType( ),
131 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // Char
132 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("f") ), 8, getCppuType( (Any
*)0) ,
133 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // any
134 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("a") ), 0, getCppuType( (OUString
*)0),
135 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
) // OUString
144 static Property
* getPropertyTable4()
146 static Property
*pTable
= 0;
149 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
151 static Property aTable
[] =
153 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("a") ), 0, getCppuType( (OUString
*)0),
154 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // OUString
155 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("b") ), 1, getCppuCharType( ),
156 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
), // Char
157 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("f") ), 2, getCppuType( (Any
*)0) ,
158 PropertyAttribute::READONLY
| PropertyAttribute::MAYBEVOID
) // Any
168 /**********************
170 * Note : all Property names must be in the 127 ASCII subset !
172 **********************/
175 void test_PropertyArrayHelper()
177 // Test getProperties() and getCount()
179 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
180 OSL_ENSURE( 6 == a1
.getCount(), "not all properties inserted" );
181 Sequence
< Property
> aProps
= a1
.getProperties();
182 Property
* pP
= aProps
.getArray();
183 OSL_ENSURE( 6 == aProps
.getLength(), "getProperties() gives not all properties" );
184 for( int i
= 0; i
< 6; i
++ )
186 OSL_ENSURE( pP
[i
].Name
== getPropertyTable1()[i
].Name
, "Name not correct" );
187 OSL_ENSURE( pP
[i
].Handle
== getPropertyTable1()[i
].Handle
, "Handle not correct" );
188 OSL_ENSURE( pP
[i
].Attributes
== getPropertyTable1()[i
].Attributes
, "Attributes not correct" );
189 OSL_ENSURE( pP
[i
].Type
== getPropertyTable1()[i
].Type
, "Type not correct" );
195 OPropertyArrayHelper
a1( getPropertyTable2(), 6, sal_False
);
196 Sequence
< Property
> aProps
= a1
.getProperties();
197 Property
* pP
= aProps
.getArray();
198 OSL_ENSURE( 6 == aProps
.getLength(), "getProperties() gives not all properties" );
200 // table to switch to sorted
201 int a
[] = { 2 , 1 , 4, 3, 5, 0 };
202 for( int i
= 0; i
< 6; i
++ )
204 OSL_ENSURE( pP
[i
].Name
== getPropertyTable2()[a
[i
]].Name
, "Name not correct" );
205 OSL_ENSURE( pP
[i
].Handle
== getPropertyTable2()[a
[i
]].Handle
, "Handle not correct" );
206 OSL_ENSURE( pP
[i
].Attributes
== getPropertyTable2()[a
[i
]].Attributes
, "Attributes not correct" );
207 OSL_ENSURE( pP
[i
].Type
== getPropertyTable2()[a
[i
]].Type
, "Type not correct" );
213 OPropertyArrayHelper
a1( getPropertyTable3(), 3, sal_False
);
214 Sequence
< Property
> aProps
= a1
.getProperties();
215 Property
* pP
= aProps
.getArray();
216 OSL_ENSURE( 3 == aProps
.getLength(), "getProperties() gives not all properties" );
217 // table to switch to sorted
218 int a
[] = { 2 , 0 , 1 };
219 for( int i
= 0; i
< 3; i
++ )
221 OSL_ENSURE( pP
[i
].Name
== getPropertyTable3()[a
[i
]].Name
, "Name not correct" );
222 OSL_ENSURE( pP
[i
].Handle
== getPropertyTable3()[a
[i
]].Handle
, "Handle not correct" );
223 OSL_ENSURE( pP
[i
].Attributes
== getPropertyTable3()[a
[i
]].Attributes
, "Attributes not correct" );
224 OSL_ENSURE( pP
[i
].Type
== getPropertyTable3()[a
[i
]].Type
, "Type not correct" );
228 // Test getPropertyByName and hasPropertyByName
230 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
231 for( int i
= 0; i
< 6; i
++ )
233 OSL_ENSURE( a1
.hasPropertyByName( getPropertyTable1()[i
].Name
), "hasPropertyByName not correct" );
234 Property aP
= a1
.getPropertyByName( getPropertyTable1()[i
].Name
);
235 OSL_ENSURE( aP
.Name
== getPropertyTable1()[i
].Name
, "Name not correct" );
236 OSL_ENSURE( aP
.Handle
== getPropertyTable1()[i
].Handle
, "Handle not correct" );
237 OSL_ENSURE( aP
.Attributes
== getPropertyTable1()[i
].Attributes
, "Attributes not correct" );
238 OSL_ENSURE( aP
.Type
== getPropertyTable1()[i
].Type
, "Type not correct" );
241 OSL_ENSURE( !a1
.hasPropertyByName( OUString( RTL_CONSTASCII_USTRINGPARAM("never exist") ) ), "hasPropertyByName not correct" );
244 a1
.getPropertyByName( OUString( RTL_CONSTASCII_USTRINGPARAM("never exist") ) );
245 OSL_ENSURE( sal_False
, "exeption not thrown" );
247 catch( UnknownPropertyException
& )
252 // Test getHandleByName
254 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
255 for( int i
= 0; i
< 6; i
++ )
257 sal_Int32 Handle
= a1
.getHandleByName( getPropertyTable1()[i
].Name
);
258 OSL_ENSURE( Handle
== getPropertyTable1()[i
].Handle
, "Handle not correct" );
260 sal_Int32 Handle
= a1
.getHandleByName( OUString( RTL_CONSTASCII_USTRINGPARAM("asdaf") ) );
261 OSL_ENSURE( Handle
== -1, "Handle not correct" );
264 // Test fillPropertyMembersByHandle
266 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
268 for( i
= 0; i
< 6; i
++ )
270 sal_Int16 nAttributes
;
272 sal_Bool b
= a1
.fillPropertyMembersByHandle( &aPropName
, &nAttributes
, getPropertyTable1()[i
].Handle
);
273 OSL_ENSURE( b
, "fillPropertyMembersByHandle: handle not found" );
274 OSL_ENSURE( nAttributes
== getPropertyTable1()[i
].Attributes
, "fillPropertyMembersByHandle: Attributes not correct" );
275 OSL_ENSURE( aPropName
== getPropertyTable1()[i
].Name
, "fillPropertyMembersByHandle: Name not correct" );
277 OSL_ENSURE( !a1
.fillPropertyMembersByHandle( NULL
, NULL
, 66666 ), "fillPropertyMembersByHandle: handle found" );
279 OPropertyArrayHelper
a4( getPropertyTable4(), 3 );
280 for( i
= 0; i
< 3; i
++ )
282 sal_Int16 nAttributes
;
284 sal_Bool b
= a1
.fillPropertyMembersByHandle( &aPropName
, &nAttributes
, getPropertyTable4()[i
].Handle
);
285 OSL_ENSURE( b
, "fillPropertyMembersByHandle: handle not found" );
286 OSL_ENSURE( nAttributes
== getPropertyTable1()[i
].Attributes
, "fillPropertyMembersByHandle: Attributes not correct" );
287 OSL_ENSURE( aPropName
== getPropertyTable1()[i
].Name
, "fillPropertyMembersByHandle: Name not correct" );
289 OSL_ENSURE( !a4
.fillPropertyMembersByHandle( NULL
, NULL
, 66666 ), "fillPropertyMembersByHandle: handle found" );
294 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
295 Sequence
< OUString
> aS( 4 );
296 sal_Int32 Handles
[4];
297 // muss sortiert sein
298 aS
.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("a") );
299 aS
.getArray()[1] = OUString( RTL_CONSTASCII_USTRINGPARAM("d") );
300 aS
.getArray()[2] = OUString( RTL_CONSTASCII_USTRINGPARAM("f") );
301 aS
.getArray()[3] = OUString( RTL_CONSTASCII_USTRINGPARAM("t") );
302 sal_Int32 nHitCount
= a1
.fillHandles( Handles
, aS
);
303 OSL_ENSURE( nHitCount
== 3, "wrong number of hits " );
304 OSL_ENSURE( Handles
[0] == getPropertyTable1()[0].Handle
, "Handle not correct" );
305 OSL_ENSURE( Handles
[1] == getPropertyTable1()[3].Handle
, "Handle not correct" );
306 OSL_ENSURE( Handles
[2] == getPropertyTable1()[5].Handle
, "Handle not correct" );
307 OSL_ENSURE( Handles
[3] == -1, "Handle not correct" );
314 //----------------------------------------------------
315 // test_OPropertySetHelper
316 //----------------------------------------------------
317 struct MutexContainer
321 class test_OPropertySetHelper
:
322 public MutexContainer
,
323 public OBroadcastHelper
,
324 public OPropertySetHelper
,
329 test_OPropertySetHelper( Property
* p
, sal_Int32 n
)
331 , OBroadcastHelper( ((MutexContainer
*)this)->aMutex
)
332 // , OPropertySetHelper( *SAL_STATIC_CAST(OBroadcastHelper *,this))
334 // OBroadcastHelper == OBroadcastHelperVar<OMultiTypeInterfaceContainerHelper>
335 , OPropertySetHelper(
336 *SAL_STATIC_CAST(OBroadcastHelper
*,this))
346 ~test_OPropertySetHelper()
353 sal_Bool bDoDispose
= sal_False
;
355 MutexGuard
aGuard( rBHelper
.rMutex
);
356 if( !rBHelper
.bDisposed
&& !rBHelper
.bInDispose
)
358 rBHelper
.bInDispose
= sal_True
;
359 bDoDispose
= sal_True
;
366 aEvt
.Source
= Reference
< XInterface
> ( SAL_STATIC_CAST( OWeakObject
* ,this) );
368 rBHelper
.aLC
.disposeAndClear( aEvt
);
369 rBHelper
.bDisposed
= sal_True
;
370 rBHelper
.bInDispose
= sal_False
;
375 Any SAL_CALL
queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(RuntimeException
)
377 Any
aRet( OPropertySetHelper::queryInterface( rType
) );
378 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
380 void SAL_CALL
acquire() throw()
381 { OWeakObject::acquire(); }
382 void SAL_CALL
release() throw()
383 { OWeakObject::release(); }
386 Reference
< XPropertySetInfo
> SAL_CALL
getPropertySetInfo()throw(RuntimeException
);
388 using OPropertySetHelper::getFastPropertyValue
;
393 Property
* pBasicProps
;
394 sal_Int32 nPropCount
;
396 IPropertyArrayHelper
& SAL_CALL
getInfoHelper() throw(RuntimeException
);
397 sal_Bool SAL_CALL
convertFastPropertyValue(
398 Any
& rConvertedValue
, Any
& rOldValue
,
399 sal_Int32 nHandle
, const Any
& rValue
)
400 throw(IllegalArgumentException
);
401 void SAL_CALL
setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const Any
& rValue
) throw(RuntimeException
);
402 void SAL_CALL
getFastPropertyValue( Any
& rValue
, sal_Int32 nHandle
) const throw(RuntimeException
);
405 //----------------------------------------------------------------------
406 //------ The Properties of this implementation -------------------------
407 //----------------------------------------------------------------------
408 // Id must be the index into the array
409 #define PROPERTY_BOOL 0
410 #define PROPERTY_INT16 1
411 #define PROPERTY_INT32 2
413 // Max number of properties
414 #define PROPERTY_COUNT 4
415 // Names of Properties
417 * All Properties of this implementation. Must be sorted by name.
419 Property
* getBasicProps()
421 static Property
*pTable
= 0;
424 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
427 static Property aBasicProps
[PROPERTY_COUNT
] =
429 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") ) , PROPERTY_BOOL
, getCppuBooleanType(), PropertyAttribute::READONLY
),
430 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ) , PROPERTY_INT16
,
431 getCppuType( (sal_Int16
*)0 ), PropertyAttribute::BOUND
| PropertyAttribute::CONSTRAINED
),
432 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") ) , PROPERTY_INT32
, getCppuType( (sal_Int32
*)0 ), PropertyAttribute::BOUND
),
433 Property( OUString( RTL_CONSTASCII_USTRINGPARAM("TEST") ) , 55 , getCppuType( (sal_Int32
*)0), PropertyAttribute::BOUND
)
435 pTable
= aBasicProps
;
442 //----------------------------------------------------
443 // test_OPropertySetHelper_Listener
444 //----------------------------------------------------
445 class test_OPropertySetHelper_Listener
446 : public WeakImplHelper3
< XPropertyChangeListener
, XPropertiesChangeListener
, XVetoableChangeListener
>
449 sal_Int32 nDisposing
;
451 Any
* pExceptedListenerValues
;
455 test_OPropertySetHelper_Listener( Any
* p
= 0, sal_Int32 n
= 0 )
457 , pExceptedListenerValues( p
)
462 ~test_OPropertySetHelper_Listener()
466 sal_Int32
getRefCount()
467 { return m_refCount
; }
470 void SAL_CALL
disposing(const EventObject
& /*evt*/) throw ( RuntimeException
)
472 MutexGuard
aGuard( aMutex
);
476 // XPropertyChangeListener
477 void SAL_CALL
propertyChange(const PropertyChangeEvent
& evt
) throw (RuntimeException
)
479 if( !pExceptedListenerValues
)
481 MutexGuard
aGuard( aMutex
);
482 OSL_ENSURE( nCurrent
+1 < nCount
, "PropertySetHelper: too many listener calls" );
484 switch( evt
.PropertyHandle
)
488 OSL_ENSURE( sal_False
, "PropertySetHelper: BOOL cannot change" );
489 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") ), "PropertySetHelper: wrong name" );
495 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), "PropertySetHelper: wrong name" );
497 OSL_ENSURE( pExceptedListenerValues
[nCurrent
].getValueType().getTypeClass() == TypeClass_SHORT
,
498 "PropertySetHelper: wrong data type" );
500 sal_Int16 nInt16
, nOldInt16
;
501 pExceptedListenerValues
[nCurrent
] >>= nInt16
;
502 evt
.OldValue
>>= nOldInt16
;
503 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong old value" );
506 pExceptedListenerValues
[nCurrent
+1] >>= nInt16
;
507 evt
.NewValue
>>= nOldInt16
;
508 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong new value" );
514 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") ), "PropertySetHelper: wrong name" );
516 sal_Int32 nInt32
,nOldInt32
;
518 pExceptedListenerValues
[nCurrent
] >>= nInt32
;
519 evt
.OldValue
>>= nOldInt32
;
520 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong old value" );
522 pExceptedListenerValues
[nCurrent
+1] >>= nInt32
;
523 evt
.NewValue
>>= nOldInt32
;
524 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong new value" );
529 OSL_ENSURE( sal_False
, "XPropeSetHelper: invalid property handle" );
534 // XVetoableChangeListener
535 void SAL_CALL
vetoableChange(const PropertyChangeEvent
& evt
) throw (PropertyVetoException
, RuntimeException
)
537 if( !pExceptedListenerValues
)
539 MutexGuard
aGuard( aMutex
);
540 OSL_ENSURE( nCurrent
+1 < nCount
, "PropertySetHelper: too many listener calls" );
542 switch( evt
.PropertyHandle
)
546 OSL_ENSURE( sal_False
, "PropertySetHelper: BOOL cannot change" );
547 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") ), "PropertySetHelper: wrong name" );
553 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), "PropertySetHelper: wrong name" );
555 sal_Int16 nInt16
, nOldInt16
;
556 pExceptedListenerValues
[nCurrent
] >>= nInt16
;
557 evt
.OldValue
>>= nOldInt16
;
559 OSL_ENSURE( nInt16
== nOldInt16
,"PropertySetHelper: wrong old value" );
561 pExceptedListenerValues
[nCurrent
+1] >>= nInt16
;
562 evt
.NewValue
>>= nOldInt16
;
563 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong new value" );
565 if( nOldInt16
== 100 )
568 throw PropertyVetoException();
575 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") ), "PropertySetHelper: wrong name" );
577 sal_Int32 nInt32
,nOldInt32
;
578 pExceptedListenerValues
[nCurrent
] >>= nInt32
;
579 evt
.OldValue
>>= nOldInt32
;
580 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong old value" );
582 pExceptedListenerValues
[nCurrent
+1] >>= nInt32
;
583 evt
.NewValue
>>= nOldInt32
;
584 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong new value" );
589 OSL_ENSURE( sal_False
, "XPropeSetHelper: invalid property handle" );
594 // XPropertiesChangeListener
595 void SAL_CALL
propertiesChange(const Sequence
< PropertyChangeEvent
>& evtSeq
) throw (RuntimeException
)
597 if( !pExceptedListenerValues
)
599 MutexGuard
aGuard( aMutex
);
600 for( sal_Int32 i
= 0; i
< evtSeq
.getLength(); i
++ )
602 const PropertyChangeEvent
& evt
= evtSeq
.getConstArray()[i
];
603 OSL_ENSURE( nCurrent
+1 < nCount
, "PropertySetHelper: too many listener calls" );
605 switch( evt
.PropertyHandle
)
609 OSL_ENSURE( sal_False
, "PropertySetHelper: BOOL cannot change" );
610 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") ), "PropertySetHelper: wrong name" );
616 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), "PropertySetHelper: wrong name" );
618 sal_Int16 nInt16
, nOldInt16
;
619 pExceptedListenerValues
[nCurrent
] >>= nInt16
;
620 evt
.OldValue
>>= nOldInt16
;
621 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong old value" );
624 pExceptedListenerValues
[nCurrent
+1] >>= nInt16
;
625 evt
.NewValue
>>= nOldInt16
;
626 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong new value" );
632 OSL_ENSURE( evt
.PropertyName
== OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") ), "PropertySetHelper: wrong name" );
635 sal_Int32 nInt32
,nOldInt32
;
636 pExceptedListenerValues
[nCurrent
] >>= nInt32
;
637 evt
.OldValue
>>= nOldInt32
;
638 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong old value" );
640 pExceptedListenerValues
[nCurrent
+1] >>= nInt32
;
641 evt
.NewValue
>>= nOldInt32
;
642 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong new value" );
647 OSL_ENSURE( sal_False
, "XPropeSetHelper: invalid property handle" );
655 * Create a table that map names to index values.
657 IPropertyArrayHelper
& test_OPropertySetHelper::getInfoHelper() throw(RuntimeException
)
659 // no multi thread protection
660 static OPropertyArrayHelper
aInfo( pBasicProps
, nPropCount
);
665 Reference
< XPropertySetInfo
> test_OPropertySetHelper::getPropertySetInfo()
666 throw(RuntimeException
)
668 // no multi thread protection
669 static Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
673 // Return sal_True, value changed
674 sal_Bool
test_OPropertySetHelper::convertFastPropertyValue
676 Any
& rConvertedValue
,
680 )throw(IllegalArgumentException
)
687 convertPropertyValue( b
, rValue
);
691 rConvertedValue
.setValue( &b
, ::getCppuBooleanType() );
692 rOldValue
.setValue( & bBOOL
, ::getCppuBooleanType() );
702 convertPropertyValue( n16
, rValue
);
706 rConvertedValue
<<= n16
;
707 rOldValue
<<= nINT16
;
717 convertPropertyValue( n32
, rValue
);
720 rConvertedValue
<<= n32
;
721 rOldValue
<<= nINT32
;
729 OSL_ENSURE( nHandle
== -1, "invalid property handle" );
735 * only set the value.
737 void test_OPropertySetHelper::setFastPropertyValue_NoBroadcast
741 )throw(RuntimeException
)
746 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_BOOLEAN
, "invalid type" );
747 bBOOL
= *((sal_Bool
*)rValue
.getValue());
751 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_SHORT
, "invalid type" );
756 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_LONG
, "invalid type" );
761 OSL_ENSURE( nHandle
== -1, "invalid property handle" );
765 //--------------------------
766 void test_OPropertySetHelper::getFastPropertyValue( Any
& rRet
, sal_Int32 nHandle
) const
767 throw(RuntimeException
)
772 rRet
.setValue( &bBOOL
, getCppuBooleanType() );
784 OSL_ENSURE( nHandle
== -1, "invalid property handle" );
789 void test_PropertySetHelper()
791 test_PropertyArrayHelper();
793 test_OPropertySetHelper
* pPS
;
795 Reference
< XPropertySet
> xPS
;
796 Reference
< XPropertyChangeListener
> xPS_L
;
797 test_OPropertySetHelper_Listener
* pPS_L
;
799 Reference
< XInterface
> x
;
801 for( int z
= 0; z
< 2; z
++ )
803 // first test aBasicProps Handles are { 0, 1, 2, 55 }
804 // first test getBasicProps() Handles are { 0, 1, 2 }
805 xPS
= pPS
= new test_OPropertySetHelper( getBasicProps(), PROPERTY_COUNT
- z
);
806 xPS_L
= static_cast< XPropertyChangeListener
* >( pPS_L
= new test_OPropertySetHelper_Listener() );
808 // Test queryInterface
809 Reference
< XPropertySet
> rProp( xPS
, UNO_QUERY
);
810 OSL_ENSURE( rProp
.is() , "PropertySetHelper: XPropertySet nor supported" );
812 Reference
< XMultiPropertySet
> rMulti( xPS
, UNO_QUERY
);
813 OSL_ENSURE( rMulti
.is() , "PropertySetHelper: XMultiPropertySet nor supported" );
815 Reference
< XFastPropertySet
> rFast( xPS
, UNO_QUERY
);
816 OSL_ENSURE( rFast
.is() , "PropertySetHelper: XFastPropertySet nor supported" );
818 x
= Reference
< XInterface
> ();
820 // Test add-remove listener
822 Reference
< XPropertiesChangeListener
> x1( xPS_L
, UNO_QUERY
);
823 Reference
< XVetoableChangeListener
> x2( xPS_L
, UNO_QUERY
);
825 xPS
->addPropertyChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), xPS_L
);
826 Sequence
<OUString
> szPN( 3 );
827 szPN
.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") );
828 szPN
.getArray()[1] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") );
829 szPN
.getArray()[2] = OUString( RTL_CONSTASCII_USTRINGPARAM("Does not exist") ); // must ne ignored by the addPropertiesChangeListener method
830 pPS
->addPropertiesChangeListener( szPN
, x1
);
832 szPN
= Sequence
<OUString
>();
833 pPS
->addPropertiesChangeListener( szPN
, x1
);
834 pPS
->addVetoableChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), x2
);
836 xPS
->removePropertyChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), xPS_L
);
837 pPS
->removePropertiesChangeListener( x1
);
838 pPS
->removePropertiesChangeListener( x1
);
839 pPS
->removeVetoableChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), x2
);
841 // this exception must thrown
844 xPS
->addPropertyChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("Does not exist") ), xPS_L
);
845 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
847 catch( UnknownPropertyException
& /*e*/ )
854 xPS
->addVetoableChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("Does not exist") ), x2
);
855 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
857 catch( UnknownPropertyException
& /*e*/ )
863 OSL_ENSURE( pPS_L
->getRefCount() == 1, "PropertySetHelper: wrong reference count" );
867 Reference
< XPropertiesChangeListener
> x1( xPS_L
, UNO_QUERY
);
868 Reference
< XVetoableChangeListener
> x2( xPS_L
, UNO_QUERY
);
870 xPS
->addPropertyChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), xPS_L
);
871 Sequence
<OUString
> szPN( 2 );
872 szPN
.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") );
873 szPN
.getArray()[1] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") );
874 pPS
->addPropertiesChangeListener( szPN
, x1
);
875 szPN
= Sequence
<OUString
>();
876 pPS
->addPropertiesChangeListener( szPN
, x1
);
877 pPS
->addVetoableChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), x2
);
880 OSL_ENSURE( pPS_L
->nDisposing
== 4 , "PropertySetHelper: wrong disposing count" );
881 OSL_ENSURE( pPS_L
->getRefCount() == 1 , "PropertySetHelper: wrong reference count" );
882 pPS_L
->nDisposing
= 0;
883 xPS
= pPS
= new test_OPropertySetHelper( getBasicProps(), PROPERTY_COUNT
- z
);
885 // Test set- and get- (Fast) propertyValue
887 // set read only property
890 // Readonly raises a vetoable exception
891 sal_Bool b
= sal_True
;
893 aBool
.setValue( &b
, getCppuBooleanType() );
894 xPS
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") ), aBool
);
895 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
897 catch( PropertyVetoException
& /*e*/ )
903 // Readonly raises a vetoable exception
904 sal_Bool b
= sal_True
;
906 aBool
.setValue( &b
, getCppuBooleanType() );
908 pPS
->setFastPropertyValue( PROPERTY_BOOL
, aBool
);
909 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
911 catch( PropertyVetoException
& /*e*/ )
915 // set unknown property
918 sal_Bool b
= sal_True
;
920 aBool
.setValue( &b
, getCppuBooleanType() );
921 xPS
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Does not exist") ), aBool
);
922 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
924 catch( UnknownPropertyException
& /*e*/ )
930 sal_Bool b
= sal_True
;
932 aBool
.setValue( &b
, getCppuBooleanType() );
933 pPS
->setFastPropertyValue( 3, aBool
);
934 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
936 catch( UnknownPropertyException
& /*e*/ )
940 // get unknown property
944 aBool
= xPS
->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Does not exist") ) );
945 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
947 catch( UnknownPropertyException
& /*e*/ )
954 aBool
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( 3 );
955 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
957 catch( UnknownPropertyException
& /*e*/ )
961 // set property with invalid type
965 xPS
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") ), aBool
);
966 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
968 catch( IllegalArgumentException
& /*e*/ )
975 pPS
->setFastPropertyValue( PROPERTY_INT32
, aBool
);
976 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
978 catch( IllegalArgumentException
& /*e*/ )
982 // narrowing conversion is not allowed!
986 aINT32
<<= (sal_Int32
) 16;
987 xPS
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), aINT32
);
988 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
990 catch( IllegalArgumentException
& /*e*/ )
998 aINT32
<<= (sal_Int32
) 16;
999 pPS
->setFastPropertyValue( PROPERTY_INT16
, aINT32
);
1000 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
1002 catch( IllegalArgumentException
& /*e*/ )
1008 aValue
= xPS
->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") ) );
1009 sal_Bool b
= *( (sal_Bool
*)aValue
.getValue());
1010 OSL_ENSURE( ! b
, "PropertySetHelper: wrong BOOL value" );
1011 aValue
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( PROPERTY_BOOL
);
1013 b
= *((sal_Bool
*)aValue
.getValue());
1014 OSL_ENSURE( !b
, "PropertySetHelper: wrong BOOL value" );
1017 aValue
<<=(sal_Int16
)22;
1018 xPS
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), aValue
);
1019 aValue
= xPS
->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ) );
1021 OSL_ENSURE( 22 == n16
, "PropertySetHelper: wrong INT16 value" );
1022 aValue
<<= (sal_Int16
)44;
1023 ((XFastPropertySet
*)pPS
)->setFastPropertyValue( PROPERTY_INT16
, aValue
);
1025 aValue
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( PROPERTY_INT16
);
1027 OSL_ENSURE( 44 == n16
, "PropertySetHelper: wrong INT16 value" );
1029 // widening conversion
1030 aValue
<<= (sal_Int16
)55;
1031 xPS
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") ), aValue
);
1032 aValue
= xPS
->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") ) );
1035 OSL_ENSURE( 55 == n32
, "PropertySetHelper: wrong INT32 value" );
1036 aValue
<<= (sal_Int16
)66;
1037 ((XFastPropertySet
*)pPS
)->setFastPropertyValue( PROPERTY_INT32
, aValue
);
1038 aValue
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( PROPERTY_INT32
);
1040 OSL_ENSURE( 66 == n32
, "PropertySetHelper: wrong INT32 value" );
1042 Sequence
< OUString
>valueNames
= Sequence
<OUString
>( 3 );
1043 valueNames
.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") );
1044 valueNames
.getArray()[1] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") );
1045 valueNames
.getArray()[2] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") );
1046 Sequence
< Any
> aValues
= pPS
->getPropertyValues( valueNames
);
1048 b
= *((sal_Bool
*)aValues
.getConstArray()[0].getValue());
1049 aValues
.getConstArray()[1] >>= n16
;
1050 aValues
.getConstArray()[2] >>= n32
;
1052 OSL_ENSURE( !b
, "PropertySetHelper: wrong BOOL value" );
1053 OSL_ENSURE( 44 == n16
, "PropertySetHelper: wrong INT16 value" );
1054 OSL_ENSURE( 66 == n32
, "PropertySetHelper: wrong INT32 value" );
1059 // Test add-remove listener
1061 Reference
< XVetoableChangeListener
> x2( xPS_L
, UNO_QUERY
);
1063 xPS
->addPropertyChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), xPS_L
);
1064 pPS
->addVetoableChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), x2
);
1067 Sequence
< Any
> aSeq( pPS_L
->nCount
);
1068 pPS_L
->nCurrent
= 0;
1070 pPS_L
->pExceptedListenerValues
= aSeq
.getArray();
1072 pPS_L
->pExceptedListenerValues
[0] <<= (sal_Int16
) 0; // old value vetoable
1073 pPS_L
->pExceptedListenerValues
[1] <<= (sal_Int16
) 22; // new value vetoable
1074 pPS_L
->pExceptedListenerValues
[2] <<= (sal_Int16
) 0; // old value bound
1075 pPS_L
->pExceptedListenerValues
[3] <<= (sal_Int16
) 22; // new value bound
1076 pPS_L
->pExceptedListenerValues
[4] <<= (sal_Int16
) 22; // old value vetoable
1077 pPS_L
->pExceptedListenerValues
[5] <<= (sal_Int16
) 44; // new value vetoable
1078 pPS_L
->pExceptedListenerValues
[6] <<= (sal_Int16
) 22; // old value bound
1079 pPS_L
->pExceptedListenerValues
[7] <<= (sal_Int16
) 44; // new value bound
1080 pPS_L
->pExceptedListenerValues
[8] <<= (sal_Int16
) 44; // old value vetoable
1081 pPS_L
->pExceptedListenerValues
[9] <<= (sal_Int16
) 100; // new value vetoable exception
1084 aValue
<<= (sal_Int16
)22;
1085 xPS
->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), aValue
);
1086 aValue
<<= (sal_Int16
) 44;
1087 ((XFastPropertySet
*)pPS
)->setFastPropertyValue( PROPERTY_INT16
, aValue
);
1088 aValue
<<= (sal_Int16
)100;// exception
1092 ((XFastPropertySet
*)pPS
)->setFastPropertyValue( PROPERTY_INT16
, aValue
);
1093 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
1095 catch( PropertyVetoException
& /*e*/ )
1099 OSL_ENSURE( pPS_L
->nCount
== pPS_L
->nCurrent
, "not all listeners called" );
1103 pPS_L
->nCurrent
= 0;
1104 pPS_L
->pExceptedListenerValues
= NULL
;
1105 xPS
->removePropertyChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), xPS_L
);
1106 pPS
->removeVetoableChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), x2
);
1109 // Test multi property set listener
1111 Reference
< XPropertiesChangeListener
> x1( xPS_L
, UNO_QUERY
);
1112 Reference
< XVetoableChangeListener
> x2( xPS_L
, UNO_QUERY
);
1114 pPS
->addVetoableChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ) , x2
);
1115 Sequence
<OUString
> szPN( 4 );
1116 szPN
.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("BOOL") );
1117 szPN
.getArray()[1] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") );
1118 szPN
.getArray()[2] = OUString( RTL_CONSTASCII_USTRINGPARAM("Does not exist") ); // must ne ignored by the addPropertiesChangeListener method
1119 szPN
.getArray()[3] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") );
1120 pPS
->addPropertiesChangeListener( szPN
, x1
);
1123 Sequence
< Any
> aSeq( pPS_L
->nCount
);
1124 pPS_L
->nCurrent
= 0;
1125 pPS_L
->pExceptedListenerValues
= aSeq
.getArray();
1126 pPS_L
->pExceptedListenerValues
[0] <<= (sal_Int16
) 0; // old value vetoable
1127 pPS_L
->pExceptedListenerValues
[1] <<= (sal_Int16
) 22; // new value vetoable
1128 // INT32 is not constrained
1129 pPS_L
->pExceptedListenerValues
[2] <<= (sal_Int16
) 0; // old value bound
1130 pPS_L
->pExceptedListenerValues
[3] <<= (sal_Int16
) 22; // new value bound
1131 pPS_L
->pExceptedListenerValues
[4] <<= (sal_Int32
) 0; // old value bound
1132 pPS_L
->pExceptedListenerValues
[5] <<= (sal_Int32
) 44; // new value bound
1134 szPN
= Sequence
<OUString
>( 2 );
1135 szPN
.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") );
1136 szPN
.getArray()[1] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT32") );
1137 Sequence
< Any
> aValues( 2 );
1138 aValues
.getArray()[0] <<= (sal_Int16
) 22;
1139 aValues
.getArray()[1] <<= (sal_Int16
) 44;
1140 pPS
->setPropertyValues( szPN
, aValues
);
1141 OSL_ENSURE( pPS_L
->nCount
== pPS_L
->nCurrent
, "not all listeners called" );
1143 //firePropertiesChangeEvent
1147 pPS_L
->nCurrent
= 0;
1148 pPS_L
->pExceptedListenerValues
[0] <<= (sal_Int16
) 8; // old value
1149 pPS_L
->pExceptedListenerValues
[1] <<= (sal_Int16
) 8; // new value
1150 pPS_L
->pExceptedListenerValues
[2] <<= (sal_Int32
) 5; // old value
1151 pPS_L
->pExceptedListenerValues
[3] <<= (sal_Int32
) 5; // new value
1152 pPS
->firePropertiesChangeEvent( szPN
, pPS_L
);
1153 OSL_ENSURE( pPS_L
->nCount
== pPS_L
->nCurrent
, "not all listeners called" );
1156 //vetoable exception with multible
1157 szPN
.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") );
1158 szPN
.getArray()[1] = OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") );
1162 pPS_L
->nCurrent
= 0;
1163 pPS_L
->pExceptedListenerValues
[0] <<= (sal_Int16
) 0; // old value vetoable
1164 pPS_L
->pExceptedListenerValues
[1] <<= (sal_Int16
) 44; // new value vetoable
1165 pPS_L
->pExceptedListenerValues
[2] <<= (sal_Int16
) 0; // old value vetoable
1166 pPS_L
->pExceptedListenerValues
[3] <<= (sal_Int16
) 100; // new value vetoable
1170 aValues
.getArray()[0] <<= (sal_Int16
)44;
1171 aValues
.getArray()[1] <<= (sal_Int16
)100;
1172 pPS
->setPropertyValues( szPN
, aValues
);
1173 OSL_ENSURE( sal_False
, "PropertySetHelper: exeption not thrown" );
1175 catch ( PropertyVetoException
& /*e*/ )
1179 OSL_ENSURE( pPS_L
->nCount
== pPS_L
->nCurrent
, "not all listeners called" );
1180 pPS
->removePropertiesChangeListener( x1
);
1181 pPS
->removeVetoableChangeListener( OUString( RTL_CONSTASCII_USTRINGPARAM("INT16") ), x2
);