1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/timertest.cpp
3 // Purpose: Test wxTimer events
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
24 #include "wx/evtloop.h"
27 // --------------------------------------------------------------------------
28 // helper class counting the number of timer events
29 // --------------------------------------------------------------------------
31 class TimerCounterHandler
: public wxEvtHandler
38 Connect(wxEVT_TIMER
, wxTimerEventHandler(TimerCounterHandler::OnTimer
));
41 int GetNumEvents() const { return m_events
; }
44 void OnTimer(wxTimerEvent
& WXUNUSED(event
))
51 virtual void Tick() { /* nothing to do in the base class */ }
55 DECLARE_NO_COPY_CLASS(TimerCounterHandler
)
58 // --------------------------------------------------------------------------
60 // --------------------------------------------------------------------------
62 class TimerEventTestCase
: public CppUnit::TestCase
65 TimerEventTestCase() {}
68 CPPUNIT_TEST_SUITE( TimerEventTestCase
);
69 CPPUNIT_TEST( OneShot
);
70 CPPUNIT_TEST( Multiple
);
71 CPPUNIT_TEST_SUITE_END();
76 DECLARE_NO_COPY_CLASS(TimerEventTestCase
)
79 // register in the unnamed registry so that these tests are run by default
80 CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase
);
82 // also include in its own registry so that these tests can be run alone
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase
, "TimerEventTestCase" );
85 void TimerEventTestCase::OneShot()
87 class ExitOnTimerHandler
: public TimerCounterHandler
90 ExitOnTimerHandler(wxEventLoopBase
& loop
)
91 : TimerCounterHandler(),
97 virtual void Tick() { m_loop
.Exit(); }
99 wxEventLoopBase
& m_loop
;
101 // don't use DECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
106 ExitOnTimerHandler
handler(loop
);
107 wxTimer
timer(&handler
);
108 timer
.Start(200, true);
112 CPPUNIT_ASSERT_EQUAL( 1, handler
.GetNumEvents() );
115 void TimerEventTestCase::Multiple()
117 // FIXME: This test crashes on wxGTK ANSI build slave for unknown reason,
118 // disable it here to let the rest of the test suite run until this
120 #if !defined(__WXGTK__) || wxUSE_UNICODE
123 TimerCounterHandler handler
;
124 wxTimer
timer(&handler
);
127 // run the loop for 2 seconds
130 const time_t tEnd
= t
+ 2;
131 while ( time(&t
) < tEnd
)
136 // we can't count on getting exactly 20 ticks but we shouldn't get more
138 const int numTicks
= handler
.GetNumEvents();
139 CPPUNIT_ASSERT( numTicks
<= 20 );
141 // and we should get a decent number of them but if the system is very
142 // loaded (as happens with build bot slaves running a couple of builds in
143 // parallel actually) it may be much less than 20 so just check that we get
145 CPPUNIT_ASSERT( numTicks
> 1 );
146 #endif // !(wxGTK Unicode)