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

Generated by: LCOV version 1.10