]> git.saurik.com Git - wxWidgets.git/blob - tests/events/evtlooptest.cpp
wxRTC: fixed guidelines overwriting adjacent cell borders; corrected capitalisation...
[wxWidgets.git] / tests / events / evtlooptest.cpp
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 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/timer.h"
20
21 // ----------------------------------------------------------------------------
22 // constants
23 // ----------------------------------------------------------------------------
24
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;
28
29 // ----------------------------------------------------------------------------
30 // test class
31 // ----------------------------------------------------------------------------
32
33 class EvtloopTestCase : public CppUnit::TestCase
34 {
35 public:
36 EvtloopTestCase() { }
37
38 private:
39 CPPUNIT_TEST_SUITE( EvtloopTestCase );
40 CPPUNIT_TEST( TestExit );
41 CPPUNIT_TEST_SUITE_END();
42
43 void TestExit();
44
45 DECLARE_NO_COPY_CLASS(EvtloopTestCase)
46 };
47
48 // register in the unnamed registry so that these tests are run by default
49 CPPUNIT_TEST_SUITE_REGISTRATION( EvtloopTestCase );
50
51 // also include in its own registry so that these tests can be run alone
52 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EvtloopTestCase, "EvtloopTestCase" );
53
54
55 // Helper class to schedule exit of the given event loop after the specified
56 // delay.
57 class ScheduleLoopExitTimer : public wxTimer
58 {
59 public:
60 ScheduleLoopExitTimer(wxEventLoop& loop, int rc)
61 : m_loop(loop),
62 m_rc(rc)
63 {
64 }
65
66 virtual void Notify()
67 {
68 m_loop.ScheduleExit(m_rc);
69 }
70
71 private:
72 wxEventLoop& m_loop;
73 const int m_rc;
74 };
75
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
79 {
80 public:
81 RunNestedAndExitBothLoopsTimer(wxTimer& timerOuter,
82 int loopOuterDuration,
83 int loopInnerDuration)
84 : m_timerOuter(timerOuter),
85 m_loopOuterDuration(loopOuterDuration),
86 m_loopInnerDuration(loopInnerDuration)
87 {
88 }
89
90 virtual void Notify()
91 {
92 wxEventLoop loopInner;
93 ScheduleLoopExitTimer timerInner(loopInner, EXIT_CODE_INNER_LOOP);
94
95 m_timerOuter.StartOnce(m_loopOuterDuration);
96 timerInner.StartOnce(m_loopInnerDuration);
97
98 CPPUNIT_ASSERT_EQUAL( EXIT_CODE_INNER_LOOP, loopInner.Run() );
99 }
100
101 private:
102 wxTimer& m_timerOuter;
103 const int m_loopOuterDuration;
104 const int m_loopInnerDuration;
105 };
106
107 void EvtloopTestCase::TestExit()
108 {
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() );
114
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() );
121
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() );
127 }