File: evtconnection.cpp

package info (click to toggle)
wxwidgets3.0 3.0.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 120,464 kB
  • sloc: cpp: 896,633; makefile: 52,303; ansic: 21,971; sh: 5,713; python: 2,940; xml: 1,534; perl: 264; javascript: 33
file content (246 lines) | stat: -rw-r--r-- 6,634 bytes parent folder | download | duplicates (7)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/weakref/evtconnection.cpp
// Purpose:     wxWeakRef<T> unit test
// Author:      Arne Steinarson
// Created:     2008-01-10
// Copyright:   (c) 2007 Arne Steinarson
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "testprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif // WX_PRECOMP

#include "wx/event.h"
#include "wx/weakref.h"

static int gs_value = 0; // Increased by 1 by first object and 0x10000 by 2nd object

static wxObject *gs_psrc1;
static wxObject *gs_psrc2;

// We need some event types
const wxEventType wxEVT_TEST = wxNewEventType(),
                  wxEVT_TEST1 = wxNewEventType(),
                  wxEVT_TEST2 = wxNewEventType();

class wxTestEvent : public wxEvent
{
public:
    wxTestEvent(wxEventType type = wxEVT_TEST) : wxEvent(0, type) { }
    virtual wxEvent *Clone() const { return new wxTestEvent(GetEventType()); }
};

class wxTestSink : public wxEvtHandler
{
public:
    void OnTestEvent(wxEvent& evt)
    {
        if ( evt.GetEventObject() == gs_psrc1 )
            gs_value += 1;
        else if ( evt.GetEventObject() == gs_psrc2 )
            gs_value += 0x10000;
    }

    void OnTestEvent1(wxEvent& )
    {
        gs_value += 0x00100;
    }

    void OnTestEvent2(wxEvent&)
    {
        gs_value += 0x01000000;
    }
};



// --------------------------------------------------------------------------
// test class
// --------------------------------------------------------------------------

class EvtConnectionTestCase : public CppUnit::TestCase
{
public:
    EvtConnectionTestCase() {}

private:
    CPPUNIT_TEST_SUITE( EvtConnectionTestCase );
        CPPUNIT_TEST( SinkTest );
        CPPUNIT_TEST( SourceDestroyTest );
        CPPUNIT_TEST( MultiConnectionTest );
    CPPUNIT_TEST_SUITE_END();

    void SinkTest();
    void SourceDestroyTest();
    void MultiConnectionTest();

    DECLARE_NO_COPY_CLASS(EvtConnectionTestCase)
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( EvtConnectionTestCase );

// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtConnectionTestCase, "EvtConnectionTestCase" );


// Helpers
void DoConnect( wxEvtHandler& eh1, wxEvtHandler& eh2, wxTestSink& ts ){
    eh1.Connect(wxEVT_TEST, (wxObjectEventFunction)&wxTestSink::OnTestEvent,
                NULL, &ts);
    eh2.Connect(wxEVT_TEST, (wxObjectEventFunction) &wxTestSink::OnTestEvent,
                NULL, &ts);
}

void DoDisconnect( wxEvtHandler& eh1, wxEvtHandler& eh2, wxTestSink& ts ){
    eh1.Disconnect(wxEVT_TEST, (wxObjectEventFunction) &wxTestSink::OnTestEvent,
                NULL, &ts);
    eh2.Disconnect(wxEVT_TEST, (wxObjectEventFunction) &wxTestSink::OnTestEvent,
                NULL, &ts);
}


void EvtConnectionTestCase::SinkTest()
{
    // Let the sink be destroyed before the sources

    // An event used below
    wxTestEvent evt;

    // Connect two event handlers to one sink
    wxEvtHandler eh1, eh2;
    gs_psrc1 = &eh1;
    gs_psrc2 = &eh2;

    {
        wxTestSink ts;
        CPPUNIT_ASSERT( !ts.GetFirst() );
        DoConnect(eh1, eh2, ts);

        DoDisconnect(eh1, eh2, ts);

        DoConnect(eh1, eh2, ts);

        // Fire events
        evt.SetEventObject(&eh1);
        eh1.ProcessEvent(evt);
        evt.SetEventObject(&eh2);
        eh2.ProcessEvent(evt);

        // Make sure they were processed correctly
        CPPUNIT_ASSERT_EQUAL( 0x00010001, gs_value );
    }

    // Fire events again, should be no sink connected now
    gs_value = 0;
    evt.SetEventObject(&eh1);
    eh1.ProcessEvent( evt );
    evt.SetEventObject(&eh2);
    eh2.ProcessEvent( evt );

    // Make sure no processing happened
    CPPUNIT_ASSERT_EQUAL( 0, gs_value );
}

void EvtConnectionTestCase::SourceDestroyTest()
{
    // Let the sources be destroyed before the sink
    wxTestSink ts;
    wxTestEvent evt;
    {
        wxEvtHandler eh1;
        {
            CPPUNIT_ASSERT( !ts.GetFirst() );

            // Connect two event handlers to one sink
            wxEvtHandler eh2;
            gs_psrc1 = &eh1;
            gs_psrc2 = &eh2;
            DoConnect( eh1, eh2, ts );

            // Fire events
            gs_value = 0;
            evt.SetEventObject(&eh1);
            eh1.ProcessEvent( evt );
            evt.SetEventObject(&eh2);
            eh2.ProcessEvent( evt );

            // Make sure they were processed correctly
            CPPUNIT_ASSERT_EQUAL( 0x00010001, gs_value );
        }

        gs_value = 0;
        evt.SetEventObject(&eh1);
        eh1.ProcessEvent( evt );

        // Make sure still connected
        CPPUNIT_ASSERT_EQUAL( 0x00000001, gs_value );
    }
    CPPUNIT_ASSERT( !ts.GetFirst() );
}

void EvtConnectionTestCase::MultiConnectionTest()
{
    // events used below
    wxTestEvent evt;
    wxTestEvent evt1(wxEVT_TEST1);
    wxTestEvent evt2(wxEVT_TEST2);

    // One source
    wxEvtHandler eh1;
    evt.SetEventObject(&eh1);
    gs_psrc1 = NULL;
    gs_psrc2 = &eh1;

    {
        // ...and one sink
        wxTestSink ts;

        eh1.Connect(wxEVT_TEST, (wxObjectEventFunction)&wxTestSink::OnTestEvent,
                    NULL, &ts);
        eh1.Connect(wxEVT_TEST1, (wxObjectEventFunction)&wxTestSink::OnTestEvent1,
                    NULL, &ts);
        eh1.Connect(wxEVT_TEST2, (wxObjectEventFunction)&wxTestSink::OnTestEvent2,
                    NULL, &ts);

        // Generate events
        gs_value = 0;
        eh1.ProcessEvent(evt);
        eh1.ProcessEvent(evt1);
        eh1.ProcessEvent(evt2);
        CPPUNIT_ASSERT( gs_value==0x01010100 );

        {
            // Declare weak references to the objects (using same list)
            wxEvtHandlerRef re(&eh1), rs(&ts);
        }
        // And now destroyed

        eh1.Disconnect(wxEVT_TEST, (wxObjectEventFunction)&wxTestSink::OnTestEvent,
                       NULL, &ts);
        eh1.ProcessEvent(evt);
        eh1.ProcessEvent(evt1);
        eh1.ProcessEvent(evt2);
        CPPUNIT_ASSERT_EQUAL( 0x02010200, gs_value );
    }

    // No connection should be left now
    gs_value = 0;
    eh1.ProcessEvent(evt);
    eh1.ProcessEvent(evt1);
    eh1.ProcessEvent(evt2);

    // Nothing should have been done
    CPPUNIT_ASSERT_EQUAL( 0, gs_value );
}