1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
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;
50 MutexGuard
guard( Mutex::getGlobalMutex() );
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
74 static Property
* getPropertyTable2()
76 static Property
*pTable
= 0;
79 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
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
102 static Property
* getPropertyTable3()
104 static Property
*pTable
= 0;
107 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
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
125 static Property
* getPropertyTable4()
127 static Property
*pTable
= 0;
130 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
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
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" );
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" );
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 );
248 for( i
= 0; i
< 6; i
++ )
250 sal_Int16 nAttributes
;
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" );
259 OPropertyArrayHelper
a4( getPropertyTable4(), 3 );
260 for( i
= 0; i
< 3; i
++ )
262 sal_Int16 nAttributes
;
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" );
274 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
275 Sequence
< OUString
> aS( 4 );
276 sal_Int32 Handles
[4];
277 // muss sortiert sein
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
298 class test_OPropertySetHelper
:
299 public MutexContainer
,
300 public OBroadcastHelper
,
301 public OPropertySetHelper
,
306 test_OPropertySetHelper( Property
* p
, sal_Int32 n
)
308 , OBroadcastHelper( ((MutexContainer
*)this)->aMutex
)
309 // , OPropertySetHelper( *(static_cast< OBroadcastHelper * >(this)))
311 // OBroadcastHelper == OBroadcastHelperVar<OMultiTypeInterfaceContainerHelper>
312 , OPropertySetHelper(
313 *(static_cast< OBroadcastHelper
* >(this)))
323 ~test_OPropertySetHelper()
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
;
343 aEvt
.Source
.set( (static_cast< OWeakObject
* >(this)) );
345 rBHelper
.aLC
.disposeAndClear( aEvt
);
346 rBHelper
.bDisposed
= sal_True
;
347 rBHelper
.bInDispose
= sal_False
;
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(); }
363 Reference
< XPropertySetInfo
> SAL_CALL
getPropertySetInfo()throw(RuntimeException
);
365 using OPropertySetHelper::getFastPropertyValue
;
370 Property
* pBasicProps
;
371 sal_Int32 nPropCount
;
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;
401 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
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
;
419 // test_OPropertySetHelper_Listener
421 class test_OPropertySetHelper_Listener
422 : public WeakImplHelper3
< XPropertyChangeListener
, XPropertiesChangeListener
, XVetoableChangeListener
>
425 sal_Int32 nDisposing
;
427 Any
* pExceptedListenerValues
;
431 test_OPropertySetHelper_Listener( Any
* p
= 0, sal_Int32 n
= 0 )
433 , pExceptedListenerValues( p
)
438 ~test_OPropertySetHelper_Listener()
442 sal_Int32
getRefCount() const
443 { return m_refCount
; }
446 void SAL_CALL
disposing(const EventObject
& /*evt*/) throw ( RuntimeException
)
448 MutexGuard
aGuard( aMutex
);
452 // XPropertyChangeListener
453 void SAL_CALL
propertyChange(const PropertyChangeEvent
& evt
) throw (RuntimeException
)
455 if( !pExceptedListenerValues
)
457 MutexGuard
aGuard( aMutex
);
458 OSL_ENSURE( nCurrent
+1 < nCount
, "PropertySetHelper: too many listener calls" );
460 switch( evt
.PropertyHandle
)
464 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
465 OSL_ENSURE( evt
.PropertyName
== "BOOL", "PropertySetHelper: wrong name" );
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" );
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" );
505 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
510 // XVetoableChangeListener
511 void SAL_CALL
vetoableChange(const PropertyChangeEvent
& evt
) throw (PropertyVetoException
, RuntimeException
)
513 if( !pExceptedListenerValues
)
515 MutexGuard
aGuard( aMutex
);
516 OSL_ENSURE( nCurrent
+1 < nCount
, "PropertySetHelper: too many listener calls" );
518 switch( evt
.PropertyHandle
)
522 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
523 OSL_ENSURE( evt
.PropertyName
== "BOOL", "PropertySetHelper: wrong name" );
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 )
544 throw PropertyVetoException();
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" );
565 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
570 // XPropertiesChangeListener
571 void SAL_CALL
propertiesChange(const Sequence
< PropertyChangeEvent
>& evtSeq
) throw (RuntimeException
)
573 if( !pExceptedListenerValues
)
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
)
585 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
586 OSL_ENSURE( evt
.PropertyName
== "BOOL", "PropertySetHelper: wrong name" );
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" );
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" );
623 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
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
);
641 Reference
< XPropertySetInfo
> test_OPropertySetHelper::getPropertySetInfo()
642 throw(RuntimeException
)
644 // no multi thread protection
645 static Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
649 // Return sal_True, value changed
650 sal_Bool
test_OPropertySetHelper::convertFastPropertyValue
652 Any
& rConvertedValue
,
656 )throw(IllegalArgumentException
)
663 convertPropertyValue( b
, rValue
);
667 rConvertedValue
.setValue( &b
, cppu::UnoType
<bool>::get() );
668 rOldValue
.setValue( & bBOOL
, cppu::UnoType
<bool>::get() );
678 convertPropertyValue( n16
, rValue
);
682 rConvertedValue
<<= n16
;
683 rOldValue
<<= nINT16
;
693 convertPropertyValue( n32
, rValue
);
696 rConvertedValue
<<= n32
;
697 rOldValue
<<= nINT32
;
705 OSL_ENSURE( nHandle
== -1, "invalid property handle" );
711 * only set the value.
713 void test_OPropertySetHelper::setFastPropertyValue_NoBroadcast
717 )throw(RuntimeException
)
722 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_BOOLEAN
, "invalid type" );
723 bBOOL
= *((sal_Bool
*)rValue
.getValue());
727 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_SHORT
, "invalid type" );
732 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_LONG
, "invalid type" );
737 OSL_ENSURE( nHandle
== -1, "invalid property handle" );
742 void test_OPropertySetHelper::getFastPropertyValue( Any
& rRet
, sal_Int32 nHandle
) const
743 throw(RuntimeException
)
748 rRet
.setValue( &bBOOL
, cppu::UnoType
<bool>::get() );
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" );
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 );
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" );
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 );
850 pPS
->addPropertiesChangeListener( szPN
, x1
);
851 szPN
= Sequence
<OUString
>();
852 pPS
->addPropertiesChangeListener( szPN
, x1
);
853 pPS
->addVetoableChangeListener( OUString("INT16"), x2
);
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
;
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
;
882 aBool
.setValue( &b
, cppu::UnoType
<bool>::get() );
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
;
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
;
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
920 aBool
= xPS
->getPropertyValue("Does not exist");
921 OSL_FAIL( "PropertySetHelper: exception not thrown" );
923 catch( UnknownPropertyException
& /*e*/ )
930 aBool
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( 3 );
931 OSL_FAIL( "PropertySetHelper: exception not thrown" );
933 catch( UnknownPropertyException
& /*e*/ )
937 // set property with invalid type
941 xPS
->setPropertyValue("INT32", aBool
);
942 OSL_FAIL( "PropertySetHelper: exception not thrown" );
944 catch( IllegalArgumentException
& /*e*/ )
951 pPS
->setFastPropertyValue( PROPERTY_INT32
, aBool
);
952 OSL_FAIL( "PropertySetHelper: exception not thrown" );
954 catch( IllegalArgumentException
& /*e*/ )
958 // narrowing conversion is not allowed!
962 aINT32
<<= (sal_Int32
) 16;
963 xPS
->setPropertyValue("INT16", aINT32
);
964 OSL_FAIL( "PropertySetHelper: exception not thrown" );
966 catch( IllegalArgumentException
& /*e*/ )
974 aINT32
<<= (sal_Int32
) 16;
975 pPS
->setFastPropertyValue( PROPERTY_INT16
, aINT32
);
976 OSL_FAIL( "PropertySetHelper: exception not thrown" );
978 catch( IllegalArgumentException
& /*e*/ )
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" );
993 aValue
<<=(sal_Int16
)22;
994 xPS
->setPropertyValue("INT16", aValue
);
995 aValue
= xPS
->getPropertyValue("INT16");
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
);
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");
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
);
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" );
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
);
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
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" );
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 );
1094 szPN
[2] = "Does not exist"; // must ne ignored by the addPropertiesChangeListener method
1096 pPS
->addPropertiesChangeListener( szPN
, x1
);
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 );
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
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
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: */