LCOV - code coverage report
Current view: top level - cppuhelper/qa/ifcontainer - cppu_ifcontainer.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 115 115 100.0 %
Date: 2015-06-13 12:38:46 Functions: 25 26 96.2 %
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             : class ContainerListener;
      38             : 
      39             : struct ContainerStats {
      40             :     int m_nAlive;
      41             :     int m_nDisposed;
      42           5 :     ContainerStats() : m_nAlive(0), m_nDisposed(0) {}
      43             : };
      44             : 
      45             : class ContainerListener : public ::cppu::WeakImplHelper1< XEventListener >
      46             : {
      47             :     ContainerStats *m_pStats;
      48             : public:
      49          80 :     explicit ContainerListener(ContainerStats *pStats)
      50          80 :         : m_pStats(pStats) { m_pStats->m_nAlive++; }
      51         160 :     virtual ~ContainerListener() { m_pStats->m_nAlive--; }
      52          10 :     virtual void SAL_CALL disposing( const EventObject& )
      53             :         throw (RuntimeException, std::exception) SAL_OVERRIDE
      54             :     {
      55          10 :         m_pStats->m_nDisposed++;
      56          10 :     }
      57             : };
      58             : 
      59             : namespace cppu_ifcontainer
      60             : {
      61          15 :     class IfTest : public CppUnit::TestFixture
      62             :     {
      63             :         osl::Mutex m_aGuard;
      64             :         static const int nTests = 10;
      65             :     public:
      66           1 :         void testCreateDispose()
      67             :         {
      68           1 :             ContainerStats aStats;
      69             :             cppu::OInterfaceContainerHelper *pContainer;
      70             : 
      71           1 :             pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
      72             : 
      73           2 :             CPPUNIT_ASSERT_MESSAGE("Empty container not empty",
      74           1 :                                    pContainer->getLength() == 0);
      75             : 
      76             :             int i;
      77          11 :             for (i = 0; i < nTests; i++)
      78             :             {
      79          10 :                 Reference<XEventListener> xRef = new ContainerListener(&aStats);
      80          10 :                 int nNewLen = pContainer->addInterface(xRef);
      81             : 
      82          20 :                 CPPUNIT_ASSERT_MESSAGE("addition length mismatch",
      83          10 :                                        nNewLen == i + 1);
      84          20 :                 CPPUNIT_ASSERT_MESSAGE("addition length mismatch",
      85          10 :                                        pContainer->getLength() == i + 1);
      86          10 :             }
      87           2 :             CPPUNIT_ASSERT_MESSAGE("alive count mismatch",
      88           1 :                                    aStats.m_nAlive == nTests);
      89             : 
      90           1 :             EventObject aObj;
      91           1 :             pContainer->disposeAndClear(aObj);
      92             : 
      93           2 :             CPPUNIT_ASSERT_MESSAGE("dispose count mismatch",
      94           1 :                                    aStats.m_nDisposed == nTests);
      95           2 :             CPPUNIT_ASSERT_MESSAGE("leaked container left alive",
      96           1 :                                    aStats.m_nAlive == 0);
      97             : 
      98           1 :             delete pContainer;
      99           1 :         }
     100             : 
     101           1 :         void testEnumerate()
     102             :         {
     103             :             int i;
     104           1 :             ContainerStats aStats;
     105             :             cppu::OInterfaceContainerHelper *pContainer;
     106           1 :             pContainer = new cppu::OInterfaceContainerHelper(m_aGuard);
     107             : 
     108           1 :             std::vector< Reference< XEventListener > > aListeners;
     109          11 :             for (i = 0; i < nTests; i++)
     110             :             {
     111          10 :                 Reference<XEventListener> xRef = new ContainerListener(&aStats);
     112          10 :                 pContainer->addInterface(xRef);
     113          10 :                 aListeners.push_back(xRef);
     114          10 :             }
     115           2 :             Sequence< Reference< XInterface > > aElements;
     116           1 :             aElements = pContainer->getElements();
     117             : 
     118           2 :             CPPUNIT_ASSERT_MESSAGE("query contents",
     119           1 :                                    (int)aElements.getLength() == nTests);
     120           1 :             if ((int)aElements.getLength() == nTests)
     121             :             {
     122          11 :                 for (i = 0; i < nTests; i++)
     123             :                 {
     124          20 :                     CPPUNIT_ASSERT_MESSAGE("mismatching elements",
     125          10 :                                            aElements[i] == aListeners[i]);
     126             :                 }
     127             :             }
     128           1 :             pContainer->clear();
     129             : 
     130           2 :             CPPUNIT_ASSERT_MESSAGE("non-empty container post clear",
     131           1 :                                    pContainer->getLength() == 0);
     132           2 :             delete pContainer;
     133           1 :         }
     134             : 
     135             :         template < typename ContainerType, typename ContainedType >
     136           3 :         void doContainerTest(const ContainedType *pTypes)
     137             :         {
     138           3 :             ContainerStats aStats;
     139             :             ContainerType *pContainer;
     140           3 :             pContainer = new ContainerType(m_aGuard);
     141             : 
     142             :             int i;
     143          63 :             Reference<XEventListener> xRefs[nTests * 2];
     144             : 
     145             :             // add these interfaces
     146          63 :             for (i = 0; i < nTests * 2; i++)
     147             :             {
     148          60 :                 xRefs[i] = new ContainerListener(&aStats);
     149          60 :                 pContainer->addInterface(pTypes[i / 2], xRefs[i]);
     150             :             }
     151             : 
     152             :             // check it is all there
     153          33 :             for (i = 0; i < nTests; i++)
     154             :             {
     155             :                 cppu::OInterfaceContainerHelper *pHelper;
     156             : 
     157          30 :                 pHelper = pContainer->getContainer(pTypes[i]);
     158             : 
     159          30 :                 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
     160          30 :                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
     161          30 :                 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 2);
     162          30 :                 CPPUNIT_ASSERT_MESSAGE("match", aSeq[0] == xRefs[i*2]);
     163          30 :                 CPPUNIT_ASSERT_MESSAGE("match", aSeq[1] == xRefs[i*2+1]);
     164             :             }
     165             : 
     166             :             // remove every other interface
     167          33 :             for (i = 0; i < nTests; i++)
     168          30 :                 pContainer->removeInterface(pTypes[i], xRefs[i*2+1]);
     169             : 
     170             :             // check it is half there
     171          33 :             for (i = 0; i < nTests; i++)
     172             :             {
     173             :                 cppu::OInterfaceContainerHelper *pHelper;
     174             : 
     175          30 :                 pHelper = pContainer->getContainer(pTypes[i]);
     176             : 
     177          30 :                 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
     178          30 :                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
     179          30 :                 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 1);
     180          30 :                 CPPUNIT_ASSERT_MESSAGE("match", aSeq[0] == xRefs[i*2]);
     181             :             }
     182             : 
     183             :             // remove the 1st half of the rest
     184          18 :             for (i = 0; i < nTests / 2; i++)
     185          15 :                 pContainer->removeInterface(pTypes[i], xRefs[i*2]);
     186             : 
     187             :             // check it is half there
     188          18 :             for (i = 0; i < nTests / 2; i++)
     189             :             {
     190             :                 cppu::OInterfaceContainerHelper *pHelper;
     191             : 
     192          15 :                 pHelper = pContainer->getContainer(pTypes[i]);
     193          15 :                 CPPUNIT_ASSERT_MESSAGE("no helper", pHelper != NULL);
     194          15 :                 Sequence<Reference< XInterface > > aSeq = pHelper->getElements();
     195          15 :                 CPPUNIT_ASSERT_MESSAGE("wrong num elements", aSeq.getLength() == 0);
     196             :             }
     197             : 
     198          63 :             delete pContainer;
     199           3 :         }
     200             : 
     201           1 :         void testOMultiTypeInterfaceContainerHelper()
     202             :         {
     203             :             uno::Type pTypes[nTests] =
     204             :             {
     205           1 :                 ::cppu::UnoType< bool >::get(),
     206           1 :                 ::cppu::UnoType< float >::get(),
     207           1 :                 ::cppu::UnoType< double >::get(),
     208           1 :                 ::cppu::UnoType< ::sal_uInt64 >::get(),
     209           1 :                 ::cppu::UnoType< ::sal_Int64 >::get(),
     210           1 :                 ::cppu::UnoType< ::sal_uInt32 >::get(),
     211           1 :                 ::cppu::UnoType< ::sal_Int32 >::get(),
     212           1 :                 ::cppu::UnoType< ::sal_Int16 >::get(),
     213           1 :                 ::cppu::UnoType< ::rtl::OUString >::get(),
     214           1 :                 ::cppu::UnoType< ::sal_Int8 >::get()
     215          21 :             };
     216             :             doContainerTest< cppu::OMultiTypeInterfaceContainerHelper,
     217          11 :                 uno::Type> (pTypes);
     218           1 :         }
     219             : 
     220           1 :         void testOMultiTypeInterfaceContainerHelperInt32()
     221             :         {
     222             :             sal_Int32 pTypes[nTests] =
     223             :             {
     224             :                 0,
     225             :                 -1,
     226             :                 1,
     227             :                 256,
     228             :                 1024,
     229             :                 3,
     230             :                 7,
     231             :                 8,
     232             :                 9,
     233             :                 10
     234           1 :             };
     235           1 :             doContainerTest< cppu::OMultiTypeInterfaceContainerHelperInt32, sal_Int32> (pTypes);
     236           1 :         }
     237             : 
     238           1 :         void testOMultiTypeInterfaceContainerHelperVar()
     239             :         {
     240             :             typedef cppu::OMultiTypeInterfaceContainerHelperVar<
     241             :                 char const *, void, rtl::CStringEqual> StrContainer;
     242             : 
     243             :             const char *pTypes[nTests] =
     244             :             {
     245             :                 "this_is", "such", "fun", "writing", "unit", "tests", "when", "it", "works", "anyway"
     246           1 :             };
     247           1 :             doContainerTest< StrContainer, const char *> (pTypes);
     248           1 :         }
     249             : 
     250             :         // Automatic registration code
     251           2 :         CPPUNIT_TEST_SUITE(IfTest);
     252           1 :         CPPUNIT_TEST(testCreateDispose);
     253           1 :         CPPUNIT_TEST(testEnumerate);
     254           1 :         CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelper);
     255           1 :         CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperVar);
     256           1 :         CPPUNIT_TEST(testOMultiTypeInterfaceContainerHelperInt32);
     257           5 :         CPPUNIT_TEST_SUITE_END();
     258             :     };
     259             : } // namespace cppu_ifcontainer
     260             : 
     261           1 : CPPUNIT_TEST_SUITE_REGISTRATION(cppu_ifcontainer::IfTest);
     262             : 
     263           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     264             : 
     265             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11