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