LCOV - code coverage report
Current view: top level - libreoffice/cppuhelper/qa/ifcontainer - cppu_ifcontainer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 117 117 100.0 %
Date: 2012-12-17 Functions: 23 24 95.8 %
Legend: Lines: hit not hit

          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         590 :     bool operator()(
      41             :         const char * const &rA,
      42             :         const char * const &rB) const
      43         590 :         { 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          10 :     ContainerStats() : m_nAlive(0), m_nDisposed(0) {}
      59             : };
      60             : 
      61             : class ContainerListener : public ::cppu::WeakImplHelper1< XEventListener >
      62             : {
      63             :     ContainerStats *m_pStats;
      64             : public:
      65         160 :     ContainerListener(ContainerStats *pStats)
      66         160 :         : m_pStats(pStats) { m_pStats->m_nAlive++; }
      67         320 :     virtual ~ContainerListener() { m_pStats->m_nAlive--; }
      68          20 :     virtual void SAL_CALL disposing( const EventObject& )
      69             :         throw (RuntimeException)
      70             :     {
      71          20 :         m_pStats->m_nDisposed++;
      72          20 :     }
      73             : };
      74             : 
      75             : namespace cppu_ifcontainer
      76             : {
      77          30 :     class IfTest : public CppUnit::TestFixture
      78             :     {
      79             :         osl::Mutex m_aGuard;
      80             :         static const int nTests = 10;
      81             :     public:
      82           2 :         void testCreateDispose()
      83             :         {
      84           2 :             ContainerStats aStats;
      85             :             cppu::OInterfaceContainerHelper *pContainer;
      86             : 
      87           2 :             pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
      88             : 
      89           4 :             CPPUNIT_ASSERT_MESSAGE("Empty container not empty",
      90           2 :                                    pContainer->getLength() == 0);
      91             : 
      92             :             int i;
      93          22 :             for (i = 0; i < nTests; i++)
      94             :             {
      95          20 :                 Reference<XEventListener> xRef = new ContainerListener(&aStats);
      96          20 :                 int nNewLen = pContainer->addInterface(xRef);
      97             : 
      98          40 :                 CPPUNIT_ASSERT_MESSAGE("addition length mismatch",
      99          20 :                                        nNewLen == i + 1);
     100          40 :                 CPPUNIT_ASSERT_MESSAGE("addition length mismatch",
     101          20 :                                        pContainer->getLength() == i + 1);
     102          20 :             }
     103           4 :             CPPUNIT_ASSERT_MESSAGE("alive count mismatch",
     104           2 :                                    aStats.m_nAlive == nTests);
     105             : 
     106           2 :             EventObject aObj;
     107           2 :             pContainer->disposeAndClear(aObj);
     108             : 
     109           4 :             CPPUNIT_ASSERT_MESSAGE("dispose count mismatch",
     110           2 :                                    aStats.m_nDisposed == nTests);
     111           4 :             CPPUNIT_ASSERT_MESSAGE("leaked container left alive",
     112           2 :                                    aStats.m_nAlive == 0);
     113             : 
     114           2 :             delete pContainer;
     115           2 :         }
     116             : 
     117           2 :         void testEnumerate()
     118             :         {
     119             :             int i;
     120           2 :             ContainerStats aStats;
     121             :             cppu::OInterfaceContainerHelper *pContainer;
     122           2 :             pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
     123             : 
     124           2 :             std::vector< Reference< XEventListener > > aListeners;
     125          22 :             for (i = 0; i < nTests; i++)
     126             :             {
     127          20 :                 Reference<XEventListener> xRef = new ContainerListener(&aStats);
     128          20 :                 pContainer->addInterface(xRef);
     129          20 :                 aListeners.push_back(xRef);
     130          20 :             }
     131           2 :             Sequence< Reference< XInterface > > aElements;
     132           2 :             aElements = pContainer->getElements();
     133             : 
     134           4 :             CPPUNIT_ASSERT_MESSAGE("query contents",
     135           2 :                                    (int)aElements.getLength() == nTests);
     136           2 :             if ((int)aElements.getLength() == nTests)
     137             :             {
     138          22 :                 for (i = 0; i < nTests; i++)
     139             :                 {
     140          40 :                     CPPUNIT_ASSERT_MESSAGE("mismatching elements",
     141          20 :                                            aElements[i] == aListeners[i]);
     142             :                 }
     143             :             }
     144           2 :             pContainer->clear();
     145             : 
     146           4 :             CPPUNIT_ASSERT_MESSAGE("non-empty container post clear",
     147           2 :                                    pContainer->getLength() == 0);
     148           2 :             delete pContainer;
     149           2 :         }
     150             : 
     151             :         template < typename ContainerType, typename ContainedType >
     152           6 :         void doContainerTest(const ContainedType *pTypes)
     153             :         {
     154           6 :             ContainerStats aStats;
     155             :             ContainerType *pContainer;
     156           6 :             pContainer = new ContainerType(m_aGuard);
     157             : 
     158             :             int i;
     159         126 :             Reference<XEventListener> xRefs[nTests * 2];
     160             : 
     161             :             // add these interfaces
     162         126 :             for (i = 0; i < nTests * 2; i++)
     163             :             {
     164         120 :                 xRefs[i] = new ContainerListener(&aStats);
     165         120 :                 pContainer->addInterface(pTypes[i / 2], xRefs[i]);
     166             :             }
     167             : 
     168             :             // check it is all there
     169          66 :             for (i = 0; i < nTests; i++)
     170             :             {
     171             :                 cppu::OInterfaceContainerHelper *pHelper;
     172             : 
     173          60 :                 pHelper = pContainer->getContainer(pTypes[i]);
     174             : 
     175          60 :                 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
     176          60 :                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
     177          60 :                 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 2);
     178          60 :                 CPPUNIT_ASSERT_MESSAGE("match", aSeq[0] == xRefs[i*2]);
     179          60 :                 CPPUNIT_ASSERT_MESSAGE("match", aSeq[1] == xRefs[i*2+1]);
     180             :             }
     181             : 
     182             :             // remove every other interface
     183          66 :             for (i = 0; i < nTests; i++)
     184          60 :                 pContainer->removeInterface(pTypes[i], xRefs[i*2+1]);
     185             : 
     186             :             // check it is half there
     187          66 :             for (i = 0; i < nTests; i++)
     188             :             {
     189             :                 cppu::OInterfaceContainerHelper *pHelper;
     190             : 
     191          60 :                 pHelper = pContainer->getContainer(pTypes[i]);
     192             : 
     193          60 :                 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
     194          60 :                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
     195          60 :                 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 1);
     196          60 :                 CPPUNIT_ASSERT_MESSAGE("match", aSeq[0] == xRefs[i*2]);
     197             :             }
     198             : 
     199             :             // remove the 1st half of the rest
     200          36 :             for (i = 0; i < nTests / 2; i++)
     201          30 :                 pContainer->removeInterface(pTypes[i], xRefs[i*2]);
     202             : 
     203             :             // check it is half there
     204          36 :             for (i = 0; i < nTests / 2; i++)
     205             :             {
     206             :                 cppu::OInterfaceContainerHelper *pHelper;
     207             : 
     208          30 :                 pHelper = pContainer->getContainer(pTypes[i]);
     209          30 :                 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
     210          30 :                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
     211          30 :                 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 0);
     212             :             }
     213             : 
     214         126 :             delete pContainer;
     215           6 :         }
     216             : 
     217           2 :         void testOMultiTypeInterfaceContainerHelper()
     218             :         {
     219             :             uno::Type pTypes[nTests] =
     220             :             {
     221           2 :                 ::cppu::UnoType< bool >::get(),
     222           2 :                 ::cppu::UnoType< float >::get(),
     223           2 :                 ::cppu::UnoType< double >::get(),
     224           2 :                 ::cppu::UnoType< ::sal_uInt64 >::get(),
     225           2 :                 ::cppu::UnoType< ::sal_Int64 >::get(),
     226           2 :                 ::cppu::UnoType< ::sal_uInt32 >::get(),
     227           2 :                 ::cppu::UnoType< ::sal_Int32 >::get(),
     228           2 :                 ::cppu::UnoType< ::sal_Int16 >::get(),
     229           2 :                 ::cppu::UnoType< ::rtl::OUString >::get(),
     230           2 :                 ::cppu::UnoType< ::sal_Int8 >::get()
     231          42 :             };
     232             :             doContainerTest< cppu::OMultiTypeInterfaceContainerHelper,
     233          22 :                 uno::Type> (pTypes);
     234           2 :         }
     235             : 
     236           2 :         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           2 :             };
     251           2 :             doContainerTest< cppu::OMultiTypeInterfaceContainerHelperInt32, sal_Int32> (pTypes);
     252           2 :         }
     253             : 
     254           2 :         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           2 :             };
     263           2 :             doContainerTest< StrContainer, const char *> (pTypes);
     264           2 :         }
     265             : 
     266             :         // Automatic registration code
     267           4 :         CPPUNIT_TEST_SUITE(IfTest);
     268           2 :         CPPUNIT_TEST(testCreateDispose);
     269           2 :         CPPUNIT_TEST(testEnumerate);
     270           2 :         CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelper);
     271           2 :         CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperVar);
     272           2 :         CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperInt32);
     273           4 :         CPPUNIT_TEST_SUITE_END();
     274             :     };
     275             : } // namespace cppu_ifcontainer
     276             : 
     277           2 : CPPUNIT_TEST_SUITE_REGISTRATION(cppu_ifcontainer::IfTest);
     278             : 
     279           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     280             : 
     281             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10