evtlooptest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/events/evtloop.cpp
  3. // Purpose: Tests for the event loop classes
  4. // Author: Rob Bresalier
  5. // Created: 2013-05-02
  6. // Copyright: (c) 2013 Rob Bresalier
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #include "wx/timer.h"
  16. // ----------------------------------------------------------------------------
  17. // constants
  18. // ----------------------------------------------------------------------------
  19. // Use two arbitrary but different return codes for the two loops.
  20. const int EXIT_CODE_OUTER_LOOP = 99;
  21. const int EXIT_CODE_INNER_LOOP = 55;
  22. // ----------------------------------------------------------------------------
  23. // test class
  24. // ----------------------------------------------------------------------------
  25. class EvtloopTestCase : public CppUnit::TestCase
  26. {
  27. public:
  28. EvtloopTestCase() { }
  29. private:
  30. CPPUNIT_TEST_SUITE( EvtloopTestCase );
  31. CPPUNIT_TEST( TestExit );
  32. CPPUNIT_TEST_SUITE_END();
  33. void TestExit();
  34. DECLARE_NO_COPY_CLASS(EvtloopTestCase)
  35. };
  36. // register in the unnamed registry so that these tests are run by default
  37. CPPUNIT_TEST_SUITE_REGISTRATION( EvtloopTestCase );
  38. // also include in its own registry so that these tests can be run alone
  39. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtloopTestCase, "EvtloopTestCase" );
  40. // Helper class to schedule exit of the given event loop after the specified
  41. // delay.
  42. class ScheduleLoopExitTimer : public wxTimer
  43. {
  44. public:
  45. ScheduleLoopExitTimer(wxEventLoop& loop, int rc)
  46. : m_loop(loop),
  47. m_rc(rc)
  48. {
  49. }
  50. virtual void Notify()
  51. {
  52. m_loop.ScheduleExit(m_rc);
  53. }
  54. private:
  55. wxEventLoop& m_loop;
  56. const int m_rc;
  57. };
  58. // Another helper which runs a nested loop and schedules exiting both the outer
  59. // and the inner loop after the specified delays.
  60. class RunNestedAndExitBothLoopsTimer : public wxTimer
  61. {
  62. public:
  63. RunNestedAndExitBothLoopsTimer(wxTimer& timerOuter,
  64. int loopOuterDuration,
  65. int loopInnerDuration)
  66. : m_timerOuter(timerOuter),
  67. m_loopOuterDuration(loopOuterDuration),
  68. m_loopInnerDuration(loopInnerDuration)
  69. {
  70. }
  71. virtual void Notify()
  72. {
  73. wxEventLoop loopInner;
  74. ScheduleLoopExitTimer timerInner(loopInner, EXIT_CODE_INNER_LOOP);
  75. m_timerOuter.StartOnce(m_loopOuterDuration);
  76. timerInner.StartOnce(m_loopInnerDuration);
  77. CPPUNIT_ASSERT_EQUAL( EXIT_CODE_INNER_LOOP, loopInner.Run() );
  78. }
  79. private:
  80. wxTimer& m_timerOuter;
  81. const int m_loopOuterDuration;
  82. const int m_loopInnerDuration;
  83. };
  84. void EvtloopTestCase::TestExit()
  85. {
  86. // Test that simply exiting the loop works.
  87. wxEventLoop loopOuter;
  88. ScheduleLoopExitTimer timerExit(loopOuter, EXIT_CODE_OUTER_LOOP);
  89. timerExit.StartOnce(1);
  90. CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP, loopOuter.Run() );
  91. // Test that exiting the outer loop before the inner loop (outer duration
  92. // parameter less than inner duration in the timer ctor below) works.
  93. ScheduleLoopExitTimer timerExitOuter(loopOuter, EXIT_CODE_OUTER_LOOP);
  94. RunNestedAndExitBothLoopsTimer timerRun(timerExitOuter, 5, 10);
  95. timerRun.StartOnce(1);
  96. CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP, loopOuter.Run() );
  97. // Test that exiting the inner loop before the outer one works too.
  98. ScheduleLoopExitTimer timerExitOuter2(loopOuter, EXIT_CODE_OUTER_LOOP);
  99. RunNestedAndExitBothLoopsTimer timerRun2(timerExitOuter, 10, 5);
  100. timerRun2.StartOnce(1);
  101. CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP, loopOuter.Run() );
  102. }