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