Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / cppuhelper / qa / ifcontainer / cppu_ifcontainer.cxx
blob43a9d87df927868159b44d8eaa7793556f9a566e
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 #include <sal/types.h>
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/plugin/TestPlugIn.h>
26 #include <com/sun/star/lang/XEventListener.hpp>
27 #include <cppuhelper/interfacecontainer.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <cppuhelper/propshlp.hxx>
31 using namespace com::sun::star;
32 using namespace com::sun::star::uno;
33 using namespace com::sun::star::lang;
35 struct ContainerStats {
36 int m_nAlive;
37 int m_nDisposed;
38 ContainerStats() : m_nAlive(0), m_nDisposed(0) {}
41 class ContainerListener : public ::cppu::WeakImplHelper< XEventListener >
43 ContainerStats *m_pStats;
44 public:
45 explicit ContainerListener(ContainerStats *pStats)
46 : m_pStats(pStats) { m_pStats->m_nAlive++; }
47 virtual ~ContainerListener() override { m_pStats->m_nAlive--; }
48 virtual void SAL_CALL disposing( const EventObject& ) override
50 m_pStats->m_nDisposed++;
54 namespace cppu_ifcontainer
56 class IfTest : public CppUnit::TestFixture
58 osl::Mutex m_aGuard;
59 static const int nTests = 10;
60 public:
61 void testCreateDispose()
63 ContainerStats aStats;
64 cppu::OInterfaceContainerHelper *pContainer;
66 pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
68 CPPUNIT_ASSERT_EQUAL_MESSAGE("Empty container not empty",
69 static_cast<sal_Int32>(0), pContainer->getLength());
71 int i;
72 for (i = 0; i < nTests; i++)
74 Reference<XEventListener> xRef = new ContainerListener(&aStats);
75 int nNewLen = pContainer->addInterface(xRef);
77 CPPUNIT_ASSERT_EQUAL_MESSAGE("addition length mismatch",
78 i + 1, nNewLen);
79 CPPUNIT_ASSERT_EQUAL_MESSAGE("addition length mismatch",
80 static_cast<sal_Int32>(i + 1), pContainer->getLength());
82 CPPUNIT_ASSERT_MESSAGE("alive count mismatch",
83 bool(aStats.m_nAlive == nTests));
85 EventObject aObj;
86 pContainer->disposeAndClear(aObj);
88 CPPUNIT_ASSERT_MESSAGE("dispose count mismatch",
89 bool(aStats.m_nDisposed == nTests));
90 CPPUNIT_ASSERT_EQUAL_MESSAGE("leaked container left alive",
91 0, aStats.m_nAlive);
93 delete pContainer;
96 void testEnumerate()
98 int i;
99 ContainerStats aStats;
100 cppu::OInterfaceContainerHelper *pContainer;
101 pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
103 std::vector< Reference< XEventListener > > aListeners;
104 for (i = 0; i < nTests; i++)
106 Reference<XEventListener> xRef = new ContainerListener(&aStats);
107 pContainer->addInterface(xRef);
108 aListeners.push_back(xRef);
110 Sequence< Reference< XInterface > > aElements = pContainer->getElements();
112 CPPUNIT_ASSERT_MESSAGE("query contents",
113 bool(static_cast<int>(aElements.getLength()) == nTests));
114 if (static_cast<int>(aElements.getLength()) == nTests)
116 for (i = 0; i < nTests; i++)
118 CPPUNIT_ASSERT_MESSAGE("mismatching elements",
119 bool(aElements[i] == aListeners[i]));
122 pContainer->clear();
124 CPPUNIT_ASSERT_EQUAL_MESSAGE("non-empty container post clear",
125 static_cast<sal_Int32>(0), pContainer->getLength());
126 delete pContainer;
129 template < typename ContainerType, typename ContainedType >
130 void doContainerTest(const ContainedType *pTypes)
132 ContainerStats aStats;
133 ContainerType *pContainer;
134 pContainer = new ContainerType(m_aGuard);
136 int i;
137 Reference<XEventListener> xRefs[nTests * 2];
139 // add these interfaces
140 for (i = 0; i < nTests * 2; i++)
142 xRefs[i] = new ContainerListener(&aStats);
143 pContainer->addInterface(pTypes[i / 2], xRefs[i]);
146 // check it is all there
147 for (i = 0; i < nTests; i++)
149 cppu::OInterfaceContainerHelper *pHelper;
151 pHelper = pContainer->getContainer(pTypes[i]);
153 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != nullptr);
154 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
155 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong num elements", static_cast<sal_Int32>(2), aSeq.getLength());
156 CPPUNIT_ASSERT_MESSAGE("match", bool(aSeq[0] == xRefs[i*2]));
157 CPPUNIT_ASSERT_MESSAGE("match", bool(aSeq[1] == xRefs[i*2+1]));
160 // remove every other interface
161 for (i = 0; i < nTests; i++)
162 pContainer->removeInterface(pTypes[i], xRefs[i*2+1]);
164 // check it is half there
165 for (i = 0; i < nTests; i++)
167 cppu::OInterfaceContainerHelper *pHelper;
169 pHelper = pContainer->getContainer(pTypes[i]);
171 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != nullptr);
172 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
173 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong num elements", static_cast<sal_Int32>(1), aSeq.getLength());
174 CPPUNIT_ASSERT_MESSAGE("match", bool(aSeq[0] == xRefs[i*2]));
177 // remove the 1st half of the rest
178 for (i = 0; i < nTests / 2; i++)
179 pContainer->removeInterface(pTypes[i], xRefs[i*2]);
181 // check it is half there
182 for (i = 0; i < nTests / 2; i++)
184 cppu::OInterfaceContainerHelper *pHelper;
186 pHelper = pContainer->getContainer(pTypes[i]);
187 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != nullptr);
188 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
189 CPPUNIT_ASSERT_EQUAL_MESSAGE("wrong num elements", static_cast<sal_Int32>(0), aSeq.getLength());
192 delete pContainer;
195 void testOMultiTypeInterfaceContainerHelper()
197 uno::Type pTypes[nTests] =
199 ::cppu::UnoType< bool >::get(),
200 ::cppu::UnoType< float >::get(),
201 ::cppu::UnoType< double >::get(),
202 ::cppu::UnoType< ::sal_uInt64 >::get(),
203 ::cppu::UnoType< ::sal_Int64 >::get(),
204 ::cppu::UnoType< ::sal_uInt32 >::get(),
205 ::cppu::UnoType< ::sal_Int32 >::get(),
206 ::cppu::UnoType< ::sal_Int16 >::get(),
207 ::cppu::UnoType< OUString >::get(),
208 ::cppu::UnoType< ::sal_Int8 >::get()
210 doContainerTest< cppu::OMultiTypeInterfaceContainerHelper,
211 uno::Type> (pTypes);
214 void testOMultiTypeInterfaceContainerHelperInt32()
216 sal_Int32 const pTypes[nTests] =
221 256,
222 1024,
229 doContainerTest< cppu::OMultiTypeInterfaceContainerHelperInt32, sal_Int32> (pTypes);
232 void testOMultiTypeInterfaceContainerHelperVar()
234 typedef cppu::OMultiTypeInterfaceContainerHelperVar<
235 char const *, void, rtl::CStringEqual> StrContainer;
237 const char * const pTypes[nTests] =
239 "this_is", "such", "fun", "writing", "unit", "tests", "when", "it", "works", "anyway"
241 doContainerTest< StrContainer, const char *> (pTypes);
244 // Automatic registration code
245 CPPUNIT_TEST_SUITE(IfTest);
246 CPPUNIT_TEST(testCreateDispose);
247 CPPUNIT_TEST(testEnumerate);
248 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelper);
249 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperVar);
250 CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperInt32);
251 CPPUNIT_TEST_SUITE_END();
253 } // namespace cppu_ifcontainer
255 CPPUNIT_TEST_SUITE_REGISTRATION(cppu_ifcontainer::IfTest);
257 CPPUNIT_PLUGIN_IMPLEMENT();
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */