xref: /trunk/main/cppuhelper/qa/ifcontainer/cppu_ifcontainer.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
19d7e27acSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39d7e27acSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49d7e27acSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59d7e27acSAndrew Rist  * distributed with this work for additional information
69d7e27acSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79d7e27acSAndrew Rist  * to you under the Apache License, Version 2.0 (the
89d7e27acSAndrew Rist  * "License"); you may not use this file except in compliance
99d7e27acSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
119d7e27acSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
139d7e27acSAndrew Rist  * Unless required by applicable law or agreed to in writing,
149d7e27acSAndrew Rist  * software distributed under the License is distributed on an
159d7e27acSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169d7e27acSAndrew Rist  * KIND, either express or implied.  See the License for the
179d7e27acSAndrew Rist  * specific language governing permissions and limitations
189d7e27acSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
209d7e27acSAndrew Rist  *************************************************************/
219d7e27acSAndrew Rist 
224b7589a4SDamjan Jovanovic #include "precompiled_cppuhelper.hxx"
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "com/sun/star/lang/XEventListener.hpp"
25cdf0e10cSrcweir #include "cppuhelper/interfacecontainer.hxx"
26cdf0e10cSrcweir #include "cppuhelper/queryinterface.hxx"
27cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
28cdf0e10cSrcweir #include "cppuhelper/propshlp.hxx"
298269660fSDamjan Jovanovic #include "gtest/gtest.h"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace com::sun::star;
32cdf0e10cSrcweir using namespace com::sun::star::uno;
33cdf0e10cSrcweir using namespace com::sun::star::lang;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir class ContainerListener;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir struct ContainerStats {
38cdf0e10cSrcweir     int m_nAlive;
39cdf0e10cSrcweir     int m_nDisposed;
ContainerStatsContainerStats40cdf0e10cSrcweir     ContainerStats() : m_nAlive(0), m_nDisposed(0) {}
41cdf0e10cSrcweir };
42cdf0e10cSrcweir 
43cdf0e10cSrcweir class ContainerListener : public ::cppu::WeakImplHelper1< XEventListener >
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     ContainerStats *m_pStats;
46cdf0e10cSrcweir public:
ContainerListener(ContainerStats * pStats)47cdf0e10cSrcweir     ContainerListener(ContainerStats *pStats)
48cdf0e10cSrcweir         : m_pStats(pStats) { m_pStats->m_nAlive++; }
~ContainerListener()49cdf0e10cSrcweir     virtual ~ContainerListener() { m_pStats->m_nAlive--; }
disposing(const EventObject &)50cdf0e10cSrcweir     virtual void SAL_CALL disposing( const EventObject& )
51cdf0e10cSrcweir         throw (RuntimeException)
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         m_pStats->m_nDisposed++;
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir };
56cdf0e10cSrcweir 
57cdf0e10cSrcweir namespace cppu_ifcontainer
58cdf0e10cSrcweir {
598269660fSDamjan Jovanovic     class IfTest : public ::testing::Test
60cdf0e10cSrcweir     {
618269660fSDamjan Jovanovic     protected:
62cdf0e10cSrcweir         osl::Mutex m_aGuard;
63cdf0e10cSrcweir         static const int nTests = 10;
64cdf0e10cSrcweir     public:
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         template < typename ContainerType, typename ContainedType >
doContainerTest(const ContainedType * pTypes)67cdf0e10cSrcweir         void doContainerTest(const ContainedType *pTypes)
68cdf0e10cSrcweir         {
69cdf0e10cSrcweir             ContainerStats aStats;
70cdf0e10cSrcweir             ContainerType *pContainer;
71cdf0e10cSrcweir             pContainer = new ContainerType(m_aGuard);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir             int i;
74cdf0e10cSrcweir             Reference<XEventListener> xRefs[nTests * 2];
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             // add these interfaces
77cdf0e10cSrcweir             for (i = 0; i < nTests * 2; i++)
78cdf0e10cSrcweir             {
79cdf0e10cSrcweir                 xRefs[i] = new ContainerListener(&aStats);
80cdf0e10cSrcweir                 pContainer->addInterface(pTypes[i / 2], xRefs[i]);
81cdf0e10cSrcweir             }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir             // check it is all there
84cdf0e10cSrcweir             for (i = 0; i < nTests; i++)
85cdf0e10cSrcweir             {
86cdf0e10cSrcweir                 cppu::OInterfaceContainerHelper *pHelper;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir                 pHelper = pContainer->getContainer(pTypes[i]);
89cdf0e10cSrcweir 
908269660fSDamjan Jovanovic                 ASSERT_TRUE(pHelper != NULL) << "no helper";
91cdf0e10cSrcweir                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
928269660fSDamjan Jovanovic                 ASSERT_TRUE(aSeq.getLength() == 2) << "wrong num elements";
938269660fSDamjan Jovanovic                 ASSERT_TRUE(aSeq[0] == xRefs[i*2]) << "match";
948269660fSDamjan Jovanovic                 ASSERT_TRUE(aSeq[1] == xRefs[i*2+1]) << "match";
95cdf0e10cSrcweir             }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             // remove every other interface
98cdf0e10cSrcweir             for (i = 0; i < nTests; i++)
99cdf0e10cSrcweir                 pContainer->removeInterface(pTypes[i], xRefs[i*2+1]);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir             // check it is half there
102cdf0e10cSrcweir             for (i = 0; i < nTests; i++)
103cdf0e10cSrcweir             {
104cdf0e10cSrcweir                 cppu::OInterfaceContainerHelper *pHelper;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir                 pHelper = pContainer->getContainer(pTypes[i]);
107cdf0e10cSrcweir 
1088269660fSDamjan Jovanovic                 ASSERT_TRUE(pHelper != NULL) << "no helper";
109cdf0e10cSrcweir                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
1108269660fSDamjan Jovanovic                 ASSERT_TRUE(aSeq.getLength() == 1) << "wrong num elements";
1118269660fSDamjan Jovanovic                 ASSERT_TRUE(aSeq[0] == xRefs[i*2]) << "match";
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             // remove the 1st half of the rest
115cdf0e10cSrcweir             for (i = 0; i < nTests / 2; i++)
116cdf0e10cSrcweir                 pContainer->removeInterface(pTypes[i], xRefs[i*2]);
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             // check it is half there
119cdf0e10cSrcweir             for (i = 0; i < nTests / 2; i++)
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir                 cppu::OInterfaceContainerHelper *pHelper;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir                 pHelper = pContainer->getContainer(pTypes[i]);
1248269660fSDamjan Jovanovic                 ASSERT_TRUE(pHelper != NULL) << "no helper";
125cdf0e10cSrcweir                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
1268269660fSDamjan Jovanovic                 ASSERT_TRUE(aSeq.getLength() == 0) << "wrong num elements";
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             delete pContainer;
130cdf0e10cSrcweir         }
1318269660fSDamjan Jovanovic     };
132cdf0e10cSrcweir 
TEST_F(IfTest,testCreateDispose)1338269660fSDamjan Jovanovic     TEST_F(IfTest, testCreateDispose)
1348269660fSDamjan Jovanovic     {
1358269660fSDamjan Jovanovic         ContainerStats aStats;
1368269660fSDamjan Jovanovic         cppu::OInterfaceContainerHelper *pContainer;
1378269660fSDamjan Jovanovic 
1388269660fSDamjan Jovanovic         pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
1398269660fSDamjan Jovanovic 
1408269660fSDamjan Jovanovic         ASSERT_TRUE(pContainer->getLength() == 0) << "Empty container not empty";
1418269660fSDamjan Jovanovic 
1428269660fSDamjan Jovanovic         int i;
1438269660fSDamjan Jovanovic         for (i = 0; i < nTests; i++)
1448269660fSDamjan Jovanovic         {
1458269660fSDamjan Jovanovic             Reference<XEventListener> xRef = new ContainerListener(&aStats);
1468269660fSDamjan Jovanovic             int nNewLen = pContainer->addInterface(xRef);
1478269660fSDamjan Jovanovic 
1488269660fSDamjan Jovanovic             ASSERT_TRUE(nNewLen == i + 1) << "addition length mismatch";
1498269660fSDamjan Jovanovic             ASSERT_TRUE(pContainer->getLength() == i + 1) << "addition length mismatch";
1508269660fSDamjan Jovanovic         }
1518269660fSDamjan Jovanovic         ASSERT_TRUE(aStats.m_nAlive == nTests) << "alive count mismatch";
1528269660fSDamjan Jovanovic 
1538269660fSDamjan Jovanovic         EventObject aObj;
1548269660fSDamjan Jovanovic         pContainer->disposeAndClear(aObj);
1558269660fSDamjan Jovanovic 
1568269660fSDamjan Jovanovic         ASSERT_TRUE(aStats.m_nDisposed == nTests) << "dispose count mismatch";
1578269660fSDamjan Jovanovic         ASSERT_TRUE(aStats.m_nAlive == 0) << "leaked container left alive";
1588269660fSDamjan Jovanovic 
1598269660fSDamjan Jovanovic         delete pContainer;
1608269660fSDamjan Jovanovic     }
1618269660fSDamjan Jovanovic 
TEST_F(IfTest,testEnumerate)1628269660fSDamjan Jovanovic     TEST_F(IfTest, testEnumerate)
1638269660fSDamjan Jovanovic     {
1648269660fSDamjan Jovanovic         int i;
1658269660fSDamjan Jovanovic         ContainerStats aStats;
1668269660fSDamjan Jovanovic         cppu::OInterfaceContainerHelper *pContainer;
1678269660fSDamjan Jovanovic         pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
1688269660fSDamjan Jovanovic 
1698269660fSDamjan Jovanovic         std::vector< Reference< XEventListener > > aListeners;
1708269660fSDamjan Jovanovic         for (i = 0; i < nTests; i++)
1718269660fSDamjan Jovanovic         {
1728269660fSDamjan Jovanovic             Reference<XEventListener> xRef = new ContainerListener(&aStats);
173*5eeab561SPeter Kovacs             pContainer->addInterface(xRef);
1748269660fSDamjan Jovanovic             aListeners.push_back(xRef);
1758269660fSDamjan Jovanovic         }
1768269660fSDamjan Jovanovic         Sequence< Reference< XInterface > > aElements;
1778269660fSDamjan Jovanovic         aElements = pContainer->getElements();
1788269660fSDamjan Jovanovic 
1798269660fSDamjan Jovanovic         ASSERT_TRUE((int)aElements.getLength() == nTests) << "query contents";
1808269660fSDamjan Jovanovic         if ((int)aElements.getLength() == nTests)
1818269660fSDamjan Jovanovic         {
1828269660fSDamjan Jovanovic             for (i = 0; i < nTests; i++)
1838269660fSDamjan Jovanovic             {
1848269660fSDamjan Jovanovic                 ASSERT_TRUE(aElements[i] == aListeners[i]) << "mismatching elements";
1858269660fSDamjan Jovanovic             }
1868269660fSDamjan Jovanovic         }
1878269660fSDamjan Jovanovic         pContainer->clear();
1888269660fSDamjan Jovanovic 
1898269660fSDamjan Jovanovic         ASSERT_TRUE(pContainer->getLength() == 0) << "non-empty container post clear";
1908269660fSDamjan Jovanovic         delete pContainer;
1918269660fSDamjan Jovanovic     }
1928269660fSDamjan Jovanovic 
TEST_F(IfTest,testOMultiTypeInterfaceContainerHelper)1938269660fSDamjan Jovanovic     TEST_F(IfTest, testOMultiTypeInterfaceContainerHelper)
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         uno::Type pTypes[nTests] =
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             ::cppu::UnoType< bool >::get(),
198cdf0e10cSrcweir             ::cppu::UnoType< float >::get(),
199cdf0e10cSrcweir             ::cppu::UnoType< double >::get(),
200cdf0e10cSrcweir             ::cppu::UnoType< ::sal_uInt64 >::get(),
201cdf0e10cSrcweir             ::cppu::UnoType< ::sal_Int64 >::get(),
202cdf0e10cSrcweir             ::cppu::UnoType< ::sal_uInt32 >::get(),
203cdf0e10cSrcweir             ::cppu::UnoType< ::sal_Int32 >::get(),
204cdf0e10cSrcweir             ::cppu::UnoType< ::sal_Int16 >::get(),
205cdf0e10cSrcweir             ::cppu::UnoType< ::rtl::OUString >::get(),
206cdf0e10cSrcweir             ::cppu::UnoType< ::sal_Int8 >::get()
207cdf0e10cSrcweir         };
208cdf0e10cSrcweir         doContainerTest< cppu::OMultiTypeInterfaceContainerHelper,
209cdf0e10cSrcweir             uno::Type> (pTypes);
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
TEST_F(IfTest,testOMultiTypeInterfaceContainerHelperInt32)2128269660fSDamjan Jovanovic     TEST_F(IfTest, testOMultiTypeInterfaceContainerHelperInt32)
213cdf0e10cSrcweir     {
214cdf0e10cSrcweir         sal_Int32 pTypes[nTests] =
215cdf0e10cSrcweir         {
216cdf0e10cSrcweir             0,
217cdf0e10cSrcweir             -1,
218cdf0e10cSrcweir             1,
219cdf0e10cSrcweir             256,
220cdf0e10cSrcweir             1024,
221cdf0e10cSrcweir             3,
222cdf0e10cSrcweir             7,
223cdf0e10cSrcweir             8,
224cdf0e10cSrcweir             9,
225cdf0e10cSrcweir             10
226cdf0e10cSrcweir         };
227cdf0e10cSrcweir         doContainerTest< cppu::OMultiTypeInterfaceContainerHelperInt32, sal_Int32> (pTypes);
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
TEST_F(IfTest,testOMultiTypeInterfaceContainerHelperVar)2308269660fSDamjan Jovanovic     TEST_F(IfTest, testOMultiTypeInterfaceContainerHelperVar)
231cdf0e10cSrcweir     {
232cdf0e10cSrcweir         typedef ::cppu::OMultiTypeInterfaceContainerHelperVar<
233fda69661SHerbert Dürr             const char*, rtl::CStringHash, rtl::CStringEqual> StrContainer;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         const char *pTypes[nTests] =
236cdf0e10cSrcweir         {
237cdf0e10cSrcweir             "this_is", "such", "fun", "writing", "unit", "tests", "when", "it", "works", "anyway"
238cdf0e10cSrcweir         };
239cdf0e10cSrcweir         doContainerTest< StrContainer, const char *> (pTypes);
240cdf0e10cSrcweir     }
241cdf0e10cSrcweir 
2428269660fSDamjan Jovanovic 
243cdf0e10cSrcweir } // namespace cppu_ifcontainer
244