LibreOffice Module test (master)  1
xcomponent.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 
10 #include <cppuhelper/implbase.hxx>
11 #include <test/lang/xcomponent.hxx>
12 
13 #include <com/sun/star/lang/XComponent.hpp>
14 #include <com/sun/star/lang/XEventListener.hpp>
15 
16 #include <cppunit/TestAssert.h>
17 
18 using namespace css;
19 using namespace css::uno;
20 namespace
21 {
22 struct TestEventListener final : ::cppu::WeakImplHelper<lang::XEventListener>
23 {
24  bool m_hasDisposingCalled;
25  TestEventListener()
26  : m_hasDisposingCalled(false)
27  {
28  }
29  virtual void SAL_CALL disposing(const lang::EventObject&) override
30  {
31  m_hasDisposingCalled = true;
32  }
33 };
34 }
35 
36 namespace apitest
37 {
38 void XComponent::testAddEventListener()
39 {
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);
46 }
47 
48 void XComponent::testRemoveEventListener()
49 {
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);
57 }
58 
59 void XComponent::testDispose()
60 {
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);
67 }
68 
69 void XComponent::testDisposedByDesktopTerminate()
70 {
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);
77 }
78 } // namespace apitest
79 
80 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
FILE * init(int, char **)