1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/timertest.cpp
3 // Purpose: Test wxTimer events
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "wx/evtloop.h"
28 // helper class counting the number of timer events
29 class TimerCounterHandler
: public wxEvtHandler
36 Connect(wxEVT_TIMER
, wxTimerEventHandler(TimerCounterHandler::OnTimer
));
39 int GetNumEvents() const { return m_events
; }
42 void OnTimer(wxTimerEvent
& WXUNUSED(event
))
49 virtual void Tick() { /* nothing to do in the base class */ }
53 DECLARE_NO_COPY_CLASS(TimerCounterHandler
)
56 // --------------------------------------------------------------------------
58 // --------------------------------------------------------------------------
60 class TimerEventTestCase
: public CppUnit::TestCase
63 TimerEventTestCase() {}
66 CPPUNIT_TEST_SUITE( TimerEventTestCase
);
67 CPPUNIT_TEST( OneShot
);
68 CPPUNIT_TEST( Multiple
);
69 CPPUNIT_TEST_SUITE_END();
74 DECLARE_NO_COPY_CLASS(TimerEventTestCase
)
77 // register in the unnamed registry so that these tests are run by default
78 CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase
);
80 // also include in it's own registry so that these tests can be run alone
81 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase
, "TimerEventTestCase" );
83 void TimerEventTestCase::OneShot()
85 class ExitOnTimerHandler
: public TimerCounterHandler
88 ExitOnTimerHandler(wxEventLoopBase
& loop
)
89 : TimerCounterHandler(),
95 virtual void Tick() { m_loop
.Exit(); }
97 wxEventLoopBase
& m_loop
;
99 // don't use DECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
104 ExitOnTimerHandler
handler(loop
);
105 wxTimer
timer(&handler
);
106 timer
.Start(200, true);
110 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetNumEvents() );
113 void TimerEventTestCase::Multiple()
117 TimerCounterHandler handler
;
118 wxTimer
timer(&handler
);
121 // run the loop for 2 seconds
124 const time_t tEnd
= t
+ 2;
125 while ( time(&t
) < tEnd
)
130 // we can't count on getting exactly 20 ticks but we shouldn't get more
132 const int numTicks
= handler
.GetNumEvents();
133 CPPUNIT_ASSERT( numTicks
<= 20 );
135 // and we should get a decent number of them (unless the system is horribly
136 // loaded so if it does happen that this test fails we may need to remove
138 CPPUNIT_ASSERT( numTicks
> 10 );