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