Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <sal/types.h>
30 : :
31 : : #include <string.h>
32 : : #include <cppunit/TestFixture.h>
33 : : #include <cppunit/extensions/HelperMacros.h>
34 : : #include <cppunit/plugin/TestPlugIn.h>
35 : :
36 : : #include "com/sun/star/lang/XEventListener.hpp"
37 : : #include "cppuhelper/interfacecontainer.hxx"
38 : : #include "cppuhelper/queryinterface.hxx"
39 : : #include "cppuhelper/implbase1.hxx"
40 : : #include "cppuhelper/propshlp.hxx"
41 : :
42 : : using namespace com::sun::star;
43 : : using namespace com::sun::star::uno;
44 : : using namespace com::sun::star::lang;
45 : :
46 : :
47 : : struct equalStr
48 : : {
49 : 885 : bool operator()(
50 : : const char * const &rA,
51 : : const char * const &rB) const
52 : 885 : { return !strcmp(rA, rB); }
53 : : };
54 : : struct hashStr
55 : : {
56 : : size_t operator()( const char * &rName ) const
57 : : {
58 : : return rtl::OString(rName).hashCode();
59 : : }
60 : : };
61 : :
62 : : class ContainerListener;
63 : :
64 : : struct ContainerStats {
65 : : int m_nAlive;
66 : : int m_nDisposed;
67 : 15 : ContainerStats() : m_nAlive(0), m_nDisposed(0) {}
68 : : };
69 : :
70 : : class ContainerListener : public ::cppu::WeakImplHelper1< XEventListener >
71 : : {
72 : : ContainerStats *m_pStats;
73 : : public:
74 : 240 : ContainerListener(ContainerStats *pStats)
75 : 240 : : m_pStats(pStats) { m_pStats->m_nAlive++; }
76 [ - + ]: 480 : virtual ~ContainerListener() { m_pStats->m_nAlive--; }
77 : 30 : virtual void SAL_CALL disposing( const EventObject& )
78 : : throw (RuntimeException)
79 : : {
80 : 30 : m_pStats->m_nDisposed++;
81 : 30 : }
82 : : };
83 : :
84 : : namespace cppu_ifcontainer
85 : : {
86 [ + - ][ - + ]: 45 : class IfTest : public CppUnit::TestFixture
[ + - ]
87 : : {
88 : : osl::Mutex m_aGuard;
89 : : static const int nTests = 10;
90 : : public:
91 : 3 : void testCreateDispose()
92 : : {
93 : 3 : ContainerStats aStats;
94 : : cppu::OInterfaceContainerHelper *pContainer;
95 : :
96 [ + - ]: 3 : pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
97 : :
98 [ + - ][ + - ]: 6 : CPPUNIT_ASSERT_MESSAGE("Empty container not empty",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
99 [ + - ]: 3 : pContainer->getLength() == 0);
100 : :
101 : : int i;
102 [ + + ]: 33 : for (i = 0; i < nTests; i++)
103 : : {
104 [ + - ][ + - ]: 30 : Reference<XEventListener> xRef = new ContainerListener(&aStats);
[ + - ]
105 [ + - ]: 30 : int nNewLen = pContainer->addInterface(xRef);
106 : :
107 [ + - ][ + - ]: 60 : CPPUNIT_ASSERT_MESSAGE("addition length mismatch",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
108 [ + - ]: 30 : nNewLen == i + 1);
109 [ + - ][ + - ]: 60 : CPPUNIT_ASSERT_MESSAGE("addition length mismatch",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
110 [ + - ]: 30 : pContainer->getLength() == i + 1);
111 : 30 : }
112 [ + - ][ + - ]: 6 : CPPUNIT_ASSERT_MESSAGE("alive count mismatch",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
113 [ + - ]: 3 : aStats.m_nAlive == nTests);
114 : :
115 [ + - ]: 3 : EventObject aObj;
116 [ + - ]: 3 : pContainer->disposeAndClear(aObj);
117 : :
118 [ + - ][ + - ]: 6 : CPPUNIT_ASSERT_MESSAGE("dispose count mismatch",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
119 [ + - ]: 3 : aStats.m_nDisposed == nTests);
120 [ + - ][ + - ]: 6 : CPPUNIT_ASSERT_MESSAGE("leaked container left alive",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
121 [ + - ]: 3 : aStats.m_nAlive == 0);
122 : :
123 [ + - ][ + - ]: 3 : delete pContainer;
[ + - ]
124 : 3 : }
125 : :
126 : 3 : void testEnumerate()
127 : : {
128 : : int i;
129 : 3 : ContainerStats aStats;
130 : : cppu::OInterfaceContainerHelper *pContainer;
131 [ + - ]: 3 : pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
132 : :
133 [ + - ]: 3 : std::vector< Reference< XEventListener > > aListeners;
134 [ + + ]: 33 : for (i = 0; i < nTests; i++)
135 : : {
136 [ + - ][ + - ]: 30 : Reference<XEventListener> xRef = new ContainerListener(&aStats);
[ + - ]
137 [ + - ]: 30 : pContainer->addInterface(xRef);
138 [ + - ]: 30 : aListeners.push_back(xRef);
139 : 30 : }
140 [ + - ]: 3 : Sequence< Reference< XInterface > > aElements;
141 [ + - ][ + - ]: 3 : aElements = pContainer->getElements();
[ + - ]
142 : :
143 [ + - ][ + - ]: 6 : CPPUNIT_ASSERT_MESSAGE("query contents",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
144 [ + - ]: 3 : (int)aElements.getLength() == nTests);
145 [ + - ]: 3 : if ((int)aElements.getLength() == nTests)
146 : : {
147 [ + + ]: 33 : for (i = 0; i < nTests; i++)
148 : : {
149 [ + - ][ + - ]: 60 : CPPUNIT_ASSERT_MESSAGE("mismatching elements",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
150 [ + - ]: 30 : aElements[i] == aListeners[i]);
151 : : }
152 : : }
153 [ + - ]: 3 : pContainer->clear();
154 : :
155 [ + - ][ + - ]: 6 : CPPUNIT_ASSERT_MESSAGE("non-empty container post clear",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
156 [ + - ]: 3 : pContainer->getLength() == 0);
157 [ + - ][ + - ]: 3 : delete pContainer;
[ + - ]
158 : 3 : }
159 : :
160 : : template < typename ContainerType, typename ContainedType >
161 : 9 : void doContainerTest(const ContainedType *pTypes)
162 : : {
163 : 9 : ContainerStats aStats;
164 : : ContainerType *pContainer;
165 [ + - + - : 9 : pContainer = new ContainerType(m_aGuard);
+ - ]
166 : :
167 : : int i;
168 [ + + ][ + + ]: 369 : Reference<XEventListener> xRefs[nTests * 2];
[ + + ]
169 : :
170 : : // add these interfaces
171 [ + + ][ + + ]: 189 : for (i = 0; i < nTests * 2; i++)
[ + + ]
172 : : {
173 [ + - ][ + - ]: 180 : xRefs[i] = new ContainerListener(&aStats);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
174 [ + - ][ + - ]: 180 : pContainer->addInterface(pTypes[i / 2], xRefs[i]);
[ + - ]
175 : : }
176 : :
177 : : // check it is all there
178 [ + + ][ + + ]: 99 : for (i = 0; i < nTests; i++)
[ + + ]
179 : : {
180 : : cppu::OInterfaceContainerHelper *pHelper;
181 : :
182 [ + - ][ + - ]: 90 : pHelper = pContainer->getContainer(pTypes[i]);
[ + - ]
183 : :
184 [ + - ][ + - ]: 90 : CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
185 [ + - + - : 90 : Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
+ - ]
186 [ + - ][ + - ]: 90 : CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 2);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
187 [ + - ][ + - ]: 90 : CPPUNIT_ASSERT_MESSAGE("match", aSeq[0] == xRefs[i*2]);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
188 [ + - ][ + - ]: 90 : CPPUNIT_ASSERT_MESSAGE("match", aSeq[1] == xRefs[i*2+1]);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
189 : : }
190 : :
191 : : // remove every other interface
192 [ + + ][ + + ]: 99 : for (i = 0; i < nTests; i++)
[ + + ]
193 [ + - ][ + - ]: 90 : pContainer->removeInterface(pTypes[i], xRefs[i*2+1]);
[ + - ]
194 : :
195 : : // check it is half there
196 [ + + ][ + + ]: 99 : for (i = 0; i < nTests; i++)
[ + + ]
197 : : {
198 : : cppu::OInterfaceContainerHelper *pHelper;
199 : :
200 [ + - ][ + - ]: 90 : pHelper = pContainer->getContainer(pTypes[i]);
[ + - ]
201 : :
202 [ + - ][ + - ]: 90 : CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
203 [ + - + - : 90 : Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
+ - ]
204 [ + - ][ + - ]: 90 : CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 1);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
205 [ + - ][ + - ]: 90 : CPPUNIT_ASSERT_MESSAGE("match", aSeq[0] == xRefs[i*2]);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
206 : : }
207 : :
208 : : // remove the 1st half of the rest
209 [ + + ][ + + ]: 54 : for (i = 0; i < nTests / 2; i++)
[ + + ]
210 [ + - ][ + - ]: 45 : pContainer->removeInterface(pTypes[i], xRefs[i*2]);
[ + - ]
211 : :
212 : : // check it is half there
213 [ + + ][ + + ]: 54 : for (i = 0; i < nTests / 2; i++)
[ + + ]
214 : : {
215 : : cppu::OInterfaceContainerHelper *pHelper;
216 : :
217 [ + - ][ + - ]: 45 : pHelper = pContainer->getContainer(pTypes[i]);
[ + - ]
218 [ + - ][ + - ]: 45 : CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
219 [ + - + - : 45 : Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
+ - ]
220 [ + - ][ + - ]: 45 : CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 0);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
221 : : }
222 : :
223 [ + - ][ + - ]: 189 : delete pContainer;
[ + + # # ]
[ + - ][ + - ]
[ + + # # ]
[ + - ][ + - ]
[ + + # # ]
224 : 9 : }
225 : :
226 : 3 : void testOMultiTypeInterfaceContainerHelper()
227 : : {
228 : : uno::Type pTypes[nTests] =
229 : : {
230 : 3 : ::cppu::UnoType< bool >::get(),
231 : 3 : ::cppu::UnoType< float >::get(),
232 : 3 : ::cppu::UnoType< double >::get(),
233 : 3 : ::cppu::UnoType< ::sal_uInt64 >::get(),
234 : 3 : ::cppu::UnoType< ::sal_Int64 >::get(),
235 : 3 : ::cppu::UnoType< ::sal_uInt32 >::get(),
236 : 3 : ::cppu::UnoType< ::sal_Int32 >::get(),
237 : 3 : ::cppu::UnoType< ::sal_Int16 >::get(),
238 : 3 : ::cppu::UnoType< ::rtl::OUString >::get(),
239 : 3 : ::cppu::UnoType< ::sal_Int8 >::get()
240 : 60 : };
241 : : doContainerTest< cppu::OMultiTypeInterfaceContainerHelper,
242 [ + + # # ]: 33 : uno::Type> (pTypes);
[ + - ]
243 : 3 : }
244 : :
245 : 3 : void testOMultiTypeInterfaceContainerHelperInt32()
246 : : {
247 : : sal_Int32 pTypes[nTests] =
248 : : {
249 : : 0,
250 : : -1,
251 : : 1,
252 : : 256,
253 : : 1024,
254 : : 3,
255 : : 7,
256 : : 8,
257 : : 9,
258 : : 10
259 : 3 : };
260 [ + - ]: 3 : doContainerTest< cppu::OMultiTypeInterfaceContainerHelperInt32, sal_Int32> (pTypes);
261 : 3 : }
262 : :
263 : 3 : void testOMultiTypeInterfaceContainerHelperVar()
264 : : {
265 : : typedef ::cppu::OMultiTypeInterfaceContainerHelperVar<
266 : : const char *,hashStr,equalStr> StrContainer;
267 : :
268 : : const char *pTypes[nTests] =
269 : : {
270 : : "this_is", "such", "fun", "writing", "unit", "tests", "when", "it", "works", "anyway"
271 : 3 : };
272 [ + - ]: 3 : doContainerTest< StrContainer, const char *> (pTypes);
273 : 3 : }
274 : :
275 : : // Automatic registration code
276 [ + - ][ + - ]: 6 : CPPUNIT_TEST_SUITE(IfTest);
[ + - ][ + - ]
[ # # ]
277 [ + - ][ + - ]: 3 : CPPUNIT_TEST(testCreateDispose);
[ + - ][ + - ]
[ + - ][ + - ]
278 [ + - ][ + - ]: 3 : CPPUNIT_TEST(testEnumerate);
[ + - ][ + - ]
[ + - ][ + - ]
279 [ + - ][ + - ]: 3 : CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelper);
[ + - ][ + - ]
[ + - ][ + - ]
280 [ + - ][ + - ]: 3 : CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperVar);
[ + - ][ + - ]
[ + - ][ + - ]
281 [ + - ][ + - ]: 3 : CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperInt32);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
282 [ + - ][ + - ]: 6 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
283 : : };
284 : : } // namespace cppu_ifcontainer
285 : :
286 : 3 : CPPUNIT_TEST_SUITE_REGISTRATION(cppu_ifcontainer::IfTest);
287 : :
288 [ + - ][ + - ]: 12 : CPPUNIT_PLUGIN_IMPLEMENT();
[ + - ][ + - ]
[ + - ][ # # ]
289 : :
290 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|