xref: /AOO41X/main/cppuhelper/test/testcontainer.cxx (revision 9d7e27acf3441a88e7e2e9d0bd0e0c145f24ac0d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppuhelper.hxx"
26 #include <osl/mutex.hxx>
27 
28 #include <cppuhelper/interfacecontainer.hxx>
29 #include <cppuhelper/implbase1.hxx>
30 
31 #include <com/sun/star/beans/XVetoableChangeListener.hpp>
32 
33 using namespace ::cppu;
34 using namespace ::osl;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::uno;
38 
39 
40 class TestListener : public WeakImplHelper1< XVetoableChangeListener >
41 {
42     // Methods
disposing(const::com::sun::star::lang::EventObject &)43     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& /*Source*/ ) throw(::com::sun::star::uno::RuntimeException)
44         {
45 
46         }
47 
vetoableChange(const::com::sun::star::beans::PropertyChangeEvent &)48     virtual void SAL_CALL vetoableChange( const ::com::sun::star::beans::PropertyChangeEvent& /*aEvent*/ )
49        throw(::com::sun::star::beans::PropertyVetoException, ::com::sun::star::uno::RuntimeException)
50         {
51 
52         }
53 
54 };
55 
test_interfacecontainer()56 void test_interfacecontainer()
57 {
58     Mutex mutex;
59 
60     {
61         OInterfaceContainerHelper helper( mutex );
62 
63         Reference< XVetoableChangeListener > r1 = new TestListener();
64         Reference< XVetoableChangeListener > r2 = new TestListener();
65         Reference< XVetoableChangeListener > r3 = new TestListener();
66 
67         helper.addInterface( r1 );
68         helper.addInterface( r2 );
69         helper.addInterface( r3 );
70 
71         helper.disposeAndClear( EventObject() );
72     }
73 
74     {
75         OInterfaceContainerHelper helper( mutex );
76 
77         Reference< XVetoableChangeListener > r1 = new TestListener();
78         Reference< XVetoableChangeListener > r2 = new TestListener();
79         Reference< XVetoableChangeListener > r3 = new TestListener();
80 
81         helper.addInterface( r1 );
82         helper.addInterface( r2 );
83         helper.addInterface( r3 );
84 
85         OInterfaceIteratorHelper iterator( helper );
86 
87         while( iterator.hasMoreElements() )
88             ((XVetoableChangeListener*)iterator.next())->vetoableChange( PropertyChangeEvent() );
89 
90         helper.disposeAndClear( EventObject() );
91     }
92 
93     {
94         OInterfaceContainerHelper helper( mutex );
95 
96         Reference< XVetoableChangeListener > r1 = new TestListener();
97         Reference< XVetoableChangeListener > r2 = new TestListener();
98         Reference< XVetoableChangeListener > r3 = new TestListener();
99 
100         helper.addInterface( r1 );
101         helper.addInterface( r2 );
102         helper.addInterface( r3 );
103 
104         OInterfaceIteratorHelper iterator( helper );
105 
106         ((XVetoableChangeListener*)iterator.next())->vetoableChange( PropertyChangeEvent() );
107         iterator.remove();
108         ((XVetoableChangeListener*)iterator.next())->vetoableChange( PropertyChangeEvent() );
109         iterator.remove();
110         ((XVetoableChangeListener*)iterator.next())->vetoableChange( PropertyChangeEvent() );
111         iterator.remove();
112 
113         OSL_ASSERT( helper.getLength() == 0 );
114         helper.disposeAndClear( EventObject() );
115     }
116 
117     {
118         OInterfaceContainerHelper helper( mutex );
119 
120         Reference< XVetoableChangeListener > r1 = new TestListener();
121         Reference< XVetoableChangeListener > r2 = new TestListener();
122         Reference< XVetoableChangeListener > r3 = new TestListener();
123 
124         helper.addInterface( r1 );
125         helper.addInterface( r2 );
126         helper.addInterface( r3 );
127 
128         {
129             OInterfaceIteratorHelper iterator( helper );
130             while( iterator.hasMoreElements() )
131             {
132                 Reference< XVetoableChangeListener > r = ((XVetoableChangeListener*)iterator.next());
133                 if( r == r1 )
134                     iterator.remove();
135             }
136         }
137         OSL_ASSERT( helper.getLength() == 2 );
138         {
139             OInterfaceIteratorHelper iterator( helper );
140             while( iterator.hasMoreElements() )
141             {
142                 Reference< XVetoableChangeListener > r = ((XVetoableChangeListener*)iterator.next());
143                 OSL_ASSERT( r != r1 && ( r == r2 || r == r3 ) );
144             }
145         }
146 
147         helper.disposeAndClear( EventObject() );
148     }
149 }
150