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 18 : 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 4 : CPPUNIT_TEST_SUITE(SfxBroadcasterTest);
28 2 : CPPUNIT_TEST(AddingListenersIncreasesCount);
29 2 : CPPUNIT_TEST(RemovingListenersDecreasesCount);
30 2 : CPPUNIT_TEST(HintsAreNotForwardedToRemovedListeners);
31 :
32 4 : CPPUNIT_TEST_SUITE_END();
33 : };
34 :
35 8 : class MockedSfxListener : public SfxListener
36 : {
37 : public:
38 8 : MockedSfxListener()
39 8 : : mNotifyWasCalled(false) {}
40 :
41 2 : void Notify(SfxBroadcaster& rBC, const SfxHint& rHint) SAL_OVERRIDE {
42 : (void)(rBC); (void)(rHint); // avoid warnings about unused parameters
43 2 : mNotifyWasCalled = true;
44 2 : }
45 :
46 4 : bool NotifyWasCalled() const {
47 4 : return mNotifyWasCalled;
48 : }
49 :
50 : private:
51 : bool mNotifyWasCalled;
52 : };
53 :
54 : void
55 2 : SfxBroadcasterTest::AddingListenersIncreasesCount()
56 : {
57 2 : SfxBroadcaster sb;
58 4 : MockedSfxListener sl;
59 :
60 2 : CPPUNIT_ASSERT_EQUAL((size_t)0, sb.GetListenerCount());
61 :
62 2 : sl.StartListening(sb, true);
63 4 : CPPUNIT_ASSERT_EQUAL((size_t)1, sb.GetListenerCount());
64 2 : }
65 :
66 : void
67 2 : SfxBroadcasterTest::RemovingListenersDecreasesCount()
68 : {
69 2 : SfxBroadcaster sb;
70 4 : MockedSfxListener sl;
71 :
72 2 : CPPUNIT_ASSERT_EQUAL((size_t)0, sb.GetListenerCount());
73 2 : sl.StartListening(sb, true);
74 2 : CPPUNIT_ASSERT_EQUAL((size_t)1, sb.GetListenerCount());
75 2 : sl.EndListening(sb, true);
76 4 : CPPUNIT_ASSERT_EQUAL((size_t)0, sb.GetListenerCount());
77 2 : }
78 :
79 : void
80 2 : SfxBroadcasterTest::HintsAreNotForwardedToRemovedListeners()
81 : {
82 2 : SfxBroadcaster sb;
83 4 : MockedSfxListener sl1;
84 4 : MockedSfxListener sl2;
85 4 : SfxHint hint;
86 :
87 2 : sl1.StartListening(sb, true);
88 2 : sl2.StartListening(sb, true);
89 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("All listeners were added.", (size_t)2, sb.GetListenerCount());
90 2 : sl1.EndListening(sb, true);
91 2 : sb.Forward(sb, hint);
92 2 : CPPUNIT_ASSERT_EQUAL(true, sl2.NotifyWasCalled());
93 4 : CPPUNIT_ASSERT_EQUAL(false, sl1.NotifyWasCalled());
94 2 : }
95 :
96 2 : CPPUNIT_TEST_SUITE_REGISTRATION(SfxBroadcasterTest);
97 :
98 8 : CPPUNIT_PLUGIN_IMPLEMENT();
|