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