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