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/.
10 #include <svl/SfxBroadcaster.hxx>
12 #include <svl/lstner.hxx>
13 #include <svl/hint.hxx>
15 #include <cppunit/TestAssert.h>
16 #include <cppunit/TestFixture.h>
17 #include <cppunit/extensions/HelperMacros.h>
18 #include <cppunit/plugin/TestPlugIn.h>
20 class SfxBroadcasterTest
: public CppUnit::TestFixture
22 void AddingListenersIncreasesCount();
23 void RemovingListenersDecreasesCount();
24 void HintsAreNotForwardedToRemovedListeners();
26 // Adds code needed to register the test suite
27 CPPUNIT_TEST_SUITE(SfxBroadcasterTest
);
28 CPPUNIT_TEST(AddingListenersIncreasesCount
);
29 CPPUNIT_TEST(RemovingListenersDecreasesCount
);
30 CPPUNIT_TEST(HintsAreNotForwardedToRemovedListeners
);
32 CPPUNIT_TEST_SUITE_END();
35 class MockedSfxListener
: public SfxListener
39 : mNotifyWasCalled(false) {}
41 void Notify(SfxBroadcaster
& rBC
, const SfxHint
& rHint
) SAL_OVERRIDE
{
42 (void)(rBC
); (void)(rHint
); // avoid warnings about unused parameters
43 mNotifyWasCalled
= true;
46 bool NotifyWasCalled() const {
47 return mNotifyWasCalled
;
51 bool mNotifyWasCalled
;
55 SfxBroadcasterTest::AddingListenersIncreasesCount()
60 CPPUNIT_ASSERT_EQUAL((size_t)0, sb
.GetListenerCount());
62 sl
.StartListening(sb
, true);
63 CPPUNIT_ASSERT_EQUAL((size_t)1, sb
.GetListenerCount());
67 SfxBroadcasterTest::RemovingListenersDecreasesCount()
72 CPPUNIT_ASSERT_EQUAL((size_t)0, sb
.GetListenerCount());
73 sl
.StartListening(sb
, true);
74 CPPUNIT_ASSERT_EQUAL((size_t)1, sb
.GetListenerCount());
75 sl
.EndListening(sb
, true);
76 CPPUNIT_ASSERT_EQUAL((size_t)0, sb
.GetListenerCount());
80 SfxBroadcasterTest::HintsAreNotForwardedToRemovedListeners()
83 MockedSfxListener sl1
;
84 MockedSfxListener sl2
;
87 sl1
.StartListening(sb
, true);
88 sl2
.StartListening(sb
, true);
89 CPPUNIT_ASSERT_EQUAL_MESSAGE("All listeners were added.", (size_t)2, sb
.GetListenerCount());
90 sl1
.EndListening(sb
, true);
92 CPPUNIT_ASSERT_EQUAL(true, sl2
.NotifyWasCalled());
93 CPPUNIT_ASSERT_EQUAL(false, sl1
.NotifyWasCalled());
96 CPPUNIT_TEST_SUITE_REGISTRATION(SfxBroadcasterTest
);
98 CPPUNIT_PLUGIN_IMPLEMENT();