]>
git.saurik.com Git - wxWidgets.git/blob - tests/controls/spinctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/spinctrltest.cpp
3 // Purpose: wxSpinCtrl unit test
4 // Author: Steven Lamerton
6 // Copyright: (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
21 #include "testableframe.h"
22 #include "wx/uiaction.h"
23 #include "wx/spinctrl.h"
25 class SpinCtrlTestCase
: public CppUnit::TestCase
28 SpinCtrlTestCase() { }
34 CPPUNIT_TEST_SUITE( SpinCtrlTestCase
);
35 CPPUNIT_TEST( Initial
);
36 CPPUNIT_TEST( NoEventsInCtor
);
37 WXUISIM_TEST( Arrows
);
39 CPPUNIT_TEST( Range
);
40 CPPUNIT_TEST( Value
);
41 CPPUNIT_TEST_SUITE_END();
44 void NoEventsInCtor();
52 DECLARE_NO_COPY_CLASS(SpinCtrlTestCase
)
55 // register in the unnamed registry so that these tests are run by default
56 CPPUNIT_TEST_SUITE_REGISTRATION( SpinCtrlTestCase
);
58 // also include in its own registry so that these tests can be run alone
59 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SpinCtrlTestCase
, "SpinCtrlTestCase" );
61 void SpinCtrlTestCase::setUp()
63 m_spin
= new wxSpinCtrl(wxTheApp
->GetTopWindow());
66 void SpinCtrlTestCase::tearDown()
71 void SpinCtrlTestCase::Initial()
73 // Initial value is defined by "initial" argument which is 0 by default.
74 CPPUNIT_ASSERT_EQUAL( 0, m_spin
->GetValue() );
76 wxWindow
* const parent
= m_spin
->GetParent();
78 // Recreate the control with another "initial" to check this.
80 m_spin
= new wxSpinCtrl(parent
, wxID_ANY
, "",
81 wxDefaultPosition
, wxDefaultSize
, 0,
83 CPPUNIT_ASSERT_EQUAL( 17, m_spin
->GetValue() );
85 // Recreate the control with another "initial" outside of standard spin
88 m_spin
= new wxSpinCtrl(parent
, wxID_ANY
, "",
89 wxDefaultPosition
, wxDefaultSize
, 0,
91 CPPUNIT_ASSERT_EQUAL( 150, m_spin
->GetValue() );
93 // But if the text string is specified, it takes precedence.
95 m_spin
= new wxSpinCtrl(parent
, wxID_ANY
, "99",
96 wxDefaultPosition
, wxDefaultSize
, 0,
98 CPPUNIT_ASSERT_EQUAL( 99, m_spin
->GetValue() );
101 void SpinCtrlTestCase::NoEventsInCtor()
103 // Verify that creating the control does not generate any events. This is
104 // unexpected and shouldn't happen.
105 wxWindow
* const parent
= m_spin
->GetParent();
107 m_spin
= new wxSpinCtrl
;
109 EventCounter
updatedSpin(m_spin
, wxEVT_SPINCTRL
);
110 EventCounter
updatedText(m_spin
, wxEVT_TEXT
);
112 m_spin
->Create(parent
, wxID_ANY
, "",
113 wxDefaultPosition
, wxDefaultSize
, 0,
116 CPPUNIT_ASSERT_EQUAL(0, updatedSpin
.GetCount());
117 CPPUNIT_ASSERT_EQUAL(0, updatedText
.GetCount());
120 void SpinCtrlTestCase::Arrows()
122 #if wxUSE_UIACTIONSIMULATOR
123 EventCounter
updated(m_spin
, wxEVT_SPINCTRL
);
125 wxUIActionSimulator sim
;
133 CPPUNIT_ASSERT_EQUAL(1, updated
.GetCount());
134 CPPUNIT_ASSERT_EQUAL(1, m_spin
->GetValue());
141 CPPUNIT_ASSERT_EQUAL(1, updated
.GetCount());
142 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
146 void SpinCtrlTestCase::Wrap()
148 #if wxUSE_UIACTIONSIMULATOR
150 m_spin
= new wxSpinCtrl(wxTheApp
->GetTopWindow(), wxID_ANY
, "",
151 wxDefaultPosition
, wxDefaultSize
,
152 wxSP_ARROW_KEYS
| wxSP_WRAP
);
154 wxUIActionSimulator sim
;
162 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetValue());
168 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
172 void SpinCtrlTestCase::Range()
174 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetMin());
175 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetMax());
177 // Test that the value is adjusted to be inside the new valid range but
178 // that this doesn't result in any events (as this is not something done by
181 EventCounter
updatedSpin(m_spin
, wxEVT_SPINCTRL
);
182 EventCounter
updatedText(m_spin
, wxEVT_TEXT
);
184 m_spin
->SetRange(1, 10);
185 CPPUNIT_ASSERT_EQUAL(1, m_spin
->GetValue());
187 CPPUNIT_ASSERT_EQUAL(0, updatedSpin
.GetCount());
188 CPPUNIT_ASSERT_EQUAL(0, updatedText
.GetCount());
191 //Test negative ranges
192 m_spin
->SetRange(-10, 10);
194 CPPUNIT_ASSERT_EQUAL(-10, m_spin
->GetMin());
195 CPPUNIT_ASSERT_EQUAL(10, m_spin
->GetMax());
197 //Test backwards ranges
198 m_spin
->SetRange(75, 50);
200 CPPUNIT_ASSERT_EQUAL(75, m_spin
->GetMin());
201 CPPUNIT_ASSERT_EQUAL(50, m_spin
->GetMax());
204 void SpinCtrlTestCase::Value()
206 EventCounter
updatedSpin(m_spin
, wxEVT_SPINCTRL
);
207 EventCounter
updatedText(m_spin
, wxEVT_TEXT
);
209 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
211 m_spin
->SetValue(50);
212 CPPUNIT_ASSERT_EQUAL(50, m_spin
->GetValue());
214 m_spin
->SetValue(-10);
215 CPPUNIT_ASSERT_EQUAL(0, m_spin
->GetValue());
217 m_spin
->SetValue(110);
218 CPPUNIT_ASSERT_EQUAL(100, m_spin
->GetValue());
220 // Calling SetValue() shouldn't have generated any events.
221 CPPUNIT_ASSERT_EQUAL(0, updatedSpin
.GetCount());
222 CPPUNIT_ASSERT_EQUAL(0, updatedText
.GetCount());