]> git.saurik.com Git - wxWidgets.git/blame - tests/events/stopwatch.cpp
Fix horizontal mouse wheel scrolling in wxGTK.
[wxWidgets.git] / tests / events / stopwatch.cpp
CommitLineData
45cb7053
FM
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/events/stopwatch.cpp
3// Purpose: Test wxStopWatch class
4// Author: Francesco Montorsi (extracted from console sample)
5// Created: 2010-05-16
45cb7053
FM
6// Copyright: (c) 2010 wxWidgets team
7///////////////////////////////////////////////////////////////////////////////
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/stopwatch.h"
26#include "wx/utils.h"
27
17d72a48
VZ
28namespace
29{
30
d1301675 31const long tolerance = 50; // in ms
17d72a48
VZ
32const int sleepTime = 500;
33
34} // anonymous namespace
35
45cb7053
FM
36// --------------------------------------------------------------------------
37// test class
38// --------------------------------------------------------------------------
39
40class StopWatchTestCase : public CppUnit::TestCase
41{
42public:
43 StopWatchTestCase() {}
44
45private:
46 CPPUNIT_TEST_SUITE( StopWatchTestCase );
47 CPPUNIT_TEST( Misc );
48 CPPUNIT_TEST( BackwardsClockBug );
17d72a48 49 CPPUNIT_TEST( RestartBug );
45cb7053
FM
50 CPPUNIT_TEST_SUITE_END();
51
52 void Misc();
53 void BackwardsClockBug();
17d72a48 54 void RestartBug();
45cb7053
FM
55
56 DECLARE_NO_COPY_CLASS(StopWatchTestCase)
57};
58
59// register in the unnamed registry so that these tests are run by default
60CPPUNIT_TEST_SUITE_REGISTRATION( StopWatchTestCase );
61
e3778b4d 62// also include in its own registry so that these tests can be run alone
45cb7053
FM
63CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StopWatchTestCase, "StopWatchTestCase" );
64
65void StopWatchTestCase::Misc()
66{
67 wxStopWatch sw;
232fdc63 68 long t;
b0ec0023 69 wxLongLong usec;
45cb7053
FM
70
71 sw.Pause(); // pause it immediately
72
b0ec0023
VZ
73 // verify that almost no time elapsed
74 usec = sw.TimeInMicro();
75 WX_ASSERT_MESSAGE
76 (
77 ("Elapsed time was %" wxLongLongFmtSpec "dus", usec),
78 usec < tolerance*1000
79 );
80
c4399985 81 wxSleep(1);
232fdc63
VZ
82 t = sw.Time();
83
84 // check that the stop watch doesn't advance while paused
85 WX_ASSERT_MESSAGE
86 (
87 ("Actual time value is %ld", t),
88 t >= 0 && t < tolerance
89 );
45cb7053
FM
90
91 sw.Resume();
c4399985 92 wxMilliSleep(sleepTime);
232fdc63 93 t = sw.Time();
c4399985 94 // check that it did advance now by ~1.5s
232fdc63
VZ
95 WX_ASSERT_MESSAGE
96 (
97 ("Actual time value is %ld", t),
c4399985 98 t > sleepTime - tolerance && t < sleepTime + tolerance
232fdc63 99 );
45cb7053
FM
100
101 sw.Pause();
c4399985
VZ
102
103 // check that this sleep won't be taken into account below
104 wxMilliSleep(sleepTime);
45cb7053
FM
105 sw.Resume();
106
c4399985 107 wxMilliSleep(sleepTime);
232fdc63
VZ
108 t = sw.Time();
109
110 // and it should advance again
111 WX_ASSERT_MESSAGE
112 (
113 ("Actual time value is %ld", t),
c4399985 114 t > 2*sleepTime - tolerance && t < 2*sleepTime + tolerance
232fdc63 115 );
45cb7053
FM
116}
117
118void StopWatchTestCase::BackwardsClockBug()
119{
120 wxStopWatch sw;
121 wxStopWatch sw2;
122
123 for ( size_t n = 0; n < 10; n++ )
124 {
125 sw2.Start();
126
127 for ( size_t m = 0; m < 10000; m++ )
128 {
129 CPPUNIT_ASSERT ( sw.Time() >= 0 && sw2.Time() >= 0 );
130 }
131 }
132}
17d72a48
VZ
133
134void StopWatchTestCase::RestartBug()
135{
136 wxStopWatch sw;
137 sw.Pause();
138
139 // Calling Start() should resume the stopwatch if it was paused.
140 static const int offset = 5000;
141 sw.Start(offset);
142 wxMilliSleep(sleepTime);
143
144 long t = sw.Time();
145 WX_ASSERT_MESSAGE
146 (
147 ("Actual time value is %ld", t),
148 t > offset + sleepTime - tolerance &&
149 t < offset + sleepTime + tolerance
150 );
151}