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 // But if the text string is specified, it takes precedence.
88 m_spin
= new wxSpinCtrl(parent
, wxID_ANY
, "99",
89 wxDefaultPosition
, wxDefaultSize
, 0,
91 CPPUNIT_ASSERT_EQUAL( 99, m_spin
->GetValue() );
94 void SpinCtrlTestCase::NoEventsInCtor()
96 // Verify that creating the control does not generate any events. This is
97 // unexpected and shouldn't happen.
98 wxWindow
* const parent
= m_spin
->GetParent();
100 m_spin
= new wxSpinCtrl
;
102 EventCounter
updated(m_spin
, wxEVT_COMMAND_SPINCTRL_UPDATED
);
104 m_spin
->Create(parent
, wxID_ANY
, "",
105 wxDefaultPosition
, wxDefaultSize
, 0,
108 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
111 void SpinCtrlTestCase::Arrows()
113 #if wxUSE_UIACTIONSIMULATOR
114 EventCounter
updated(m_spin
, wxEVT_COMMAND_SPINCTRL_UPDATED
);
116 wxUIActionSimulator sim
;
124 CPPUNIT_ASSERT_EQUAL(1, updated
.GetCount());
125 CPPUNIT_ASSERT_EQUAL(1, m_spin
->GetValue());
132 CPPUNIT_ASSERT_EQUAL(1, updated
.GetCount());
133 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
137 void SpinCtrlTestCase::Wrap()
139 #if wxUSE_UIACTIONSIMULATOR
141 m_spin
= new wxSpinCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
142 wxDefaultPosition
, wxDefaultSize
,
143 wxSP_ARROW_KEYS
| wxSP_WRAP
);
145 wxUIActionSimulator sim
;
153 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetValue());
159 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
163 void SpinCtrlTestCase::Range()
165 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetMin());
166 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetMax());
168 // Test that the value is adjusted to be inside the new valid range but
169 // that this doesn't result in any events (as this is not something done by
172 EventCounter
updated(m_spin
, wxEVT_COMMAND_SPINCTRL_UPDATED
);
174 m_spin
->SetRange(1, 10);
175 CPPUNIT_ASSERT_EQUAL(1, m_spin
->GetValue());
176 CPPUNIT_ASSERT_EQUAL(0, updated
.GetCount());
179 //Test negative ranges
180 m_spin
->SetRange(-10, 10);
182 CPPUNIT_ASSERT_EQUAL(-10, m_spin
->GetMin());
183 CPPUNIT_ASSERT_EQUAL(10, m_spin
->GetMax());
185 //Test backwards ranges
186 m_spin
->SetRange(75, 50);
188 CPPUNIT_ASSERT_EQUAL(75, m_spin
->GetMin());
189 CPPUNIT_ASSERT_EQUAL(50, m_spin
->GetMax());
192 void SpinCtrlTestCase::Value()
194 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
196 m_spin
->SetValue(50);
198 CPPUNIT_ASSERT_EQUAL(50, m_spin
->GetValue());
200 m_spin
->SetValue(-10);
202 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
204 m_spin
->SetValue(110);
206 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetValue());