1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/validators/valnum.cpp
3 // Purpose: Unit tests for numeric validators.
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
18 #include "wx/textctrl.h"
19 #include "wx/validate.h"
22 #include "wx/valnum.h"
24 #include "asserthelper.h"
25 #include "testableframe.h"
26 #include "wx/uiaction.h"
28 class NumValidatorTestCase
: public CppUnit::TestCase
31 NumValidatorTestCase() { }
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();
47 void TransferUnsigned();
50 void NoTrailingZeroes();
51 #if wxUSE_UIACTIONSIMULATOR
53 #endif // wxUSE_UIACTIONSIMULATOR
57 wxDECLARE_NO_COPY_CLASS(NumValidatorTestCase
);
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( NumValidatorTestCase
);
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumValidatorTestCase
, "NumValidatorTestCase" );
66 void NumValidatorTestCase::setUp()
68 m_text
= new wxTextCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
);
71 void NumValidatorTestCase::tearDown()
73 wxTheApp
->GetTopWindow()->DestroyChildren();
76 void NumValidatorTestCase::TransferInt()
79 wxIntegerValidator
<int> valInt(&value
);
80 valInt
.SetWindow(m_text
);
82 CPPUNIT_ASSERT( valInt
.TransferToWindow() );
83 CPPUNIT_ASSERT_EQUAL( "0", m_text
->GetValue() );
86 CPPUNIT_ASSERT( valInt
.TransferToWindow() );
87 CPPUNIT_ASSERT_EQUAL( "17", m_text
->GetValue() );
90 m_text
->ChangeValue("foobar");
91 CPPUNIT_ASSERT( !valInt
.TransferFromWindow() );
93 m_text
->ChangeValue("-234");
94 CPPUNIT_ASSERT( valInt
.TransferFromWindow() );
95 CPPUNIT_ASSERT_EQUAL( -234, value
);
97 m_text
->ChangeValue("9223372036854775808"); // == LLONG_MAX + 1
98 CPPUNIT_ASSERT( !valInt
.TransferFromWindow() );
101 CPPUNIT_ASSERT( !valInt
.TransferFromWindow() );
104 void NumValidatorTestCase::TransferUnsigned()
107 wxIntegerValidator
<unsigned> valUnsigned(&value
);
108 valUnsigned
.SetWindow(m_text
);
110 CPPUNIT_ASSERT( valUnsigned
.TransferToWindow() );
111 CPPUNIT_ASSERT_EQUAL( "0", m_text
->GetValue() );
114 CPPUNIT_ASSERT( valUnsigned
.TransferToWindow() );
115 CPPUNIT_ASSERT_EQUAL( "17", m_text
->GetValue() );
118 m_text
->ChangeValue("foobar");
119 CPPUNIT_ASSERT( !valUnsigned
.TransferFromWindow() );
121 m_text
->ChangeValue("-234");
122 CPPUNIT_ASSERT( !valUnsigned
.TransferFromWindow() );
124 m_text
->ChangeValue("234");
125 CPPUNIT_ASSERT( valUnsigned
.TransferFromWindow() );
126 CPPUNIT_ASSERT_EQUAL( 234, value
);
128 m_text
->ChangeValue("18446744073709551616"); // == ULLONG_MAX + 1
129 CPPUNIT_ASSERT( !valUnsigned
.TransferFromWindow() );
132 CPPUNIT_ASSERT( !valUnsigned
.TransferFromWindow() );
135 void NumValidatorTestCase::TransferFloat()
137 // We need a locale with point as decimal separator.
138 wxLocale
loc(wxLANGUAGE_ENGLISH_UK
, wxLOCALE_DONT_LOAD_DEFAULT
);
141 wxFloatingPointValidator
<float> valFloat(3, &value
);
142 valFloat
.SetWindow(m_text
);
144 CPPUNIT_ASSERT( valFloat
.TransferToWindow() );
145 CPPUNIT_ASSERT_EQUAL( "0.000", m_text
->GetValue() );
148 CPPUNIT_ASSERT( valFloat
.TransferToWindow() );
149 CPPUNIT_ASSERT_EQUAL( "1.234", m_text
->GetValue() );
152 CPPUNIT_ASSERT( valFloat
.TransferToWindow() );
153 CPPUNIT_ASSERT_EQUAL( "1.235", m_text
->GetValue() );
156 m_text
->ChangeValue("foobar");
157 CPPUNIT_ASSERT( !valFloat
.TransferFromWindow() );
159 m_text
->ChangeValue("-234.567");
160 CPPUNIT_ASSERT( valFloat
.TransferFromWindow() );
161 CPPUNIT_ASSERT_EQUAL( -234.567f
, value
);
164 CPPUNIT_ASSERT( !valFloat
.TransferFromWindow() );
167 void NumValidatorTestCase::ZeroAsBlank()
170 m_text
->SetValidator(
171 wxMakeIntegerValidator(&value
, wxNUM_VAL_ZERO_AS_BLANK
));
173 wxValidator
* const val
= m_text
->GetValidator();
175 CPPUNIT_ASSERT( val
->TransferToWindow() );
176 CPPUNIT_ASSERT_EQUAL( "", m_text
->GetValue() );
179 CPPUNIT_ASSERT( val
->TransferFromWindow() );
180 CPPUNIT_ASSERT_EQUAL( 0, value
);
183 void NumValidatorTestCase::NoTrailingZeroes()
185 // We need a locale with point as decimal separator.
186 wxLocale
loc(wxLANGUAGE_ENGLISH_UK
, wxLOCALE_DONT_LOAD_DEFAULT
);
189 m_text
->SetValidator(
190 wxMakeFloatingPointValidator(3, &value
, wxNUM_VAL_NO_TRAILING_ZEROES
));
192 wxValidator
* const val
= m_text
->GetValidator();
194 CPPUNIT_ASSERT( val
->TransferToWindow() );
195 CPPUNIT_ASSERT_EQUAL( "1.2", m_text
->GetValue() );
198 CPPUNIT_ASSERT( val
->TransferToWindow() );
199 CPPUNIT_ASSERT_EQUAL( "1.234", m_text
->GetValue() );
202 #if wxUSE_UIACTIONSIMULATOR
204 void NumValidatorTestCase::Interactive()
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() )
214 // Set a locale using comma as thousands separator character.
215 wxLocale
loc(wxLANGUAGE_ENGLISH_UK
, wxLOCALE_DONT_LOAD_DEFAULT
);
217 m_text
->SetValidator(
218 wxIntegerValidator
<unsigned>(NULL
, wxNUM_VAL_THOUSANDS_SEPARATOR
));
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
);
228 wxUIActionSimulator sim
;
230 // Entering '-' in a control with positive range is not allowed.
234 CPPUNIT_ASSERT_EQUAL( "", m_text
->GetValue() );
236 // Neither is entering '.' or any non-digit character.
239 CPPUNIT_ASSERT_EQUAL( "", m_text
->GetValue() );
241 // Entering digits should work though and after leaving the control the
242 // contents should be normalized.
248 CPPUNIT_ASSERT_EQUAL( "1,234,567", m_text
->GetValue() );
250 CPPUNIT_ASSERT_EQUAL( "1234567", m_text
->GetValue() );
253 // Entering both '-' and '.' in this control should work but only in the
257 CPPUNIT_ASSERT_EQUAL( "-", text2
->GetValue() );
259 text2
->SetInsertionPoint(0);
262 CPPUNIT_ASSERT_EQUAL( "-", text2
->GetValue() );
264 text2
->SetInsertionPointEnd();
267 CPPUNIT_ASSERT_EQUAL( "-.", text2
->GetValue() );
269 // Adding up to three digits after the point should work.
272 CPPUNIT_ASSERT_EQUAL( "-.987", text2
->GetValue() );
277 CPPUNIT_ASSERT_EQUAL( "-.987", text2
->GetValue() );
279 // We can remove one digit and another one though.
284 CPPUNIT_ASSERT_EQUAL( "-.96", text2
->GetValue() );
287 // Also test the range constraint.
292 CPPUNIT_ASSERT_EQUAL( "9", text2
->GetValue() );
296 CPPUNIT_ASSERT_EQUAL( "9", text2
->GetValue() );
299 #endif // wxUSE_UIACTIONSIMULATOR