]>
Commit | Line | Data |
---|---|---|
232fdc63 VZ |
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 ); | |
6e36db5e | 36 | CPPUNIT_TEST( Initial ); |
232fdc63 VZ |
37 | WXUISIM_TEST( Arrows ); |
38 | WXUISIM_TEST( Wrap ); | |
39 | CPPUNIT_TEST( Range ); | |
40 | CPPUNIT_TEST( Value ); | |
41 | CPPUNIT_TEST_SUITE_END(); | |
42 | ||
6e36db5e | 43 | void Initial(); |
232fdc63 VZ |
44 | void Arrows(); |
45 | void Wrap(); | |
46 | void Range(); | |
47 | void Value(); | |
48 | ||
49 | wxSpinCtrl* m_spin; | |
50 | ||
51 | DECLARE_NO_COPY_CLASS(SpinCtrlTestCase) | |
52 | }; | |
53 | ||
54 | // register in the unnamed registry so that these tests are run by default | |
55 | CPPUNIT_TEST_SUITE_REGISTRATION( SpinCtrlTestCase ); | |
56 | ||
e3778b4d | 57 | // also include in its own registry so that these tests can be run alone |
232fdc63 VZ |
58 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SpinCtrlTestCase, "SpinCtrlTestCase" ); |
59 | ||
60 | void SpinCtrlTestCase::setUp() | |
61 | { | |
62 | m_spin = new wxSpinCtrl(wxTheApp->GetTopWindow()); | |
63 | } | |
64 | ||
65 | void SpinCtrlTestCase::tearDown() | |
66 | { | |
67 | wxDELETE(m_spin); | |
68 | } | |
69 | ||
6e36db5e VZ |
70 | void SpinCtrlTestCase::Initial() |
71 | { | |
72 | // Initial value is defined by "initial" argument which is 0 by default. | |
73 | CPPUNIT_ASSERT_EQUAL( 0, m_spin->GetValue() ); | |
74 | ||
75 | wxWindow* const parent = m_spin->GetParent(); | |
76 | ||
77 | // Recreate the control with another "initial" to check this. | |
78 | delete m_spin; | |
79 | m_spin = new wxSpinCtrl(parent, wxID_ANY, "", | |
80 | wxDefaultPosition, wxDefaultSize, 0, | |
81 | 0, 100, 17); | |
82 | CPPUNIT_ASSERT_EQUAL( 17, m_spin->GetValue() ); | |
83 | ||
84 | // But if the text string is specified, it takes precedence. | |
85 | delete m_spin; | |
86 | m_spin = new wxSpinCtrl(parent, wxID_ANY, "99", | |
87 | wxDefaultPosition, wxDefaultSize, 0, | |
88 | 0, 100, 17); | |
89 | CPPUNIT_ASSERT_EQUAL( 99, m_spin->GetValue() ); | |
90 | } | |
91 | ||
232fdc63 VZ |
92 | void SpinCtrlTestCase::Arrows() |
93 | { | |
94 | #if wxUSE_UIACTIONSIMULATOR | |
744d91d4 | 95 | EventCounter updated(m_spin, wxEVT_COMMAND_SPINCTRL_UPDATED); |
232fdc63 VZ |
96 | |
97 | wxUIActionSimulator sim; | |
98 | ||
99 | m_spin->SetFocus(); | |
100 | ||
101 | sim.Char(WXK_UP); | |
102 | ||
103 | wxYield(); | |
104 | ||
744d91d4 | 105 | CPPUNIT_ASSERT_EQUAL(1, updated.GetCount()); |
232fdc63 | 106 | CPPUNIT_ASSERT_EQUAL(1, m_spin->GetValue()); |
744d91d4 | 107 | updated.Clear(); |
232fdc63 VZ |
108 | |
109 | sim.Char(WXK_DOWN); | |
110 | ||
111 | wxYield(); | |
112 | ||
744d91d4 | 113 | CPPUNIT_ASSERT_EQUAL(1, updated.GetCount()); |
232fdc63 VZ |
114 | CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue()); |
115 | #endif | |
116 | } | |
117 | ||
118 | void SpinCtrlTestCase::Wrap() | |
119 | { | |
120 | #if wxUSE_UIACTIONSIMULATOR | |
121 | wxDELETE(m_spin); | |
122 | m_spin = new wxSpinCtrl(wxTheApp->GetTopWindow(), wxID_ANY, "", | |
123 | wxDefaultPosition, wxDefaultSize, | |
124 | wxSP_ARROW_KEYS | wxSP_WRAP); | |
125 | ||
126 | wxUIActionSimulator sim; | |
127 | ||
128 | m_spin->SetFocus(); | |
129 | ||
130 | sim.Char(WXK_DOWN); | |
131 | ||
132 | wxYield(); | |
133 | ||
134 | CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue()); | |
135 | ||
136 | sim.Char(WXK_UP); | |
137 | ||
138 | wxYield(); | |
139 | ||
140 | CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue()); | |
141 | #endif | |
142 | } | |
143 | ||
144 | void SpinCtrlTestCase::Range() | |
145 | { | |
146 | CPPUNIT_ASSERT_EQUAL(0, m_spin->GetMin()); | |
147 | CPPUNIT_ASSERT_EQUAL(100, m_spin->GetMax()); | |
148 | ||
149 | //Test neagtive ranges | |
150 | m_spin->SetRange(-10, 10); | |
151 | ||
152 | CPPUNIT_ASSERT_EQUAL(-10, m_spin->GetMin()); | |
153 | CPPUNIT_ASSERT_EQUAL(10, m_spin->GetMax()); | |
154 | ||
155 | //Test backwards ranges | |
156 | m_spin->SetRange(75, 50); | |
157 | ||
158 | CPPUNIT_ASSERT_EQUAL(75, m_spin->GetMin()); | |
159 | CPPUNIT_ASSERT_EQUAL(50, m_spin->GetMax()); | |
160 | } | |
161 | ||
162 | void SpinCtrlTestCase::Value() | |
163 | { | |
164 | CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue()); | |
165 | ||
166 | m_spin->SetValue(50); | |
167 | ||
168 | CPPUNIT_ASSERT_EQUAL(50, m_spin->GetValue()); | |
169 | ||
170 | m_spin->SetValue(-10); | |
171 | ||
172 | CPPUNIT_ASSERT_EQUAL(0, m_spin->GetValue()); | |
173 | ||
174 | m_spin->SetValue(110); | |
175 | ||
176 | CPPUNIT_ASSERT_EQUAL(100, m_spin->GetValue()); | |
177 | } | |
178 | ||
179 | #endif |