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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
|
///////////////////////////////////////////////////////////////////////////////
// Name: test.cpp
// Purpose: Test program for wxWidgets
// Author: Mike Wetherell
// Copyright: (c) 2004 Mike Wetherell
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx/wx.h"
// and "catch.hpp"
#include "testprec.h"
// Suppress some warnings in catch_impl.hpp.
wxCLANG_WARNING_SUPPRESS(missing-braces)
wxCLANG_WARNING_SUPPRESS(logical-op-parentheses)
wxCLANG_WARNING_SUPPRESS(inconsistent-missing-override)
// This file needs to get the CATCH definitions in addition to the usual
// assertion macros declarations from catch.hpp included by testprec.h.
// Including an internal file like this is ugly, but there doesn't seem to be
// any better way, see https://github.com/philsquared/Catch/issues/1061
#include "internal/catch_impl.hpp"
wxCLANG_WARNING_RESTORE(missing-braces)
wxCLANG_WARNING_RESTORE(logical-op-parentheses)
wxCLANG_WARNING_RESTORE(inconsistent-missing-override)
// This probably could be done by predefining CLARA_CONFIG_MAIN, but at the
// point where we are, just define this global variable manually.
namespace Catch { namespace Clara { UnpositionalTag _; } }
// Also define our own global variables.
namespace wxPrivate
{
std::string wxTheCurrentTestClass, wxTheCurrentTestMethod;
}
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include "wx/apptrait.h"
#include "wx/cmdline.h"
#include <exception>
#include <iostream>
#ifdef __WINDOWS__
#include "wx/msw/msvcrt.h"
#endif
#ifdef __WXOSX__
#include "wx/osx/private.h"
#endif
#if wxUSE_GUI
#include "testableframe.h"
#ifdef __WXGTK__
#include <glib.h>
#endif // __WXGTK__
#endif // wxUSE_GUI
#include "wx/socket.h"
#include "wx/evtloop.h"
using namespace std;
// ----------------------------------------------------------------------------
// helper classes
// ----------------------------------------------------------------------------
// exception class for MSVC debug CRT assertion failures
#ifdef wxUSE_VC_CRTDBG
struct CrtAssertFailure
{
CrtAssertFailure(const char *message) : m_msg(message) { }
const wxString m_msg;
wxDECLARE_NO_ASSIGN_CLASS(CrtAssertFailure);
};
CATCH_TRANSLATE_EXCEPTION(CrtAssertFailure& e)
{
return "CRT assert failure: " + e.m_msg.ToStdString(wxConvUTF8);
}
#endif // wxUSE_VC_CRTDBG
#if wxDEBUG_LEVEL
// Information about the last not yet handled assertion.
static wxString s_lastAssertMessage;
static wxString FormatAssertMessage(const wxString& file,
int line,
const wxString& func,
const wxString& cond,
const wxString& msg)
{
wxString str;
str << wxASCII_STR("wxWidgets assert: ") << cond
<< wxASCII_STR(" failed at ") << file << wxASCII_STR(":") << line
<< wxASCII_STR(" in ") << func << wxASCII_STR(" with message '")
<< msg << wxASCII_STR("'");
return str;
}
static void TestAssertHandler(const wxString& file,
int line,
const wxString& func,
const wxString& cond,
const wxString& msg)
{
// Determine whether we can safely throw an exception to just make the test
// fail or whether we need to abort (in this case "msg" will contain the
// explanation why did we decide to do it).
wxString abortReason;
const wxString
assertMessage = FormatAssertMessage(file, line, func, cond, msg);
if ( !wxIsMainThread() )
{
// Exceptions thrown from worker threads are not caught currently and
// so we'd just die without any useful information -- abort instead.
abortReason << assertMessage << wxASCII_STR(" in a worker thread.");
}
#if wxCHECK_CXX_STD(201703L)
else if ( uncaught_exceptions() )
#else
else if ( uncaught_exception() )
#endif
{
// Throwing while already handling an exception would result in
// terminate() being called and we wouldn't get any useful information
// about why the test failed then.
if ( s_lastAssertMessage.empty() )
{
abortReason << assertMessage
<< wxASCII_STR("while handling an exception");
}
else // In this case the exception is due to a previous assert.
{
abortReason << s_lastAssertMessage
<< wxASCII_STR("\n and another ") << assertMessage
<< wxASCII_STR(" while handling it.");
}
}
else // Can "safely" throw from here.
{
// Remember this in case another assert happens while handling this
// exception: we want to show the original assert as it's usually more
// useful to determine the real root of the problem.
s_lastAssertMessage = assertMessage;
throw TestAssertFailure(file, line, func, cond, msg);
}
#if wxUSE_STACKWALKER
const wxString& stackTrace = wxApp::GetValidTraits().GetAssertStackTrace();
if ( !stackTrace.empty() )
abortReason << wxASCII_STR("\n\nAssert call stack:\n") << stackTrace;
#endif // wxUSE_STACKWALKER
wxFputs(abortReason, stderr);
fflush(stderr);
_exit(-1);
}
CATCH_TRANSLATE_EXCEPTION(TestAssertFailure& e)
{
wxString desc = e.m_msg;
if ( desc.empty() )
desc.Printf(wxASCII_STR("Condition \"%s\" failed"), e.m_cond);
desc += wxString::Format(wxASCII_STR(" in %s() at %s:%d"), e.m_func, e.m_file, e.m_line);
return desc.ToStdString(wxConvUTF8);
}
#endif // wxDEBUG_LEVEL
#if wxUSE_LOG
// Custom log target used while running the tests.
class TestLogger : public wxLog
{
public:
TestLogger()
{
// Use standard time-stamp instead of the locale-specific one and show
// milliseconds too.
wxLog::SetTimestamp("%Y-%m-%d %H:%M:%S.%l");
}
// This is used by TestLogEnabler to enable logging all messages.
static int ms_enableCount;
protected:
virtual void DoLogRecord(wxLogLevel level,
const wxString& msg,
const wxLogRecordInfo& info) wxOVERRIDE
{
// If logging was explicitly enabled, show everything on the console.
//
// Otherwise we only show trace messages as they are not given by
// default and can be only activated by setting WXTRACE.
if ( ms_enableCount || level == wxLOG_Trace )
{
wxString ts;
TimeStampMS(&ts, info.timestampMS);
const wxString levels[] =
{
"Fatal", "Error", "Warning",
"Message", "Status", "Info",
"Debug", "Trace", "Progress"
};
wxString prefix;
if ( level < WXSIZEOF(levels) )
prefix = levels[level];
else
prefix.Printf("Level %d", level);
m_out.Output(wxString::Format("[%s] %s: %s", ts, prefix, msg));
}
}
wxMessageOutputStderr m_out;
};
// By default, normal logging is disabled as it would interfere with the normal
// test output.
int TestLogger::ms_enableCount = 0;
TestLogEnabler::TestLogEnabler()
{
TestLogger::ms_enableCount++;
}
TestLogEnabler::~TestLogEnabler()
{
TestLogger::ms_enableCount--;
}
#endif // wxUSE_LOG
#if wxUSE_GUI
typedef wxApp TestAppBase;
typedef wxGUIAppTraits TestAppTraitsBase;
#else
typedef wxAppConsole TestAppBase;
typedef wxConsoleAppTraits TestAppTraitsBase;
#endif
// The application class
//
class TestApp : public TestAppBase
{
public:
TestApp();
// standard overrides
virtual bool OnInit() wxOVERRIDE;
virtual int OnExit() wxOVERRIDE;
#ifdef __WIN32__
virtual wxAppTraits *CreateTraits() wxOVERRIDE
{
// Define a new class just to customize CanUseStderr() behaviour.
class TestAppTraits : public TestAppTraitsBase
{
public:
// We want to always use stderr, tests are also run unattended and
// in this case we really don't want to show any message boxes, as
// wxMessageOutputBest, used e.g. from the default implementation
// of wxApp::OnUnhandledException(), would do by default.
virtual bool CanUseStderr() wxOVERRIDE { return true; }
// Overriding CanUseStderr() is not enough, we also need to
// override this one to avoid returning false from it.
virtual bool WriteToStderr(const wxString& text) wxOVERRIDE
{
wxFputs(text, stderr);
fflush(stderr);
// Intentionally ignore any errors, we really don't want to
// show any message boxes in any case.
return true;
}
};
return new TestAppTraits;
}
#endif // __WIN32__
// Also override this method to avoid showing any dialogs from here -- and
// show some details about the exception along the way.
virtual bool OnExceptionInMainLoop() wxOVERRIDE
{
wxFprintf(stderr, wxASCII_STR("Unhandled exception in the main loop: %s\n"),
wxASCII_STR(Catch::translateActiveException().c_str()));
throw;
}
// used by events propagation test
virtual int FilterEvent(wxEvent& event) wxOVERRIDE;
virtual bool ProcessEvent(wxEvent& event) wxOVERRIDE;
void SetFilterEventFunc(FilterEventFunc f) { m_filterEventFunc = f; }
void SetProcessEventFunc(ProcessEventFunc f) { m_processEventFunc = f; }
// In console applications we run the tests directly from the overridden
// OnRun(), but in the GUI ones we run them when we get the first call to
// our EVT_IDLE handler to ensure that we do everything from inside the
// main event loop. This is especially important under wxOSX/Cocoa where
// the main event loop is different from the others but it's also safer to
// do it like this in the other ports as we test the GUI code in the same
// context as it's used usually, in normal programs, and it might behave
// differently without the event loop.
#if wxUSE_GUI
void OnIdle(wxIdleEvent& event)
{
if ( m_runTests )
{
m_runTests = false;
#ifdef __WXOSX__
// we need to wait until the window is activated and fully ready
// otherwise no events can be posted
wxEventLoopBase* const loop = wxEventLoop::GetActive();
if ( loop )
{
loop->DispatchTimeout(1000);
loop->Yield();
}
#endif // __WXOSX__
m_exitcode = RunTests();
ExitMainLoop();
}
event.Skip();
}
virtual int OnRun() wxOVERRIDE
{
if ( TestAppBase::OnRun() != 0 )
m_exitcode = EXIT_FAILURE;
return m_exitcode;
}
#else // !wxUSE_GUI
virtual int OnRun() wxOVERRIDE
{
return RunTests();
}
#endif // wxUSE_GUI/!wxUSE_GUI
private:
int RunTests();
// flag telling us whether we should run tests from our EVT_IDLE handler
bool m_runTests;
// event handling hooks
FilterEventFunc m_filterEventFunc;
ProcessEventFunc m_processEventFunc;
#if wxUSE_GUI
// the program exit code
int m_exitcode;
#endif // wxUSE_GUI
};
wxIMPLEMENT_APP_NO_MAIN(TestApp);
// ----------------------------------------------------------------------------
// global functions
// ----------------------------------------------------------------------------
#ifdef wxUSE_VC_CRTDBG
static int TestCrtReportHook(int reportType, char *message, int *)
{
if ( reportType != _CRT_ASSERT )
return FALSE;
throw CrtAssertFailure(message);
}
#endif // wxUSE_VC_CRTDBG
int main(int argc, char **argv)
{
// tests can be ran non-interactively so make sure we don't show any assert
// dialog boxes -- neither our own nor from MSVC debug CRT -- which would
// prevent them from completing
#if wxDEBUG_LEVEL
wxSetAssertHandler(TestAssertHandler);
#endif // wxDEBUG_LEVEL
#ifdef wxUSE_VC_CRTDBG
_CrtSetReportHook(TestCrtReportHook);
#endif // wxUSE_VC_CRTDBG
return wxEntry(argc, argv);
}
extern void SetFilterEventFunc(FilterEventFunc func)
{
wxGetApp().SetFilterEventFunc(func);
}
extern void SetProcessEventFunc(ProcessEventFunc func)
{
wxGetApp().SetProcessEventFunc(func);
}
static bool DoCheckConnection()
{
// NOTE: we could use wxDialUpManager here if it was in wxNet; since it's in
// wxCore we use a simple rough test:
wxSocketBase::Initialize();
wxIPV4address addr;
if (!addr.Hostname(0xadfe5c16) || !addr.Service(wxASCII_STR("www")))
{
wxSocketBase::Shutdown();
return false;
}
const char* const
HTTP_GET = "GET / HTTP /1.1\r\nHost: www.wxwidgets.org\r\n\r\n";
wxSocketClient sock;
sock.SetTimeout(10); // 10 secs
bool online = sock.Connect(addr) &&
(sock.Write(HTTP_GET, strlen(HTTP_GET)), sock.WaitForRead(1));
wxSocketBase::Shutdown();
return online;
}
extern bool IsNetworkAvailable()
{
static int s_isNetworkAvailable = -1;
if ( s_isNetworkAvailable == -1 )
s_isNetworkAvailable = DoCheckConnection();
return s_isNetworkAvailable == 1;
}
extern bool IsAutomaticTest()
{
static int s_isAutomatic = -1;
if ( s_isAutomatic == -1 )
{
s_isAutomatic = wxGetEnv(wxASCII_STR("GITHUB_ACTIONS"), NULL) ||
wxGetEnv(wxASCII_STR("APPVEYOR"), NULL);
}
return s_isAutomatic == 1;
}
extern bool IsRunningUnderXVFB()
{
static int s_isRunningUnderXVFB = -1;
if ( s_isRunningUnderXVFB == -1 )
{
wxString value;
s_isRunningUnderXVFB = wxGetEnv(wxASCII_STR("wxUSE_XVFB"), &value) && value == wxASCII_STR("1");
}
return s_isRunningUnderXVFB == 1;
}
#if wxUSE_GUI
bool EnableUITests()
{
static int s_enabled = -1;
if ( s_enabled == -1 )
{
// Allow explicitly configuring this via an environment variable under
// all platforms.
wxString enabled;
if ( wxGetEnv(wxASCII_STR("WX_UI_TESTS"), &enabled) )
{
if ( enabled == wxASCII_STR("1") )
s_enabled = 1;
else if ( enabled == wxASCII_STR("0") )
s_enabled = 0;
else
wxFprintf(stderr, wxASCII_STR("Unknown \"WX_UI_TESTS\" value \"%s\" ignored.\n"), enabled);
}
if ( s_enabled == -1 )
{
#if defined(__WXMSW__) || defined(__WXGTK__)
s_enabled = 1;
#else // !(__WXMSW__ || __WXGTK__)
s_enabled = 0;
#endif // (__WXMSW__ || __WXGTK__)
}
}
return s_enabled == 1;
}
void DeleteTestWindow(wxWindow* win)
{
if ( !win )
return;
wxWindow* const capture = wxWindow::GetCapture();
if ( capture )
{
if ( capture == win ||
capture->GetMainWindowOfCompositeControl() == win )
capture->ReleaseMouse();
}
delete win;
}
#ifdef __WXGTK__
#ifdef GDK_WINDOWING_X11
#include "X11/Xlib.h"
extern "C"
int wxTestX11ErrorHandler(Display*, XErrorEvent*)
{
fprintf(stderr, "\n*** X11 error while running %s(): ",
wxGetCurrentTestName().c_str());
return 0;
}
#endif // GDK_WINDOWING_X11
extern "C"
void
wxTestGLogHandler(const gchar* domain,
GLogLevelFlags level,
const gchar* message,
gpointer data)
{
// Check if debug messages in this domain will be logged.
if ( level == G_LOG_LEVEL_DEBUG )
{
static const char* const allowed = getenv("G_MESSAGES_DEBUG");
// By default debug messages are dropped, but if G_MESSAGES_DEBUG is
// defined, they're logged for the domains specified in it and if it
// has the special value "all", then all debug messages are shown.
//
// Note that the check here can result in false positives, e.g. domain
// "foo" would pass it even if G_MESSAGES_DEBUG only contains "foobar",
// but such cases don't seem to be important enough to bother
// accounting for them.
if ( !allowed ||
(strcmp(allowed, "all") != 0 && !strstr(allowed, domain)) )
{
return;
}
}
fprintf(stderr, "\n*** GTK log message while running %s(): ",
wxGetCurrentTestName().c_str());
g_log_default_handler(domain, level, message, data);
}
#endif // __WXGTK__
#endif // wxUSE_GUI
// ----------------------------------------------------------------------------
// TestApp
// ----------------------------------------------------------------------------
TestApp::TestApp()
{
m_runTests = true;
m_filterEventFunc = NULL;
m_processEventFunc = NULL;
#if wxUSE_GUI
m_exitcode = EXIT_SUCCESS;
#endif // wxUSE_GUI
}
// Init
//
bool TestApp::OnInit()
{
// Hack: don't call TestAppBase::OnInit() to let CATCH handle command line.
// Output some important information about the test environment.
#if wxUSE_GUI
cout << "Test program for wxWidgets GUI features\n"
#else
cout << "Test program for wxWidgets non-GUI features\n"
#endif
<< "build: " << WX_BUILD_OPTIONS_SIGNATURE << "\n"
<< "compiled using "
#if defined(__clang__)
<< "clang " << __clang_major__ << "." << __clang_minor__ << "." << __clang_patchlevel__
#elif defined(__INTEL_COMPILER)
<< "icc " << __INTEL_COMPILER
#elif defined(__GNUG__)
<< "gcc " << __GNUC__ << "." << __GNUC_MINOR__
#elif defined(_MSC_VER)
<< "msvc " << _MSC_VER
#if defined(_MSC_FULL_VER)
<< " (full: " << _MSC_FULL_VER << ")"
#endif
#else
<< "unidentified compiler"
#endif
<< "\n"
<< "running under " << wxGetOsDescription()
<< " as " << wxGetUserId()
<< std::endl;
#if wxUSE_GUI
// create a parent window to be used as parent for the GUI controls
new wxTestableFrame();
Connect(wxEVT_IDLE, wxIdleEventHandler(TestApp::OnIdle));
#ifdef __WXGTK20__
g_log_set_default_handler(wxTestGLogHandler, NULL);
#endif // __WXGTK__
#ifdef GDK_WINDOWING_X11
XSetErrorHandler(wxTestX11ErrorHandler);
#endif // GDK_WINDOWING_X11
#endif // wxUSE_GUI
return true;
}
// Event handling
int TestApp::FilterEvent(wxEvent& event)
{
if ( m_filterEventFunc )
return (*m_filterEventFunc)(event);
return TestAppBase::FilterEvent(event);
}
bool TestApp::ProcessEvent(wxEvent& event)
{
if ( m_processEventFunc )
return (*m_processEventFunc)(event);
return TestAppBase::ProcessEvent(event);
}
// Run
//
int TestApp::RunTests()
{
#if wxUSE_LOG
delete wxLog::SetActiveTarget(new TestLogger);
#endif
// Cast is needed under MSW where Catch also provides an overload taking
// wchar_t, but as it simply converts arguments to char internally anyhow,
// we can just as well always use the char version.
return Catch::Session().run(argc, static_cast<char**>(argv));
}
int TestApp::OnExit()
{
#if wxUSE_GUI
delete GetTopWindow();
#endif // wxUSE_GUI
return TestAppBase::OnExit();
}
|