valnum.cpp 8.3 KB

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