summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Lippert <drtl@fastmail.fm>2014-06-20 11:01:30 +0200
committerNoel Grandin <noelgrandin@gmail.com>2014-07-22 06:25:22 +0000
commit312926823dc6da7e87eb60c98ba084f14f0aa676 (patch)
treedb1185fbe2ffc98430faff9a56bafbf78844e163
parentf413cb08b8c0f67b9bf7278dd654dac9001cf490 (diff)
Add unittests for SfxBroadcaster
Change-Id: I9ecd8fec395eafbdc2675dda9fa25ac32761123c Reviewed-on: https://gerrit.libreoffice.org/10343 Reviewed-by: Noel Grandin <noelgrandin@gmail.com> Tested-by: Noel Grandin <noelgrandin@gmail.com>
-rw-r--r--include/svl/SfxBroadcaster.hxx7
-rw-r--r--svl/CppunitTest_svl_notify.mk36
-rw-r--r--svl/Module_svl.mk1
-rw-r--r--svl/qa/unit/notify/test_SfxBroadcaster.cxx98
4 files changed, 139 insertions, 3 deletions
diff --git a/include/svl/SfxBroadcaster.hxx b/include/svl/SfxBroadcaster.hxx
index 8fd62b9cc27f..5c135b5dfca2 100644
--- a/include/svl/SfxBroadcaster.hxx
+++ b/include/svl/SfxBroadcaster.hxx
@@ -25,12 +25,10 @@
class SfxListener;
class SfxHint;
-
-
+class SfxBroadcasterTest;
class SVL_DLLPUBLIC SfxBroadcaster
{
- friend class SfxListener;
typedef std::vector<SfxListener*> SfxListenerArr_Impl;
SfxListenerArr_Impl m_Listeners;
@@ -61,6 +59,9 @@ public:
{
return m_Listeners[nNo];
}
+
+ friend class SfxListener;
+ friend class ::SfxBroadcasterTest;
};
#endif
diff --git a/svl/CppunitTest_svl_notify.mk b/svl/CppunitTest_svl_notify.mk
new file mode 100644
index 000000000000..ff1dbe7cfb8c
--- /dev/null
+++ b/svl/CppunitTest_svl_notify.mk
@@ -0,0 +1,36 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
+#
+# 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/.
+#
+
+$(eval $(call gb_CppunitTest_CppunitTest,svl_notify))
+
+$(eval $(call gb_CppunitTest_use_external,svl_notify,boost_headers))
+
+$(eval $(call gb_CppunitTest_use_api,svl_notify, \
+ offapi \
+ udkapi \
+))
+
+$(eval $(call gb_CppunitTest_add_exception_objects,svl_notify, \
+ svl/qa/unit/notify/test_SfxBroadcaster \
+))
+
+$(eval $(call gb_CppunitTest_use_libraries,svl_notify, \
+ svl \
+ comphelper \
+ sal \
+ cppu \
+ cppuhelper \
+))
+
+$(eval $(call gb_CppunitTest_set_include,svl_notify,\
+ -I$(SRCDIR)/svl/source/inc \
+ $$(INCLUDE) \
+))
+
+# vim: set noet sw=4 ts=4:
diff --git a/svl/Module_svl.mk b/svl/Module_svl.mk
index 75890301e31b..54f6b6c6073e 100644
--- a/svl/Module_svl.mk
+++ b/svl/Module_svl.mk
@@ -35,6 +35,7 @@ $(eval $(call gb_Module_add_check_targets,svl,\
CppunitTest_svl_inetcontenttype \
CppunitTest_svl_items \
CppunitTest_svl_itempool \
+ CppunitTest_svl_notify \
))
#TODO: CppunitTest_svl_urihelper depends on ucb, can only be added once svl is
diff --git a/svl/qa/unit/notify/test_SfxBroadcaster.cxx b/svl/qa/unit/notify/test_SfxBroadcaster.cxx
new file mode 100644
index 000000000000..8139ed1eba2d
--- /dev/null
+++ b/svl/qa/unit/notify/test_SfxBroadcaster.cxx
@@ -0,0 +1,98 @@
+/* -*- 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();
+
+ // Adds code needed to register the test suite
+ CPPUNIT_TEST_SUITE(SfxBroadcasterTest);
+ CPPUNIT_TEST(AddingListenersIncreasesCount);
+ //CPPUNIT_TEST(RemovingListenersDecreasesCount);
+ CPPUNIT_TEST(HintsAreNotForwardedToRemovedListeners);
+
+ CPPUNIT_TEST_SUITE_END();
+};
+
+class MockedSfxListener : public SfxListener
+{
+public:
+ MockedSfxListener()
+ : mNotifyWasCalled(false) {}
+
+ void Notify(SfxBroadcaster& rBC, const SfxHint& rHint) SAL_OVERRIDE {
+ (void)(rBC); (void)(rHint); // avoid warnings about unused parameters
+ 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, true);
+ 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, true);
+ 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, true);
+ sl2.StartListening(sb, true);
+ 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());
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(SfxBroadcasterTest);
+
+CPPUNIT_PLUGIN_IMPLEMENT();