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 // --------------------------------------------------------------------------
29 // helper class counting the number of timer events
30 // --------------------------------------------------------------------------
32 class TimerCounterHandler
: public wxEvtHandler
39 Connect(wxEVT_TIMER
, wxTimerEventHandler(TimerCounterHandler::OnTimer
));
42 int GetNumEvents() const { return m_events
; }
45 void OnTimer(wxTimerEvent
& WXUNUSED(event
))
52 virtual void Tick() { /* nothing to do in the base class */ }
56 DECLARE_NO_COPY_CLASS(TimerCounterHandler
)
59 // --------------------------------------------------------------------------
61 // --------------------------------------------------------------------------
63 class TimerEventTestCase
: public CppUnit::TestCase
66 TimerEventTestCase() {}
69 CPPUNIT_TEST_SUITE( TimerEventTestCase
);
70 CPPUNIT_TEST( OneShot
);
71 CPPUNIT_TEST( Multiple
);
72 CPPUNIT_TEST_SUITE_END();
77 DECLARE_NO_COPY_CLASS(TimerEventTestCase
)
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase
);
83 // also include in it's own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase
, "TimerEventTestCase" );
86 void TimerEventTestCase::OneShot()
88 class ExitOnTimerHandler
: public TimerCounterHandler
91 ExitOnTimerHandler(wxEventLoopBase
& loop
)
92 : TimerCounterHandler(),
98 virtual void Tick() { m_loop
.Exit(); }
100 wxEventLoopBase
& m_loop
;
102 // don't use DECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
107 ExitOnTimerHandler
handler(loop
);
108 wxTimer
timer(&handler
);
109 timer
.Start(200, true);
113 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetNumEvents() );
116 void TimerEventTestCase::Multiple()
118 // FIXME: This test crashes on wxGTK ANSI build slave for unknown reason,
119 // disable it here to let the rest of the test suite run until this
121 #if !defined(__WXGTK__) || wxUSE_UNICODE
124 TimerCounterHandler handler
;
125 wxTimer
timer(&handler
);
128 // run the loop for 2 seconds
131 const time_t tEnd
= t
+ 2;
132 while ( time(&t
) < tEnd
)
137 // we can't count on getting exactly 20 ticks but we shouldn't get more
139 const int numTicks
= handler
.GetNumEvents();
140 CPPUNIT_ASSERT( numTicks
<= 20 );
142 // and we should get a decent number of them but if the system is very
143 // loaded (as happens with build bot slaves running a couple of builds in
144 // parallel actually) it may be much less than 20 so just check that we get
146 CPPUNIT_ASSERT( numTicks
> 1 );
147 #endif // !(wxGTK Unicode)