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