]>
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
);
44 CPPUNIT_TEST( ToolTip
);
46 CPPUNIT_TEST( Parent
);
47 CPPUNIT_TEST( Siblings
);
48 CPPUNIT_TEST( Children
);
49 CPPUNIT_TEST( Focus
);
50 CPPUNIT_TEST( Positioning
);
52 CPPUNIT_TEST( Enable
);
53 CPPUNIT_TEST( FindWindowBy
);
54 CPPUNIT_TEST_SUITE_END();
74 DECLARE_NO_COPY_CLASS(WindowTestCase
)
77 // register in the unnamed registry so that these tests are run by default
78 CPPUNIT_TEST_SUITE_REGISTRATION( WindowTestCase
);
80 // also include in it's own registry so that these tests can be run alone
81 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WindowTestCase
, "WindowTestCase" );
83 void WindowTestCase::setUp()
85 m_window
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
88 void WindowTestCase::tearDown()
90 wxTheApp
->GetTopWindow()->DestroyChildren();
93 void WindowTestCase::ShowHideEvent()
95 #if defined(__WXMSW__) || defined (__WXPM__)
96 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
99 EventCounter
count(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, frame
->GetEventCount());
115 void WindowTestCase::KeyEvent()
117 #if wxUSE_UIACTIONSIMULATOR
118 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
121 EventCounter
count(m_window
, wxEVT_KEY_DOWN
);
122 EventCounter
count1(m_window
, wxEVT_KEY_UP
);
123 EventCounter
count2(m_window
, wxEVT_CHAR
);
125 wxUIActionSimulator sim
;
127 m_window
->SetFocus();
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
));
139 void WindowTestCase::FocusEvent()
142 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
145 EventCounter
count(m_window
, wxEVT_SET_FOCUS
);
146 EventCounter
count1(m_window
, wxEVT_KILL_FOCUS
);
148 m_window
->SetFocus();
150 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_SET_FOCUS
));
151 CPPUNIT_ASSERT(m_window
->HasFocus());
153 wxButton
* button
= new wxButton(wxTheApp
->GetTopWindow(), wxID_ANY
);
157 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_KILL_FOCUS
));
158 CPPUNIT_ASSERT(!m_window
->HasFocus());
162 void WindowTestCase::Mouse()
164 wxCursor
cursor(wxCURSOR_CHAR
);
165 m_window
->SetCursor(cursor
);
167 CPPUNIT_ASSERT(m_window
->GetCursor().IsOk());
169 //A plain window doesn't have a caret
170 CPPUNIT_ASSERT(!m_window
->GetCaret());
172 wxCaret
* caret
= new wxCaret(m_window
, 16, 16);
173 m_window
->SetCaret(caret
);
175 CPPUNIT_ASSERT(m_window
->GetCaret()->IsOk());
177 m_window
->CaptureMouse();
179 CPPUNIT_ASSERT(m_window
->HasCapture());
181 m_window
->ReleaseMouse();
183 CPPUNIT_ASSERT(!m_window
->HasCapture());
186 void WindowTestCase::Properties()
188 m_window
->SetLabel("label");
190 CPPUNIT_ASSERT_EQUAL("label", m_window
->GetLabel());
192 m_window
->SetName("name");
194 CPPUNIT_ASSERT_EQUAL("name", m_window
->GetName());
196 //As we used wxID_ANY we should have a negative id
197 CPPUNIT_ASSERT(m_window
->GetId() < 0);
199 m_window
->SetId(wxID_HIGHEST
+ 10);
201 CPPUNIT_ASSERT_EQUAL(wxID_HIGHEST
+ 10, m_window
->GetId());
204 void WindowTestCase::ToolTip()
206 CPPUNIT_ASSERT(!m_window
->GetToolTip());
207 CPPUNIT_ASSERT_EQUAL("", m_window
->GetToolTipText());
209 m_window
->SetToolTip("text tip");
211 CPPUNIT_ASSERT_EQUAL("text tip", m_window
->GetToolTipText());
213 m_window
->UnsetToolTip();
215 CPPUNIT_ASSERT(!m_window
->GetToolTip());
216 CPPUNIT_ASSERT_EQUAL("", m_window
->GetToolTipText());
218 wxToolTip
* tip
= new wxToolTip("other tip");
220 m_window
->SetToolTip(tip
);
222 CPPUNIT_ASSERT_EQUAL(tip
, m_window
->GetToolTip());
223 CPPUNIT_ASSERT_EQUAL("other tip", m_window
->GetToolTipText());
226 void WindowTestCase::Help()
228 wxHelpProvider::Set(new wxSimpleHelpProvider());
230 CPPUNIT_ASSERT_EQUAL("", m_window
->GetHelpText());
232 m_window
->SetHelpText("helptext");
234 CPPUNIT_ASSERT_EQUAL("helptext", m_window
->GetHelpText());
237 void WindowTestCase::Parent()
239 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetGrandParent());
240 CPPUNIT_ASSERT_EQUAL(wxTheApp
->GetTopWindow(), m_window
->GetParent());
243 void WindowTestCase::Siblings()
245 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetNextSibling());
246 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetPrevSibling());
248 wxWindow
* newwin
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
250 CPPUNIT_ASSERT_EQUAL(newwin
, m_window
->GetNextSibling());
251 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetPrevSibling());
253 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), newwin
->GetNextSibling());
254 CPPUNIT_ASSERT_EQUAL(m_window
, newwin
->GetPrevSibling());
259 void WindowTestCase::Children()
261 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
263 wxWindow
* child1
= new wxWindow(m_window
, wxID_ANY
);
265 CPPUNIT_ASSERT_EQUAL(1, m_window
->GetChildren().GetCount());
267 m_window
->RemoveChild(child1
);
269 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
271 child1
->SetId(wxID_HIGHEST
+ 1);
272 child1
->SetName("child1");
274 m_window
->AddChild(child1
);
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"));
280 m_window
->DestroyChildren();
282 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
285 void WindowTestCase::Focus()
288 CPPUNIT_ASSERT(!m_window
->HasFocus());
290 if ( m_window
->AcceptsFocus() )
292 m_window
->SetFocus();
293 CPPUNIT_ASSERT(m_window
->HasFocus());
296 //Set the focus back to the main window
297 wxTheApp
->GetTopWindow()->SetFocus();
299 if ( m_window
->AcceptsFocusFromKeyboard() )
301 m_window
->SetFocusFromKbd();
302 CPPUNIT_ASSERT(m_window
->HasFocus());
307 void WindowTestCase::Positioning()
309 //Some basic tests for consistency
311 m_window
->GetPosition(&x
, &y
);
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());
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());
325 void WindowTestCase::Show()
327 CPPUNIT_ASSERT(m_window
->IsShown());
331 CPPUNIT_ASSERT(!m_window
->IsShown());
335 CPPUNIT_ASSERT(m_window
->IsShown());
337 m_window
->Show(false);
339 CPPUNIT_ASSERT(!m_window
->IsShown());
341 m_window
->ShowWithEffect(wxSHOW_EFFECT_BLEND
);
343 CPPUNIT_ASSERT(m_window
->IsShown());
345 m_window
->HideWithEffect(wxSHOW_EFFECT_BLEND
);
347 CPPUNIT_ASSERT(!m_window
->IsShown());
350 void WindowTestCase::Enable()
352 CPPUNIT_ASSERT(m_window
->IsEnabled());
356 CPPUNIT_ASSERT(!m_window
->IsEnabled());
360 CPPUNIT_ASSERT(m_window
->IsEnabled());
362 m_window
->Enable(false);
364 CPPUNIT_ASSERT(!m_window
->IsEnabled());
367 void WindowTestCase::FindWindowBy()
369 m_window
->SetId(wxID_HIGHEST
+ 1);
370 m_window
->SetName("name");
371 m_window
->SetLabel("label");
373 CPPUNIT_ASSERT_EQUAL(m_window
, wxWindow::FindWindowById(wxID_HIGHEST
+ 1));
374 CPPUNIT_ASSERT_EQUAL(m_window
, wxWindow::FindWindowByName("name"));
375 CPPUNIT_ASSERT_EQUAL(m_window
, wxWindow::FindWindowByLabel("label"));
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"));