Avoid a warning shown since bumping Windows baseline
[LibreOffice.git] / svl / qa / unit / notify / test_SfxBroadcaster.cxx
blobffed864ff631800d0fe25b9fb62590af3773766a
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();
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();
39 namespace
41 class MockedSfxListener : public SfxListener
43 public:
44 MockedSfxListener()
45 : mNotifyWasCalled(false)
49 void Notify(SfxBroadcaster&, const SfxHint&) override { mNotifyWasCalled = true; }
51 bool NotifyWasCalled() const { return mNotifyWasCalled; }
53 private:
54 bool mNotifyWasCalled;
58 void SfxBroadcasterTest::AddingListenersIncreasesCount()
60 SfxBroadcaster sb;
61 MockedSfxListener sl;
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()
71 SfxBroadcaster sb;
72 MockedSfxListener sl;
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()
83 SfxBroadcaster sb;
84 MockedSfxListener sl1;
85 MockedSfxListener sl2;
86 SfxHint hint;
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);
92 sb.Forward(sb, hint);
93 CPPUNIT_ASSERT_EQUAL(true, sl2.NotifyWasCalled());
94 CPPUNIT_ASSERT_EQUAL(false, sl1.NotifyWasCalled());
97 void SfxBroadcasterTest::SameListenerCanBeAddedMoreThanOnce()
99 MockedSfxListener sl;
100 SfxBroadcaster sb;
101 sb.AddListener(sl);
102 sb.AddListener(sl);
103 CPPUNIT_ASSERT_EQUAL(size_t(2), sb.GetListenerCount());
106 void SfxBroadcasterTest::StoppingListeningAffectsOnlyFirstOfIdenticalListeners()
108 MockedSfxListener sl;
109 SfxBroadcaster sb;
110 sb.AddListener(sl);
111 sb.AddListener(sl);
112 sb.RemoveListener(sl);
113 CPPUNIT_ASSERT_EQUAL(size_t(1), sb.GetListenerCount());
116 CPPUNIT_TEST_SUITE_REGISTRATION(SfxBroadcasterTest);
118 CPPUNIT_PLUGIN_IMPLEMENT();