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