1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/controls/windowtest.cpp
// Purpose: wxWindow unit test
// Author: Steven Lamerton
// Created: 2010-07-10
// Copyright: (c) 2010 Steven Lamerton
///////////////////////////////////////////////////////////////////////////////
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/window.h"
#include "wx/button.h"
#endif // WX_PRECOMP
#include "asserthelper.h"
#include "testableframe.h"
#include "wx/uiaction.h"
#include "wx/caret.h"
#include "wx/cshelp.h"
#include "wx/tooltip.h"
class WindowTestCase : public CppUnit::TestCase
{
public:
WindowTestCase() { }
void setUp();
void tearDown();
private:
CPPUNIT_TEST_SUITE( WindowTestCase );
CPPUNIT_TEST( ShowHideEvent );
WXUISIM_TEST( KeyEvent );
CPPUNIT_TEST( FocusEvent );
CPPUNIT_TEST( Mouse );
CPPUNIT_TEST( Properties );
#if wxUSE_TOOLTIPS
CPPUNIT_TEST( ToolTip );
#endif // wxUSE_TOOLTIPS
CPPUNIT_TEST( Help );
CPPUNIT_TEST( Parent );
CPPUNIT_TEST( Siblings );
CPPUNIT_TEST( Children );
CPPUNIT_TEST( Focus );
CPPUNIT_TEST( Positioning );
CPPUNIT_TEST( Show );
CPPUNIT_TEST( Enable );
CPPUNIT_TEST( FindWindowBy );
CPPUNIT_TEST_SUITE_END();
void ShowHideEvent();
void KeyEvent();
void FocusEvent();
void Mouse();
void Properties();
#if wxUSE_TOOLTIPS
void ToolTip();
#endif // wxUSE_TOOLTIPS
void Help();
void Parent();
void Siblings();
void Children();
void Focus();
void Positioning();
void Show();
void Enable();
void FindWindowBy();
wxWindow *m_window;
DECLARE_NO_COPY_CLASS(WindowTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( WindowTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WindowTestCase, "WindowTestCase" );
void WindowTestCase::setUp()
{
m_window = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY);
}
void WindowTestCase::tearDown()
{
wxTheApp->GetTopWindow()->DestroyChildren();
}
void WindowTestCase::ShowHideEvent()
{
#if defined(__WXMSW__) || defined (__WXPM__)
EventCounter show(m_window, wxEVT_SHOW);
CPPUNIT_ASSERT(m_window->IsShown());
m_window->Show(false);
CPPUNIT_ASSERT(!m_window->IsShown());
m_window->Show();
CPPUNIT_ASSERT(m_window->IsShown());
CPPUNIT_ASSERT_EQUAL(2, show.GetCount());
#endif
}
void WindowTestCase::KeyEvent()
{
#if wxUSE_UIACTIONSIMULATOR
EventCounter keydown(m_window, wxEVT_KEY_DOWN);
EventCounter keyup(m_window, wxEVT_KEY_UP);
EventCounter keychar(m_window, wxEVT_CHAR);
wxUIActionSimulator sim;
m_window->SetFocus();
sim.Text("text");
sim.Char(WXK_SHIFT);
wxYield();
CPPUNIT_ASSERT_EQUAL(5, keydown.GetCount());
CPPUNIT_ASSERT_EQUAL(5, keyup.GetCount());
CPPUNIT_ASSERT_EQUAL(4, keychar.GetCount());
#endif
}
void WindowTestCase::FocusEvent()
{
#ifndef __WXOSX__
EventCounter setfocus(m_window, wxEVT_SET_FOCUS);
EventCounter killfocus(m_window, wxEVT_KILL_FOCUS);
m_window->SetFocus();
CPPUNIT_ASSERT_EQUAL(1, setfocus.GetCount());
CPPUNIT_ASSERT(m_window->HasFocus());
wxButton* button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY);
button->SetFocus();
CPPUNIT_ASSERT_EQUAL(1, killfocus.GetCount());
CPPUNIT_ASSERT(!m_window->HasFocus());
#endif
}
void WindowTestCase::Mouse()
{
wxCursor cursor(wxCURSOR_CHAR);
m_window->SetCursor(cursor);
CPPUNIT_ASSERT(m_window->GetCursor().IsOk());
//A plain window doesn't have a caret
CPPUNIT_ASSERT(!m_window->GetCaret());
wxCaret* caret = new wxCaret(m_window, 16, 16);
m_window->SetCaret(caret);
CPPUNIT_ASSERT(m_window->GetCaret()->IsOk());
m_window->CaptureMouse();
CPPUNIT_ASSERT(m_window->HasCapture());
m_window->ReleaseMouse();
CPPUNIT_ASSERT(!m_window->HasCapture());
}
void WindowTestCase::Properties()
{
m_window->SetLabel("label");
CPPUNIT_ASSERT_EQUAL("label", m_window->GetLabel());
m_window->SetName("name");
CPPUNIT_ASSERT_EQUAL("name", m_window->GetName());
//As we used wxID_ANY we should have a negative id
CPPUNIT_ASSERT(m_window->GetId() < 0);
m_window->SetId(wxID_HIGHEST + 10);
CPPUNIT_ASSERT_EQUAL(wxID_HIGHEST + 10, m_window->GetId());
}
#if wxUSE_TOOLTIPS
void WindowTestCase::ToolTip()
{
CPPUNIT_ASSERT(!m_window->GetToolTip());
CPPUNIT_ASSERT_EQUAL("", m_window->GetToolTipText());
m_window->SetToolTip("text tip");
CPPUNIT_ASSERT_EQUAL("text tip", m_window->GetToolTipText());
m_window->UnsetToolTip();
CPPUNIT_ASSERT(!m_window->GetToolTip());
CPPUNIT_ASSERT_EQUAL("", m_window->GetToolTipText());
wxToolTip* tip = new wxToolTip("other tip");
m_window->SetToolTip(tip);
CPPUNIT_ASSERT_EQUAL(tip, m_window->GetToolTip());
CPPUNIT_ASSERT_EQUAL("other tip", m_window->GetToolTipText());
}
#endif // wxUSE_TOOLTIPS
void WindowTestCase::Help()
{
wxHelpProvider::Set(new wxSimpleHelpProvider());
CPPUNIT_ASSERT_EQUAL("", m_window->GetHelpText());
m_window->SetHelpText("helptext");
CPPUNIT_ASSERT_EQUAL("helptext", m_window->GetHelpText());
}
void WindowTestCase::Parent()
{
CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetGrandParent());
CPPUNIT_ASSERT_EQUAL(wxTheApp->GetTopWindow(), m_window->GetParent());
}
void WindowTestCase::Siblings()
{
CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetNextSibling());
CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetPrevSibling());
wxWindow* newwin = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY);
CPPUNIT_ASSERT_EQUAL(newwin, m_window->GetNextSibling());
CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetPrevSibling());
CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), newwin->GetNextSibling());
CPPUNIT_ASSERT_EQUAL(m_window, newwin->GetPrevSibling());
wxDELETE(newwin);
}
void WindowTestCase::Children()
{
CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount());
wxWindow* child1 = new wxWindow(m_window, wxID_ANY);
CPPUNIT_ASSERT_EQUAL(1, m_window->GetChildren().GetCount());
m_window->RemoveChild(child1);
CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount());
child1->SetId(wxID_HIGHEST + 1);
child1->SetName("child1");
m_window->AddChild(child1);
CPPUNIT_ASSERT_EQUAL(1, m_window->GetChildren().GetCount());
CPPUNIT_ASSERT_EQUAL(child1, m_window->FindWindow(wxID_HIGHEST + 1));
CPPUNIT_ASSERT_EQUAL(child1, m_window->FindWindow("child1"));
m_window->DestroyChildren();
CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount());
}
void WindowTestCase::Focus()
{
#ifndef __WXOSX__
CPPUNIT_ASSERT(!m_window->HasFocus());
if ( m_window->AcceptsFocus() )
{
m_window->SetFocus();
CPPUNIT_ASSERT(m_window->HasFocus());
}
//Set the focus back to the main window
wxTheApp->GetTopWindow()->SetFocus();
if ( m_window->AcceptsFocusFromKeyboard() )
{
m_window->SetFocusFromKbd();
CPPUNIT_ASSERT(m_window->HasFocus());
}
#endif
}
void WindowTestCase::Positioning()
{
//Some basic tests for consistency
int x, y;
m_window->GetPosition(&x, &y);
CPPUNIT_ASSERT_EQUAL(x, m_window->GetPosition().x);
CPPUNIT_ASSERT_EQUAL(y, m_window->GetPosition().y);
CPPUNIT_ASSERT_EQUAL(m_window->GetPosition(),
m_window->GetRect().GetTopLeft());
m_window->GetScreenPosition(&x, &y);
CPPUNIT_ASSERT_EQUAL(x, m_window->GetScreenPosition().x);
CPPUNIT_ASSERT_EQUAL(y, m_window->GetScreenPosition().y);
CPPUNIT_ASSERT_EQUAL(m_window->GetScreenPosition(),
m_window->GetScreenRect().GetTopLeft());
}
void WindowTestCase::Show()
{
CPPUNIT_ASSERT(m_window->IsShown());
m_window->Hide();
CPPUNIT_ASSERT(!m_window->IsShown());
m_window->Show();
CPPUNIT_ASSERT(m_window->IsShown());
m_window->Show(false);
CPPUNIT_ASSERT(!m_window->IsShown());
m_window->ShowWithEffect(wxSHOW_EFFECT_BLEND);
CPPUNIT_ASSERT(m_window->IsShown());
m_window->HideWithEffect(wxSHOW_EFFECT_BLEND);
CPPUNIT_ASSERT(!m_window->IsShown());
}
void WindowTestCase::Enable()
{
CPPUNIT_ASSERT(m_window->IsEnabled());
m_window->Disable();
CPPUNIT_ASSERT(!m_window->IsEnabled());
m_window->Enable();
CPPUNIT_ASSERT(m_window->IsEnabled());
m_window->Enable(false);
CPPUNIT_ASSERT(!m_window->IsEnabled());
}
void WindowTestCase::FindWindowBy()
{
m_window->SetId(wxID_HIGHEST + 1);
m_window->SetName("name");
m_window->SetLabel("label");
CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowById(wxID_HIGHEST + 1));
CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowByName("name"));
CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowByLabel("label"));
CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL),
wxWindow::FindWindowById(wxID_HIGHEST + 3));
CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL),
wxWindow::FindWindowByName("noname"));
CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL),
wxWindow::FindWindowByLabel("nolabel"));
}
|