1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #include <cppuhelper/implbase.hxx>
11 #include <test/lang/xcomponent.hxx>
13 #include <com/sun/star/lang/XComponent.hpp>
14 #include <com/sun/star/lang/XEventListener.hpp>
16 #include <cppunit/TestAssert.h>
19 using namespace css::uno
;
22 struct TestEventListener final
: ::cppu::WeakImplHelper
<lang::XEventListener
>
24 bool m_hasDisposingCalled
;
26 : m_hasDisposingCalled(false)
29 virtual void SAL_CALL
disposing(const lang::EventObject
&) override
31 m_hasDisposingCalled
= true;
38 void XComponent::testAddEventListener()
40 Reference
<lang::XComponent
> xComponent(init(), uno::UNO_QUERY_THROW
);
41 auto pListenerAdded(new TestEventListener());
42 Reference
<lang::XEventListener
> xListenerAdded(pListenerAdded
);
43 xComponent
->addEventListener(xListenerAdded
);
44 xComponent
->dispose();
45 CPPUNIT_ASSERT_EQUAL(true, pListenerAdded
->m_hasDisposingCalled
);
48 void XComponent::testRemoveEventListener()
50 Reference
<lang::XComponent
> xComponent(init(), uno::UNO_QUERY_THROW
);
51 auto pListenerAddedAndRemoved(new TestEventListener());
52 Reference
<lang::XEventListener
> xListenerAddedAndRemoved(pListenerAddedAndRemoved
);
53 xComponent
->addEventListener(xListenerAddedAndRemoved
);
54 xComponent
->removeEventListener(xListenerAddedAndRemoved
);
55 xComponent
->dispose();
56 CPPUNIT_ASSERT_EQUAL(false, pListenerAddedAndRemoved
->m_hasDisposingCalled
);
59 void XComponent::testDispose()
61 Reference
<lang::XComponent
> xComponent(init(), uno::UNO_QUERY_THROW
);
62 auto pListenerAdded(new TestEventListener());
63 Reference
<lang::XEventListener
> xListenerAdded(pListenerAdded
);
64 xComponent
->addEventListener(xListenerAdded
);
65 xComponent
->dispose();
66 CPPUNIT_ASSERT_EQUAL(true, pListenerAdded
->m_hasDisposingCalled
);
69 void XComponent::testDisposedByDesktopTerminate()
71 Reference
<lang::XComponent
> xComponent(init(), uno::UNO_QUERY_THROW
);
72 auto pListenerAdded(new TestEventListener());
73 Reference
<lang::XEventListener
> xListenerAdded(pListenerAdded
);
74 xComponent
->addEventListener(xListenerAdded
);
75 triggerDesktopTerminate();
76 CPPUNIT_ASSERT_EQUAL(true, pListenerAdded
->m_hasDisposingCalled
);
78 } // namespace apitest
80 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */