Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <svl/SfxBroadcaster.hxx>
11 :
12 : #include <svl/lstner.hxx>
13 : #include <svl/hint.hxx>
14 :
15 : #include <cppunit/TestAssert.h>
16 : #include <cppunit/TestFixture.h>
17 : #include <cppunit/extensions/HelperMacros.h>
18 : #include <cppunit/plugin/TestPlugIn.h>
19 :
20 9 : class SfxBroadcasterTest : public CppUnit::TestFixture
21 : {
22 : void AddingListenersIncreasesCount();
23 : void RemovingListenersDecreasesCount();
24 : void HintsAreNotForwardedToRemovedListeners();
25 :
26 : // Adds code needed to register the test suite
27 2 : CPPUNIT_TEST_SUITE(SfxBroadcasterTest);
28 1 : CPPUNIT_TEST(AddingListenersIncreasesCount);
29 1 : CPPUNIT_TEST(RemovingListenersDecreasesCount);
30 1 : CPPUNIT_TEST(HintsAreNotForwardedToRemovedListeners);
31 :
32 5 : CPPUNIT_TEST_SUITE_END();
33 : };
34 :
35 4 : class MockedSfxListener : public SfxListener
36 : {
37 : public:
38 4 : MockedSfxListener()
39 4 : : mNotifyWasCalled(false) {}
40 :
41 1 : void Notify(SfxBroadcaster& rBC, const SfxHint& rHint) SAL_OVERRIDE {
42 : (void)(rBC); (void)(rHint); // avoid warnings about unused parameters
43 1 : mNotifyWasCalled = true;
44 1 : }
45 :
46 2 : bool NotifyWasCalled() const {
47 2 : return mNotifyWasCalled;
48 : }
49 :
50 : private:
51 : bool mNotifyWasCalled;
52 : };
53 :
54 : void
55 1 : SfxBroadcasterTest::AddingListenersIncreasesCount()
56 : {
57 1 : SfxBroadcaster sb;
58 2 : MockedSfxListener sl;
59 :
60 1 : CPPUNIT_ASSERT_EQUAL((size_t)0, sb.GetListenerCount());
61 :
62 1 : sl.StartListening(sb, true);
63 2 : CPPUNIT_ASSERT_EQUAL((size_t)1, sb.GetListenerCount());
64 1 : }
65 :
66 : void
67 1 : SfxBroadcasterTest::RemovingListenersDecreasesCount()
68 : {
69 1 : SfxBroadcaster sb;
70 2 : MockedSfxListener sl;
71 :
72 1 : CPPUNIT_ASSERT_EQUAL((size_t)0, sb.GetListenerCount());
73 1 : sl.StartListening(sb, true);
74 1 : CPPUNIT_ASSERT_EQUAL((size_t)1, sb.GetListenerCount());
75 1 : sl.EndListening(sb, true);
76 2 : CPPUNIT_ASSERT_EQUAL((size_t)0, sb.GetListenerCount());
77 1 : }
78 :
79 : void
80 1 : SfxBroadcasterTest::HintsAreNotForwardedToRemovedListeners()
81 : {
82 1 : SfxBroadcaster sb;
83 2 : MockedSfxListener sl1;
84 2 : MockedSfxListener sl2;
85 2 : SfxHint hint;
86 :
87 1 : sl1.StartListening(sb, true);
88 1 : sl2.StartListening(sb, true);
89 1 : CPPUNIT_ASSERT_EQUAL_MESSAGE("All listeners were added.", (size_t)2, sb.GetListenerCount());
90 1 : sl1.EndListening(sb, true);
91 1 : sb.Forward(sb, hint);
92 1 : CPPUNIT_ASSERT_EQUAL(true, sl2.NotifyWasCalled());
93 2 : CPPUNIT_ASSERT_EQUAL(false, sl1.NotifyWasCalled());
94 1 : }
95 :
96 1 : CPPUNIT_TEST_SUITE_REGISTRATION(SfxBroadcasterTest);
97 :
98 4 : CPPUNIT_PLUGIN_IMPLEMENT();
|