1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/validators/valnum.cpp
3 // Purpose: Unit tests for numeric validators.
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
19 #include "wx/textctrl.h"
20 #include "wx/validate.h"
23 #include "wx/valnum.h"
25 #include "asserthelper.h"
26 #include "testableframe.h"
27 #include "wx/uiaction.h"
29 class NumValidatorTestCase
: public CppUnit::TestCase
32 NumValidatorTestCase() { }
38 CPPUNIT_TEST_SUITE( NumValidatorTestCase
);
39 CPPUNIT_TEST( TransferInt
);
40 CPPUNIT_TEST( TransferUnsigned
);
41 CPPUNIT_TEST( TransferFloat
);
42 CPPUNIT_TEST( ZeroAsBlank
);
43 CPPUNIT_TEST( NoTrailingZeroes
);
44 WXUISIM_TEST( Interactive
);
45 CPPUNIT_TEST_SUITE_END();
48 void TransferUnsigned();
51 void NoTrailingZeroes();
52 #if wxUSE_UIACTIONSIMULATOR
54 #endif // wxUSE_UIACTIONSIMULATOR
58 wxDECLARE_NO_COPY_CLASS(NumValidatorTestCase
);
61 // register in the unnamed registry so that these tests are run by default
62 CPPUNIT_TEST_SUITE_REGISTRATION( NumValidatorTestCase
);
64 // also include in its own registry so that these tests can be run alone
65 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumValidatorTestCase
, "NumValidatorTestCase" );
67 void NumValidatorTestCase::setUp()
69 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
72 void NumValidatorTestCase::tearDown()
74 wxTheApp
->GetTopWindow()->DestroyChildren();
77 void NumValidatorTestCase::TransferInt()
80 wxIntegerValidator
<int> valInt(&value
);
81 valInt
.SetWindow(m_text
);
83 CPPUNIT_ASSERT( valInt
.TransferToWindow() );
84 CPPUNIT_ASSERT_EQUAL( "0", m_text
->GetValue() );
87 CPPUNIT_ASSERT( valInt
.TransferToWindow() );
88 CPPUNIT_ASSERT_EQUAL( "17", m_text
->GetValue() );
91 m_text
->ChangeValue("foobar");
92 CPPUNIT_ASSERT( !valInt
.TransferFromWindow() );
94 m_text
->ChangeValue("-234");
95 CPPUNIT_ASSERT( valInt
.TransferFromWindow() );
96 CPPUNIT_ASSERT_EQUAL( -234, value
);
98 m_text
->ChangeValue("9223372036854775808"); // == LLONG_MAX + 1
99 CPPUNIT_ASSERT( !valInt
.TransferFromWindow() );
102 CPPUNIT_ASSERT( !valInt
.TransferFromWindow() );
105 void NumValidatorTestCase::TransferUnsigned()
108 wxIntegerValidator
<unsigned> valUnsigned(&value
);
109 valUnsigned
.SetWindow(m_text
);
111 CPPUNIT_ASSERT( valUnsigned
.TransferToWindow() );
112 CPPUNIT_ASSERT_EQUAL( "0", m_text
->GetValue() );
115 CPPUNIT_ASSERT( valUnsigned
.TransferToWindow() );
116 CPPUNIT_ASSERT_EQUAL( "17", m_text
->GetValue() );
119 m_text
->ChangeValue("foobar");
120 CPPUNIT_ASSERT( !valUnsigned
.TransferFromWindow() );
122 m_text
->ChangeValue("-234");
123 CPPUNIT_ASSERT( !valUnsigned
.TransferFromWindow() );
125 m_text
->ChangeValue("234");
126 CPPUNIT_ASSERT( valUnsigned
.TransferFromWindow() );
127 CPPUNIT_ASSERT_EQUAL( 234, value
);
129 m_text
->ChangeValue("18446744073709551616"); // == ULLONG_MAX + 1
130 CPPUNIT_ASSERT( !valUnsigned
.TransferFromWindow() );
133 CPPUNIT_ASSERT( !valUnsigned
.TransferFromWindow() );
136 void NumValidatorTestCase::TransferFloat()
138 // We need a locale with point as decimal separator.
139 wxLocale
loc(wxLANGUAGE_ENGLISH_UK
, wxLOCALE_DONT_LOAD_DEFAULT
);
142 wxFloatingPointValidator
<float> valFloat(3, &value
);
143 valFloat
.SetWindow(m_text
);
145 CPPUNIT_ASSERT( valFloat
.TransferToWindow() );
146 CPPUNIT_ASSERT_EQUAL( "0.000", m_text
->GetValue() );
149 CPPUNIT_ASSERT( valFloat
.TransferToWindow() );
150 CPPUNIT_ASSERT_EQUAL( "1.234", m_text
->GetValue() );
153 CPPUNIT_ASSERT( valFloat
.TransferToWindow() );
154 CPPUNIT_ASSERT_EQUAL( "1.235", m_text
->GetValue() );
157 m_text
->ChangeValue("foobar");
158 CPPUNIT_ASSERT( !valFloat
.TransferFromWindow() );
160 m_text
->ChangeValue("-234.567");
161 CPPUNIT_ASSERT( valFloat
.TransferFromWindow() );
162 CPPUNIT_ASSERT_EQUAL( -234.567f
, value
);
165 CPPUNIT_ASSERT( !valFloat
.TransferFromWindow() );
168 void NumValidatorTestCase::ZeroAsBlank()
171 m_text
->SetValidator(
172 wxMakeIntegerValidator(&value
, wxNUM_VAL_ZERO_AS_BLANK
));
174 wxValidator
* const val
= m_text
->GetValidator();
176 CPPUNIT_ASSERT( val
->TransferToWindow() );
177 CPPUNIT_ASSERT_EQUAL( "", m_text
->GetValue() );
180 CPPUNIT_ASSERT( val
->TransferFromWindow() );
181 CPPUNIT_ASSERT_EQUAL( 0, value
);
184 void NumValidatorTestCase::NoTrailingZeroes()
186 // We need a locale with point as decimal separator.
187 wxLocale
loc(wxLANGUAGE_ENGLISH_UK
, wxLOCALE_DONT_LOAD_DEFAULT
);
190 m_text
->SetValidator(
191 wxMakeFloatingPointValidator(3, &value
, wxNUM_VAL_NO_TRAILING_ZEROES
));
193 wxValidator
* const val
= m_text
->GetValidator();
195 CPPUNIT_ASSERT( val
->TransferToWindow() );
196 CPPUNIT_ASSERT_EQUAL( "1.2", m_text
->GetValue() );
199 CPPUNIT_ASSERT( val
->TransferToWindow() );
200 CPPUNIT_ASSERT_EQUAL( "1.234", m_text
->GetValue() );
203 #if wxUSE_UIACTIONSIMULATOR
205 void NumValidatorTestCase::Interactive()
208 // FIXME: This test fails on MSW buildbot slaves although works fine on
209 // development machine, no idea why. It seems to be a problem with
210 // wxUIActionSimulator rather the wxListCtrl control itself however.
211 if ( IsAutomaticTest() )
215 // Set a locale using comma as thousands separator character.
216 wxLocale
loc(wxLANGUAGE_ENGLISH_UK
, wxLOCALE_DONT_LOAD_DEFAULT
);
218 m_text
->SetValidator(
219 wxIntegerValidator
<unsigned>(NULL
, wxNUM_VAL_THOUSANDS_SEPARATOR
));
221 // Create a sibling text control to be able to switch focus and thus
222 // trigger the control validation/normalization.
223 wxTextCtrl
* const text2
= new wxTextCtrl(m_text
->GetParent(), wxID_ANY
);
224 text2
->Move(10, 80); // Just to see it better while debugging...
225 wxFloatingPointValidator
<float> valFloat(3);
226 valFloat
.SetRange(-10., 10.);
227 text2
->SetValidator(valFloat
);
229 wxUIActionSimulator sim
;
231 // Entering '-' in a control with positive range is not allowed.
235 CPPUNIT_ASSERT_EQUAL( "", m_text
->GetValue() );
237 // Neither is entering '.' or any non-digit character.
240 CPPUNIT_ASSERT_EQUAL( "", m_text
->GetValue() );
242 // Entering digits should work though and after leaving the control the
243 // contents should be normalized.
249 CPPUNIT_ASSERT_EQUAL( "1,234,567", m_text
->GetValue() );
251 CPPUNIT_ASSERT_EQUAL( "1234567", m_text
->GetValue() );
254 // Entering both '-' and '.' in this control should work but only in the
258 CPPUNIT_ASSERT_EQUAL( "-", text2
->GetValue() );
260 text2
->SetInsertionPoint(0);
263 CPPUNIT_ASSERT_EQUAL( "-", text2
->GetValue() );
265 text2
->SetInsertionPointEnd();
268 CPPUNIT_ASSERT_EQUAL( "-.", text2
->GetValue() );
270 // Adding up to three digits after the point should work.
273 CPPUNIT_ASSERT_EQUAL( "-.987", text2
->GetValue() );
278 CPPUNIT_ASSERT_EQUAL( "-.987", text2
->GetValue() );
280 // We can remove one digit and another one though.
285 CPPUNIT_ASSERT_EQUAL( "-.96", text2
->GetValue() );
288 // Also test the range constraint.
293 CPPUNIT_ASSERT_EQUAL( "9", text2
->GetValue() );
297 CPPUNIT_ASSERT_EQUAL( "9", text2
->GetValue() );
300 #endif // wxUSE_UIACTIONSIMULATOR