summaryrefslogtreecommitdiffstats
path: root/svl/qa/unit/notify/test_SfxBroadcaster.cxx
blob: 79b6543ad6814357d07b9b1a0e83492d8ffbd844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* -*- 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();