timertest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/events/timertest.cpp
  3. // Purpose: Test wxTimer events
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-10-22
  6. // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #endif // WX_PRECOMP
  17. #include <time.h>
  18. #include "wx/evtloop.h"
  19. #include "wx/timer.h"
  20. // --------------------------------------------------------------------------
  21. // helper class counting the number of timer events
  22. // --------------------------------------------------------------------------
  23. class TimerCounterHandler : public wxEvtHandler
  24. {
  25. public:
  26. TimerCounterHandler()
  27. {
  28. m_events = 0;
  29. Connect(wxEVT_TIMER, wxTimerEventHandler(TimerCounterHandler::OnTimer));
  30. }
  31. int GetNumEvents() const { return m_events; }
  32. private:
  33. void OnTimer(wxTimerEvent& WXUNUSED(event))
  34. {
  35. m_events++;
  36. Tick();
  37. }
  38. virtual void Tick() { /* nothing to do in the base class */ }
  39. int m_events;
  40. DECLARE_NO_COPY_CLASS(TimerCounterHandler)
  41. };
  42. // --------------------------------------------------------------------------
  43. // test class
  44. // --------------------------------------------------------------------------
  45. class TimerEventTestCase : public CppUnit::TestCase
  46. {
  47. public:
  48. TimerEventTestCase() {}
  49. private:
  50. CPPUNIT_TEST_SUITE( TimerEventTestCase );
  51. CPPUNIT_TEST( OneShot );
  52. CPPUNIT_TEST( Multiple );
  53. CPPUNIT_TEST_SUITE_END();
  54. void OneShot();
  55. void Multiple();
  56. DECLARE_NO_COPY_CLASS(TimerEventTestCase)
  57. };
  58. // register in the unnamed registry so that these tests are run by default
  59. CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase );
  60. // also include in its own registry so that these tests can be run alone
  61. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase, "TimerEventTestCase" );
  62. void TimerEventTestCase::OneShot()
  63. {
  64. class ExitOnTimerHandler : public TimerCounterHandler
  65. {
  66. public:
  67. ExitOnTimerHandler(wxEventLoopBase& loop)
  68. : TimerCounterHandler(),
  69. m_loop(loop)
  70. {
  71. }
  72. private:
  73. virtual void Tick() { m_loop.Exit(); }
  74. wxEventLoopBase& m_loop;
  75. // don't use DECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
  76. };
  77. wxEventLoop loop;
  78. ExitOnTimerHandler handler(loop);
  79. wxTimer timer(&handler);
  80. timer.Start(200, true);
  81. loop.Run();
  82. CPPUNIT_ASSERT_EQUAL( 1, handler.GetNumEvents() );
  83. }
  84. void TimerEventTestCase::Multiple()
  85. {
  86. // FIXME: This test crashes on wxGTK ANSI build slave for unknown reason,
  87. // disable it here to let the rest of the test suite run until this
  88. // can be fixed.
  89. #if !defined(__WXGTK__) || wxUSE_UNICODE
  90. wxEventLoop loop;
  91. TimerCounterHandler handler;
  92. wxTimer timer(&handler);
  93. timer.Start(100);
  94. // run the loop for 2 seconds
  95. time_t t;
  96. time(&t);
  97. const time_t tEnd = t + 2;
  98. while ( time(&t) < tEnd )
  99. {
  100. loop.Dispatch();
  101. }
  102. // we can't count on getting exactly 20 ticks but we shouldn't get more
  103. // than this
  104. const int numTicks = handler.GetNumEvents();
  105. CPPUNIT_ASSERT( numTicks <= 20 );
  106. // and we should get a decent number of them but if the system is very
  107. // loaded (as happens with build bot slaves running a couple of builds in
  108. // parallel actually) it may be much less than 20 so just check that we get
  109. // more than one
  110. CPPUNIT_ASSERT( numTicks > 1 );
  111. #endif // !(wxGTK Unicode)
  112. }