1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/spinctrltest.cpp
3 // Purpose: wxSpinCtrl unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
22 #include "testableframe.h"
23 #include "wx/uiaction.h"
24 #include "wx/spinctrl.h"
26 class SpinCtrlTestCase
: public CppUnit::TestCase
29 SpinCtrlTestCase() { }
35 CPPUNIT_TEST_SUITE( SpinCtrlTestCase
);
36 CPPUNIT_TEST( Initial
);
37 CPPUNIT_TEST( NoEventsInCtor
);
38 WXUISIM_TEST( Arrows
);
40 CPPUNIT_TEST( Range
);
41 CPPUNIT_TEST( Value
);
42 CPPUNIT_TEST_SUITE_END();
45 void NoEventsInCtor();
53 DECLARE_NO_COPY_CLASS(SpinCtrlTestCase
)
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( SpinCtrlTestCase
);
59 // also include in its own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SpinCtrlTestCase
, "SpinCtrlTestCase" );
62 void SpinCtrlTestCase::setUp()
64 m_spin
= new wxSpinCtrl(wxTheApp
->GetTopWindow());
67 void SpinCtrlTestCase::tearDown()
72 void SpinCtrlTestCase::Initial()
74 // Initial value is defined by "initial" argument which is 0 by default.
75 CPPUNIT_ASSERT_EQUAL( 0, m_spin
->GetValue() );
77 wxWindow
* const parent
= m_spin
->GetParent();
79 // Recreate the control with another "initial" to check this.
81 m_spin
= new wxSpinCtrl(parent
, wxID_ANY
, "",
82 wxDefaultPosition
, wxDefaultSize
, 0,
84 CPPUNIT_ASSERT_EQUAL( 17, m_spin
->GetValue() );
86 // Recreate the control with another "initial" outside of standard spin
89 m_spin
= new wxSpinCtrl(parent
, wxID_ANY
, "",
90 wxDefaultPosition
, wxDefaultSize
, 0,
92 CPPUNIT_ASSERT_EQUAL( 150, m_spin
->GetValue() );
94 // But if the text string is specified, it takes precedence.
96 m_spin
= new wxSpinCtrl(parent
, wxID_ANY
, "99",
97 wxDefaultPosition
, wxDefaultSize
, 0,
99 CPPUNIT_ASSERT_EQUAL( 99, m_spin
->GetValue() );
102 void SpinCtrlTestCase::NoEventsInCtor()
104 // Verify that creating the control does not generate any events. This is
105 // unexpected and shouldn't happen.
106 wxWindow
* const parent
= m_spin
->GetParent();
108 m_spin
= new wxSpinCtrl
;
110 EventCounter
updated(m_spin
, wxEVT_COMMAND_SPINCTRL_UPDATED
);
112 m_spin
->Create(parent
, wxID_ANY
, "",
113 wxDefaultPosition
, wxDefaultSize
, 0,
116 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
119 void SpinCtrlTestCase::Arrows()
121 #if wxUSE_UIACTIONSIMULATOR
122 EventCounter
updated(m_spin
, wxEVT_COMMAND_SPINCTRL_UPDATED
);
124 wxUIActionSimulator sim
;
132 CPPUNIT_ASSERT_EQUAL(1, updated
.GetCount());
133 CPPUNIT_ASSERT_EQUAL(1, m_spin
->GetValue());
140 CPPUNIT_ASSERT_EQUAL(1, updated
.GetCount());
141 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
145 void SpinCtrlTestCase::Wrap()
147 #if wxUSE_UIACTIONSIMULATOR
149 m_spin
= new wxSpinCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
150 wxDefaultPosition
, wxDefaultSize
,
151 wxSP_ARROW_KEYS
| wxSP_WRAP
);
153 wxUIActionSimulator sim
;
161 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetValue());
167 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
171 void SpinCtrlTestCase::Range()
173 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetMin());
174 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetMax());
176 // Test that the value is adjusted to be inside the new valid range but
177 // that this doesn't result in any events (as this is not something done by
180 EventCounter
updated(m_spin
, wxEVT_COMMAND_SPINCTRL_UPDATED
);
182 m_spin
->SetRange(1, 10);
183 CPPUNIT_ASSERT_EQUAL(1, m_spin
->GetValue());
184 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
187 //Test negative ranges
188 m_spin
->SetRange(-10, 10);
190 CPPUNIT_ASSERT_EQUAL(-10, m_spin
->GetMin());
191 CPPUNIT_ASSERT_EQUAL(10, m_spin
->GetMax());
193 //Test backwards ranges
194 m_spin
->SetRange(75, 50);
196 CPPUNIT_ASSERT_EQUAL(75, m_spin
->GetMin());
197 CPPUNIT_ASSERT_EQUAL(50, m_spin
->GetMax());
200 void SpinCtrlTestCase::Value()
202 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
204 m_spin
->SetValue(50);
206 CPPUNIT_ASSERT_EQUAL(50, m_spin
->GetValue());
208 m_spin
->SetValue(-10);
210 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
212 m_spin
->SetValue(110);
214 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetValue());