spinctrltest.cpp 5.8 KB

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