From b014150e64cdc23dfd999061bc210f0ad701f0a2 Mon Sep 17 00:00:00 2001 From: Tobias Lippert Date: Tue, 9 Jun 2015 21:25:40 +0200 Subject: tdf#68016 Write fixture for current behaviour of SfxListener Also: Make destructor of SfxListener more robust, so that it can be used in tests. Change-Id: I02b273ca8e527705c2d3ea3295ed0dec1c4f83ae Reviewed-on: https://gerrit.libreoffice.org/16483 Tested-by: Jenkins Reviewed-by: Markus Mohrhard Tested-by: Markus Mohrhard --- svl/qa/unit/notify/test_SfxBroadcaster.cxx | 25 +++++++++++++++++++++++++ svl/source/notify/lstner.cxx | 5 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'svl') diff --git a/svl/qa/unit/notify/test_SfxBroadcaster.cxx b/svl/qa/unit/notify/test_SfxBroadcaster.cxx index 60960295ccc7..292cd9dcbe93 100644 --- a/svl/qa/unit/notify/test_SfxBroadcaster.cxx +++ b/svl/qa/unit/notify/test_SfxBroadcaster.cxx @@ -22,12 +22,16 @@ 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(); }; @@ -93,6 +97,27 @@ SfxBroadcasterTest::HintsAreNotForwardedToRemovedListeners() 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(); diff --git a/svl/source/notify/lstner.cxx b/svl/source/notify/lstner.cxx index ee0809cd0db7..4fc3426b8bd4 100644 --- a/svl/source/notify/lstner.cxx +++ b/svl/source/notify/lstner.cxx @@ -68,7 +68,10 @@ SfxListener::~SfxListener() void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster& rBroadcaster ) { - mpImpl->maBCs.erase( std::find( mpImpl->maBCs.begin(), mpImpl->maBCs.end(), &rBroadcaster ) ); + auto it = std::find( mpImpl->maBCs.begin(), mpImpl->maBCs.end(), &rBroadcaster ); + if (it != mpImpl->maBCs.end()) { + mpImpl->maBCs.erase( it ); + } } -- cgit v1.2.3