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