]> git.saurik.com Git - wxWidgets.git/blame_incremental - tests/validators/valnum.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / tests / validators / valnum.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/validators/valnum.cpp
3// Purpose: Unit tests for numeric validators.
4// Author: Vadim Zeitlin
5// Created: 2011-01-18
6// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7///////////////////////////////////////////////////////////////////////////////
8
9#include "testprec.h"
10
11#ifdef __BORLANDC__
12 #pragma hdrstop
13#endif
14
15#ifndef WX_PRECOMP
16 #include "wx/app.h"
17 #include "wx/intl.h"
18 #include "wx/textctrl.h"
19 #include "wx/validate.h"
20#endif // WX_PRECOMP
21
22#include "wx/valnum.h"
23
24#include "asserthelper.h"
25#include "testableframe.h"
26#include "wx/uiaction.h"
27
28class NumValidatorTestCase : public CppUnit::TestCase
29{
30public:
31 NumValidatorTestCase() { }
32
33 void setUp();
34 void tearDown();
35
36private:
37 CPPUNIT_TEST_SUITE( NumValidatorTestCase );
38 CPPUNIT_TEST( TransferInt );
39 CPPUNIT_TEST( TransferUnsigned );
40 CPPUNIT_TEST( TransferFloat );
41 CPPUNIT_TEST( ZeroAsBlank );
42 CPPUNIT_TEST( NoTrailingZeroes );
43 WXUISIM_TEST( Interactive );
44 CPPUNIT_TEST_SUITE_END();
45
46 void TransferInt();
47 void TransferUnsigned();
48 void TransferFloat();
49 void ZeroAsBlank();
50 void NoTrailingZeroes();
51#if wxUSE_UIACTIONSIMULATOR
52 void Interactive();
53#endif // wxUSE_UIACTIONSIMULATOR
54
55 wxTextCtrl *m_text;
56
57 wxDECLARE_NO_COPY_CLASS(NumValidatorTestCase);
58};
59
60// register in the unnamed registry so that these tests are run by default
61CPPUNIT_TEST_SUITE_REGISTRATION( NumValidatorTestCase );
62
63// also include in its own registry so that these tests can be run alone
64CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumValidatorTestCase, "NumValidatorTestCase" );
65
66void NumValidatorTestCase::setUp()
67{
68 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
69}
70
71void NumValidatorTestCase::tearDown()
72{
73 wxTheApp->GetTopWindow()->DestroyChildren();
74}
75
76void NumValidatorTestCase::TransferInt()
77{
78 int value = 0;
79 wxIntegerValidator<int> valInt(&value);
80 valInt.SetWindow(m_text);
81
82 CPPUNIT_ASSERT( valInt.TransferToWindow() );
83 CPPUNIT_ASSERT_EQUAL( "0", m_text->GetValue() );
84
85 value = 17;
86 CPPUNIT_ASSERT( valInt.TransferToWindow() );
87 CPPUNIT_ASSERT_EQUAL( "17", m_text->GetValue() );
88
89
90 m_text->ChangeValue("foobar");
91 CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
92
93 m_text->ChangeValue("-234");
94 CPPUNIT_ASSERT( valInt.TransferFromWindow() );
95 CPPUNIT_ASSERT_EQUAL( -234, value );
96
97 m_text->ChangeValue("9223372036854775808"); // == LLONG_MAX + 1
98 CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
99
100 m_text->Clear();
101 CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
102}
103
104void NumValidatorTestCase::TransferUnsigned()
105{
106 unsigned value = 0;
107 wxIntegerValidator<unsigned> valUnsigned(&value);
108 valUnsigned.SetWindow(m_text);
109
110 CPPUNIT_ASSERT( valUnsigned.TransferToWindow() );
111 CPPUNIT_ASSERT_EQUAL( "0", m_text->GetValue() );
112
113 value = 17;
114 CPPUNIT_ASSERT( valUnsigned.TransferToWindow() );
115 CPPUNIT_ASSERT_EQUAL( "17", m_text->GetValue() );
116
117
118 m_text->ChangeValue("foobar");
119 CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
120
121 m_text->ChangeValue("-234");
122 CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
123
124 m_text->ChangeValue("234");
125 CPPUNIT_ASSERT( valUnsigned.TransferFromWindow() );
126 CPPUNIT_ASSERT_EQUAL( 234, value );
127
128 m_text->ChangeValue("18446744073709551616"); // == ULLONG_MAX + 1
129 CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
130
131 m_text->Clear();
132 CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
133}
134
135void NumValidatorTestCase::TransferFloat()
136{
137 // We need a locale with point as decimal separator.
138 wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
139
140 float value = 0;
141 wxFloatingPointValidator<float> valFloat(3, &value);
142 valFloat.SetWindow(m_text);
143
144 CPPUNIT_ASSERT( valFloat.TransferToWindow() );
145 CPPUNIT_ASSERT_EQUAL( "0.000", m_text->GetValue() );
146
147 value = 1.234f;
148 CPPUNIT_ASSERT( valFloat.TransferToWindow() );
149 CPPUNIT_ASSERT_EQUAL( "1.234", m_text->GetValue() );
150
151 value = 1.2345678f;
152 CPPUNIT_ASSERT( valFloat.TransferToWindow() );
153 CPPUNIT_ASSERT_EQUAL( "1.235", m_text->GetValue() );
154
155
156 m_text->ChangeValue("foobar");
157 CPPUNIT_ASSERT( !valFloat.TransferFromWindow() );
158
159 m_text->ChangeValue("-234.567");
160 CPPUNIT_ASSERT( valFloat.TransferFromWindow() );
161 CPPUNIT_ASSERT_EQUAL( -234.567f, value );
162
163 m_text->Clear();
164 CPPUNIT_ASSERT( !valFloat.TransferFromWindow() );
165}
166
167void NumValidatorTestCase::ZeroAsBlank()
168{
169 long value = 0;
170 m_text->SetValidator(
171 wxMakeIntegerValidator(&value, wxNUM_VAL_ZERO_AS_BLANK));
172
173 wxValidator * const val = m_text->GetValidator();
174
175 CPPUNIT_ASSERT( val->TransferToWindow() );
176 CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
177
178 value++;
179 CPPUNIT_ASSERT( val->TransferFromWindow() );
180 CPPUNIT_ASSERT_EQUAL( 0, value );
181}
182
183void NumValidatorTestCase::NoTrailingZeroes()
184{
185 // We need a locale with point as decimal separator.
186 wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
187
188 double value = 1.2;
189 m_text->SetValidator(
190 wxMakeFloatingPointValidator(3, &value, wxNUM_VAL_NO_TRAILING_ZEROES));
191
192 wxValidator * const val = m_text->GetValidator();
193
194 CPPUNIT_ASSERT( val->TransferToWindow() );
195 CPPUNIT_ASSERT_EQUAL( "1.2", m_text->GetValue() );
196
197 value = 1.234;
198 CPPUNIT_ASSERT( val->TransferToWindow() );
199 CPPUNIT_ASSERT_EQUAL( "1.234", m_text->GetValue() );
200}
201
202#if wxUSE_UIACTIONSIMULATOR
203
204void NumValidatorTestCase::Interactive()
205{
206#ifdef __WXMSW__
207 // FIXME: This test fails on MSW buildbot slaves although works fine on
208 // development machine, no idea why. It seems to be a problem with
209 // wxUIActionSimulator rather the wxListCtrl control itself however.
210 if ( IsAutomaticTest() )
211 return;
212#endif // __WXMSW__
213
214 // Set a locale using comma as thousands separator character.
215 wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
216
217 m_text->SetValidator(
218 wxIntegerValidator<unsigned>(NULL, wxNUM_VAL_THOUSANDS_SEPARATOR));
219
220 // Create a sibling text control to be able to switch focus and thus
221 // trigger the control validation/normalization.
222 wxTextCtrl * const text2 = new wxTextCtrl(m_text->GetParent(), wxID_ANY);
223 text2->Move(10, 80); // Just to see it better while debugging...
224 wxFloatingPointValidator<float> valFloat(3);
225 valFloat.SetRange(-10., 10.);
226 text2->SetValidator(valFloat);
227
228 wxUIActionSimulator sim;
229
230 // Entering '-' in a control with positive range is not allowed.
231 m_text->SetFocus();
232 sim.Char('-');
233 wxYield();
234 CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
235
236 // Neither is entering '.' or any non-digit character.
237 sim.Text(".a+/");
238 wxYield();
239 CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
240
241 // Entering digits should work though and after leaving the control the
242 // contents should be normalized.
243 sim.Text("1234567");
244 wxYield();
245 text2->SetFocus();
246 wxYield();
247 if ( loc.IsOk() )
248 CPPUNIT_ASSERT_EQUAL( "1,234,567", m_text->GetValue() );
249 else
250 CPPUNIT_ASSERT_EQUAL( "1234567", m_text->GetValue() );
251
252
253 // Entering both '-' and '.' in this control should work but only in the
254 // correct order.
255 sim.Char('-');
256 wxYield();
257 CPPUNIT_ASSERT_EQUAL( "-", text2->GetValue() );
258
259 text2->SetInsertionPoint(0);
260 sim.Char('.');
261 wxYield();
262 CPPUNIT_ASSERT_EQUAL( "-", text2->GetValue() );
263
264 text2->SetInsertionPointEnd();
265 sim.Char('.');
266 wxYield();
267 CPPUNIT_ASSERT_EQUAL( "-.", text2->GetValue() );
268
269 // Adding up to three digits after the point should work.
270 sim.Text("987");
271 wxYield();
272 CPPUNIT_ASSERT_EQUAL( "-.987", text2->GetValue() );
273
274 // But no more.
275 sim.Text("654");
276 wxYield();
277 CPPUNIT_ASSERT_EQUAL( "-.987", text2->GetValue() );
278
279 // We can remove one digit and another one though.
280 sim.Char(WXK_BACK);
281 sim.Char(WXK_BACK);
282 sim.Char('6');
283 wxYield();
284 CPPUNIT_ASSERT_EQUAL( "-.96", text2->GetValue() );
285
286
287 // Also test the range constraint.
288 text2->Clear();
289
290 sim.Char('9');
291 wxYield();
292 CPPUNIT_ASSERT_EQUAL( "9", text2->GetValue() );
293
294 sim.Char('9');
295 wxYield();
296 CPPUNIT_ASSERT_EQUAL( "9", text2->GetValue() );
297}
298
299#endif // wxUSE_UIACTIONSIMULATOR