]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/spinctrltest.cpp
2338bb4248f8b948494977ccf72a2db6c2305cf2
[wxWidgets.git] / tests / controls / spinctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/spinctrltest.cpp
3 // Purpose: wxSpinCtrl unit test
4 // Author: Steven Lamerton
5 // Created: 2010-07-21
6 // RCS-ID: $Id$
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #if wxUSE_SPINCTRL
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #endif // WX_PRECOMP
21
22 #include "testableframe.h"
23 #include "wx/uiaction.h"
24 #include "wx/spinctrl.h"
25
26 class SpinCtrlTestCase : public CppUnit::TestCase
27 {
28 public:
29 SpinCtrlTestCase() { }
30
31 void setUp();
32 void tearDown();
33
34 private:
35 CPPUNIT_TEST_SUITE( SpinCtrlTestCase );
36 CPPUNIT_TEST( Initial );
37 CPPUNIT_TEST( NoEventsInCtor );
38 WXUISIM_TEST( Arrows );
39 WXUISIM_TEST( Wrap );
40 CPPUNIT_TEST( Range );
41 CPPUNIT_TEST( Value );
42 CPPUNIT_TEST_SUITE_END();
43
44 void Initial();
45 void NoEventsInCtor();
46 void Arrows();
47 void Wrap();
48 void Range();
49 void Value();
50
51 wxSpinCtrl* m_spin;
52
53 DECLARE_NO_COPY_CLASS(SpinCtrlTestCase)
54 };
55
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( SpinCtrlTestCase );
58
59 // also include in its own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SpinCtrlTestCase, "SpinCtrlTestCase" );
61
62 void SpinCtrlTestCase::setUp()
63 {
64 m_spin = new wxSpinCtrl(wxTheApp->GetTopWindow());
65 }
66
67 void SpinCtrlTestCase::tearDown()
68 {
69 wxDELETE(m_spin);
70 }
71
72 void SpinCtrlTestCase::Initial()
73 {
74 // Initial value is defined by "initial" argument which is 0 by default.
75 CPPUNIT_ASSERT_EQUAL( 0, m_spin->GetValue() );
76
77 wxWindow* const parent = m_spin->GetParent();
78
79 // Recreate the control with another "initial" to check this.
80 delete m_spin;
81 m_spin = new wxSpinCtrl(parent, wxID_ANY, "",
82 wxDefaultPosition, wxDefaultSize, 0,
83 0, 100, 17);
84 CPPUNIT_ASSERT_EQUAL( 17, m_spin->GetValue() );
85
86 // Recreate the control with another "initial" outside of standard spin
87 // ctrl range.
88 delete m_spin;
89 m_spin = new wxSpinCtrl(parent, wxID_ANY, "",
90 wxDefaultPosition, wxDefaultSize, 0,
91 0, 200, 150);
92 CPPUNIT_ASSERT_EQUAL( 150, m_spin->GetValue() );
93
94 // But if the text string is specified, it takes precedence.
95 delete m_spin;
96 m_spin = new wxSpinCtrl(parent, wxID_ANY, "99",
97 wxDefaultPosition, wxDefaultSize, 0,
98 0, 100, 17);
99 CPPUNIT_ASSERT_EQUAL( 99, m_spin->GetValue() );
100 }
101
102 void SpinCtrlTestCase::NoEventsInCtor()
103 {
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();
107 delete m_spin;
108 m_spin = new wxSpinCtrl;
109
110 EventCounter updated(m_spin, wxEVT_COMMAND_SPINCTRL_UPDATED);
111
112 m_spin->Create(parent, wxID_ANY, "",
113 wxDefaultPosition, wxDefaultSize, 0,
114 0, 100, 17);
115
116 CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
117 }
118
119 void SpinCtrlTestCase::Arrows()
120 {
121 #if wxUSE_UIACTIONSIMULATOR
122 EventCounter updated(m_spin, wxEVT_COMMAND_SPINCTRL_UPDATED);
123
124 wxUIActionSimulator sim;
125
126 m_spin->SetFocus();
127
128 sim.Char(WXK_UP);
129
130 wxYield();
131
132 CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
133 CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue());
134 updated.Clear();
135
136 sim.Char(WXK_DOWN);
137
138 wxYield();
139
140 CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
141 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
142 #endif
143 }
144
145 void SpinCtrlTestCase::Wrap()
146 {
147 #if wxUSE_UIACTIONSIMULATOR
148 wxDELETE(m_spin);
149 m_spin = new wxSpinCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
150 wxDefaultPosition, wxDefaultSize,
151 wxSP_ARROW_KEYS | wxSP_WRAP);
152
153 wxUIActionSimulator sim;
154
155 m_spin->SetFocus();
156
157 sim.Char(WXK_DOWN);
158
159 wxYield();
160
161 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue());
162
163 sim.Char(WXK_UP);
164
165 wxYield();
166
167 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
168 #endif
169 }
170
171 void SpinCtrlTestCase::Range()
172 {
173 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetMin());
174 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetMax());
175
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
178 // the user).
179 {
180 EventCounter updated(m_spin, wxEVT_COMMAND_SPINCTRL_UPDATED);
181
182 m_spin->SetRange(1, 10);
183 CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue());
184 CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
185 }
186
187 //Test negative ranges
188 m_spin->SetRange(-10, 10);
189
190 CPPUNIT_ASSERT_EQUAL(-10, m_spin->GetMin());
191 CPPUNIT_ASSERT_EQUAL(10, m_spin->GetMax());
192
193 //Test backwards ranges
194 m_spin->SetRange(75, 50);
195
196 CPPUNIT_ASSERT_EQUAL(75, m_spin->GetMin());
197 CPPUNIT_ASSERT_EQUAL(50, m_spin->GetMax());
198 }
199
200 void SpinCtrlTestCase::Value()
201 {
202 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
203
204 m_spin->SetValue(50);
205
206 CPPUNIT_ASSERT_EQUAL(50, m_spin->GetValue());
207
208 m_spin->SetValue(-10);
209
210 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
211
212 m_spin->SetValue(110);
213
214 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue());
215 }
216
217 #endif