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
149 /**********************
151 * Note : all Property names must be in the 127 ASCII subset !
153 **********************/
156 void test_PropertyArrayHelper()
158 // Test getProperties() and getCount()
160 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
161 OSL_ENSURE( 6 == a1
.getCount(), "not all properties inserted" );
162 Sequence
< Property
> aProps
= a1
.getProperties();
163 Property
* pP
= aProps
.getArray();
164 OSL_ENSURE( 6 == aProps
.getLength(), "getProperties() gives not all properties" );
165 for( int i
= 0; i
< 6; i
++ )
167 OSL_ENSURE( pP
[i
].Name
== getPropertyTable1()[i
].Name
, "Name not correct" );
168 OSL_ENSURE( pP
[i
].Handle
== getPropertyTable1()[i
].Handle
, "Handle not correct" );
169 OSL_ENSURE( pP
[i
].Attributes
== getPropertyTable1()[i
].Attributes
, "Attributes not correct" );
170 OSL_ENSURE( pP
[i
].Type
== getPropertyTable1()[i
].Type
, "Type not correct" );
176 OPropertyArrayHelper
a1( getPropertyTable2(), 6, sal_False
);
177 Sequence
< Property
> aProps
= a1
.getProperties();
178 Property
* pP
= aProps
.getArray();
179 OSL_ENSURE( 6 == aProps
.getLength(), "getProperties() gives not all properties" );
181 // table to switch to sorted
182 int a
[] = { 2 , 1 , 4, 3, 5, 0 };
183 for( int i
= 0; i
< 6; i
++ )
185 OSL_ENSURE( pP
[i
].Name
== getPropertyTable2()[a
[i
]].Name
, "Name not correct" );
186 OSL_ENSURE( pP
[i
].Handle
== getPropertyTable2()[a
[i
]].Handle
, "Handle not correct" );
187 OSL_ENSURE( pP
[i
].Attributes
== getPropertyTable2()[a
[i
]].Attributes
, "Attributes not correct" );
188 OSL_ENSURE( pP
[i
].Type
== getPropertyTable2()[a
[i
]].Type
, "Type not correct" );
194 OPropertyArrayHelper
a1( getPropertyTable3(), 3, sal_False
);
195 Sequence
< Property
> aProps
= a1
.getProperties();
196 Property
* pP
= aProps
.getArray();
197 OSL_ENSURE( 3 == aProps
.getLength(), "getProperties() gives not all properties" );
198 // table to switch to sorted
199 int a
[] = { 2 , 0 , 1 };
200 for( int i
= 0; i
< 3; i
++ )
202 OSL_ENSURE( pP
[i
].Name
== getPropertyTable3()[a
[i
]].Name
, "Name not correct" );
203 OSL_ENSURE( pP
[i
].Handle
== getPropertyTable3()[a
[i
]].Handle
, "Handle not correct" );
204 OSL_ENSURE( pP
[i
].Attributes
== getPropertyTable3()[a
[i
]].Attributes
, "Attributes not correct" );
205 OSL_ENSURE( pP
[i
].Type
== getPropertyTable3()[a
[i
]].Type
, "Type not correct" );
209 // Test getPropertyByName and hasPropertyByName
211 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
212 for( int i
= 0; i
< 6; i
++ )
214 OSL_ENSURE( a1
.hasPropertyByName( getPropertyTable1()[i
].Name
), "hasPropertyByName not correct" );
215 Property aP
= a1
.getPropertyByName( getPropertyTable1()[i
].Name
);
216 OSL_ENSURE( aP
.Name
== getPropertyTable1()[i
].Name
, "Name not correct" );
217 OSL_ENSURE( aP
.Handle
== getPropertyTable1()[i
].Handle
, "Handle not correct" );
218 OSL_ENSURE( aP
.Attributes
== getPropertyTable1()[i
].Attributes
, "Attributes not correct" );
219 OSL_ENSURE( aP
.Type
== getPropertyTable1()[i
].Type
, "Type not correct" );
222 OSL_ENSURE( !a1
.hasPropertyByName("never exist"), "hasPropertyByName not correct" );
225 a1
.getPropertyByName( OUString("never exist") );
226 OSL_FAIL( "exception not thrown" );
228 catch( UnknownPropertyException
& )
233 // Test getHandleByName
235 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
236 for( int i
= 0; i
< 6; i
++ )
238 sal_Int32 Handle
= a1
.getHandleByName( getPropertyTable1()[i
].Name
);
239 OSL_ENSURE( Handle
== getPropertyTable1()[i
].Handle
, "Handle not correct" );
241 sal_Int32 Handle
= a1
.getHandleByName( OUString("asdaf") );
242 OSL_ENSURE( Handle
== -1, "Handle not correct" );
245 // Test fillPropertyMembersByHandle
247 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
249 for( i
= 0; i
< 6; i
++ )
251 sal_Int16 nAttributes
;
253 sal_Bool b
= a1
.fillPropertyMembersByHandle( &aPropName
, &nAttributes
, getPropertyTable1()[i
].Handle
);
254 OSL_ENSURE( b
, "fillPropertyMembersByHandle: handle not found" );
255 OSL_ENSURE( nAttributes
== getPropertyTable1()[i
].Attributes
, "fillPropertyMembersByHandle: Attributes not correct" );
256 OSL_ENSURE( aPropName
== getPropertyTable1()[i
].Name
, "fillPropertyMembersByHandle: Name not correct" );
258 OSL_ENSURE( !a1
.fillPropertyMembersByHandle( NULL
, NULL
, 66666 ), "fillPropertyMembersByHandle: handle found" );
260 OPropertyArrayHelper
a4( getPropertyTable4(), 3 );
261 for( i
= 0; i
< 3; i
++ )
263 sal_Int16 nAttributes
;
265 sal_Bool b
= a1
.fillPropertyMembersByHandle( &aPropName
, &nAttributes
, getPropertyTable4()[i
].Handle
);
266 OSL_ENSURE( b
, "fillPropertyMembersByHandle: handle not found" );
267 OSL_ENSURE( nAttributes
== getPropertyTable1()[i
].Attributes
, "fillPropertyMembersByHandle: Attributes not correct" );
268 OSL_ENSURE( aPropName
== getPropertyTable1()[i
].Name
, "fillPropertyMembersByHandle: Name not correct" );
270 OSL_ENSURE( !a4
.fillPropertyMembersByHandle( NULL
, NULL
, 66666 ), "fillPropertyMembersByHandle: handle found" );
275 OPropertyArrayHelper
a1( getPropertyTable1(), 6 );
276 Sequence
< OUString
> aS( 4 );
277 sal_Int32 Handles
[4];
278 // muss sortiert sein
283 sal_Int32 nHitCount
= a1
.fillHandles( Handles
, aS
);
284 OSL_ENSURE( nHitCount
== 3, "wrong number of hits " );
285 OSL_ENSURE( Handles
[0] == getPropertyTable1()[0].Handle
, "Handle not correct" );
286 OSL_ENSURE( Handles
[1] == getPropertyTable1()[3].Handle
, "Handle not correct" );
287 OSL_ENSURE( Handles
[2] == getPropertyTable1()[5].Handle
, "Handle not correct" );
288 OSL_ENSURE( Handles
[3] == -1, "Handle not correct" );
296 // test_OPropertySetHelper
298 struct MutexContainer
302 class test_OPropertySetHelper
:
303 public MutexContainer
,
304 public OBroadcastHelper
,
305 public OPropertySetHelper
,
310 test_OPropertySetHelper( Property
* p
, sal_Int32 n
)
312 , OBroadcastHelper( ((MutexContainer
*)this)->aMutex
)
313 // , OPropertySetHelper( *(static_cast< OBroadcastHelper * >(this)))
315 // OBroadcastHelper == OBroadcastHelperVar<OMultiTypeInterfaceContainerHelper>
316 , OPropertySetHelper(
317 *(static_cast< OBroadcastHelper
* >(this)))
327 ~test_OPropertySetHelper()
334 sal_Bool bDoDispose
= sal_False
;
336 MutexGuard
aGuard( rBHelper
.rMutex
);
337 if( !rBHelper
.bDisposed
&& !rBHelper
.bInDispose
)
339 rBHelper
.bInDispose
= sal_True
;
340 bDoDispose
= sal_True
;
347 aEvt
.Source
= Reference
< XInterface
> ( (static_cast< OWeakObject
* >(this)) );
349 rBHelper
.aLC
.disposeAndClear( aEvt
);
350 rBHelper
.bDisposed
= sal_True
;
351 rBHelper
.bInDispose
= sal_False
;
356 Any SAL_CALL
queryInterface( const ::com::sun::star::uno::Type
& rType
) throw(RuntimeException
)
358 Any
aRet( OPropertySetHelper::queryInterface( rType
) );
359 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
361 void SAL_CALL
acquire() throw()
362 { OWeakObject::acquire(); }
363 void SAL_CALL
release() throw()
364 { OWeakObject::release(); }
367 Reference
< XPropertySetInfo
> SAL_CALL
getPropertySetInfo()throw(RuntimeException
);
369 using OPropertySetHelper::getFastPropertyValue
;
374 Property
* pBasicProps
;
375 sal_Int32 nPropCount
;
377 IPropertyArrayHelper
& SAL_CALL
getInfoHelper() throw(RuntimeException
);
378 sal_Bool SAL_CALL
convertFastPropertyValue(
379 Any
& rConvertedValue
, Any
& rOldValue
,
380 sal_Int32 nHandle
, const Any
& rValue
)
381 throw(IllegalArgumentException
);
382 void SAL_CALL
setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const Any
& rValue
) throw(RuntimeException
);
383 void SAL_CALL
getFastPropertyValue( Any
& rValue
, sal_Int32 nHandle
) const throw(RuntimeException
);
387 //------ The Properties of this implementation -------------------------
389 // Id must be the index into the array
390 #define PROPERTY_BOOL 0
391 #define PROPERTY_INT16 1
392 #define PROPERTY_INT32 2
394 // Max number of properties
395 #define PROPERTY_COUNT 4
396 // Names of Properties
398 * All Properties of this implementation. Must be sorted by name.
400 Property
* getBasicProps()
402 static Property
*pTable
= 0;
405 MutexGuard
guard( ::osl::Mutex::getGlobalMutex() );
408 static Property aBasicProps
[PROPERTY_COUNT
] =
410 Property( OUString("BOOL") , PROPERTY_BOOL
, cppu::UnoType
<bool>::get(), PropertyAttribute::READONLY
),
411 Property( OUString("INT16") , PROPERTY_INT16
,
412 cppu::UnoType
<sal_Int16
>::get(), PropertyAttribute::BOUND
| PropertyAttribute::CONSTRAINED
),
413 Property( OUString("INT32") , PROPERTY_INT32
, cppu::UnoType
<sal_Int32
>::get(), PropertyAttribute::BOUND
),
414 Property( OUString("TEST") , 55 , cppu::UnoType
<sal_Int32
>::get(), PropertyAttribute::BOUND
)
416 pTable
= aBasicProps
;
424 // test_OPropertySetHelper_Listener
426 class test_OPropertySetHelper_Listener
427 : public WeakImplHelper3
< XPropertyChangeListener
, XPropertiesChangeListener
, XVetoableChangeListener
>
430 sal_Int32 nDisposing
;
432 Any
* pExceptedListenerValues
;
436 test_OPropertySetHelper_Listener( Any
* p
= 0, sal_Int32 n
= 0 )
438 , pExceptedListenerValues( p
)
443 ~test_OPropertySetHelper_Listener()
447 sal_Int32
getRefCount() const
448 { return m_refCount
; }
451 void SAL_CALL
disposing(const EventObject
& /*evt*/) throw ( RuntimeException
)
453 MutexGuard
aGuard( aMutex
);
457 // XPropertyChangeListener
458 void SAL_CALL
propertyChange(const PropertyChangeEvent
& evt
) throw (RuntimeException
)
460 if( !pExceptedListenerValues
)
462 MutexGuard
aGuard( aMutex
);
463 OSL_ENSURE( nCurrent
+1 < nCount
, "PropertySetHelper: too many listener calls" );
465 switch( evt
.PropertyHandle
)
469 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
470 OSL_ENSURE( evt
.PropertyName
== "BOOL", "PropertySetHelper: wrong name" );
476 OSL_ENSURE( evt
.PropertyName
== "INT16", "PropertySetHelper: wrong name" );
478 OSL_ENSURE( pExceptedListenerValues
[nCurrent
].getValueType().getTypeClass() == TypeClass_SHORT
,
479 "PropertySetHelper: wrong data type" );
481 sal_Int16
nInt16(0), nOldInt16(0);
482 pExceptedListenerValues
[nCurrent
] >>= nInt16
;
483 evt
.OldValue
>>= nOldInt16
;
484 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong old value" );
487 pExceptedListenerValues
[nCurrent
+1] >>= nInt16
;
488 evt
.NewValue
>>= nOldInt16
;
489 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong new value" );
495 OSL_ENSURE( evt
.PropertyName
== "INT32", "PropertySetHelper: wrong name" );
497 sal_Int32
nInt32(0),nOldInt32(0);
499 pExceptedListenerValues
[nCurrent
] >>= nInt32
;
500 evt
.OldValue
>>= nOldInt32
;
501 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong old value" );
503 pExceptedListenerValues
[nCurrent
+1] >>= nInt32
;
504 evt
.NewValue
>>= nOldInt32
;
505 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong new value" );
510 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
515 // XVetoableChangeListener
516 void SAL_CALL
vetoableChange(const PropertyChangeEvent
& evt
) throw (PropertyVetoException
, RuntimeException
)
518 if( !pExceptedListenerValues
)
520 MutexGuard
aGuard( aMutex
);
521 OSL_ENSURE( nCurrent
+1 < nCount
, "PropertySetHelper: too many listener calls" );
523 switch( evt
.PropertyHandle
)
527 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
528 OSL_ENSURE( evt
.PropertyName
== "BOOL", "PropertySetHelper: wrong name" );
534 OSL_ENSURE( evt
.PropertyName
== "INT16", "PropertySetHelper: wrong name" );
536 sal_Int16
nInt16(0), nOldInt16(0);
537 pExceptedListenerValues
[nCurrent
] >>= nInt16
;
538 evt
.OldValue
>>= nOldInt16
;
540 OSL_ENSURE( nInt16
== nOldInt16
,"PropertySetHelper: wrong old value" );
542 pExceptedListenerValues
[nCurrent
+1] >>= nInt16
;
543 evt
.NewValue
>>= nOldInt16
;
544 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong new value" );
546 if( nOldInt16
== 100 )
549 throw PropertyVetoException();
556 OSL_ENSURE( evt
.PropertyName
== "INT32", "PropertySetHelper: wrong name" );
558 sal_Int32
nInt32(0),nOldInt32(0);
559 pExceptedListenerValues
[nCurrent
] >>= nInt32
;
560 evt
.OldValue
>>= nOldInt32
;
561 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong old value" );
563 pExceptedListenerValues
[nCurrent
+1] >>= nInt32
;
564 evt
.NewValue
>>= nOldInt32
;
565 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong new value" );
570 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
575 // XPropertiesChangeListener
576 void SAL_CALL
propertiesChange(const Sequence
< PropertyChangeEvent
>& evtSeq
) throw (RuntimeException
)
578 if( !pExceptedListenerValues
)
580 MutexGuard
aGuard( aMutex
);
581 for( sal_Int32 i
= 0; i
< evtSeq
.getLength(); i
++ )
583 const PropertyChangeEvent
& evt
= evtSeq
.getConstArray()[i
];
584 OSL_ENSURE( nCurrent
+1 < nCount
, "PropertySetHelper: too many listener calls" );
586 switch( evt
.PropertyHandle
)
590 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
591 OSL_ENSURE( evt
.PropertyName
== "BOOL", "PropertySetHelper: wrong name" );
597 OSL_ENSURE( evt
.PropertyName
== "INT16", "PropertySetHelper: wrong name" );
599 sal_Int16
nInt16(0), nOldInt16(0);
600 pExceptedListenerValues
[nCurrent
] >>= nInt16
;
601 evt
.OldValue
>>= nOldInt16
;
602 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong old value" );
605 pExceptedListenerValues
[nCurrent
+1] >>= nInt16
;
606 evt
.NewValue
>>= nOldInt16
;
607 OSL_ENSURE( nInt16
== nOldInt16
, "PropertySetHelper: wrong new value" );
613 OSL_ENSURE( evt
.PropertyName
== "INT32", "PropertySetHelper: wrong name" );
616 sal_Int32
nInt32(0),nOldInt32(0);
617 pExceptedListenerValues
[nCurrent
] >>= nInt32
;
618 evt
.OldValue
>>= nOldInt32
;
619 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong old value" );
621 pExceptedListenerValues
[nCurrent
+1] >>= nInt32
;
622 evt
.NewValue
>>= nOldInt32
;
623 OSL_ENSURE( nInt32
== nOldInt32
, "PropertySetHelper: wrong new value" );
628 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
636 * Create a table that map names to index values.
638 IPropertyArrayHelper
& test_OPropertySetHelper::getInfoHelper() throw(RuntimeException
)
640 // no multi thread protection
641 static OPropertyArrayHelper
aInfo( pBasicProps
, nPropCount
);
646 Reference
< XPropertySetInfo
> test_OPropertySetHelper::getPropertySetInfo()
647 throw(RuntimeException
)
649 // no multi thread protection
650 static Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
654 // Return sal_True, value changed
655 sal_Bool
test_OPropertySetHelper::convertFastPropertyValue
657 Any
& rConvertedValue
,
661 )throw(IllegalArgumentException
)
668 convertPropertyValue( b
, rValue
);
672 rConvertedValue
.setValue( &b
, cppu::UnoType
<bool>::get() );
673 rOldValue
.setValue( & bBOOL
, cppu::UnoType
<bool>::get() );
683 convertPropertyValue( n16
, rValue
);
687 rConvertedValue
<<= n16
;
688 rOldValue
<<= nINT16
;
698 convertPropertyValue( n32
, rValue
);
701 rConvertedValue
<<= n32
;
702 rOldValue
<<= nINT32
;
710 OSL_ENSURE( nHandle
== -1, "invalid property handle" );
716 * only set the value.
718 void test_OPropertySetHelper::setFastPropertyValue_NoBroadcast
722 )throw(RuntimeException
)
727 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_BOOLEAN
, "invalid type" );
728 bBOOL
= *((sal_Bool
*)rValue
.getValue());
732 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_SHORT
, "invalid type" );
737 OSL_ENSURE( rValue
.getValueType().getTypeClass() == TypeClass_LONG
, "invalid type" );
742 OSL_ENSURE( nHandle
== -1, "invalid property handle" );
747 void test_OPropertySetHelper::getFastPropertyValue( Any
& rRet
, sal_Int32 nHandle
) const
748 throw(RuntimeException
)
753 rRet
.setValue( &bBOOL
, cppu::UnoType
<bool>::get() );
765 OSL_ENSURE( nHandle
== -1, "invalid property handle" );
770 void test_PropertySetHelper()
772 test_PropertyArrayHelper();
774 test_OPropertySetHelper
* pPS
;
776 Reference
< XPropertySet
> xPS
;
777 Reference
< XPropertyChangeListener
> xPS_L
;
778 test_OPropertySetHelper_Listener
* pPS_L
;
780 Reference
< XInterface
> x
;
782 for( int z
= 0; z
< 2; z
++ )
784 // first test aBasicProps Handles are { 0, 1, 2, 55 }
785 // first test getBasicProps() Handles are { 0, 1, 2 }
786 xPS
= pPS
= new test_OPropertySetHelper( getBasicProps(), PROPERTY_COUNT
- z
);
787 xPS_L
= static_cast< XPropertyChangeListener
* >( pPS_L
= new test_OPropertySetHelper_Listener() );
789 // Test queryInterface
790 Reference
< XPropertySet
> rProp( xPS
, UNO_QUERY
);
791 OSL_ENSURE( rProp
.is() , "PropertySetHelper: XPropertySet nor supported" );
793 Reference
< XMultiPropertySet
> rMulti( xPS
, UNO_QUERY
);
794 OSL_ENSURE( rMulti
.is() , "PropertySetHelper: XMultiPropertySet nor supported" );
796 Reference
< XFastPropertySet
> rFast( xPS
, UNO_QUERY
);
797 OSL_ENSURE( rFast
.is() , "PropertySetHelper: XFastPropertySet nor supported" );
799 x
= Reference
< XInterface
> ();
801 // Test add-remove listener
803 Reference
< XPropertiesChangeListener
> x1( xPS_L
, UNO_QUERY
);
804 Reference
< XVetoableChangeListener
> x2( xPS_L
, UNO_QUERY
);
806 xPS
->addPropertyChangeListener( OUString("INT16"), xPS_L
);
807 Sequence
<OUString
> szPN( 3 );
810 szPN
[2] = "Does not exist"; // must ne ignored by the addPropertiesChangeListener method
811 pPS
->addPropertiesChangeListener( szPN
, x1
);
813 szPN
= Sequence
<OUString
>();
814 pPS
->addPropertiesChangeListener( szPN
, x1
);
815 pPS
->addVetoableChangeListener( OUString("INT16"), x2
);
817 xPS
->removePropertyChangeListener( OUString("INT16"), xPS_L
);
818 pPS
->removePropertiesChangeListener( x1
);
819 pPS
->removePropertiesChangeListener( x1
);
820 pPS
->removeVetoableChangeListener( OUString("INT16"), x2
);
822 // this exception must thrown
825 xPS
->addPropertyChangeListener( OUString("Does not exist"), xPS_L
);
826 OSL_FAIL( "PropertySetHelper: exception not thrown" );
828 catch( UnknownPropertyException
& /*e*/ )
835 xPS
->addVetoableChangeListener( OUString("Does not exist"), x2
);
836 OSL_FAIL( "PropertySetHelper: exception not thrown" );
838 catch( UnknownPropertyException
& /*e*/ )
844 OSL_ENSURE( pPS_L
->getRefCount() == 1, "PropertySetHelper: wrong reference count" );
848 Reference
< XPropertiesChangeListener
> x1( xPS_L
, UNO_QUERY
);
849 Reference
< XVetoableChangeListener
> x2( xPS_L
, UNO_QUERY
);
851 xPS
->addPropertyChangeListener( OUString("INT16"), xPS_L
);
852 Sequence
<OUString
> szPN( 2 );
855 pPS
->addPropertiesChangeListener( szPN
, x1
);
856 szPN
= Sequence
<OUString
>();
857 pPS
->addPropertiesChangeListener( szPN
, x1
);
858 pPS
->addVetoableChangeListener( OUString("INT16"), x2
);
861 OSL_ENSURE( pPS_L
->nDisposing
== 4 , "PropertySetHelper: wrong disposing count" );
862 OSL_ENSURE( pPS_L
->getRefCount() == 1 , "PropertySetHelper: wrong reference count" );
863 pPS_L
->nDisposing
= 0;
864 xPS
= pPS
= new test_OPropertySetHelper( getBasicProps(), PROPERTY_COUNT
- z
);
866 // Test set- and get- (Fast) propertyValue
868 // set read only property
871 // Readonly raises a vetoable exception
872 sal_Bool b
= sal_True
;
874 aBool
.setValue( &b
, cppu::UnoType
<bool>::get() );
875 xPS
->setPropertyValue("BOOL", aBool
);
876 OSL_FAIL( "PropertySetHelper: exception not thrown" );
878 catch( PropertyVetoException
& /*e*/ )
884 // Readonly raises a vetoable exception
885 sal_Bool b
= sal_True
;
887 aBool
.setValue( &b
, cppu::UnoType
<bool>::get() );
889 pPS
->setFastPropertyValue( PROPERTY_BOOL
, aBool
);
890 OSL_FAIL( "PropertySetHelper: exception not thrown" );
892 catch( PropertyVetoException
& /*e*/ )
896 // set unknown property
899 sal_Bool b
= sal_True
;
901 aBool
.setValue( &b
, cppu::UnoType
<bool>::get() );
902 xPS
->setPropertyValue("Does not exist", aBool
);
903 OSL_FAIL( "PropertySetHelper: exception not thrown" );
905 catch( UnknownPropertyException
& /*e*/ )
911 sal_Bool b
= sal_True
;
913 aBool
.setValue( &b
, cppu::UnoType
<bool>::get() );
914 pPS
->setFastPropertyValue( 3, aBool
);
915 OSL_FAIL( "PropertySetHelper: exception not thrown" );
917 catch( UnknownPropertyException
& /*e*/ )
921 // get unknown property
925 aBool
= xPS
->getPropertyValue("Does not exist");
926 OSL_FAIL( "PropertySetHelper: exception not thrown" );
928 catch( UnknownPropertyException
& /*e*/ )
935 aBool
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( 3 );
936 OSL_FAIL( "PropertySetHelper: exception not thrown" );
938 catch( UnknownPropertyException
& /*e*/ )
942 // set property with invalid type
946 xPS
->setPropertyValue("INT32", aBool
);
947 OSL_FAIL( "PropertySetHelper: exception not thrown" );
949 catch( IllegalArgumentException
& /*e*/ )
956 pPS
->setFastPropertyValue( PROPERTY_INT32
, aBool
);
957 OSL_FAIL( "PropertySetHelper: exception not thrown" );
959 catch( IllegalArgumentException
& /*e*/ )
963 // narrowing conversion is not allowed!
967 aINT32
<<= (sal_Int32
) 16;
968 xPS
->setPropertyValue("INT16", aINT32
);
969 OSL_FAIL( "PropertySetHelper: exception not thrown" );
971 catch( IllegalArgumentException
& /*e*/ )
979 aINT32
<<= (sal_Int32
) 16;
980 pPS
->setFastPropertyValue( PROPERTY_INT16
, aINT32
);
981 OSL_FAIL( "PropertySetHelper: exception not thrown" );
983 catch( IllegalArgumentException
& /*e*/ )
989 aValue
= xPS
->getPropertyValue("BOOL");
990 sal_Bool b
= *( (sal_Bool
*)aValue
.getValue());
991 OSL_ENSURE( ! b
, "PropertySetHelper: wrong BOOL value" );
992 aValue
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( PROPERTY_BOOL
);
994 b
= *((sal_Bool
*)aValue
.getValue());
995 OSL_ENSURE( !b
, "PropertySetHelper: wrong BOOL value" );
998 aValue
<<=(sal_Int16
)22;
999 xPS
->setPropertyValue("INT16", aValue
);
1000 aValue
= xPS
->getPropertyValue("INT16");
1002 OSL_ENSURE( 22 == n16
, "PropertySetHelper: wrong INT16 value" );
1003 aValue
<<= (sal_Int16
)44;
1004 ((XFastPropertySet
*)pPS
)->setFastPropertyValue( PROPERTY_INT16
, aValue
);
1006 aValue
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( PROPERTY_INT16
);
1008 OSL_ENSURE( 44 == n16
, "PropertySetHelper: wrong INT16 value" );
1010 // widening conversion
1011 aValue
<<= (sal_Int16
)55;
1012 xPS
->setPropertyValue("INT32", aValue
);
1013 aValue
= xPS
->getPropertyValue("INT32");
1016 OSL_ENSURE( 55 == n32
, "PropertySetHelper: wrong INT32 value" );
1017 aValue
<<= (sal_Int16
)66;
1018 ((XFastPropertySet
*)pPS
)->setFastPropertyValue( PROPERTY_INT32
, aValue
);
1019 aValue
= ((XFastPropertySet
*)pPS
)->getFastPropertyValue( PROPERTY_INT32
);
1021 OSL_ENSURE( 66 == n32
, "PropertySetHelper: wrong INT32 value" );
1023 Sequence
< OUString
>valueNames
= Sequence
<OUString
>( 3 );
1024 valueNames
[0] = "BOOL";
1025 valueNames
[1] = "INT16";
1026 valueNames
[2] = "INT32";
1027 Sequence
< Any
> aValues
= pPS
->getPropertyValues( valueNames
);
1029 b
= *((sal_Bool
*)aValues
.getConstArray()[0].getValue());
1030 aValues
.getConstArray()[1] >>= n16
;
1031 aValues
.getConstArray()[2] >>= n32
;
1033 OSL_ENSURE( !b
, "PropertySetHelper: wrong BOOL value" );
1034 OSL_ENSURE( 44 == n16
, "PropertySetHelper: wrong INT16 value" );
1035 OSL_ENSURE( 66 == n32
, "PropertySetHelper: wrong INT32 value" );
1040 // Test add-remove listener
1042 Reference
< XVetoableChangeListener
> x2( xPS_L
, UNO_QUERY
);
1044 xPS
->addPropertyChangeListener( OUString("INT16"), xPS_L
);
1045 pPS
->addVetoableChangeListener( OUString("INT16"), x2
);
1048 Sequence
< Any
> aSeq( pPS_L
->nCount
);
1049 pPS_L
->nCurrent
= 0;
1051 pPS_L
->pExceptedListenerValues
= aSeq
.getArray();
1053 pPS_L
->pExceptedListenerValues
[0] <<= (sal_Int16
) 0; // old value vetoable
1054 pPS_L
->pExceptedListenerValues
[1] <<= (sal_Int16
) 22; // new value vetoable
1055 pPS_L
->pExceptedListenerValues
[2] <<= (sal_Int16
) 0; // old value bound
1056 pPS_L
->pExceptedListenerValues
[3] <<= (sal_Int16
) 22; // new value bound
1057 pPS_L
->pExceptedListenerValues
[4] <<= (sal_Int16
) 22; // old value vetoable
1058 pPS_L
->pExceptedListenerValues
[5] <<= (sal_Int16
) 44; // new value vetoable
1059 pPS_L
->pExceptedListenerValues
[6] <<= (sal_Int16
) 22; // old value bound
1060 pPS_L
->pExceptedListenerValues
[7] <<= (sal_Int16
) 44; // new value bound
1061 pPS_L
->pExceptedListenerValues
[8] <<= (sal_Int16
) 44; // old value vetoable
1062 pPS_L
->pExceptedListenerValues
[9] <<= (sal_Int16
) 100; // new value vetoable exception
1065 aValue
<<= (sal_Int16
)22;
1066 xPS
->setPropertyValue("INT16", aValue
);
1067 aValue
<<= (sal_Int16
) 44;
1068 ((XFastPropertySet
*)pPS
)->setFastPropertyValue( PROPERTY_INT16
, aValue
);
1069 aValue
<<= (sal_Int16
)100;// exception
1073 ((XFastPropertySet
*)pPS
)->setFastPropertyValue( PROPERTY_INT16
, aValue
);
1074 OSL_FAIL( "PropertySetHelper: exception not thrown" );
1076 catch( PropertyVetoException
& /*e*/ )
1080 OSL_ENSURE( pPS_L
->nCount
== pPS_L
->nCurrent
, "not all listeners called" );
1084 pPS_L
->nCurrent
= 0;
1085 pPS_L
->pExceptedListenerValues
= NULL
;
1086 xPS
->removePropertyChangeListener( OUString("INT16"), xPS_L
);
1087 pPS
->removeVetoableChangeListener( OUString("INT16"), x2
);
1090 // Test multi property set listener
1092 Reference
< XPropertiesChangeListener
> x1( xPS_L
, UNO_QUERY
);
1093 Reference
< XVetoableChangeListener
> x2( xPS_L
, UNO_QUERY
);
1095 pPS
->addVetoableChangeListener( OUString("INT16") , x2
);
1096 Sequence
<OUString
> szPN( 4 );
1099 szPN
[2] = "Does not exist"; // must ne ignored by the addPropertiesChangeListener method
1101 pPS
->addPropertiesChangeListener( szPN
, x1
);
1104 Sequence
< Any
> aSeq( pPS_L
->nCount
);
1105 pPS_L
->nCurrent
= 0;
1106 pPS_L
->pExceptedListenerValues
= aSeq
.getArray();
1107 pPS_L
->pExceptedListenerValues
[0] <<= (sal_Int16
) 0; // old value vetoable
1108 pPS_L
->pExceptedListenerValues
[1] <<= (sal_Int16
) 22; // new value vetoable
1109 // INT32 is not constrained
1110 pPS_L
->pExceptedListenerValues
[2] <<= (sal_Int16
) 0; // old value bound
1111 pPS_L
->pExceptedListenerValues
[3] <<= (sal_Int16
) 22; // new value bound
1112 pPS_L
->pExceptedListenerValues
[4] <<= (sal_Int32
) 0; // old value bound
1113 pPS_L
->pExceptedListenerValues
[5] <<= (sal_Int32
) 44; // new value bound
1115 szPN
= Sequence
<OUString
>( 2 );
1118 Sequence
< Any
> aValues( 2 );
1119 aValues
.getArray()[0] <<= (sal_Int16
) 22;
1120 aValues
.getArray()[1] <<= (sal_Int16
) 44;
1121 pPS
->setPropertyValues( szPN
, aValues
);
1122 OSL_ENSURE( pPS_L
->nCount
== pPS_L
->nCurrent
, "not all listeners called" );
1124 //firePropertiesChangeEvent
1128 pPS_L
->nCurrent
= 0;
1129 pPS_L
->pExceptedListenerValues
[0] <<= (sal_Int16
) 8; // old value
1130 pPS_L
->pExceptedListenerValues
[1] <<= (sal_Int16
) 8; // new value
1131 pPS_L
->pExceptedListenerValues
[2] <<= (sal_Int32
) 5; // old value
1132 pPS_L
->pExceptedListenerValues
[3] <<= (sal_Int32
) 5; // new value
1133 pPS
->firePropertiesChangeEvent( szPN
, pPS_L
);
1134 OSL_ENSURE( pPS_L
->nCount
== pPS_L
->nCurrent
, "not all listeners called" );
1137 //vetoable exception with multible
1143 pPS_L
->nCurrent
= 0;
1144 pPS_L
->pExceptedListenerValues
[0] <<= (sal_Int16
) 0; // old value vetoable
1145 pPS_L
->pExceptedListenerValues
[1] <<= (sal_Int16
) 44; // new value vetoable
1146 pPS_L
->pExceptedListenerValues
[2] <<= (sal_Int16
) 0; // old value vetoable
1147 pPS_L
->pExceptedListenerValues
[3] <<= (sal_Int16
) 100; // new value vetoable
1151 aValues
.getArray()[0] <<= (sal_Int16
)44;
1152 aValues
.getArray()[1] <<= (sal_Int16
)100;
1153 pPS
->setPropertyValues( szPN
, aValues
);
1154 OSL_FAIL( "PropertySetHelper: exception not thrown" );
1156 catch ( PropertyVetoException
& /*e*/ )
1160 OSL_ENSURE( pPS_L
->nCount
== pPS_L
->nCurrent
, "not all listeners called" );
1161 pPS
->removePropertiesChangeListener( x1
);
1162 pPS
->removeVetoableChangeListener( OUString("INT16"), x2
);
1167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */