]>
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
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
18 #include "wx/window.h"
19 #include "wx/button.h"
22 #include "asserthelper.h"
23 #include "testableframe.h"
24 #include "wx/uiaction.h"
26 #include "wx/cshelp.h"
27 #include "wx/tooltip.h"
29 class WindowTestCase
: public CppUnit::TestCase
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
);
45 CPPUNIT_TEST( ToolTip
);
46 #endif // wxUSE_TOOLTIPS
48 CPPUNIT_TEST( Parent
);
49 CPPUNIT_TEST( Siblings
);
50 CPPUNIT_TEST( Children
);
51 CPPUNIT_TEST( Focus
);
52 CPPUNIT_TEST( Positioning
);
54 CPPUNIT_TEST( Enable
);
55 CPPUNIT_TEST( FindWindowBy
);
56 CPPUNIT_TEST_SUITE_END();
65 #endif // wxUSE_TOOLTIPS
78 DECLARE_NO_COPY_CLASS(WindowTestCase
)
81 // register in the unnamed registry so that these tests are run by default
82 CPPUNIT_TEST_SUITE_REGISTRATION( WindowTestCase
);
84 // also include in its own registry so that these tests can be run alone
85 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WindowTestCase
, "WindowTestCase" );
87 void WindowTestCase::setUp()
89 m_window
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
92 void WindowTestCase::tearDown()
94 wxTheApp
->GetTopWindow()->DestroyChildren();
97 void WindowTestCase::ShowHideEvent()
99 #if defined(__WXMSW__) || defined (__WXPM__)
100 EventCounter
show(m_window
, wxEVT_SHOW
);
102 CPPUNIT_ASSERT(m_window
->IsShown());
104 m_window
->Show(false);
106 CPPUNIT_ASSERT(!m_window
->IsShown());
110 CPPUNIT_ASSERT(m_window
->IsShown());
112 CPPUNIT_ASSERT_EQUAL(2, show
.GetCount());
116 void WindowTestCase::KeyEvent()
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
);
123 wxUIActionSimulator sim
;
125 m_window
->SetFocus();
131 CPPUNIT_ASSERT_EQUAL(5, keydown
.GetCount());
132 CPPUNIT_ASSERT_EQUAL(5, keyup
.GetCount());
133 CPPUNIT_ASSERT_EQUAL(4, keychar
.GetCount());
137 void WindowTestCase::FocusEvent()
140 EventCounter
setfocus(m_window
, wxEVT_SET_FOCUS
);
141 EventCounter
killfocus(m_window
, wxEVT_KILL_FOCUS
);
143 m_window
->SetFocus();
145 CPPUNIT_ASSERT_EQUAL(1, setfocus
.GetCount());
146 CPPUNIT_ASSERT(m_window
->HasFocus());
148 wxButton
* button
= new wxButton(wxTheApp
->GetTopWindow(), wxID_ANY
);
152 CPPUNIT_ASSERT_EQUAL(1, killfocus
.GetCount());
153 CPPUNIT_ASSERT(!m_window
->HasFocus());
157 void WindowTestCase::Mouse()
159 wxCursor
cursor(wxCURSOR_CHAR
);
160 m_window
->SetCursor(cursor
);
162 CPPUNIT_ASSERT(m_window
->GetCursor().IsOk());
164 //A plain window doesn't have a caret
165 CPPUNIT_ASSERT(!m_window
->GetCaret());
167 wxCaret
* caret
= new wxCaret(m_window
, 16, 16);
168 m_window
->SetCaret(caret
);
170 CPPUNIT_ASSERT(m_window
->GetCaret()->IsOk());
172 m_window
->CaptureMouse();
174 CPPUNIT_ASSERT(m_window
->HasCapture());
176 m_window
->ReleaseMouse();
178 CPPUNIT_ASSERT(!m_window
->HasCapture());
181 void WindowTestCase::Properties()
183 m_window
->SetLabel("label");
185 CPPUNIT_ASSERT_EQUAL("label", m_window
->GetLabel());
187 m_window
->SetName("name");
189 CPPUNIT_ASSERT_EQUAL("name", m_window
->GetName());
191 //As we used wxID_ANY we should have a negative id
192 CPPUNIT_ASSERT(m_window
->GetId() < 0);
194 m_window
->SetId(wxID_HIGHEST
+ 10);
196 CPPUNIT_ASSERT_EQUAL(wxID_HIGHEST
+ 10, m_window
->GetId());
200 void WindowTestCase::ToolTip()
202 CPPUNIT_ASSERT(!m_window
->GetToolTip());
203 CPPUNIT_ASSERT_EQUAL("", m_window
->GetToolTipText());
205 m_window
->SetToolTip("text tip");
207 CPPUNIT_ASSERT_EQUAL("text tip", m_window
->GetToolTipText());
209 m_window
->UnsetToolTip();
211 CPPUNIT_ASSERT(!m_window
->GetToolTip());
212 CPPUNIT_ASSERT_EQUAL("", m_window
->GetToolTipText());
214 wxToolTip
* tip
= new wxToolTip("other tip");
216 m_window
->SetToolTip(tip
);
218 CPPUNIT_ASSERT_EQUAL(tip
, m_window
->GetToolTip());
219 CPPUNIT_ASSERT_EQUAL("other tip", m_window
->GetToolTipText());
221 #endif // wxUSE_TOOLTIPS
223 void WindowTestCase::Help()
225 wxHelpProvider::Set(new wxSimpleHelpProvider());
227 CPPUNIT_ASSERT_EQUAL("", m_window
->GetHelpText());
229 m_window
->SetHelpText("helptext");
231 CPPUNIT_ASSERT_EQUAL("helptext", m_window
->GetHelpText());
234 void WindowTestCase::Parent()
236 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetGrandParent());
237 CPPUNIT_ASSERT_EQUAL(wxTheApp
->GetTopWindow(), m_window
->GetParent());
240 void WindowTestCase::Siblings()
242 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetNextSibling());
243 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetPrevSibling());
245 wxWindow
* newwin
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
247 CPPUNIT_ASSERT_EQUAL(newwin
, m_window
->GetNextSibling());
248 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetPrevSibling());
250 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), newwin
->GetNextSibling());
251 CPPUNIT_ASSERT_EQUAL(m_window
, newwin
->GetPrevSibling());
256 void WindowTestCase::Children()
258 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
260 wxWindow
* child1
= new wxWindow(m_window
, wxID_ANY
);
262 CPPUNIT_ASSERT_EQUAL(1, m_window
->GetChildren().GetCount());
264 m_window
->RemoveChild(child1
);
266 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
268 child1
->SetId(wxID_HIGHEST
+ 1);
269 child1
->SetName("child1");
271 m_window
->AddChild(child1
);
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"));
277 m_window
->DestroyChildren();
279 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
282 void WindowTestCase::Focus()
285 CPPUNIT_ASSERT(!m_window
->HasFocus());
287 if ( m_window
->AcceptsFocus() )
289 m_window
->SetFocus();
290 CPPUNIT_ASSERT(m_window
->HasFocus());
293 //Set the focus back to the main window
294 wxTheApp
->GetTopWindow()->SetFocus();
296 if ( m_window
->AcceptsFocusFromKeyboard() )
298 m_window
->SetFocusFromKbd();
299 CPPUNIT_ASSERT(m_window
->HasFocus());
304 void WindowTestCase::Positioning()
306 //Some basic tests for consistency
308 m_window
->GetPosition(&x
, &y
);
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());
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());
322 void WindowTestCase::Show()
324 CPPUNIT_ASSERT(m_window
->IsShown());
328 CPPUNIT_ASSERT(!m_window
->IsShown());
332 CPPUNIT_ASSERT(m_window
->IsShown());
334 m_window
->Show(false);
336 CPPUNIT_ASSERT(!m_window
->IsShown());
338 m_window
->ShowWithEffect(wxSHOW_EFFECT_BLEND
);
340 CPPUNIT_ASSERT(m_window
->IsShown());
342 m_window
->HideWithEffect(wxSHOW_EFFECT_BLEND
);
344 CPPUNIT_ASSERT(!m_window
->IsShown());
347 void WindowTestCase::Enable()
349 CPPUNIT_ASSERT(m_window
->IsEnabled());
353 CPPUNIT_ASSERT(!m_window
->IsEnabled());
357 CPPUNIT_ASSERT(m_window
->IsEnabled());
359 m_window
->Enable(false);
361 CPPUNIT_ASSERT(!m_window
->IsEnabled());
364 void WindowTestCase::FindWindowBy()
366 m_window
->SetId(wxID_HIGHEST
+ 1);
367 m_window
->SetName("name");
368 m_window
->SetLabel("label");
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"));
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"));