]> git.saurik.com Git - wxWidgets.git/blame - tests/events/timertest.cpp
only run tests using multiline strings with multiline text control; document that...
[wxWidgets.git] / tests / events / timertest.cpp
CommitLineData
438febca
VZ
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// helper class counting the number of timer events
29class TimerCounterHandler : public wxEvtHandler
30{
31public:
32 TimerCounterHandler()
33 {
34 m_events = 0;
35
36 Connect(wxEVT_TIMER, wxTimerEventHandler(TimerCounterHandler::OnTimer));
37 }
38
39 int GetNumEvents() const { return m_events; }
40
41private:
42 void OnTimer(wxTimerEvent& WXUNUSED(event))
43 {
44 m_events++;
45
46 Tick();
47 }
48
49 virtual void Tick() { /* nothing to do in the base class */ }
50
51 int m_events;
52
53 DECLARE_NO_COPY_CLASS(TimerCounterHandler)
54};
55
56// --------------------------------------------------------------------------
57// test class
58// --------------------------------------------------------------------------
59
60class TimerEventTestCase : public CppUnit::TestCase
61{
62public:
63 TimerEventTestCase() {}
64
65private:
66 CPPUNIT_TEST_SUITE( TimerEventTestCase );
67 CPPUNIT_TEST( OneShot );
68 CPPUNIT_TEST( Multiple );
69 CPPUNIT_TEST_SUITE_END();
70
71 void OneShot();
72 void Multiple();
73
74 DECLARE_NO_COPY_CLASS(TimerEventTestCase)
75};
76
77// register in the unnamed registry so that these tests are run by default
78CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase );
79
80// also include in it's own registry so that these tests can be run alone
81CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase, "TimerEventTestCase" );
82
83void TimerEventTestCase::OneShot()
84{
85 class ExitOnTimerHandler : public TimerCounterHandler
86 {
87 public:
88 ExitOnTimerHandler(wxEventLoopBase& loop)
89 : TimerCounterHandler(),
90 m_loop(loop)
91 {
92 }
93
94 private:
95 virtual void Tick() { m_loop.Exit(); }
96
97 wxEventLoopBase& m_loop;
98
99 // don't use DECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
100 };
101
102 wxEventLoop loop;
103
104 ExitOnTimerHandler handler(loop);
105 wxTimer timer(&handler);
106 timer.Start(200, true);
107
108 loop.Run();
109
110 CPPUNIT_ASSERT_EQUAL( 1, handler.GetNumEvents() );
111}
112
113void TimerEventTestCase::Multiple()
114{
115 wxEventLoop loop;
116
117 TimerCounterHandler handler;
118 wxTimer timer(&handler);
119 timer.Start(100);
120
121 // run the loop for 2 seconds
122 time_t t;
123 time(&t);
124 const time_t tEnd = t + 2;
125 while ( time(&t) < tEnd )
126 {
127 loop.Dispatch();
128 }
129
130 // we can't count on getting exactly 20 ticks but we shouldn't get more
131 // than this
132 const int numTicks = handler.GetNumEvents();
133 CPPUNIT_ASSERT( numTicks <= 20 );
134
2057acc2
VZ
135 // and we should get a decent number of them but if the system is very
136 // loaded (as happens with build bot slaves running a couple of builds in
137 // parallel actually) it may be much less than 20 so just check that we get
138 // more than one
139 CPPUNIT_ASSERT( numTicks > 1 );
438febca 140}