]> git.saurik.com Git - wxWidgets.git/blame - tests/events/timertest.cpp
Better fix for duplicate wxContextMenuEvent generation under MSW.
[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
45cb7053 28// --------------------------------------------------------------------------
438febca 29// helper class counting the number of timer events
45cb7053
FM
30// --------------------------------------------------------------------------
31
438febca
VZ
32class TimerCounterHandler : public wxEvtHandler
33{
34public:
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
44private:
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
63class TimerEventTestCase : public CppUnit::TestCase
64{
65public:
66 TimerEventTestCase() {}
67
68private:
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
81CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase );
82
e3778b4d 83// also include in its own registry so that these tests can be run alone
438febca
VZ
84CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase, "TimerEventTestCase" );
85
86void 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
116void TimerEventTestCase::Multiple()
117{
9a1ff951
VZ
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
438febca
VZ
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
2057acc2
VZ
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 );
9a1ff951 147#endif // !(wxGTK Unicode)
438febca 148}