Enable wxAny<double>::GetAs<unsigned long>() test.
[wxWidgets.git] / tests / events / timertest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/timertest.cpp
3 // Purpose: Test wxTimer events
4 // Author: Vadim Zeitlin
5 // Created: 2008-10-22
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #endif // WX_PRECOMP
22
23 #include <time.h>
24
25 #include "wx/evtloop.h"
26 #include "wx/timer.h"
27
28 // --------------------------------------------------------------------------
29 // helper class counting the number of timer events
30 // --------------------------------------------------------------------------
31
32 class TimerCounterHandler : public wxEvtHandler
33 {
34 public:
35 TimerCounterHandler()
36 {
37 m_events = 0;
38
39 Connect(wxEVT_TIMER, wxTimerEventHandler(TimerCounterHandler::OnTimer));
40 }
41
42 int GetNumEvents() const { return m_events; }
43
44 private:
45 void OnTimer(wxTimerEvent& WXUNUSED(event))
46 {
47 m_events++;
48
49 Tick();
50 }
51
52 virtual void Tick() { /* nothing to do in the base class */ }
53
54 int m_events;
55
56 DECLARE_NO_COPY_CLASS(TimerCounterHandler)
57 };
58
59 // --------------------------------------------------------------------------
60 // test class
61 // --------------------------------------------------------------------------
62
63 class TimerEventTestCase : public CppUnit::TestCase
64 {
65 public:
66 TimerEventTestCase() {}
67
68 private:
69 CPPUNIT_TEST_SUITE( TimerEventTestCase );
70 CPPUNIT_TEST( OneShot );
71 CPPUNIT_TEST( Multiple );
72 CPPUNIT_TEST_SUITE_END();
73
74 void OneShot();
75 void Multiple();
76
77 DECLARE_NO_COPY_CLASS(TimerEventTestCase)
78 };
79
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase );
82
83 // also include in its own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase, "TimerEventTestCase" );
85
86 void TimerEventTestCase::OneShot()
87 {
88 class ExitOnTimerHandler : public TimerCounterHandler
89 {
90 public:
91 ExitOnTimerHandler(wxEventLoopBase& loop)
92 : TimerCounterHandler(),
93 m_loop(loop)
94 {
95 }
96
97 private:
98 virtual void Tick() { m_loop.Exit(); }
99
100 wxEventLoopBase& m_loop;
101
102 // don't use DECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
103 };
104
105 wxEventLoop loop;
106
107 ExitOnTimerHandler handler(loop);
108 wxTimer timer(&handler);
109 timer.Start(200, true);
110
111 loop.Run();
112
113 CPPUNIT_ASSERT_EQUAL( 1, handler.GetNumEvents() );
114 }
115
116 void TimerEventTestCase::Multiple()
117 {
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
120 // can be fixed.
121 #if !defined(__WXGTK__) || wxUSE_UNICODE
122 wxEventLoop loop;
123
124 TimerCounterHandler handler;
125 wxTimer timer(&handler);
126 timer.Start(100);
127
128 // run the loop for 2 seconds
129 time_t t;
130 time(&t);
131 const time_t tEnd = t + 2;
132 while ( time(&t) < tEnd )
133 {
134 loop.Dispatch();
135 }
136
137 // we can't count on getting exactly 20 ticks but we shouldn't get more
138 // than this
139 const int numTicks = handler.GetNumEvents();
140 CPPUNIT_ASSERT( numTicks <= 20 );
141
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
145 // more than one
146 CPPUNIT_ASSERT( numTicks > 1 );
147 #endif // !(wxGTK Unicode)
148 }