1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/evtloop.cpp
3 // Purpose: Tests for the event loop classes
4 // Author: Rob Bresalier
6 // Copyright: (c) 2013 Rob Bresalier
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // Use two arbitrary but different return codes for the two loops.
26 const int EXIT_CODE_OUTER_LOOP
= 99;
27 const int EXIT_CODE_INNER_LOOP
= 55;
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class EvtloopTestCase
: public CppUnit::TestCase
39 CPPUNIT_TEST_SUITE( EvtloopTestCase
);
40 CPPUNIT_TEST( TestExit
);
41 CPPUNIT_TEST_SUITE_END();
45 DECLARE_NO_COPY_CLASS(EvtloopTestCase
)
48 // register in the unnamed registry so that these tests are run by default
49 CPPUNIT_TEST_SUITE_REGISTRATION( EvtloopTestCase
);
51 // also include in its own registry so that these tests can be run alone
52 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtloopTestCase
, "EvtloopTestCase" );
55 // Helper class to schedule exit of the given event loop after the specified
57 class ScheduleLoopExitTimer
: public wxTimer
60 ScheduleLoopExitTimer(wxEventLoop
& loop
, int rc
)
68 m_loop
.ScheduleExit(m_rc
);
76 // Another helper which runs a nested loop and schedules exiting both the outer
77 // and the inner loop after the specified delays.
78 class RunNestedAndExitBothLoopsTimer
: public wxTimer
81 RunNestedAndExitBothLoopsTimer(wxTimer
& timerOuter
,
82 int loopOuterDuration
,
83 int loopInnerDuration
)
84 : m_timerOuter(timerOuter
),
85 m_loopOuterDuration(loopOuterDuration
),
86 m_loopInnerDuration(loopInnerDuration
)
92 wxEventLoop loopInner
;
93 ScheduleLoopExitTimer
timerInner(loopInner
, EXIT_CODE_INNER_LOOP
);
95 m_timerOuter
.StartOnce(m_loopOuterDuration
);
96 timerInner
.StartOnce(m_loopInnerDuration
);
98 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_INNER_LOOP
, loopInner
.Run() );
102 wxTimer
& m_timerOuter
;
103 const int m_loopOuterDuration
;
104 const int m_loopInnerDuration
;
107 void EvtloopTestCase::TestExit()
109 // Test that simply exiting the loop works.
110 wxEventLoop loopOuter
;
111 ScheduleLoopExitTimer
timerExit(loopOuter
, EXIT_CODE_OUTER_LOOP
);
112 timerExit
.StartOnce(1);
113 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP
, loopOuter
.Run() );
115 // Test that exiting the outer loop before the inner loop (outer duration
116 // parameter less than inner duration in the timer ctor below) works.
117 ScheduleLoopExitTimer
timerExitOuter(loopOuter
, EXIT_CODE_OUTER_LOOP
);
118 RunNestedAndExitBothLoopsTimer
timerRun(timerExitOuter
, 5, 10);
119 timerRun
.StartOnce(1);
120 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP
, loopOuter
.Run() );
122 // Test that exiting the inner loop before the outer one works too.
123 ScheduleLoopExitTimer
timerExitOuter2(loopOuter
, EXIT_CODE_OUTER_LOOP
);
124 RunNestedAndExitBothLoopsTimer
timerRun2(timerExitOuter
, 10, 5);
125 timerRun2
.StartOnce(1);
126 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP
, loopOuter
.Run() );