bump product version to 5.0.4.1
[LibreOffice.git] / svl / qa / unit / notify / test_SfxBroadcaster.cxx
blob60960295ccc7b5669e15ae91e62e6de075cb9d47
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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
37 public:
38 MockedSfxListener()
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;
50 private:
51 bool mNotifyWasCalled;
54 void
55 SfxBroadcasterTest::AddingListenersIncreasesCount()
57 SfxBroadcaster sb;
58 MockedSfxListener sl;
60 CPPUNIT_ASSERT_EQUAL((size_t)0, sb.GetListenerCount());
62 sl.StartListening(sb, true);
63 CPPUNIT_ASSERT_EQUAL((size_t)1, sb.GetListenerCount());
66 void
67 SfxBroadcasterTest::RemovingListenersDecreasesCount()
69 SfxBroadcaster sb;
70 MockedSfxListener sl;
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());
79 void
80 SfxBroadcasterTest::HintsAreNotForwardedToRemovedListeners()
82 SfxBroadcaster sb;
83 MockedSfxListener sl1;
84 MockedSfxListener sl2;
85 SfxHint hint;
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);
91 sb.Forward(sb, hint);
92 CPPUNIT_ASSERT_EQUAL(true, sl2.NotifyWasCalled());
93 CPPUNIT_ASSERT_EQUAL(false, sl1.NotifyWasCalled());
96 CPPUNIT_TEST_SUITE_REGISTRATION(SfxBroadcasterTest);
98 CPPUNIT_PLUGIN_IMPLEMENT();