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();
25 void SameListenerCanBeAddedMoreThanOnce();
26 void StoppingListeningAffectsOnlyFirstOfIdenticalListeners();
28 // Adds code needed to register the test suite
29 CPPUNIT_TEST_SUITE(SfxBroadcasterTest
);
30 CPPUNIT_TEST(AddingListenersIncreasesCount
);
31 CPPUNIT_TEST(RemovingListenersDecreasesCount
);
32 CPPUNIT_TEST(HintsAreNotForwardedToRemovedListeners
);
33 CPPUNIT_TEST(SameListenerCanBeAddedMoreThanOnce
);
34 CPPUNIT_TEST(StoppingListeningAffectsOnlyFirstOfIdenticalListeners
);
36 CPPUNIT_TEST_SUITE_END();
41 class MockedSfxListener
: public SfxListener
45 : mNotifyWasCalled(false)
49 void Notify(SfxBroadcaster
&, const SfxHint
&) override
{ mNotifyWasCalled
= true; }
51 bool NotifyWasCalled() const { return mNotifyWasCalled
; }
54 bool mNotifyWasCalled
;
58 void SfxBroadcasterTest::AddingListenersIncreasesCount()
63 CPPUNIT_ASSERT_EQUAL(size_t(0), sb
.GetListenerCount());
65 sl
.StartListening(sb
, DuplicateHandling::Prevent
);
66 CPPUNIT_ASSERT_EQUAL(size_t(1), sb
.GetListenerCount());
69 void SfxBroadcasterTest::RemovingListenersDecreasesCount()
74 CPPUNIT_ASSERT_EQUAL(size_t(0), sb
.GetListenerCount());
75 sl
.StartListening(sb
, DuplicateHandling::Prevent
);
76 CPPUNIT_ASSERT_EQUAL(size_t(1), sb
.GetListenerCount());
77 sl
.EndListening(sb
, true);
78 CPPUNIT_ASSERT_EQUAL(size_t(0), sb
.GetListenerCount());
81 void SfxBroadcasterTest::HintsAreNotForwardedToRemovedListeners()
84 MockedSfxListener sl1
;
85 MockedSfxListener sl2
;
88 sl1
.StartListening(sb
, DuplicateHandling::Prevent
);
89 sl2
.StartListening(sb
, DuplicateHandling::Prevent
);
90 CPPUNIT_ASSERT_EQUAL_MESSAGE("All listeners were added.", size_t(2), sb
.GetListenerCount());
91 sl1
.EndListening(sb
, true);
93 CPPUNIT_ASSERT_EQUAL(true, sl2
.NotifyWasCalled());
94 CPPUNIT_ASSERT_EQUAL(false, sl1
.NotifyWasCalled());
97 void SfxBroadcasterTest::SameListenerCanBeAddedMoreThanOnce()
103 CPPUNIT_ASSERT_EQUAL(size_t(2), sb
.GetListenerCount());
106 void SfxBroadcasterTest::StoppingListeningAffectsOnlyFirstOfIdenticalListeners()
108 MockedSfxListener sl
;
112 sb
.RemoveListener(sl
);
113 CPPUNIT_ASSERT_EQUAL(size_t(1), sb
.GetListenerCount());
116 CPPUNIT_TEST_SUITE_REGISTRATION(SfxBroadcasterTest
);
118 CPPUNIT_PLUGIN_IMPLEMENT();