]>
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 ); | |
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 | { | |
232fdc63 VZ |
188 | m_window->SetLabel("label"); |
189 | ||
190 | CPPUNIT_ASSERT_EQUAL("label", m_window->GetLabel()); | |
232fdc63 VZ |
191 | |
192 | m_window->SetName("name"); | |
193 | ||
194 | CPPUNIT_ASSERT_EQUAL("name", m_window->GetName()); | |
195 | ||
196 | //As we used wxID_ANY we should have a negative id | |
197 | CPPUNIT_ASSERT(m_window->GetId() < 0); | |
198 | ||
199 | m_window->SetId(wxID_HIGHEST + 10); | |
200 | ||
201 | CPPUNIT_ASSERT_EQUAL(wxID_HIGHEST + 10, m_window->GetId()); | |
202 | } | |
203 | ||
204 | void WindowTestCase::ToolTip() | |
205 | { | |
206 | CPPUNIT_ASSERT(!m_window->GetToolTip()); | |
207 | CPPUNIT_ASSERT_EQUAL("", m_window->GetToolTipText()); | |
208 | ||
209 | m_window->SetToolTip("text tip"); | |
210 | ||
211 | CPPUNIT_ASSERT_EQUAL("text tip", m_window->GetToolTipText()); | |
212 | ||
213 | m_window->UnsetToolTip(); | |
214 | ||
215 | CPPUNIT_ASSERT(!m_window->GetToolTip()); | |
216 | CPPUNIT_ASSERT_EQUAL("", m_window->GetToolTipText()); | |
217 | ||
218 | wxToolTip* tip = new wxToolTip("other tip"); | |
219 | ||
220 | m_window->SetToolTip(tip); | |
221 | ||
222 | CPPUNIT_ASSERT_EQUAL(tip, m_window->GetToolTip()); | |
223 | CPPUNIT_ASSERT_EQUAL("other tip", m_window->GetToolTipText()); | |
224 | } | |
225 | ||
226 | void WindowTestCase::Help() | |
227 | { | |
228 | wxHelpProvider::Set(new wxSimpleHelpProvider()); | |
229 | ||
230 | CPPUNIT_ASSERT_EQUAL("", m_window->GetHelpText()); | |
231 | ||
232 | m_window->SetHelpText("helptext"); | |
233 | ||
234 | CPPUNIT_ASSERT_EQUAL("helptext", m_window->GetHelpText()); | |
235 | } | |
236 | ||
237 | void WindowTestCase::Parent() | |
238 | { | |
239 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetGrandParent()); | |
240 | CPPUNIT_ASSERT_EQUAL(wxTheApp->GetTopWindow(), m_window->GetParent()); | |
241 | } | |
242 | ||
243 | void WindowTestCase::Siblings() | |
244 | { | |
245 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetNextSibling()); | |
246 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetPrevSibling()); | |
247 | ||
248 | wxWindow* newwin = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY); | |
249 | ||
250 | CPPUNIT_ASSERT_EQUAL(newwin, m_window->GetNextSibling()); | |
251 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetPrevSibling()); | |
252 | ||
253 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), newwin->GetNextSibling()); | |
254 | CPPUNIT_ASSERT_EQUAL(m_window, newwin->GetPrevSibling()); | |
255 | ||
256 | wxDELETE(newwin); | |
257 | } | |
258 | ||
259 | void WindowTestCase::Children() | |
260 | { | |
261 | CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount()); | |
262 | ||
263 | wxWindow* child1 = new wxWindow(m_window, wxID_ANY); | |
264 | ||
265 | CPPUNIT_ASSERT_EQUAL(1, m_window->GetChildren().GetCount()); | |
266 | ||
267 | m_window->RemoveChild(child1); | |
268 | ||
269 | CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount()); | |
270 | ||
271 | child1->SetId(wxID_HIGHEST + 1); | |
272 | child1->SetName("child1"); | |
273 | ||
274 | m_window->AddChild(child1); | |
275 | ||
276 | CPPUNIT_ASSERT_EQUAL(1, m_window->GetChildren().GetCount()); | |
277 | CPPUNIT_ASSERT_EQUAL(child1, m_window->FindWindow(wxID_HIGHEST + 1)); | |
278 | CPPUNIT_ASSERT_EQUAL(child1, m_window->FindWindow("child1")); | |
279 | ||
280 | m_window->DestroyChildren(); | |
281 | ||
282 | CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount()); | |
283 | } | |
284 | ||
285 | void WindowTestCase::Focus() | |
286 | { | |
287 | #ifndef __WXOSX__ | |
288 | CPPUNIT_ASSERT(!m_window->HasFocus()); | |
289 | ||
290 | if ( m_window->AcceptsFocus() ) | |
291 | { | |
292 | m_window->SetFocus(); | |
293 | CPPUNIT_ASSERT(m_window->HasFocus()); | |
294 | } | |
295 | ||
296 | //Set the focus back to the main window | |
297 | wxTheApp->GetTopWindow()->SetFocus(); | |
298 | ||
299 | if ( m_window->AcceptsFocusFromKeyboard() ) | |
300 | { | |
301 | m_window->SetFocusFromKbd(); | |
302 | CPPUNIT_ASSERT(m_window->HasFocus()); | |
303 | } | |
304 | #endif | |
305 | } | |
306 | ||
307 | void WindowTestCase::Positioning() | |
308 | { | |
309 | //Some basic tests for consistency | |
310 | int x, y; | |
311 | m_window->GetPosition(&x, &y); | |
312 | ||
313 | CPPUNIT_ASSERT_EQUAL(x, m_window->GetPosition().x); | |
314 | CPPUNIT_ASSERT_EQUAL(y, m_window->GetPosition().y); | |
315 | CPPUNIT_ASSERT_EQUAL(m_window->GetPosition(), | |
316 | m_window->GetRect().GetTopLeft()); | |
317 | ||
318 | m_window->GetScreenPosition(&x, &y); | |
319 | CPPUNIT_ASSERT_EQUAL(x, m_window->GetScreenPosition().x); | |
320 | CPPUNIT_ASSERT_EQUAL(y, m_window->GetScreenPosition().y); | |
321 | CPPUNIT_ASSERT_EQUAL(m_window->GetScreenPosition(), | |
322 | m_window->GetScreenRect().GetTopLeft()); | |
323 | } | |
324 | ||
325 | void WindowTestCase::Show() | |
326 | { | |
327 | CPPUNIT_ASSERT(m_window->IsShown()); | |
328 | ||
329 | m_window->Hide(); | |
330 | ||
331 | CPPUNIT_ASSERT(!m_window->IsShown()); | |
332 | ||
333 | m_window->Show(); | |
334 | ||
335 | CPPUNIT_ASSERT(m_window->IsShown()); | |
336 | ||
337 | m_window->Show(false); | |
338 | ||
339 | CPPUNIT_ASSERT(!m_window->IsShown()); | |
340 | ||
341 | m_window->ShowWithEffect(wxSHOW_EFFECT_BLEND); | |
342 | ||
343 | CPPUNIT_ASSERT(m_window->IsShown()); | |
344 | ||
345 | m_window->HideWithEffect(wxSHOW_EFFECT_BLEND); | |
346 | ||
347 | CPPUNIT_ASSERT(!m_window->IsShown()); | |
348 | } | |
349 | ||
350 | void WindowTestCase::Enable() | |
351 | { | |
352 | CPPUNIT_ASSERT(m_window->IsEnabled()); | |
353 | ||
354 | m_window->Disable(); | |
355 | ||
356 | CPPUNIT_ASSERT(!m_window->IsEnabled()); | |
357 | ||
358 | m_window->Enable(); | |
359 | ||
360 | CPPUNIT_ASSERT(m_window->IsEnabled()); | |
361 | ||
362 | m_window->Enable(false); | |
363 | ||
364 | CPPUNIT_ASSERT(!m_window->IsEnabled()); | |
365 | } | |
366 | ||
367 | void WindowTestCase::FindWindowBy() | |
368 | { | |
369 | m_window->SetId(wxID_HIGHEST + 1); | |
370 | m_window->SetName("name"); | |
232fdc63 | 371 | m_window->SetLabel("label"); |
232fdc63 VZ |
372 | |
373 | CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowById(wxID_HIGHEST + 1)); | |
374 | CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowByName("name")); | |
232fdc63 | 375 | CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowByLabel("label")); |
232fdc63 VZ |
376 | |
377 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), | |
378 | wxWindow::FindWindowById(wxID_HIGHEST + 3)); | |
379 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), | |
380 | wxWindow::FindWindowByName("noname")); | |
381 | CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), | |
382 | wxWindow::FindWindowByLabel("nolabel")); | |
383 | } |