wxMessageBox off the main thread lost result code.
[wxWidgets.git] / tests / controls / spinctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/spinctrltest.cpp
3 // Purpose: wxSpinCtrl unit test
4 // Author: Steven Lamerton
5 // Created: 2010-07-21
6 // Copyright: (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
8
9 #include "testprec.h"
10
11 #if wxUSE_SPINCTRL
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #ifndef WX_PRECOMP
18 #include "wx/app.h"
19 #endif // WX_PRECOMP
20
21 #include "testableframe.h"
22 #include "wx/uiaction.h"
23 #include "wx/spinctrl.h"
24
25 class SpinCtrlTestCase : public CppUnit::TestCase
26 {
27 public:
28 SpinCtrlTestCase() { }
29
30 void setUp();
31 void tearDown();
32
33 private:
34 CPPUNIT_TEST_SUITE( SpinCtrlTestCase );
35 CPPUNIT_TEST( Initial );
36 CPPUNIT_TEST( NoEventsInCtor );
37 WXUISIM_TEST( Arrows );
38 WXUISIM_TEST( Wrap );
39 CPPUNIT_TEST( Range );
40 CPPUNIT_TEST( Value );
41 CPPUNIT_TEST_SUITE_END();
42
43 void Initial();
44 void NoEventsInCtor();
45 void Arrows();
46 void Wrap();
47 void Range();
48 void Value();
49
50 wxSpinCtrl* m_spin;
51
52 DECLARE_NO_COPY_CLASS(SpinCtrlTestCase)
53 };
54
55 // register in the unnamed registry so that these tests are run by default
56 CPPUNIT_TEST_SUITE_REGISTRATION( SpinCtrlTestCase );
57
58 // also include in its own registry so that these tests can be run alone
59 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SpinCtrlTestCase, "SpinCtrlTestCase" );
60
61 void SpinCtrlTestCase::setUp()
62 {
63 m_spin = new wxSpinCtrl(wxTheApp->GetTopWindow());
64 }
65
66 void SpinCtrlTestCase::tearDown()
67 {
68 wxDELETE(m_spin);
69 }
70
71 void SpinCtrlTestCase::Initial()
72 {
73 // Initial value is defined by "initial" argument which is 0 by default.
74 CPPUNIT_ASSERT_EQUAL( 0, m_spin->GetValue() );
75
76 wxWindow* const parent = m_spin->GetParent();
77
78 // Recreate the control with another "initial" to check this.
79 delete m_spin;
80 m_spin = new wxSpinCtrl(parent, wxID_ANY, "",
81 wxDefaultPosition, wxDefaultSize, 0,
82 0, 100, 17);
83 CPPUNIT_ASSERT_EQUAL( 17, m_spin->GetValue() );
84
85 // Recreate the control with another "initial" outside of standard spin
86 // ctrl range.
87 delete m_spin;
88 m_spin = new wxSpinCtrl(parent, wxID_ANY, "",
89 wxDefaultPosition, wxDefaultSize, 0,
90 0, 200, 150);
91 CPPUNIT_ASSERT_EQUAL( 150, m_spin->GetValue() );
92
93 // But if the text string is specified, it takes precedence.
94 delete m_spin;
95 m_spin = new wxSpinCtrl(parent, wxID_ANY, "99",
96 wxDefaultPosition, wxDefaultSize, 0,
97 0, 100, 17);
98 CPPUNIT_ASSERT_EQUAL( 99, m_spin->GetValue() );
99 }
100
101 void SpinCtrlTestCase::NoEventsInCtor()
102 {
103 // Verify that creating the control does not generate any events. This is
104 // unexpected and shouldn't happen.
105 wxWindow* const parent = m_spin->GetParent();
106 delete m_spin;
107 m_spin = new wxSpinCtrl;
108
109 EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
110 EventCounter updatedText(m_spin, wxEVT_TEXT);
111
112 m_spin->Create(parent, wxID_ANY, "",
113 wxDefaultPosition, wxDefaultSize, 0,
114 0, 100, 17);
115
116 CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
117 CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
118 }
119
120 void SpinCtrlTestCase::Arrows()
121 {
122 #if wxUSE_UIACTIONSIMULATOR
123 EventCounter updated(m_spin, wxEVT_SPINCTRL);
124
125 wxUIActionSimulator sim;
126
127 m_spin->SetFocus();
128
129 sim.Char(WXK_UP);
130
131 wxYield();
132
133 CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
134 CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue());
135 updated.Clear();
136
137 sim.Char(WXK_DOWN);
138
139 wxYield();
140
141 CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
142 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
143 #endif
144 }
145
146 void SpinCtrlTestCase::Wrap()
147 {
148 #if wxUSE_UIACTIONSIMULATOR
149 wxDELETE(m_spin);
150 m_spin = new wxSpinCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
151 wxDefaultPosition, wxDefaultSize,
152 wxSP_ARROW_KEYS | wxSP_WRAP);
153
154 wxUIActionSimulator sim;
155
156 m_spin->SetFocus();
157
158 sim.Char(WXK_DOWN);
159
160 wxYield();
161
162 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue());
163
164 sim.Char(WXK_UP);
165
166 wxYield();
167
168 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
169 #endif
170 }
171
172 void SpinCtrlTestCase::Range()
173 {
174 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetMin());
175 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetMax());
176
177 // Test that the value is adjusted to be inside the new valid range but
178 // that this doesn't result in any events (as this is not something done by
179 // the user).
180 {
181 EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
182 EventCounter updatedText(m_spin, wxEVT_TEXT);
183
184 m_spin->SetRange(1, 10);
185 CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue());
186
187 CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
188 CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
189 }
190
191 //Test negative ranges
192 m_spin->SetRange(-10, 10);
193
194 CPPUNIT_ASSERT_EQUAL(-10, m_spin->GetMin());
195 CPPUNIT_ASSERT_EQUAL(10, m_spin->GetMax());
196
197 //Test backwards ranges
198 m_spin->SetRange(75, 50);
199
200 CPPUNIT_ASSERT_EQUAL(75, m_spin->GetMin());
201 CPPUNIT_ASSERT_EQUAL(50, m_spin->GetMax());
202 }
203
204 void SpinCtrlTestCase::Value()
205 {
206 EventCounter updatedSpin(m_spin, wxEVT_SPINCTRL);
207 EventCounter updatedText(m_spin, wxEVT_TEXT);
208
209 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
210
211 m_spin->SetValue(50);
212 CPPUNIT_ASSERT_EQUAL(50, m_spin->GetValue());
213
214 m_spin->SetValue(-10);
215 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
216
217 m_spin->SetValue(110);
218 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue());
219
220 // Calling SetValue() shouldn't have generated any events.
221 CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
222 CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
223 }
224
225 #endif