]>
git.saurik.com Git - wxWidgets.git/blob - tests/controls/windowtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/windowtest.cpp
3 // Purpose: wxWindow unit test
4 // Author: Steven Lamerton
6 // Copyright: (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
17 #include "wx/window.h"
18 #include "wx/button.h"
21 #include "asserthelper.h"
22 #include "testableframe.h"
23 #include "wx/uiaction.h"
25 #include "wx/cshelp.h"
26 #include "wx/tooltip.h"
28 class WindowTestCase
: public CppUnit::TestCase
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
);
44 CPPUNIT_TEST( ToolTip
);
45 #endif // wxUSE_TOOLTIPS
47 CPPUNIT_TEST( Parent
);
48 CPPUNIT_TEST( Siblings
);
49 CPPUNIT_TEST( Children
);
50 CPPUNIT_TEST( Focus
);
51 CPPUNIT_TEST( Positioning
);
53 CPPUNIT_TEST( Enable
);
54 CPPUNIT_TEST( FindWindowBy
);
55 CPPUNIT_TEST_SUITE_END();
64 #endif // wxUSE_TOOLTIPS
77 DECLARE_NO_COPY_CLASS(WindowTestCase
)
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( WindowTestCase
);
83 // also include in its own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WindowTestCase
, "WindowTestCase" );
86 void WindowTestCase::setUp()
88 m_window
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
91 void WindowTestCase::tearDown()
93 wxTheApp
->GetTopWindow()->DestroyChildren();
96 void WindowTestCase::ShowHideEvent()
98 #if defined(__WXMSW__) || defined (__WXPM__)
99 EventCounter
show(m_window
, wxEVT_SHOW
);
101 CPPUNIT_ASSERT(m_window
->IsShown());
103 m_window
->Show(false);
105 CPPUNIT_ASSERT(!m_window
->IsShown());
109 CPPUNIT_ASSERT(m_window
->IsShown());
111 CPPUNIT_ASSERT_EQUAL(2, show
.GetCount());
115 void WindowTestCase::KeyEvent()
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
);
122 wxUIActionSimulator sim
;
124 m_window
->SetFocus();
130 CPPUNIT_ASSERT_EQUAL(5, keydown
.GetCount());
131 CPPUNIT_ASSERT_EQUAL(5, keyup
.GetCount());
132 CPPUNIT_ASSERT_EQUAL(4, keychar
.GetCount());
136 void WindowTestCase::FocusEvent()
139 EventCounter
setfocus(m_window
, wxEVT_SET_FOCUS
);
140 EventCounter
killfocus(m_window
, wxEVT_KILL_FOCUS
);
142 m_window
->SetFocus();
144 CPPUNIT_ASSERT_EQUAL(1, setfocus
.GetCount());
145 CPPUNIT_ASSERT(m_window
->HasFocus());
147 wxButton
* button
= new wxButton(wxTheApp
->GetTopWindow(), wxID_ANY
);
151 CPPUNIT_ASSERT_EQUAL(1, killfocus
.GetCount());
152 CPPUNIT_ASSERT(!m_window
->HasFocus());
156 void WindowTestCase::Mouse()
158 wxCursor
cursor(wxCURSOR_CHAR
);
159 m_window
->SetCursor(cursor
);
161 CPPUNIT_ASSERT(m_window
->GetCursor().IsOk());
163 //A plain window doesn't have a caret
164 CPPUNIT_ASSERT(!m_window
->GetCaret());
166 wxCaret
* caret
= new wxCaret(m_window
, 16, 16);
167 m_window
->SetCaret(caret
);
169 CPPUNIT_ASSERT(m_window
->GetCaret()->IsOk());
171 m_window
->CaptureMouse();
173 CPPUNIT_ASSERT(m_window
->HasCapture());
175 m_window
->ReleaseMouse();
177 CPPUNIT_ASSERT(!m_window
->HasCapture());
180 void WindowTestCase::Properties()
182 m_window
->SetLabel("label");
184 CPPUNIT_ASSERT_EQUAL("label", m_window
->GetLabel());
186 m_window
->SetName("name");
188 CPPUNIT_ASSERT_EQUAL("name", m_window
->GetName());
190 //As we used wxID_ANY we should have a negative id
191 CPPUNIT_ASSERT(m_window
->GetId() < 0);
193 m_window
->SetId(wxID_HIGHEST
+ 10);
195 CPPUNIT_ASSERT_EQUAL(wxID_HIGHEST
+ 10, m_window
->GetId());
199 void WindowTestCase::ToolTip()
201 CPPUNIT_ASSERT(!m_window
->GetToolTip());
202 CPPUNIT_ASSERT_EQUAL("", m_window
->GetToolTipText());
204 m_window
->SetToolTip("text tip");
206 CPPUNIT_ASSERT_EQUAL("text tip", m_window
->GetToolTipText());
208 m_window
->UnsetToolTip();
210 CPPUNIT_ASSERT(!m_window
->GetToolTip());
211 CPPUNIT_ASSERT_EQUAL("", m_window
->GetToolTipText());
213 wxToolTip
* tip
= new wxToolTip("other tip");
215 m_window
->SetToolTip(tip
);
217 CPPUNIT_ASSERT_EQUAL(tip
, m_window
->GetToolTip());
218 CPPUNIT_ASSERT_EQUAL("other tip", m_window
->GetToolTipText());
220 #endif // wxUSE_TOOLTIPS
222 void WindowTestCase::Help()
224 wxHelpProvider::Set(new wxSimpleHelpProvider());
226 CPPUNIT_ASSERT_EQUAL("", m_window
->GetHelpText());
228 m_window
->SetHelpText("helptext");
230 CPPUNIT_ASSERT_EQUAL("helptext", m_window
->GetHelpText());
233 void WindowTestCase::Parent()
235 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetGrandParent());
236 CPPUNIT_ASSERT_EQUAL(wxTheApp
->GetTopWindow(), m_window
->GetParent());
239 void WindowTestCase::Siblings()
241 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetNextSibling());
242 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetPrevSibling());
244 wxWindow
* newwin
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
246 CPPUNIT_ASSERT_EQUAL(newwin
, m_window
->GetNextSibling());
247 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetPrevSibling());
249 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), newwin
->GetNextSibling());
250 CPPUNIT_ASSERT_EQUAL(m_window
, newwin
->GetPrevSibling());
255 void WindowTestCase::Children()
257 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
259 wxWindow
* child1
= new wxWindow(m_window
, wxID_ANY
);
261 CPPUNIT_ASSERT_EQUAL(1, m_window
->GetChildren().GetCount());
263 m_window
->RemoveChild(child1
);
265 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
267 child1
->SetId(wxID_HIGHEST
+ 1);
268 child1
->SetName("child1");
270 m_window
->AddChild(child1
);
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"));
276 m_window
->DestroyChildren();
278 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
281 void WindowTestCase::Focus()
284 CPPUNIT_ASSERT(!m_window
->HasFocus());
286 if ( m_window
->AcceptsFocus() )
288 m_window
->SetFocus();
289 CPPUNIT_ASSERT(m_window
->HasFocus());
292 //Set the focus back to the main window
293 wxTheApp
->GetTopWindow()->SetFocus();
295 if ( m_window
->AcceptsFocusFromKeyboard() )
297 m_window
->SetFocusFromKbd();
298 CPPUNIT_ASSERT(m_window
->HasFocus());
303 void WindowTestCase::Positioning()
305 //Some basic tests for consistency
307 m_window
->GetPosition(&x
, &y
);
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());
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());
321 void WindowTestCase::Show()
323 CPPUNIT_ASSERT(m_window
->IsShown());
327 CPPUNIT_ASSERT(!m_window
->IsShown());
331 CPPUNIT_ASSERT(m_window
->IsShown());
333 m_window
->Show(false);
335 CPPUNIT_ASSERT(!m_window
->IsShown());
337 m_window
->ShowWithEffect(wxSHOW_EFFECT_BLEND
);
339 CPPUNIT_ASSERT(m_window
->IsShown());
341 m_window
->HideWithEffect(wxSHOW_EFFECT_BLEND
);
343 CPPUNIT_ASSERT(!m_window
->IsShown());
346 void WindowTestCase::Enable()
348 CPPUNIT_ASSERT(m_window
->IsEnabled());
352 CPPUNIT_ASSERT(!m_window
->IsEnabled());
356 CPPUNIT_ASSERT(m_window
->IsEnabled());
358 m_window
->Enable(false);
360 CPPUNIT_ASSERT(!m_window
->IsEnabled());
363 void WindowTestCase::FindWindowBy()
365 m_window
->SetId(wxID_HIGHEST
+ 1);
366 m_window
->SetName("name");
367 m_window
->SetLabel("label");
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"));
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"));