]> git.saurik.com Git - wxWidgets.git/blob - tests/events/timertest.cpp
Fix compilation of wxSpinCtrlGenericBase when PCH are not used.
[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 it's 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 wxEventLoop loop;
119
120 TimerCounterHandler handler;
121 wxTimer timer(&handler);
122 timer.Start(100);
123
124 // run the loop for 2 seconds
125 time_t t;
126 time(&t);
127 const time_t tEnd = t + 2;
128 while ( time(&t) < tEnd )
129 {
130 loop.Dispatch();
131 }
132
133 // we can't count on getting exactly 20 ticks but we shouldn't get more
134 // than this
135 const int numTicks = handler.GetNumEvents();
136 CPPUNIT_ASSERT( numTicks <= 20 );
137
138 // and we should get a decent number of them but if the system is very
139 // loaded (as happens with build bot slaves running a couple of builds in
140 // parallel actually) it may be much less than 20 so just check that we get
141 // more than one
142 CPPUNIT_ASSERT( numTicks > 1 );
143 }