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