update emoji autocorrect entries from po-files
[LibreOffice.git] / cppuhelper / test / testpropshlp.cxx
blob64ea895db1b2edaa96d1a49929ddef80cb91c9eb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #if !defined(OSL_DEBUG_LEVEL) || OSL_DEBUG_LEVEL == 0
21 # undef OSL_DEBUG_LEVEL
22 # define OSL_DEBUG_LEVEL 2
23 #endif
26 #include <osl/mutex.hxx>
27 #include <osl/diagnose.h>
29 #include <cppuhelper/propshlp.hxx>
30 #include <cppuhelper/weak.hxx>
32 #include <cppuhelper/proptypehlp.hxx>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <cppuhelper/implbase3.hxx>
39 using namespace ::cppu;
40 using namespace ::osl;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::lang;
45 static Property * getPropertyTable1()
47 static Property *pTable = 0;
49 if( ! pTable ) {
50 MutexGuard guard( Mutex::getGlobalMutex() );
51 if( ! pTable ) {
52 static Property aTable[] =
54 Property( OUString("a"), 0, cppu::UnoType<OUString>::get() ,
55 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //OUString
56 Property( OUString("b"), 1, cppu::UnoType<cppu::UnoCharType>::get() ,
57 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //Char
58 Property( OUString("c"), 2, cppu::UnoType<sal_Int32>::get(),
59 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //sal_Int32
60 Property( OUString("d"), 5, cppu::UnoType<double>::get() ,
61 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //double
62 Property( OUString("e"), 7, cppu::UnoType<bool>::get(),
63 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), //BOOL
64 Property( OUString("f"), 8, cppu::UnoType<Any>::get() ,
65 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) //Any
67 pTable = aTable;
70 return pTable;
74 static Property * getPropertyTable2()
76 static Property *pTable = 0;
78 if( ! pTable ) {
79 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
80 if( ! pTable ) {
81 static Property aTable[] =
83 Property( OUString("f"), 8, cppu::UnoType<Any>::get() ,
84 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Any
85 Property( OUString("b"), 1, cppu::UnoType<cppu::UnoCharType>::get(),
86 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
87 Property( OUString("a"), 0, cppu::UnoType<OUString>::get(),
88 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // OUString
89 Property( OUString("d"), 5, cppu::UnoType<double>::get() ,
90 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Double
91 Property( OUString("c"), 2, cppu::UnoType<sal_Int32>::get(),
92 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // sal_Int32
93 Property( OUString("e"), 7, cppu::UnoType<bool>::get(),
94 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // Bool
96 pTable = aTable;
99 return pTable;
102 static Property * getPropertyTable3()
104 static Property *pTable = 0;
106 if( ! pTable ) {
107 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
108 if( ! pTable ) {
109 static Property aTable[] =
111 Property( OUString("b"), 1, cppu::UnoType<cppu::UnoCharType>::get(),
112 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
113 Property( OUString("f"), 8, cppu::UnoType<Any>::get() ,
114 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // any
115 Property( OUString("a"), 0, cppu::UnoType<OUString>::get(),
116 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // OUString
118 pTable = aTable;
121 return pTable;
125 static Property * getPropertyTable4()
127 static Property *pTable = 0;
129 if( ! pTable ) {
130 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
131 if( ! pTable ) {
132 static Property aTable[] =
134 Property( OUString("a"), 0, cppu::UnoType<OUString>::get(),
135 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // OUString
136 Property( OUString("b"), 1, cppu::UnoType<cppu::UnoCharType>::get(),
137 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ), // Char
138 Property( OUString("f"), 2, cppu::UnoType<Any>::get() ,
139 PropertyAttribute::READONLY | PropertyAttribute::MAYBEVOID ) // Any
141 pTable = aTable;
144 return pTable;
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" );
174 // Test sorting
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" );
192 // Test sorting
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 );
248 int i;
249 for( i = 0; i < 6; i++ )
251 sal_Int16 nAttributes;
252 OUString aPropName;
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" );
259 // optimized table
260 OPropertyArrayHelper a4( getPropertyTable4(), 3 );
261 for( i = 0; i < 3; i++ )
263 sal_Int16 nAttributes;
264 OUString aPropName;
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" );
273 // Test fillHandles
275 OPropertyArrayHelper a1( getPropertyTable1(), 6 );
276 Sequence< OUString > aS( 4 );
277 sal_Int32 Handles[4];
278 // muss sortiert sein
279 aS[0] = "a";
280 aS[1] = "d";
281 aS[2] = "f";
282 aS[3] = "t";
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
300 Mutex aMutex;
302 class test_OPropertySetHelper :
303 public MutexContainer,
304 public OBroadcastHelper ,
305 public OPropertySetHelper,
306 public OWeakObject
308 public:
310 test_OPropertySetHelper( Property * p, sal_Int32 n )
311 : MutexContainer()
312 , OBroadcastHelper( ((MutexContainer *)this)->aMutex )
313 // , OPropertySetHelper( *(static_cast< OBroadcastHelper * >(this)))
314 // MSCI 4 bug ! :
315 // OBroadcastHelper == OBroadcastHelperVar<OMultiTypeInterfaceContainerHelper>
316 , OPropertySetHelper(
317 *(static_cast< OBroadcastHelper * >(this)))
318 , bBOOL( sal_False )
319 , nINT16( 0 )
320 , nINT32( 0 )
321 , pBasicProps( p )
322 , nPropCount( n )
327 ~test_OPropertySetHelper()
331 void dispose()
333 // see comphlp.cxx
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;
343 if( bDoDispose )
345 disposing();
346 EventObject aEvt;
347 aEvt.Source = Reference < XInterface > ( (static_cast< OWeakObject * >(this)) );
349 rBHelper.aLC.disposeAndClear( aEvt );
350 rBHelper.bDisposed = sal_True;
351 rBHelper.bInDispose = sal_False;
355 // XInterface
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(); }
366 // XPropertySet
367 Reference < XPropertySetInfo > SAL_CALL getPropertySetInfo()throw(RuntimeException);
369 using OPropertySetHelper::getFastPropertyValue;
371 sal_Bool bBOOL;
372 sal_Int16 nINT16;
373 sal_Int32 nINT32;
374 Property * pBasicProps;
375 sal_Int32 nPropCount;
376 protected:
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;
404 if( ! pTable ) {
405 MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
406 if( ! pTable ) {
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;
419 return pTable;
424 // test_OPropertySetHelper_Listener
426 class test_OPropertySetHelper_Listener
427 : public WeakImplHelper3< XPropertyChangeListener, XPropertiesChangeListener, XVetoableChangeListener >
429 public:
430 sal_Int32 nDisposing;
431 Mutex aMutex;
432 Any * pExceptedListenerValues;
433 sal_Int32 nCount;
434 sal_Int32 nCurrent;
436 test_OPropertySetHelper_Listener( Any * p = 0, sal_Int32 n = 0 )
437 : nDisposing( 0 )
438 , pExceptedListenerValues( p )
439 , nCount( n )
440 , nCurrent( 0 )
443 ~test_OPropertySetHelper_Listener()
447 sal_Int32 getRefCount() const
448 { return m_refCount; }
450 // XEventListener
451 void SAL_CALL disposing(const EventObject& /*evt*/) throw ( RuntimeException)
453 MutexGuard aGuard( aMutex );
454 nDisposing++;
457 // XPropertyChangeListener
458 void SAL_CALL propertyChange(const PropertyChangeEvent& evt) throw (RuntimeException)
460 if( !pExceptedListenerValues )
461 return;
462 MutexGuard aGuard( aMutex );
463 OSL_ENSURE( nCurrent +1 < nCount, "PropertySetHelper: too many listener calls" );
465 switch( evt.PropertyHandle )
467 case PROPERTY_BOOL:
469 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
470 OSL_ENSURE( evt.PropertyName == "BOOL", "PropertySetHelper: wrong name" );
472 break;
474 case PROPERTY_INT16:
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" );
491 break;
493 case PROPERTY_INT32:
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" );
507 break;
509 default:
510 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
512 nCurrent += 2;
515 // XVetoableChangeListener
516 void SAL_CALL vetoableChange(const PropertyChangeEvent& evt) throw (PropertyVetoException, RuntimeException)
518 if( !pExceptedListenerValues )
519 return;
520 MutexGuard aGuard( aMutex );
521 OSL_ENSURE( nCurrent +1 < nCount, "PropertySetHelper: too many listener calls" );
523 switch( evt.PropertyHandle )
525 case PROPERTY_BOOL:
527 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
528 OSL_ENSURE( evt.PropertyName == "BOOL", "PropertySetHelper: wrong name" );
530 break;
532 case PROPERTY_INT16:
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 )
548 nCurrent += 2;
549 throw PropertyVetoException();
552 break;
554 case PROPERTY_INT32:
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" );
567 break;
569 default:
570 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
572 nCurrent += 2;
575 // XPropertiesChangeListener
576 void SAL_CALL propertiesChange(const Sequence< PropertyChangeEvent >& evtSeq) throw (RuntimeException)
578 if( !pExceptedListenerValues )
579 return;
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 )
588 case PROPERTY_BOOL:
590 OSL_FAIL( "PropertySetHelper: BOOL cannot change" );
591 OSL_ENSURE( evt.PropertyName == "BOOL", "PropertySetHelper: wrong name" );
593 break;
595 case PROPERTY_INT16:
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" );
609 break;
611 case PROPERTY_INT32:
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" );
625 break;
627 default:
628 OSL_FAIL( "XPropeSetHelper: invalid property handle" );
630 nCurrent += 2;
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 );
642 return aInfo;
645 // XPropertySet
646 Reference < XPropertySetInfo > test_OPropertySetHelper::getPropertySetInfo()
647 throw(RuntimeException)
649 // no multi thread protection
650 static Reference < XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
651 return xInfo;
654 // Return sal_True, value changed
655 sal_Bool test_OPropertySetHelper::convertFastPropertyValue
657 Any & rConvertedValue,
658 Any & rOldValue,
659 sal_Int32 nHandle,
660 const Any& rValue
661 )throw(IllegalArgumentException)
663 switch( nHandle )
665 case PROPERTY_BOOL:
667 sal_Bool b;
668 convertPropertyValue( b , rValue );
669 if( b != bBOOL )
672 rConvertedValue.setValue( &b , cppu::UnoType<bool>::get() );
673 rOldValue.setValue( & bBOOL , cppu::UnoType<bool>::get() );
674 return sal_True;
676 else
677 return sal_False;
680 case PROPERTY_INT16:
682 sal_Int16 n16;
683 convertPropertyValue( n16 , rValue );
685 if( n16 != nINT16 )
687 rConvertedValue <<= n16;
688 rOldValue <<= nINT16;
689 return sal_True;
691 else
692 return sal_False;
695 case PROPERTY_INT32:
697 sal_Int32 n32;
698 convertPropertyValue( n32 , rValue );
699 if( n32 != nINT32 )
701 rConvertedValue <<= n32;
702 rOldValue <<= nINT32;
703 return sal_True;
705 else
706 return sal_False;
709 default:
710 OSL_ENSURE( nHandle == -1, "invalid property handle" );
711 return sal_False;
716 * only set the value.
718 void test_OPropertySetHelper::setFastPropertyValue_NoBroadcast
720 sal_Int32 nHandle,
721 const Any& rValue
722 )throw(RuntimeException)
724 switch( nHandle )
726 case PROPERTY_BOOL:
727 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_BOOLEAN, "invalid type" );
728 bBOOL = *((sal_Bool*)rValue.getValue());
729 break;
731 case PROPERTY_INT16:
732 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_SHORT, "invalid type" );
733 rValue >>= nINT16;
734 break;
736 case PROPERTY_INT32:
737 OSL_ENSURE( rValue.getValueType().getTypeClass() == TypeClass_LONG, "invalid type" );
738 rValue >>= nINT32;
739 break;
741 default:
742 OSL_ENSURE( nHandle == -1, "invalid property handle" );
747 void test_OPropertySetHelper::getFastPropertyValue( Any & rRet, sal_Int32 nHandle ) const
748 throw(RuntimeException)
750 switch( nHandle )
752 case PROPERTY_BOOL:
753 rRet.setValue( &bBOOL , cppu::UnoType<bool>::get() );
754 break;
756 case PROPERTY_INT16:
757 rRet <<= nINT16;
758 break;
760 case PROPERTY_INT32:
761 rRet <<= nINT32;
762 break;
764 default:
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 );
808 szPN[0] = "BOOL";
809 szPN[1] = "INT32";
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" );
846 // Test disposing
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 );
853 szPN[0] = "BOOL";
854 szPN[1] = "INT32";
855 pPS->addPropertiesChangeListener( szPN, x1 );
856 szPN = Sequence<OUString>();
857 pPS->addPropertiesChangeListener( szPN, x1 );
858 pPS->addVetoableChangeListener( OUString("INT16"), x2 );
859 pPS->dispose();
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;
873 Any aBool;
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;
886 Any aBool;
887 aBool.setValue( &b , cppu::UnoType<bool>::get() );
888 // BOOL i s0
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;
900 Any aBool;
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;
912 Any aBool;
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
924 Any aBool;
925 aBool = xPS->getPropertyValue("Does not exist");
926 OSL_FAIL( "PropertySetHelper: exception not thrown" );
928 catch( UnknownPropertyException & /*e*/ )
934 Any aBool;
935 aBool = ((XFastPropertySet *)pPS)->getFastPropertyValue( 3 );
936 OSL_FAIL( "PropertySetHelper: exception not thrown" );
938 catch( UnknownPropertyException & /*e*/ )
942 // set property with invalid type
945 Any aBool;
946 xPS->setPropertyValue("INT32", aBool );
947 OSL_FAIL( "PropertySetHelper: exception not thrown" );
949 catch( IllegalArgumentException & /*e*/ )
955 Any aBool;
956 pPS->setFastPropertyValue( PROPERTY_INT32, aBool );
957 OSL_FAIL( "PropertySetHelper: exception not thrown" );
959 catch( IllegalArgumentException & /*e*/ )
963 // narrowing conversion is not allowed!
966 Any aINT32;
967 aINT32 <<= (sal_Int32 ) 16;
968 xPS->setPropertyValue("INT16", aINT32 );
969 OSL_FAIL( "PropertySetHelper: exception not thrown" );
971 catch( IllegalArgumentException & /*e*/ )
978 Any aINT32;
979 aINT32 <<= (sal_Int32) 16;
980 pPS->setFastPropertyValue( PROPERTY_INT16, aINT32 );
981 OSL_FAIL( "PropertySetHelper: exception not thrown" );
983 catch( IllegalArgumentException & /*e*/ )
988 Any aValue;
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" );
997 sal_Int16 n16(0);
998 aValue <<=(sal_Int16)22;
999 xPS->setPropertyValue("INT16", aValue );
1000 aValue = xPS->getPropertyValue("INT16");
1001 aValue >>= n16;
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 );
1007 aValue >>= n16;
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");
1014 sal_Int32 n32(0);
1015 aValue >>= n32;
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 );
1020 aValue >>= n32;
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" );
1037 pPS->nINT32 = 0;
1038 pPS->nINT16 = 0;
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 );
1047 pPS_L->nCount = 10;
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
1064 Any aValue;
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" );
1081 pPS->nINT32 = 0;
1082 pPS->nINT16 = 0;
1083 pPS_L->nCount = 0;
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 );
1097 szPN[0] = "BOOL";
1098 szPN[1] = "INT32";
1099 szPN[2] = "Does not exist"; // must ne ignored by the addPropertiesChangeListener method
1100 szPN[3] = "INT16";
1101 pPS->addPropertiesChangeListener( szPN, x1 );
1103 pPS_L->nCount = 6;
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 );
1116 szPN[0] = "INT16";
1117 szPN[1] = "INT32";
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
1125 pPS->nINT16 = 8;
1126 pPS->nINT32 = 5;
1127 pPS_L->nCount = 4;
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
1138 szPN[0] = "INT16";
1139 szPN[1] = "INT16";
1140 pPS->nINT32 = 0;
1141 pPS->nINT16 = 0;
1142 pPS_L->nCount = 4;
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: */