1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/evtloop.cpp
3 // Purpose: Tests for the event loop classes
4 // Author: Rob Bresalier
7 // Copyright: (c) 2013 Rob Bresalier
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // Use two arbitrary but different return codes for the two loops.
27 const int EXIT_CODE_OUTER_LOOP
= 99;
28 const int EXIT_CODE_INNER_LOOP
= 55;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class EvtloopTestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE( EvtloopTestCase
);
41 CPPUNIT_TEST( TestExit
);
42 CPPUNIT_TEST_SUITE_END();
46 DECLARE_NO_COPY_CLASS(EvtloopTestCase
)
49 // register in the unnamed registry so that these tests are run by default
50 CPPUNIT_TEST_SUITE_REGISTRATION( EvtloopTestCase
);
52 // also include in its own registry so that these tests can be run alone
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtloopTestCase
, "EvtloopTestCase" );
56 // Helper class to schedule exit of the given event loop after the specified
58 class ScheduleLoopExitTimer
: public wxTimer
61 ScheduleLoopExitTimer(wxEventLoop
& loop
, int rc
)
69 m_loop
.ScheduleExit(m_rc
);
77 // Another helper which runs a nested loop and schedules exiting both the outer
78 // and the inner loop after the specified delays.
79 class RunNestedAndExitBothLoopsTimer
: public wxTimer
82 RunNestedAndExitBothLoopsTimer(wxTimer
& timerOuter
,
83 int loopOuterDuration
,
84 int loopInnerDuration
)
85 : m_timerOuter(timerOuter
),
86 m_loopOuterDuration(loopOuterDuration
),
87 m_loopInnerDuration(loopInnerDuration
)
93 wxEventLoop loopInner
;
94 ScheduleLoopExitTimer
timerInner(loopInner
, EXIT_CODE_INNER_LOOP
);
96 m_timerOuter
.StartOnce(m_loopOuterDuration
);
97 timerInner
.StartOnce(m_loopInnerDuration
);
99 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_INNER_LOOP
, loopInner
.Run() );
103 wxTimer
& m_timerOuter
;
104 const int m_loopOuterDuration
;
105 const int m_loopInnerDuration
;
108 void EvtloopTestCase::TestExit()
110 // Test that simply exiting the loop works.
111 wxEventLoop loopOuter
;
112 ScheduleLoopExitTimer
timerExit(loopOuter
, EXIT_CODE_OUTER_LOOP
);
113 timerExit
.StartOnce(1);
114 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP
, loopOuter
.Run() );
116 // Test that exiting the outer loop before the inner loop (outer duration
117 // parameter less than inner duration in the timer ctor below) works.
118 ScheduleLoopExitTimer
timerExitOuter(loopOuter
, EXIT_CODE_OUTER_LOOP
);
119 RunNestedAndExitBothLoopsTimer
timerRun(timerExitOuter
, 5, 10);
120 timerRun
.StartOnce(1);
121 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP
, loopOuter
.Run() );
123 // Test that exiting the inner loop before the outer one works too.
124 ScheduleLoopExitTimer
timerExitOuter2(loopOuter
, EXIT_CODE_OUTER_LOOP
);
125 RunNestedAndExitBothLoopsTimer
timerRun2(timerExitOuter
, 10, 5);
126 timerRun2
.StartOnce(1);
127 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_OUTER_LOOP
, loopOuter
.Run() );