File: timertest.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 (147 lines) | stat: -rw-r--r-- 3,855 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
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/events/timertest.cpp
// Purpose:     Test wxTimer events
// Author:      Vadim Zeitlin
// Created:     2008-10-22
// Copyright:   (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////

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

#include "testprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
#endif // WX_PRECOMP

#include <time.h>

#include "wx/evtloop.h"
#include "wx/timer.h"

// --------------------------------------------------------------------------
// helper class counting the number of timer events
// --------------------------------------------------------------------------

class TimerCounterHandler : public wxEvtHandler
{
public:
    TimerCounterHandler()
    {
        m_events = 0;

        Connect(wxEVT_TIMER, wxTimerEventHandler(TimerCounterHandler::OnTimer));
    }

    int GetNumEvents() const { return m_events; }

private:
    void OnTimer(wxTimerEvent& WXUNUSED(event))
    {
        m_events++;

        Tick();
    }

    virtual void Tick() { /* nothing to do in the base class */ }

    int m_events;

    DECLARE_NO_COPY_CLASS(TimerCounterHandler)
};

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

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

private:
    CPPUNIT_TEST_SUITE( TimerEventTestCase );
        CPPUNIT_TEST( OneShot );
        CPPUNIT_TEST( Multiple );
    CPPUNIT_TEST_SUITE_END();

    void OneShot();
    void Multiple();

    DECLARE_NO_COPY_CLASS(TimerEventTestCase)
};

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

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

void TimerEventTestCase::OneShot()
{
    class ExitOnTimerHandler : public TimerCounterHandler
    {
    public:
        ExitOnTimerHandler(wxEventLoopBase& loop)
            : TimerCounterHandler(),
              m_loop(loop)
        {
        }

    private:
        virtual void Tick() { m_loop.Exit(); }

        wxEventLoopBase& m_loop;

        // don't use DECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
    };

    wxEventLoop loop;

    ExitOnTimerHandler handler(loop);
    wxTimer timer(&handler);
    timer.Start(200, true);

    loop.Run();

    CPPUNIT_ASSERT_EQUAL( 1, handler.GetNumEvents() );
}

void TimerEventTestCase::Multiple()
{
    // FIXME: This test crashes on wxGTK ANSI build slave for unknown reason,
    //        disable it here to let the rest of the test suite run until this
    //        can be fixed.
#if !defined(__WXGTK__) || wxUSE_UNICODE
    wxEventLoop loop;

    TimerCounterHandler handler;
    wxTimer timer(&handler);
    timer.Start(100);

    // run the loop for 2 seconds
    time_t t;
    time(&t);
    const time_t tEnd = t + 2;
    while ( time(&t) < tEnd )
    {
        loop.Dispatch();
    }

    // we can't count on getting exactly 20 ticks but we shouldn't get more
    // than this
    const int numTicks = handler.GetNumEvents();
    CPPUNIT_ASSERT( numTicks <= 20 );

    // and we should get a decent number of them but if the system is very
    // loaded (as happens with build bot slaves running a couple of builds in
    // parallel actually) it may be much less than 20 so just check that we get
    // more than one
    CPPUNIT_ASSERT( numTicks > 1 );
#endif // !(wxGTK Unicode)
}