X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/232fdc630c42eb165f7659981043e794be03b3b7..d73efa0b9c06aec0a4b39af3b7d1eab424068f83:/tests/events/stopwatch.cpp?ds=sidebyside diff --git a/tests/events/stopwatch.cpp b/tests/events/stopwatch.cpp index a590dc326d..1650484039 100644 --- a/tests/events/stopwatch.cpp +++ b/tests/events/stopwatch.cpp @@ -3,7 +3,6 @@ // Purpose: Test wxStopWatch class // Author: Francesco Montorsi (extracted from console sample) // Created: 2010-05-16 -// RCS-ID: $Id$ // Copyright: (c) 2010 wxWidgets team /////////////////////////////////////////////////////////////////////////////// @@ -26,6 +25,14 @@ #include "wx/stopwatch.h" #include "wx/utils.h" +namespace +{ + +const long tolerance = 50; // in ms +const int sleepTime = 500; + +} // anonymous namespace + // -------------------------------------------------------------------------- // test class // -------------------------------------------------------------------------- @@ -39,10 +46,12 @@ private: CPPUNIT_TEST_SUITE( StopWatchTestCase ); CPPUNIT_TEST( Misc ); CPPUNIT_TEST( BackwardsClockBug ); + CPPUNIT_TEST( RestartBug ); CPPUNIT_TEST_SUITE_END(); void Misc(); void BackwardsClockBug(); + void RestartBug(); DECLARE_NO_COPY_CLASS(StopWatchTestCase) }; @@ -50,19 +59,26 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( StopWatchTestCase ); -// also include in it's own registry so that these tests can be run alone +// also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StopWatchTestCase, "StopWatchTestCase" ); void StopWatchTestCase::Misc() { - static const long tolerance = 100; // in ms - wxStopWatch sw; long t; + wxLongLong usec; sw.Pause(); // pause it immediately - wxSleep(2); + // verify that almost no time elapsed + usec = sw.TimeInMicro(); + WX_ASSERT_MESSAGE + ( + ("Elapsed time was %" wxLongLongFmtSpec "dus", usec), + usec < tolerance*1000 + ); + + wxSleep(1); t = sw.Time(); // check that the stop watch doesn't advance while paused @@ -73,26 +89,29 @@ void StopWatchTestCase::Misc() ); sw.Resume(); - wxSleep(3); + wxMilliSleep(sleepTime); t = sw.Time(); - // check that it did advance now by ~3s + // check that it did advance now by ~1.5s WX_ASSERT_MESSAGE ( ("Actual time value is %ld", t), - t > 3000 - tolerance && t < 3000 + tolerance + t > sleepTime - tolerance && t < sleepTime + tolerance ); sw.Pause(); + + // check that this sleep won't be taken into account below + wxMilliSleep(sleepTime); sw.Resume(); - wxSleep(2); + wxMilliSleep(sleepTime); t = sw.Time(); // and it should advance again WX_ASSERT_MESSAGE ( ("Actual time value is %ld", t), - t > 5000 - tolerance && t < 5000 + tolerance + t > 2*sleepTime - tolerance && t < 2*sleepTime + tolerance ); } @@ -111,3 +130,22 @@ void StopWatchTestCase::BackwardsClockBug() } } } + +void StopWatchTestCase::RestartBug() +{ + wxStopWatch sw; + sw.Pause(); + + // Calling Start() should resume the stopwatch if it was paused. + static const int offset = 5000; + sw.Start(offset); + wxMilliSleep(sleepTime); + + long t = sw.Time(); + WX_ASSERT_MESSAGE + ( + ("Actual time value is %ld", t), + t > offset + sleepTime - tolerance && + t < offset + sleepTime + tolerance + ); +}