Don't generate events from wxSpinCtrl::SetRange() in wxMSW.
[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 // But if the text string is specified, it takes precedence.
87 delete m_spin;
88 m_spin = new wxSpinCtrl(parent, wxID_ANY, "99",
89 wxDefaultPosition, wxDefaultSize, 0,
90 0, 100, 17);
91 CPPUNIT_ASSERT_EQUAL( 99, m_spin->GetValue() );
92 }
93
94 void SpinCtrlTestCase::NoEventsInCtor()
95 {
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();
99 delete m_spin;
100 m_spin = new wxSpinCtrl;
101
102 EventCounter updated(m_spin, wxEVT_COMMAND_SPINCTRL_UPDATED);
103
104 m_spin->Create(parent, wxID_ANY, "",
105 wxDefaultPosition, wxDefaultSize, 0,
106 0, 100, 17);
107
108 CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
109 }
110
111 void SpinCtrlTestCase::Arrows()
112 {
113 #if wxUSE_UIACTIONSIMULATOR
114 EventCounter updated(m_spin, wxEVT_COMMAND_SPINCTRL_UPDATED);
115
116 wxUIActionSimulator sim;
117
118 m_spin->SetFocus();
119
120 sim.Char(WXK_UP);
121
122 wxYield();
123
124 CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
125 CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue());
126 updated.Clear();
127
128 sim.Char(WXK_DOWN);
129
130 wxYield();
131
132 CPPUNIT_ASSERT_EQUAL(1, updated.GetCount());
133 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
134 #endif
135 }
136
137 void SpinCtrlTestCase::Wrap()
138 {
139 #if wxUSE_UIACTIONSIMULATOR
140 wxDELETE(m_spin);
141 m_spin = new wxSpinCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "",
142 wxDefaultPosition, wxDefaultSize,
143 wxSP_ARROW_KEYS | wxSP_WRAP);
144
145 wxUIActionSimulator sim;
146
147 m_spin->SetFocus();
148
149 sim.Char(WXK_DOWN);
150
151 wxYield();
152
153 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue());
154
155 sim.Char(WXK_UP);
156
157 wxYield();
158
159 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
160 #endif
161 }
162
163 void SpinCtrlTestCase::Range()
164 {
165 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetMin());
166 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetMax());
167
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
170 // the user).
171 {
172 EventCounter updated(m_spin, wxEVT_COMMAND_SPINCTRL_UPDATED);
173
174 m_spin->SetRange(1, 10);
175 CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue());
176 CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
177 }
178
179 //Test negative ranges
180 m_spin->SetRange(-10, 10);
181
182 CPPUNIT_ASSERT_EQUAL(-10, m_spin->GetMin());
183 CPPUNIT_ASSERT_EQUAL(10, m_spin->GetMax());
184
185 //Test backwards ranges
186 m_spin->SetRange(75, 50);
187
188 CPPUNIT_ASSERT_EQUAL(75, m_spin->GetMin());
189 CPPUNIT_ASSERT_EQUAL(50, m_spin->GetMax());
190 }
191
192 void SpinCtrlTestCase::Value()
193 {
194 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
195
196 m_spin->SetValue(50);
197
198 CPPUNIT_ASSERT_EQUAL(50, m_spin->GetValue());
199
200 m_spin->SetValue(-10);
201
202 CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue());
203
204 m_spin->SetValue(110);
205
206 CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue());
207 }
208
209 #endif