diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /svl/qa/unit/notify | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'svl/qa/unit/notify')
-rw-r--r-- | svl/qa/unit/notify/test_SfxBroadcaster.cxx | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/svl/qa/unit/notify/test_SfxBroadcaster.cxx b/svl/qa/unit/notify/test_SfxBroadcaster.cxx new file mode 100644 index 0000000000..ffed864ff6 --- /dev/null +++ b/svl/qa/unit/notify/test_SfxBroadcaster.cxx @@ -0,0 +1,118 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <svl/SfxBroadcaster.hxx> + +#include <svl/lstner.hxx> +#include <svl/hint.hxx> + +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +class SfxBroadcasterTest : public CppUnit::TestFixture +{ + void AddingListenersIncreasesCount(); + void RemovingListenersDecreasesCount(); + void HintsAreNotForwardedToRemovedListeners(); + void SameListenerCanBeAddedMoreThanOnce(); + void StoppingListeningAffectsOnlyFirstOfIdenticalListeners(); + + // Adds code needed to register the test suite + CPPUNIT_TEST_SUITE(SfxBroadcasterTest); + CPPUNIT_TEST(AddingListenersIncreasesCount); + CPPUNIT_TEST(RemovingListenersDecreasesCount); + CPPUNIT_TEST(HintsAreNotForwardedToRemovedListeners); + CPPUNIT_TEST(SameListenerCanBeAddedMoreThanOnce); + CPPUNIT_TEST(StoppingListeningAffectsOnlyFirstOfIdenticalListeners); + + CPPUNIT_TEST_SUITE_END(); +}; + +namespace +{ +class MockedSfxListener : public SfxListener +{ +public: + MockedSfxListener() + : mNotifyWasCalled(false) + { + } + + void Notify(SfxBroadcaster&, const SfxHint&) override { mNotifyWasCalled = true; } + + bool NotifyWasCalled() const { return mNotifyWasCalled; } + +private: + bool mNotifyWasCalled; +}; +} + +void SfxBroadcasterTest::AddingListenersIncreasesCount() +{ + SfxBroadcaster sb; + MockedSfxListener sl; + + CPPUNIT_ASSERT_EQUAL(size_t(0), sb.GetListenerCount()); + + sl.StartListening(sb, DuplicateHandling::Prevent); + CPPUNIT_ASSERT_EQUAL(size_t(1), sb.GetListenerCount()); +} + +void SfxBroadcasterTest::RemovingListenersDecreasesCount() +{ + SfxBroadcaster sb; + MockedSfxListener sl; + + CPPUNIT_ASSERT_EQUAL(size_t(0), sb.GetListenerCount()); + sl.StartListening(sb, DuplicateHandling::Prevent); + CPPUNIT_ASSERT_EQUAL(size_t(1), sb.GetListenerCount()); + sl.EndListening(sb, true); + CPPUNIT_ASSERT_EQUAL(size_t(0), sb.GetListenerCount()); +} + +void SfxBroadcasterTest::HintsAreNotForwardedToRemovedListeners() +{ + SfxBroadcaster sb; + MockedSfxListener sl1; + MockedSfxListener sl2; + SfxHint hint; + + sl1.StartListening(sb, DuplicateHandling::Prevent); + sl2.StartListening(sb, DuplicateHandling::Prevent); + CPPUNIT_ASSERT_EQUAL_MESSAGE("All listeners were added.", size_t(2), sb.GetListenerCount()); + sl1.EndListening(sb, true); + sb.Forward(sb, hint); + CPPUNIT_ASSERT_EQUAL(true, sl2.NotifyWasCalled()); + CPPUNIT_ASSERT_EQUAL(false, sl1.NotifyWasCalled()); +} + +void SfxBroadcasterTest::SameListenerCanBeAddedMoreThanOnce() +{ + MockedSfxListener sl; + SfxBroadcaster sb; + sb.AddListener(sl); + sb.AddListener(sl); + CPPUNIT_ASSERT_EQUAL(size_t(2), sb.GetListenerCount()); +} + +void SfxBroadcasterTest::StoppingListeningAffectsOnlyFirstOfIdenticalListeners() +{ + MockedSfxListener sl; + SfxBroadcaster sb; + sb.AddListener(sl); + sb.AddListener(sl); + sb.RemoveListener(sl); + CPPUNIT_ASSERT_EQUAL(size_t(1), sb.GetListenerCount()); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(SfxBroadcasterTest); + +CPPUNIT_PLUGIN_IMPLEMENT(); |