spinctrldbltest.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/spinctrldbltest.cpp
  3. // Purpose: wxSpinCtrlDouble unit test
  4. // Author: Steven Lamerton
  5. // Created: 2010-07-22
  6. // Copyright: (c) 2010 Steven Lamerton
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #ifdef __BORLANDC__
  10. #pragma hdrstop
  11. #endif
  12. #ifndef WX_PRECOMP
  13. #include "wx/app.h"
  14. #endif // WX_PRECOMP
  15. #include "testableframe.h"
  16. #include "wx/uiaction.h"
  17. #include "wx/spinctrl.h"
  18. class SpinCtrlDoubleTestCase : public CppUnit::TestCase
  19. {
  20. public:
  21. SpinCtrlDoubleTestCase() { }
  22. void setUp();
  23. void tearDown();
  24. private:
  25. CPPUNIT_TEST_SUITE( SpinCtrlDoubleTestCase );
  26. CPPUNIT_TEST( NoEventsInCtor );
  27. WXUISIM_TEST( Arrows );
  28. WXUISIM_TEST( Wrap );
  29. CPPUNIT_TEST( Range );
  30. CPPUNIT_TEST( Value );
  31. WXUISIM_TEST( Increment );
  32. CPPUNIT_TEST( Digits );
  33. CPPUNIT_TEST_SUITE_END();
  34. void NoEventsInCtor();
  35. void Arrows();
  36. void Wrap();
  37. void Range();
  38. void Value();
  39. void Increment();
  40. void Digits();
  41. wxSpinCtrlDouble* m_spin;
  42. DECLARE_NO_COPY_CLASS(SpinCtrlDoubleTestCase)
  43. };
  44. // register in the unnamed registry so that these tests are run by default
  45. CPPUNIT_TEST_SUITE_REGISTRATION( SpinCtrlDoubleTestCase );
  46. // also include in its own registry so that these tests can be run alone
  47. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SpinCtrlDoubleTestCase, "SpinCtrlDoubleTestCase" );
  48. void SpinCtrlDoubleTestCase::setUp()
  49. {
  50. m_spin = new wxSpinCtrlDouble(wxTheApp->GetTopWindow());
  51. }
  52. void SpinCtrlDoubleTestCase::tearDown()
  53. {
  54. wxDELETE(m_spin);
  55. }
  56. void SpinCtrlDoubleTestCase::NoEventsInCtor()
  57. {
  58. // Verify that creating the control does not generate any events. This is
  59. // unexpected and shouldn't happen.
  60. wxWindow* const parent = m_spin->GetParent();
  61. delete m_spin;
  62. m_spin = new wxSpinCtrlDouble;
  63. EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
  64. EventCounter updatedText(m_spin, wxEVT_TEXT);
  65. m_spin->Create(parent, wxID_ANY, "",
  66. wxDefaultPosition, wxDefaultSize, 0,
  67. 0., 100., 17.);
  68. CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
  69. CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
  70. }
  71. void SpinCtrlDoubleTestCase::Arrows()
  72. {
  73. #ifndef __WXGTK__
  74. EventCounter updated(m_spin, wxEVT_SPINCTRLDOUBLE);
  75. wxUIActionSimulator sim;
  76. m_spin->SetFocus();
  77. wxYield();
  78. sim.Char(WXK_UP);
  79. wxYield();
  80. CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
  81. CPPUNIT_ASSERT_EQUAL(1.0, m_spin->GetValue());
  82. updated.Clear();
  83. sim.Char(WXK_DOWN);
  84. wxYield();
  85. CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
  86. CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue());
  87. #endif
  88. }
  89. void SpinCtrlDoubleTestCase::Wrap()
  90. {
  91. #if wxUSE_UIACTIONSIMULATOR
  92. wxDELETE(m_spin);
  93. m_spin = new wxSpinCtrlDouble(wxTheApp->GetTopWindow(), wxID_ANY, "",
  94. wxDefaultPosition, wxDefaultSize,
  95. wxSP_ARROW_KEYS | wxSP_WRAP);
  96. wxUIActionSimulator sim;
  97. m_spin->SetFocus();
  98. sim.Char(WXK_DOWN);
  99. wxYield();
  100. CPPUNIT_ASSERT_EQUAL(100.0, m_spin->GetValue());
  101. sim.Char(WXK_UP);
  102. wxYield();
  103. CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue());
  104. #endif
  105. }
  106. void SpinCtrlDoubleTestCase::Range()
  107. {
  108. CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetMin());
  109. CPPUNIT_ASSERT_EQUAL(100.0, m_spin->GetMax());
  110. // Test that the value is adjusted to be inside the new valid range but
  111. // that this doesn't result in any events (as this is not something done by
  112. // the user).
  113. {
  114. EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
  115. EventCounter updatedText(m_spin, wxEVT_TEXT);
  116. m_spin->SetRange(1., 10.);
  117. CPPUNIT_ASSERT_EQUAL(1., m_spin->GetValue());
  118. CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
  119. CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
  120. }
  121. //Test negative ranges
  122. m_spin->SetRange(-10.0, 10.0);
  123. CPPUNIT_ASSERT_EQUAL(-10.0, m_spin->GetMin());
  124. CPPUNIT_ASSERT_EQUAL(10.0, m_spin->GetMax());
  125. //Test backwards ranges
  126. m_spin->SetRange(75.0, 50.0);
  127. CPPUNIT_ASSERT_EQUAL(75.0, m_spin->GetMin());
  128. CPPUNIT_ASSERT_EQUAL(50.0, m_spin->GetMax());
  129. }
  130. void SpinCtrlDoubleTestCase::Value()
  131. {
  132. EventCounter updatedSpin(m_spin, wxEVT_SPINCTRLDOUBLE);
  133. EventCounter updatedText(m_spin, wxEVT_TEXT);
  134. m_spin->SetDigits(2);
  135. m_spin->SetIncrement(0.1);
  136. CPPUNIT_ASSERT_EQUAL(0.0, m_spin->GetValue());
  137. m_spin->SetValue(50.0);
  138. CPPUNIT_ASSERT_EQUAL(50.0, m_spin->GetValue());
  139. m_spin->SetValue(49.1);
  140. CPPUNIT_ASSERT_EQUAL(49.1, m_spin->GetValue());
  141. // Calling SetValue() shouldn't have generated any events.
  142. CPPUNIT_ASSERT_EQUAL(0, updatedSpin.GetCount());
  143. CPPUNIT_ASSERT_EQUAL(0, updatedText.GetCount());
  144. }
  145. void SpinCtrlDoubleTestCase::Increment()
  146. {
  147. #if wxUSE_UIACTIONSIMULATOR && !defined(__WXGTK__)
  148. CPPUNIT_ASSERT_EQUAL(1.0, m_spin->GetIncrement());
  149. m_spin->SetIncrement(0.1);
  150. CPPUNIT_ASSERT_EQUAL(0.1, m_spin->GetIncrement());
  151. wxUIActionSimulator sim;
  152. m_spin->SetFocus();
  153. sim.Char(WXK_UP);
  154. wxYield();
  155. CPPUNIT_ASSERT_EQUAL(0.1, m_spin->GetValue());
  156. #endif
  157. }
  158. void SpinCtrlDoubleTestCase::Digits()
  159. {
  160. m_spin->SetDigits(5);
  161. CPPUNIT_ASSERT_EQUAL(5, m_spin->GetDigits());
  162. }