]>
Commit | Line | Data |
---|---|---|
232fdc63 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/controls/windowtest.cpp | |
3 | // Purpose: wxWindow unit test | |
4 | // Author: Steven Lamerton | |
5 | // Created: 2010-07-10 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2010 Steven Lamerton | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #include "testprec.h" | |
11 | ||
12 | #ifdef __BORLANDC__ | |
13 | #pragma hdrstop | |
14 | #endif | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/app.h" | |
18 | #include "wx/window.h" | |
19 | #include "wx/button.h" | |
20 | #endif // WX_PRECOMP | |
21 | ||
22 | #include "asserthelper.h" | |
23 | #include "testableframe.h" | |
24 | #include "wx/uiaction.h" | |
25 | #include "wx/caret.h" | |
26 | #include "wx/cshelp.h" | |
27 | #include "wx/tooltip.h" | |
28 | ||
29 | class WindowTestCase : public CppUnit::TestCase | |
30 | { | |
31 | public: | |
32 | WindowTestCase() { } | |
33 | ||
34 | void setUp(); | |
35 | void tearDown(); | |
36 | ||
37 | private: | |
38 | CPPUNIT_TEST_SUITE( WindowTestCase ); | |
39 | CPPUNIT_TEST( ShowHideEvent ); | |
40 | WXUISIM_TEST( KeyEvent ); | |
41 | CPPUNIT_TEST( FocusEvent ); | |
42 | CPPUNIT_TEST( Mouse ); | |
43 | CPPUNIT_TEST( Properties ); | |
fa380736 | 44 | #if wxUSE_TOOLTIPS |
232fdc63 | 45 | CPPUNIT_TEST( ToolTip ); |
fa380736 | 46 | #endif // wxUSE_TOOLTIPS |
232fdc63 VZ |
47 | CPPUNIT_TEST( Help ); |
48 | CPPUNIT_TEST( Parent ); | |
49 | CPPUNIT_TEST( Siblings ); | |
50 | CPPUNIT_TEST( Children ); | |
51 | CPPUNIT_TEST( Focus ); | |
52 | CPPUNIT_TEST( Positioning ); | |
53 | CPPUNIT_TEST( Show ); | |
54 | CPPUNIT_TEST( Enable ); | |
55 | CPPUNIT_TEST( FindWindowBy ); | |
56 | CPPUNIT_TEST_SUITE_END(); | |
57 | ||
58 | void ShowHideEvent(); | |
59 | void KeyEvent(); | |
60 | void FocusEvent(); | |
61 | void Mouse(); | |
62 | void Properties(); | |
fa380736 | 63 | #if wxUSE_TOOLTIPS |
232fdc63 | 64 | void ToolTip(); |
fa380736 | 65 | #endif // wxUSE_TOOLTIPS |
232fdc63 VZ |
66 | void Help(); |
67 | void Parent(); | |
68 | void Siblings(); | |
69 | void Children(); | |
70 | void Focus(); | |
71 | void Positioning(); | |
72 | void Show(); | |
73 | void Enable(); | |
74 | void FindWindowBy(); | |
75 | ||
76 | wxWindow *m_window; | |
77 | ||
78 | DECLARE_NO_COPY_CLASS(WindowTestCase) | |
79 | }; | |
80 | ||
81 | // register in the unnamed registry so that these tests are run by default | |
82 | CPPUNIT_TEST_SUITE_REGISTRATION( WindowTestCase ); | |
83 | ||
e3778b4d | 84 | // also include in its own registry so that these tests can be run alone |
232fdc63 VZ |
85 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WindowTestCase, "WindowTestCase" ); |
86 | ||
87 | void WindowTestCase::setUp() | |
88 | { | |
89 | m_window = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY); | |
90 | } | |
91 | ||
92 | void WindowTestCase::tearDown() | |
93 | { | |
94 | wxTheApp->GetTopWindow()->DestroyChildren(); | |
95 | } | |
96 | ||
97 | void WindowTestCase::ShowHideEvent() | |
98 | { | |
99 | #if defined(__WXMSW__) || defined (__WXPM__) | |
100 | wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(), | |
101 | wxTestableFrame); | |
102 | ||
103 | EventCounter count(m_window, wxEVT_SHOW); | |
104 | ||
105 | CPPUNIT_ASSERT(m_window->IsShown()); | |
106 | ||
107 | m_window->Show(false); | |
108 | ||
109 | CPPUNIT_ASSERT(!m_window->IsShown()); | |
110 | ||
111 | m_window->Show(); | |
112 | ||
113 | CPPUNIT_ASSERT(m_window->IsShown()); | |
114 | ||
115 | CPPUNIT_ASSERT_EQUAL(2, frame->GetEventCount()); | |
116 | #endif | |
117 | } | |
118 | ||
119 | void WindowTestCase::KeyEvent() | |
120 | { | |
121 | #if wxUSE_UIACTIONSIMULATOR | |
122 | wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(), | |
123 | wxTestableFrame); | |
124 | ||
125 | EventCounter count(m_window, wxEVT_KEY_DOWN); | |
126 | EventCounter count1(m_window, wxEVT_KEY_UP); | |
127 | EventCounter count2(m_window, wxEVT_CHAR); | |
128 | ||
129 | wxUIActionSimulator sim; | |
130 | ||
131 | m_window->SetFocus(); | |
132 | ||
133 | sim.Text("text"); | |
134 | sim.Char(WXK_SHIFT); | |
135 | wxYield(); | |
136 | ||
137 | CPPUNIT_ASSERT_EQUAL(5, frame->GetEventCount(wxEVT_KEY_DOWN)); | |
138 | CPPUNIT_ASSERT_EQUAL(5, frame->GetEventCount(wxEVT_KEY_UP)); | |
139 | CPPUNIT_ASSERT_EQUAL(4, frame->GetEventCount(wxEVT_CHAR)); | |
140 | #endif | |
141 | } | |
142 | ||
143 | void WindowTestCase::FocusEvent() | |
144 | { | |
145 | #ifndef __WXOSX__ | |
146 | wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(), | |
147 | wxTestableFrame); | |
148 | ||
149 | EventCounter count(m_window, wxEVT_SET_FOCUS); | |
150 | EventCounter count1(m_window, wxEVT_KILL_FOCUS); | |
151 | ||
152 | m_window->SetFocus(); | |
153 | ||
154 | CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_SET_FOCUS)); | |
155 | CPPUNIT_ASSERT(m_window->HasFocus()); | |
156 | ||
157 | wxButton* button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY); | |
158 | ||
159 | button->SetFocus(); | |
160 | ||
161 | CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_KILL_FOCUS)); | |
162 | CPPUNIT_ASSERT(!m_window->HasFocus()); | |
163 | #endif | |
164 | } | |
165 | ||
166 | void WindowTestCase::Mouse() | |
167 | { | |
168 | wxCursor cursor(wxCURSOR_CHAR); | |
169 | m_window->SetCursor(cursor); | |
170 | ||
171 | CPPUNIT_ASSERT(m_window->GetCursor().IsOk()); | |
172 | ||
173 | //A plain window doesn't have a caret | |
174 | CPPUNIT_ASSERT(!m_window->GetCaret()); | |
175 | ||
176 | wxCaret* caret = new wxCaret(m_window, 16, 16); | |
177 | m_window->SetCaret(caret); | |
178 | ||
179 | CPPUNIT_ASSERT(m_window->GetCaret()->IsOk()); | |
180 | ||
181 | m_window->CaptureMouse(); | |
182 | ||
183 | CPPUNIT_ASSERT(m_window->HasCapture()); | |
184 | ||
185 | m_window->ReleaseMouse(); | |
186 | ||
187 | CPPUNIT_ASSERT(!m_window->HasCapture()); | |
188 | } | |
189 | ||
190 | void WindowTestCase::Properties() | |
191 | { | |
232fdc63 VZ |
192 | m_window->SetLabel("label"); |
193 | ||
194 | CPPUNIT_ASSERT_EQUAL("label", m_window->GetLabel()); | |
232fdc63 VZ |
195 | |
196 | m_window->SetName("name"); | |
197 | ||
198 | CPPUNIT_ASSERT_EQUAL("name", m_window->GetName()); | |
199 | ||
200 | //As we used wxID_ANY we should have a negative id | |
201 | CPPUNIT_ASSERT(m_window->GetId() < 0); | |
202 | ||
203 | m_window->SetId(wxID_HIGHEST + 10); | |
204 | ||
205 | CPPUNIT_ASSERT_EQUAL(wxID_HIGHEST + 10, m_window->GetId()); | |
206 | } | |
207 | ||
fa380736 | 208 | #if wxUSE_TOOLTIPS |
232fdc63 VZ |
209 | void WindowTestCase::ToolTip() |
210 | { | |
211 | CPPUNIT_ASSERT(!m_window->GetToolTip()); | |
212 | CPPUNIT_ASSERT_EQUAL("", m_window->GetToolTipText()); | |
213 | ||
214 | m_window->SetToolTip("text tip"); | |
215 | ||
216 | CPPUNIT_ASSERT_EQUAL("text tip", m_window->GetToolTipText()); | |
217 | ||
218 | m_window->UnsetToolTip(); | |
219 | ||
220 | CPPUNIT_ASSERT(!m_window->GetToolTip()); | |
221 | CPPUNIT_ASSERT_EQUAL("", m_window->GetToolTipText()); | |
222 | ||
223 | wxToolTip* tip = new wxToolTip("other tip"); | |
224 | ||
225 | m_window->SetToolTip(tip); | |
226 | ||
227 | CPPUNIT_ASSERT_EQUAL(tip, m_window->GetToolTip()); | |
228 | CPPUNIT_ASSERT_EQUAL("other tip", m_window->GetToolTipText()); | |
229 | } | |
fa380736 | 230 | #endif // wxUSE_TOOLTIPS |
232fdc63 VZ |
231 | |
232 | void WindowTestCase::Help() | |
233 | { | |
234 | wxHelpProvider::Set(new wxSimpleHelpProvider()); | |
235 | ||
236 | CPPUNIT_ASSERT_EQUAL("", m_window->GetHelpText()); | |
237 | ||
238 | m_window->SetHelpText("helptext"); | |
239 | ||
240 | CPPUNIT_ASSERT_EQUAL("helptext", m_window->GetHelpText()); | |
241 | } | |
242 | ||
243 | void WindowTestCase::Parent() | |
244 | { | |
245 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetGrandParent()); | |
246 | CPPUNIT_ASSERT_EQUAL(wxTheApp->GetTopWindow(), m_window->GetParent()); | |
247 | } | |
248 | ||
249 | void WindowTestCase::Siblings() | |
250 | { | |
251 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetNextSibling()); | |
252 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetPrevSibling()); | |
253 | ||
254 | wxWindow* newwin = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY); | |
255 | ||
256 | CPPUNIT_ASSERT_EQUAL(newwin, m_window->GetNextSibling()); | |
257 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetPrevSibling()); | |
258 | ||
259 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), newwin->GetNextSibling()); | |
260 | CPPUNIT_ASSERT_EQUAL(m_window, newwin->GetPrevSibling()); | |
261 | ||
262 | wxDELETE(newwin); | |
263 | } | |
264 | ||
265 | void WindowTestCase::Children() | |
266 | { | |
267 | CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount()); | |
268 | ||
269 | wxWindow* child1 = new wxWindow(m_window, wxID_ANY); | |
270 | ||
271 | CPPUNIT_ASSERT_EQUAL(1, m_window->GetChildren().GetCount()); | |
272 | ||
273 | m_window->RemoveChild(child1); | |
274 | ||
275 | CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount()); | |
276 | ||
277 | child1->SetId(wxID_HIGHEST + 1); | |
278 | child1->SetName("child1"); | |
279 | ||
280 | m_window->AddChild(child1); | |
281 | ||
282 | CPPUNIT_ASSERT_EQUAL(1, m_window->GetChildren().GetCount()); | |
283 | CPPUNIT_ASSERT_EQUAL(child1, m_window->FindWindow(wxID_HIGHEST + 1)); | |
284 | CPPUNIT_ASSERT_EQUAL(child1, m_window->FindWindow("child1")); | |
285 | ||
286 | m_window->DestroyChildren(); | |
287 | ||
288 | CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount()); | |
289 | } | |
290 | ||
291 | void WindowTestCase::Focus() | |
292 | { | |
293 | #ifndef __WXOSX__ | |
294 | CPPUNIT_ASSERT(!m_window->HasFocus()); | |
295 | ||
296 | if ( m_window->AcceptsFocus() ) | |
297 | { | |
298 | m_window->SetFocus(); | |
299 | CPPUNIT_ASSERT(m_window->HasFocus()); | |
300 | } | |
301 | ||
302 | //Set the focus back to the main window | |
303 | wxTheApp->GetTopWindow()->SetFocus(); | |
304 | ||
305 | if ( m_window->AcceptsFocusFromKeyboard() ) | |
306 | { | |
307 | m_window->SetFocusFromKbd(); | |
308 | CPPUNIT_ASSERT(m_window->HasFocus()); | |
309 | } | |
310 | #endif | |
311 | } | |
312 | ||
313 | void WindowTestCase::Positioning() | |
314 | { | |
315 | //Some basic tests for consistency | |
316 | int x, y; | |
317 | m_window->GetPosition(&x, &y); | |
318 | ||
319 | CPPUNIT_ASSERT_EQUAL(x, m_window->GetPosition().x); | |
320 | CPPUNIT_ASSERT_EQUAL(y, m_window->GetPosition().y); | |
321 | CPPUNIT_ASSERT_EQUAL(m_window->GetPosition(), | |
322 | m_window->GetRect().GetTopLeft()); | |
323 | ||
324 | m_window->GetScreenPosition(&x, &y); | |
325 | CPPUNIT_ASSERT_EQUAL(x, m_window->GetScreenPosition().x); | |
326 | CPPUNIT_ASSERT_EQUAL(y, m_window->GetScreenPosition().y); | |
327 | CPPUNIT_ASSERT_EQUAL(m_window->GetScreenPosition(), | |
328 | m_window->GetScreenRect().GetTopLeft()); | |
329 | } | |
330 | ||
331 | void WindowTestCase::Show() | |
332 | { | |
333 | CPPUNIT_ASSERT(m_window->IsShown()); | |
334 | ||
335 | m_window->Hide(); | |
336 | ||
337 | CPPUNIT_ASSERT(!m_window->IsShown()); | |
338 | ||
339 | m_window->Show(); | |
340 | ||
341 | CPPUNIT_ASSERT(m_window->IsShown()); | |
342 | ||
343 | m_window->Show(false); | |
344 | ||
345 | CPPUNIT_ASSERT(!m_window->IsShown()); | |
346 | ||
347 | m_window->ShowWithEffect(wxSHOW_EFFECT_BLEND); | |
348 | ||
349 | CPPUNIT_ASSERT(m_window->IsShown()); | |
350 | ||
351 | m_window->HideWithEffect(wxSHOW_EFFECT_BLEND); | |
352 | ||
353 | CPPUNIT_ASSERT(!m_window->IsShown()); | |
354 | } | |
355 | ||
356 | void WindowTestCase::Enable() | |
357 | { | |
358 | CPPUNIT_ASSERT(m_window->IsEnabled()); | |
359 | ||
360 | m_window->Disable(); | |
361 | ||
362 | CPPUNIT_ASSERT(!m_window->IsEnabled()); | |
363 | ||
364 | m_window->Enable(); | |
365 | ||
366 | CPPUNIT_ASSERT(m_window->IsEnabled()); | |
367 | ||
368 | m_window->Enable(false); | |
369 | ||
370 | CPPUNIT_ASSERT(!m_window->IsEnabled()); | |
371 | } | |
372 | ||
373 | void WindowTestCase::FindWindowBy() | |
374 | { | |
375 | m_window->SetId(wxID_HIGHEST + 1); | |
376 | m_window->SetName("name"); | |
232fdc63 | 377 | m_window->SetLabel("label"); |
232fdc63 VZ |
378 | |
379 | CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowById(wxID_HIGHEST + 1)); | |
380 | CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowByName("name")); | |
232fdc63 | 381 | CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowByLabel("label")); |
232fdc63 VZ |
382 | |
383 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), | |
384 | wxWindow::FindWindowById(wxID_HIGHEST + 3)); | |
385 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), | |
386 | wxWindow::FindWindowByName("noname")); | |
387 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), | |
388 | wxWindow::FindWindowByLabel("nolabel")); | |
389 | } |