1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/types.h>
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/plugin/TestPlugIn.h>
26 #include <com/sun/star/lang/XEventListener.hpp>
27 #include <comphelper/interfacecontainer2.hxx>
28 #include <cppuhelper/implbase.hxx>
30 using namespace com::sun::star
;
31 using namespace com::sun::star::uno
;
32 using namespace com::sun::star::lang
;
36 struct ContainerStats
{
39 ContainerStats() : m_nAlive(0), m_nDisposed(0) {}
42 class ContainerListener
: public cppu::WeakImplHelper
< XEventListener
>
44 ContainerStats
* const m_pStats
;
46 explicit ContainerListener(ContainerStats
*pStats
)
47 : m_pStats(pStats
) { m_pStats
->m_nAlive
++; }
48 virtual ~ContainerListener() override
{ m_pStats
->m_nAlive
--; }
49 virtual void SAL_CALL
disposing( const EventObject
& ) override
51 m_pStats
->m_nDisposed
++;
57 namespace comphelper_ifcontainer
59 const int nTests
= 10;
60 class IfTest
: public CppUnit::TestFixture
64 void testCreateDispose()
66 ContainerStats aStats
;
67 comphelper::OInterfaceContainerHelper2
*pContainer
;
69 pContainer
= new comphelper::OInterfaceContainerHelper2(m_aGuard
);
71 CPPUNIT_ASSERT_EQUAL_MESSAGE("Empty container not empty",
72 static_cast<sal_Int32
>(0), pContainer
->getLength());
75 for (i
= 0; i
< nTests
; i
++)
77 Reference
<XEventListener
> xRef
= new ContainerListener(&aStats
);
78 int nNewLen
= pContainer
->addInterface(xRef
);
80 CPPUNIT_ASSERT_EQUAL_MESSAGE("addition length mismatch",
82 CPPUNIT_ASSERT_EQUAL_MESSAGE("addition length mismatch",
83 static_cast<sal_Int32
>(i
+ 1), pContainer
->getLength());
85 CPPUNIT_ASSERT_EQUAL_MESSAGE("alive count mismatch",
86 nTests
, aStats
.m_nAlive
);
89 pContainer
->disposeAndClear(aObj
);
91 CPPUNIT_ASSERT_EQUAL_MESSAGE("dispose count mismatch",
92 nTests
, aStats
.m_nDisposed
);
93 CPPUNIT_ASSERT_EQUAL_MESSAGE("leaked container left alive",
102 ContainerStats aStats
;
103 comphelper::OInterfaceContainerHelper2
*pContainer
;
104 pContainer
= new comphelper::OInterfaceContainerHelper2(m_aGuard
);
106 std::vector
< Reference
< XEventListener
> > aListeners
;
107 for (i
= 0; i
< nTests
; i
++)
109 Reference
<XEventListener
> xRef
= new ContainerListener(&aStats
);
110 pContainer
->addInterface(xRef
);
111 aListeners
.push_back(xRef
);
113 std::vector
< Reference
< XInterface
> > aElements
= pContainer
->getElements();
115 CPPUNIT_ASSERT_EQUAL_MESSAGE("query contents",
116 nTests
, static_cast<int>(aElements
.size()));
117 if (aElements
.size() == nTests
)
119 for (i
= 0; i
< nTests
; i
++)
121 CPPUNIT_ASSERT_MESSAGE("mismatching elements",
122 bool(aElements
[i
] == aListeners
[i
]));
127 CPPUNIT_ASSERT_EQUAL_MESSAGE("non-empty container post clear",
128 static_cast<sal_Int32
>(0), pContainer
->getLength());
132 // Automatic registration code
133 CPPUNIT_TEST_SUITE(IfTest
);
134 CPPUNIT_TEST(testCreateDispose
);
135 CPPUNIT_TEST(testEnumerate
);
136 CPPUNIT_TEST_SUITE_END();
138 } // namespace cppu_ifcontainer
140 CPPUNIT_TEST_SUITE_REGISTRATION(comphelper_ifcontainer::IfTest
);
142 CPPUNIT_PLUGIN_IMPLEMENT();
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */