]>
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 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
103 EventCounter
count(m_window
, wxEVT_SHOW
);
105 CPPUNIT_ASSERT(m_window
->IsShown());
107 m_window
->Show(false);
109 CPPUNIT_ASSERT(!m_window
->IsShown());
113 CPPUNIT_ASSERT(m_window
->IsShown());
115 CPPUNIT_ASSERT_EQUAL(2, frame
->GetEventCount());
119 void WindowTestCase::KeyEvent()
121 #if wxUSE_UIACTIONSIMULATOR
122 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
125 EventCounter
count(m_window
, wxEVT_KEY_DOWN
);
126 EventCounter
count1(m_window
, wxEVT_KEY_UP
);
127 EventCounter
count2(m_window
, wxEVT_CHAR
);
129 wxUIActionSimulator sim
;
131 m_window
->SetFocus();
137 CPPUNIT_ASSERT_EQUAL(5, frame
->GetEventCount(wxEVT_KEY_DOWN
));
138 CPPUNIT_ASSERT_EQUAL(5, frame
->GetEventCount(wxEVT_KEY_UP
));
139 CPPUNIT_ASSERT_EQUAL(4, frame
->GetEventCount(wxEVT_CHAR
));
143 void WindowTestCase::FocusEvent()
146 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
149 EventCounter
count(m_window
, wxEVT_SET_FOCUS
);
150 EventCounter
count1(m_window
, wxEVT_KILL_FOCUS
);
152 m_window
->SetFocus();
154 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_SET_FOCUS
));
155 CPPUNIT_ASSERT(m_window
->HasFocus());
157 wxButton
* button
= new wxButton(wxTheApp
->GetTopWindow(), wxID_ANY
);
161 CPPUNIT_ASSERT_EQUAL(1, frame
->GetEventCount(wxEVT_KILL_FOCUS
));
162 CPPUNIT_ASSERT(!m_window
->HasFocus());
166 void WindowTestCase::Mouse()
168 wxCursor
cursor(wxCURSOR_CHAR
);
169 m_window
->SetCursor(cursor
);
171 CPPUNIT_ASSERT(m_window
->GetCursor().IsOk());
173 //A plain window doesn't have a caret
174 CPPUNIT_ASSERT(!m_window
->GetCaret());
176 wxCaret
* caret
= new wxCaret(m_window
, 16, 16);
177 m_window
->SetCaret(caret
);
179 CPPUNIT_ASSERT(m_window
->GetCaret()->IsOk());
181 m_window
->CaptureMouse();
183 CPPUNIT_ASSERT(m_window
->HasCapture());
185 m_window
->ReleaseMouse();
187 CPPUNIT_ASSERT(!m_window
->HasCapture());
190 void WindowTestCase::Properties()
192 m_window
->SetLabel("label");
194 CPPUNIT_ASSERT_EQUAL("label", m_window
->GetLabel());
196 m_window
->SetName("name");
198 CPPUNIT_ASSERT_EQUAL("name", m_window
->GetName());
200 //As we used wxID_ANY we should have a negative id
201 CPPUNIT_ASSERT(m_window
->GetId() < 0);
203 m_window
->SetId(wxID_HIGHEST
+ 10);
205 CPPUNIT_ASSERT_EQUAL(wxID_HIGHEST
+ 10, m_window
->GetId());
209 void WindowTestCase::ToolTip()
211 CPPUNIT_ASSERT(!m_window
->GetToolTip());
212 CPPUNIT_ASSERT_EQUAL("", m_window
->GetToolTipText());
214 m_window
->SetToolTip("text tip");
216 CPPUNIT_ASSERT_EQUAL("text tip", m_window
->GetToolTipText());
218 m_window
->UnsetToolTip();
220 CPPUNIT_ASSERT(!m_window
->GetToolTip());
221 CPPUNIT_ASSERT_EQUAL("", m_window
->GetToolTipText());
223 wxToolTip
* tip
= new wxToolTip("other tip");
225 m_window
->SetToolTip(tip
);
227 CPPUNIT_ASSERT_EQUAL(tip
, m_window
->GetToolTip());
228 CPPUNIT_ASSERT_EQUAL("other tip", m_window
->GetToolTipText());
230 #endif // wxUSE_TOOLTIPS
232 void WindowTestCase::Help()
234 wxHelpProvider::Set(new wxSimpleHelpProvider());
236 CPPUNIT_ASSERT_EQUAL("", m_window
->GetHelpText());
238 m_window
->SetHelpText("helptext");
240 CPPUNIT_ASSERT_EQUAL("helptext", m_window
->GetHelpText());
243 void WindowTestCase::Parent()
245 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetGrandParent());
246 CPPUNIT_ASSERT_EQUAL(wxTheApp
->GetTopWindow(), m_window
->GetParent());
249 void WindowTestCase::Siblings()
251 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetNextSibling());
252 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetPrevSibling());
254 wxWindow
* newwin
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
256 CPPUNIT_ASSERT_EQUAL(newwin
, m_window
->GetNextSibling());
257 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), m_window
->GetPrevSibling());
259 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
), newwin
->GetNextSibling());
260 CPPUNIT_ASSERT_EQUAL(m_window
, newwin
->GetPrevSibling());
265 void WindowTestCase::Children()
267 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
269 wxWindow
* child1
= new wxWindow(m_window
, wxID_ANY
);
271 CPPUNIT_ASSERT_EQUAL(1, m_window
->GetChildren().GetCount());
273 m_window
->RemoveChild(child1
);
275 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
277 child1
->SetId(wxID_HIGHEST
+ 1);
278 child1
->SetName("child1");
280 m_window
->AddChild(child1
);
282 CPPUNIT_ASSERT_EQUAL(1, m_window
->GetChildren().GetCount());
283 CPPUNIT_ASSERT_EQUAL(child1
, m_window
->FindWindow(wxID_HIGHEST
+ 1));
284 CPPUNIT_ASSERT_EQUAL(child1
, m_window
->FindWindow("child1"));
286 m_window
->DestroyChildren();
288 CPPUNIT_ASSERT_EQUAL(0, m_window
->GetChildren().GetCount());
291 void WindowTestCase::Focus()
294 CPPUNIT_ASSERT(!m_window
->HasFocus());
296 if ( m_window
->AcceptsFocus() )
298 m_window
->SetFocus();
299 CPPUNIT_ASSERT(m_window
->HasFocus());
302 //Set the focus back to the main window
303 wxTheApp
->GetTopWindow()->SetFocus();
305 if ( m_window
->AcceptsFocusFromKeyboard() )
307 m_window
->SetFocusFromKbd();
308 CPPUNIT_ASSERT(m_window
->HasFocus());
313 void WindowTestCase::Positioning()
315 //Some basic tests for consistency
317 m_window
->GetPosition(&x
, &y
);
319 CPPUNIT_ASSERT_EQUAL(x
, m_window
->GetPosition().x
);
320 CPPUNIT_ASSERT_EQUAL(y
, m_window
->GetPosition().y
);
321 CPPUNIT_ASSERT_EQUAL(m_window
->GetPosition(),
322 m_window
->GetRect().GetTopLeft());
324 m_window
->GetScreenPosition(&x
, &y
);
325 CPPUNIT_ASSERT_EQUAL(x
, m_window
->GetScreenPosition().x
);
326 CPPUNIT_ASSERT_EQUAL(y
, m_window
->GetScreenPosition().y
);
327 CPPUNIT_ASSERT_EQUAL(m_window
->GetScreenPosition(),
328 m_window
->GetScreenRect().GetTopLeft());
331 void WindowTestCase::Show()
333 CPPUNIT_ASSERT(m_window
->IsShown());
337 CPPUNIT_ASSERT(!m_window
->IsShown());
341 CPPUNIT_ASSERT(m_window
->IsShown());
343 m_window
->Show(false);
345 CPPUNIT_ASSERT(!m_window
->IsShown());
347 m_window
->ShowWithEffect(wxSHOW_EFFECT_BLEND
);
349 CPPUNIT_ASSERT(m_window
->IsShown());
351 m_window
->HideWithEffect(wxSHOW_EFFECT_BLEND
);
353 CPPUNIT_ASSERT(!m_window
->IsShown());
356 void WindowTestCase::Enable()
358 CPPUNIT_ASSERT(m_window
->IsEnabled());
362 CPPUNIT_ASSERT(!m_window
->IsEnabled());
366 CPPUNIT_ASSERT(m_window
->IsEnabled());
368 m_window
->Enable(false);
370 CPPUNIT_ASSERT(!m_window
->IsEnabled());
373 void WindowTestCase::FindWindowBy()
375 m_window
->SetId(wxID_HIGHEST
+ 1);
376 m_window
->SetName("name");
377 m_window
->SetLabel("label");
379 CPPUNIT_ASSERT_EQUAL(m_window
, wxWindow::FindWindowById(wxID_HIGHEST
+ 1));
380 CPPUNIT_ASSERT_EQUAL(m_window
, wxWindow::FindWindowByName("name"));
381 CPPUNIT_ASSERT_EQUAL(m_window
, wxWindow::FindWindowByLabel("label"));
383 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
),
384 wxWindow::FindWindowById(wxID_HIGHEST
+ 3));
385 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
),
386 wxWindow::FindWindowByName("noname"));
387 CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow
*>(NULL
),
388 wxWindow::FindWindowByLabel("nolabel"));