]> git.saurik.com Git - wxWidgets.git/blame - tests/test.cpp
Correct the fix that broke wxRegion::ConvertToBitmap().
[wxWidgets.git] / tests / test.cpp
CommitLineData
670ec357
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: test.cpp
3// Purpose: Test program for wxWidgets
4// Author: Mike Wetherell
5// RCS-ID: $Id$
6// Copyright: (c) 2004 Mike Wetherell
526954c5 7// Licence: wxWindows licence
670ec357
VS
8///////////////////////////////////////////////////////////////////////////////
9
1f51673b
FM
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
8899b155
RN
14// For compilers that support precompilation, includes "wx/wx.h"
15// and "wx/cppunit.h"
16#include "testprec.h"
670ec357
VS
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22// for all others, include the necessary headers
23#ifndef WX_PRECOMP
24 #include "wx/wx.h"
25#endif
26
afd90468 27#include "wx/beforestd.h"
6d9407fe
VZ
28#ifdef __VISUALC__
29 #pragma warning(disable:4100)
30#endif
de830910 31
afd90468 32#include <cppunit/TestListener.h>
b33e98f0 33#include <cppunit/Protector.h>
afd90468
VZ
34#include <cppunit/Test.h>
35#include <cppunit/TestResult.h>
29e0c68e 36#include <cppunit/TestFailure.h>
de830910
VZ
37
38#ifdef __VISUALC__
39 #pragma warning(default:4100)
40#endif
afd90468
VZ
41#include "wx/afterstd.h"
42
670ec357 43#include "wx/cmdline.h"
33f7fa34 44#include <exception>
670ec357
VS
45#include <iostream>
46
b33e98f0
VZ
47#ifdef __WXMSW__
48 #include "wx/msw/msvcrt.h"
49#endif
50
8b7b1a57
SC
51#ifdef __WXOSX__
52 #include "wx/osx/private.h"
53#endif
54
232fdc63
VZ
55#if wxUSE_GUI
56 #include "testableframe.h"
57#endif
58
1f51673b 59#include "wx/socket.h"
232fdc63 60#include "wx/evtloop.h"
1f51673b 61
b33e98f0
VZ
62using namespace std;
63
3e5f6c1c
WS
64using CppUnit::Test;
65using CppUnit::TestSuite;
66using CppUnit::TestFactoryRegistry;
3e5f6c1c 67
1f51673b
FM
68
69// ----------------------------------------------------------------------------
70// helper classes
71// ----------------------------------------------------------------------------
72
b33e98f0
VZ
73// exception class for MSVC debug CRT assertion failures
74#ifdef wxUSE_VC_CRTDBG
75
76struct CrtAssertFailure
77{
78 CrtAssertFailure(const char *message) : m_msg(message) { }
79
80 const wxString m_msg;
81
82 wxDECLARE_NO_ASSIGN_CLASS(CrtAssertFailure);
83};
84
85#endif // wxUSE_VC_CRTDBG
86
884ca4e4
VZ
87#if wxDEBUG_LEVEL
88
89static wxString FormatAssertMessage(const wxString& file,
90 int line,
91 const wxString& func,
92 const wxString& cond,
93 const wxString& msg)
94{
95 wxString str;
96 str << "wxWidgets assert: " << cond << " failed "
97 "at " << file << ":" << line << " in " << func
98 << " with message '" << msg << "'";
99 return str;
100}
101
102static void TestAssertHandler(const wxString& file,
103 int line,
104 const wxString& func,
105 const wxString& cond,
106 const wxString& msg)
107{
33f7fa34
VZ
108 // Determine whether we can safely throw an exception to just make the test
109 // fail or whether we need to abort (in this case "msg" will contain the
110 // explanation why did we decide to do it).
111 wxString abortReason;
884ca4e4
VZ
112 if ( !wxIsMainThread() )
113 {
33f7fa34
VZ
114 // Exceptions thrown from worker threads are not caught currently and
115 // so we'd just die without any useful information -- abort instead.
116 abortReason = "in a worker thread";
117 }
118 else if ( uncaught_exception() )
119 {
120 // Throwing while already handling an exception would result in
121 // terminate() being called and we wouldn't get any useful information
122 // about why the test failed then.
123 abortReason = "while handling an exception";
124 }
125 else // Can "safely" throw from here.
126 {
127 throw TestAssertFailure(file, line, func, cond, msg);
884ca4e4
VZ
128 }
129
33f7fa34
VZ
130 wxFprintf(stderr, "%s %s -- aborting.",
131 FormatAssertMessage(file, line, func, cond, msg),
132 abortReason);
133 fflush(stderr);
134 _exit(-1);
884ca4e4
VZ
135}
136
137#endif // wxDEBUG_LEVEL
138
b33e98f0
VZ
139// this function should only be called from a catch clause
140static string GetExceptionMessage()
141{
142 wxString msg;
143
144 try
145 {
146 throw;
147 }
148#if wxDEBUG_LEVEL
149 catch ( TestAssertFailure& e )
150 {
884ca4e4
VZ
151 msg << FormatAssertMessage(e.m_file, e.m_line, e.m_func,
152 e.m_cond, e.m_msg);
b33e98f0
VZ
153 }
154#endif // wxDEBUG_LEVEL
155#ifdef wxUSE_VC_CRTDBG
156 catch ( CrtAssertFailure& e )
157 {
158 msg << "CRT assert failure: " << e.m_msg;
159 }
160#endif // wxUSE_VC_CRTDBG
161 catch ( std::exception& e )
162 {
163 msg << "std::exception: " << e.what();
164 }
165 catch ( ... )
166 {
167 msg = "Unknown exception caught.";
168 }
169
170 return string(msg.mb_str());
171}
172
173// Protector adding handling of wx-specific (this includes MSVC debug CRT in
174// this context) exceptions
175class wxUnitTestProtector : public CppUnit::Protector
176{
177public:
178 virtual bool protect(const CppUnit::Functor &functor,
179 const CppUnit::ProtectorContext& context)
180 {
181 try
182 {
183 return functor();
184 }
92f8206f 185 catch ( std::exception& )
9912799c
VZ
186 {
187 // cppunit deals with the standard exceptions itself, let it do as
188 // it output more details (especially for std::exception-derived
189 // CppUnit::Exception) than we do
190 throw;
191 }
b33e98f0
VZ
192 catch ( ... )
193 {
194 reportError(context, CppUnit::Message("Uncaught exception",
195 GetExceptionMessage()));
196 }
197
198 return false;
199 }
200};
afd90468 201
6d9407fe
VZ
202// Displays the test name before starting to execute it: this helps with
203// diagnosing where exactly does a test crash or hang when/if it does.
afd90468
VZ
204class DetailListener : public CppUnit::TestListener
205{
206public:
207 DetailListener(bool doTiming = false):
208 CppUnit::TestListener(),
209 m_timing(doTiming)
210 {
211 }
212
213 virtual void startTest(CppUnit::Test *test)
214 {
29e0c68e
VZ
215 wxPrintf(" %-60s ", test->getName());
216 m_result = RESULT_OK;
afd90468
VZ
217 m_watch.Start();
218 }
219
62714742
VZ
220 virtual void addFailure(const CppUnit::TestFailure& failure)
221 {
29e0c68e
VZ
222 m_result = failure.isError() ? RESULT_ERROR : RESULT_FAIL;
223 }
224
afd90468
VZ
225 virtual void endTest(CppUnit::Test * WXUNUSED(test))
226 {
227 m_watch.Pause();
29e0c68e
VZ
228 wxPrintf(GetResultStr(m_result));
229 if (m_timing)
6222ad28 230 wxPrintf(" %6ld ms", m_watch.Time());
29e0c68e 231 wxPrintf("\n");
afd90468
VZ
232 }
233
234protected :
62714742
VZ
235 enum ResultType
236 {
29e0c68e
VZ
237 RESULT_OK = 0,
238 RESULT_FAIL,
62714742
VZ
239 RESULT_ERROR,
240 RESULT_MAX
29e0c68e
VZ
241 };
242
62714742
VZ
243 wxString GetResultStr(ResultType type) const
244 {
245 static const char *resultTypeNames[] =
246 {
247 " OK",
248 "FAIL",
249 " ERR"
29e0c68e 250 };
62714742
VZ
251
252 wxCOMPILE_TIME_ASSERT( WXSIZEOF(resultTypeNames) == RESULT_MAX,
253 ResultTypeNamesMismatch );
254
255 return resultTypeNames[type];
29e0c68e
VZ
256 }
257
afd90468
VZ
258 bool m_timing;
259 wxStopWatch m_watch;
29e0c68e 260 ResultType m_result;
afd90468
VZ
261};
262
c0d9b217
VZ
263#if wxUSE_GUI
264 typedef wxApp TestAppBase;
265#else
266 typedef wxAppConsole TestAppBase;
267#endif
268
670ec357
VS
269// The application class
270//
c0d9b217 271class TestApp : public TestAppBase
670ec357
VS
272{
273public:
274 TestApp();
275
276 // standard overrides
c0d9b217
VZ
277 virtual void OnInitCmdLine(wxCmdLineParser& parser);
278 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
279 virtual bool OnInit();
280 virtual int OnRun();
281 virtual int OnExit();
670ec357 282
1649d288
VZ
283 // used by events propagation test
284 virtual int FilterEvent(wxEvent& event);
285 virtual bool ProcessEvent(wxEvent& event);
286
287 void SetFilterEventFunc(FilterEventFunc f) { m_filterEventFunc = f; }
288 void SetProcessEventFunc(ProcessEventFunc f) { m_processEventFunc = f; }
289
670ec357 290private:
8dae9169 291 void List(Test *test, const string& parent = "") const;
670ec357 292
e992b97f
VZ
293 // call List() if m_list or runner.addTest() otherwise
294 void AddTest(CppUnit::TestRunner& runner, Test *test)
295 {
296 if (m_list)
297 List(test);
298 else
299 runner.addTest(test);
300 }
301
670ec357
VS
302 // command lines options/parameters
303 bool m_list;
a81f3066 304 bool m_longlist;
afd90468
VZ
305 bool m_detail;
306 bool m_timing;
6582f592 307 wxArrayString m_registries;
ad16130f 308 wxLocale *m_locale;
1649d288 309
232fdc63
VZ
310 // event loop for GUI tests
311 wxEventLoop* m_eventloop;
312
1649d288
VZ
313 // event handling hooks
314 FilterEventFunc m_filterEventFunc;
315 ProcessEventFunc m_processEventFunc;
670ec357
VS
316};
317
b33e98f0
VZ
318IMPLEMENT_APP_NO_MAIN(TestApp)
319
1f51673b
FM
320
321// ----------------------------------------------------------------------------
322// global functions
323// ----------------------------------------------------------------------------
324
b33e98f0
VZ
325#ifdef wxUSE_VC_CRTDBG
326
327static int TestCrtReportHook(int reportType, char *message, int *)
328{
329 if ( reportType != _CRT_ASSERT )
330 return FALSE;
331
332 throw CrtAssertFailure(message);
333}
334
335#endif // wxUSE_VC_CRTDBG
336
b33e98f0
VZ
337int main(int argc, char **argv)
338{
339 // tests can be ran non-interactively so make sure we don't show any assert
340 // dialog boxes -- neither our own nor from MSVC debug CRT -- which would
341 // prevent them from completing
342
343#if wxDEBUG_LEVEL
344 wxSetAssertHandler(TestAssertHandler);
345#endif // wxDEBUG_LEVEL
346
347#ifdef wxUSE_VC_CRTDBG
348 _CrtSetReportHook(TestCrtReportHook);
349#endif // wxUSE_VC_CRTDBG
350
351 try
352 {
353 return wxEntry(argc, argv);
354 }
355 catch ( ... )
356 {
357 cerr << "\n" << GetExceptionMessage() << endl;
358 }
359
360 return -1;
361}
670ec357 362
1f51673b
FM
363extern void SetFilterEventFunc(FilterEventFunc func)
364{
365 wxGetApp().SetFilterEventFunc(func);
366}
367
368extern void SetProcessEventFunc(ProcessEventFunc func)
369{
370 wxGetApp().SetProcessEventFunc(func);
371}
372
373extern bool IsNetworkAvailable()
374{
375 // NOTE: we could use wxDialUpManager here if it was in wxNet; since it's in
376 // wxCore we use a simple rough test:
377
378 wxSocketBase::Initialize();
379
380 wxIPV4address addr;
381 if (!addr.Hostname("www.google.com") || !addr.Service("www"))
382 {
383 wxSocketBase::Shutdown();
384 return false;
385 }
386
387 wxSocketClient sock;
5b105c6f 388 sock.SetTimeout(10); // 10 secs
1f51673b
FM
389 bool online = sock.Connect(addr);
390
391 wxSocketBase::Shutdown();
392
393 return online;
394}
395
396// helper of OnRun(): gets the test with the given name, returning NULL (and
397// not an empty test suite) if there is no such test
398static Test *GetTestByName(const wxString& name)
399{
400 Test *
401 test = TestFactoryRegistry::getRegistry(string(name.mb_str())).makeTest();
402 if ( test )
403 {
404 TestSuite * const suite = dynamic_cast<TestSuite *>(test);
405 if ( !suite || !suite->countTestCases() )
406 {
407 // it's a bogus test, don't use it
408 delete test;
409 test = NULL;
410 }
411 }
412
413 return test;
414}
415
416
417// ----------------------------------------------------------------------------
418// TestApp
419// ----------------------------------------------------------------------------
420
670ec357 421TestApp::TestApp()
8dae9169
VS
422 : m_list(false),
423 m_longlist(false)
670ec357 424{
1649d288
VZ
425 m_filterEventFunc = NULL;
426 m_processEventFunc = NULL;
ad16130f
VZ
427
428 m_locale = NULL;
232fdc63 429 m_eventloop = NULL;
670ec357
VS
430}
431
432// Init
433//
434bool TestApp::OnInit()
435{
c0d9b217
VZ
436 if ( !TestAppBase::OnInit() )
437 return false;
438
9e91447c
FM
439#if wxUSE_GUI
440 cout << "Test program for wxWidgets GUI features\n"
441#else
442 cout << "Test program for wxWidgets non-GUI features\n"
443#endif
3e5f6c1c 444 << "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
9f7a07ab 445
c0d9b217
VZ
446#if wxUSE_GUI
447 // create a hidden parent window to be used as parent for the GUI controls
232fdc63
VZ
448 wxTestableFrame* frame = new wxTestableFrame();
449 frame->Show();
450
451 m_eventloop = new wxEventLoop;
452 wxEventLoop::SetActive(m_eventloop);
c0d9b217
VZ
453#endif // wxUSE_GUI
454
455 return true;
9f10e7c7 456}
670ec357
VS
457
458// The table of command line options
459//
460void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
461{
c0d9b217 462 TestAppBase::OnInitCmdLine(parser);
670ec357
VS
463
464 static const wxCmdLineEntryDesc cmdLineDesc[] = {
aa3b041e
VZ
465 { wxCMD_LINE_SWITCH, "l", "list",
466 "list the test suites, do not run them",
a81f3066 467 wxCMD_LINE_VAL_NONE, 0 },
aa3b041e
VZ
468 { wxCMD_LINE_SWITCH, "L", "longlist",
469 "list the test cases, do not run them",
670ec357 470 wxCMD_LINE_VAL_NONE, 0 },
afd90468
VZ
471 { wxCMD_LINE_SWITCH, "d", "detail",
472 "print the test case names, run them",
473 wxCMD_LINE_VAL_NONE, 0 },
474 { wxCMD_LINE_SWITCH, "t", "timing",
20ba398d 475 "print names and measure running time of individual test, run them",
afd90468 476 wxCMD_LINE_VAL_NONE, 0 },
ad16130f
VZ
477 { wxCMD_LINE_OPTION, "", "locale",
478 "locale to use when running the program",
479 wxCMD_LINE_VAL_STRING, 0 },
aa3b041e 480 { wxCMD_LINE_PARAM, NULL, NULL, "REGISTRY", wxCMD_LINE_VAL_STRING,
a81f3066 481 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
aa3b041e 482 wxCMD_LINE_DESC_END
670ec357
VS
483 };
484
485 parser.SetDesc(cmdLineDesc);
486}
487
488// Handle command line options
489//
490bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
491{
a81f3066 492 if (parser.GetParamCount())
ad16130f 493 {
a81f3066 494 for (size_t i = 0; i < parser.GetParamCount(); i++)
6582f592 495 m_registries.push_back(parser.GetParam(i));
ad16130f 496 }
ad16130f
VZ
497
498 m_longlist = parser.Found("longlist");
499 m_list = m_longlist || parser.Found("list");
500 m_timing = parser.Found("timing");
501 m_detail = !m_timing && parser.Found("detail");
502
503 wxString loc;
504 if ( parser.Found("locale", &loc) )
505 {
506 const wxLanguageInfo * const info = wxLocale::FindLanguageInfo(loc);
507 if ( !info )
508 {
509 cerr << "Locale \"" << string(loc.mb_str()) << "\" is unknown.\n";
510 return false;
511 }
3e5f6c1c 512
ad16130f
VZ
513 m_locale = new wxLocale(info->Language);
514 if ( !m_locale->IsOk() )
515 {
516 cerr << "Using locale \"" << string(loc.mb_str()) << "\" failed.\n";
517 return false;
518 }
519 }
670ec357 520
c0d9b217 521 return TestAppBase::OnCmdLineParsed(parser);
670ec357
VS
522}
523
1649d288
VZ
524// Event handling
525int TestApp::FilterEvent(wxEvent& event)
526{
527 if ( m_filterEventFunc )
528 return (*m_filterEventFunc)(event);
529
530 return TestAppBase::FilterEvent(event);
531}
532
533bool TestApp::ProcessEvent(wxEvent& event)
534{
535 if ( m_processEventFunc )
536 return (*m_processEventFunc)(event);
537
538 return TestAppBase::ProcessEvent(event);
539}
540
670ec357
VS
541// Run
542//
543int TestApp::OnRun()
544{
8b7b1a57
SC
545#if wxUSE_GUI
546#ifdef __WXOSX__
547 // make sure there's always an autorelease pool ready
548 wxMacAutoreleasePool autoreleasepool;
549#endif
550#endif
551
875f82b1
VZ
552#if wxUSE_LOG
553 // Switch off logging unless --verbose
554 bool verbose = wxLog::GetVerbose();
555 wxLog::EnableLogging(verbose);
556#else
557 bool verbose = false;
558#endif
559
2976d6cb 560 CppUnit::TextTestRunner runner;
a81f3066 561
e992b97f 562 if ( m_registries.empty() )
6d423df0 563 {
20ba398d
FM
564 // run or list all tests which use the CPPUNIT_TEST_SUITE_REGISTRATION() macro
565 // (i.e. those registered in the "All tests" registry); if there are other
566 // tests not registered with the CPPUNIT_TEST_SUITE_REGISTRATION() macro
567 // then they won't be listed/run!
e992b97f 568 AddTest(runner, TestFactoryRegistry::getRegistry().makeTest());
20ba398d
FM
569
570 if (m_list)
571 {
572 cout << "\nNote that the list above is not complete as it doesn't include the \n";
573 cout << "tests disabled by default.\n";
574 }
e992b97f
VZ
575 }
576 else // run only the selected tests
577 {
578 for (size_t i = 0; i < m_registries.size(); i++)
d73a5209 579 {
e992b97f
VZ
580 const wxString reg = m_registries[i];
581 Test *test = GetTestByName(reg);
d73a5209
VZ
582
583 if ( !test && !reg.EndsWith("TestCase") )
584 {
585 test = GetTestByName(reg + "TestCase");
586 }
587
588 if ( !test )
589 {
590 cerr << "No such test suite: " << string(reg.mb_str()) << endl;
591 return 2;
592 }
a81f3066 593
e992b97f
VZ
594 AddTest(runner, test);
595 }
670ec357 596 }
a81f3066 597
2976d6cb 598 if ( m_list )
1dd8319a 599 return EXIT_SUCCESS;
2976d6cb
VZ
600
601 runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), cout));
14dc53b2 602
2976d6cb
VZ
603 // there is a bug
604 // (http://sf.net/tracker/index.php?func=detail&aid=1649369&group_id=11795&atid=111795)
605 // in some versions of cppunit: they write progress dots to cout (and not
606 // cerr) and don't flush it so all the dots appear at once at the end which
607 // is not very useful so unbuffer cout to work around this
608 cout.setf(ios::unitbuf);
609
afd90468
VZ
610 // add detail listener if needed
611 DetailListener detailListener(m_timing);
612 if ( m_detail || m_timing )
613 runner.eventManager().addListener(&detailListener);
614
b33e98f0
VZ
615 // finally ensure that we report our own exceptions nicely instead of
616 // giving "uncaught exception of unknown type" messages
617 runner.eventManager().pushProtector(new wxUnitTestProtector);
618
29e0c68e
VZ
619 bool printProgress = !(verbose || m_detail || m_timing);
620 return runner.run("", false, true, printProgress) ? EXIT_SUCCESS : EXIT_FAILURE;
670ec357
VS
621}
622
c0d9b217
VZ
623int TestApp::OnExit()
624{
ad16130f
VZ
625 delete m_locale;
626
c0d9b217
VZ
627#if wxUSE_GUI
628 delete GetTopWindow();
232fdc63
VZ
629 wxEventLoop::SetActive(NULL);
630 delete m_eventloop;
c0d9b217
VZ
631#endif // wxUSE_GUI
632
633 return 0;
634}
635
670ec357
VS
636// List the tests
637//
8dae9169 638void TestApp::List(Test *test, const string& parent /*=""*/) const
670ec357 639{
670ec357 640 TestSuite *suite = dynamic_cast<TestSuite*>(test);
8dae9169
VS
641 string name;
642
bc10103e 643 if (suite) {
8dae9169
VS
644 // take the last component of the name and append to the parent
645 name = test->getName();
646 string::size_type i = name.find_last_of(".:");
f44eaed6
RN
647 if (i != string::npos)
648 name = name.substr(i + 1);
649 name = parent + "." + name;
8dae9169
VS
650
651 // drop the 1st component from the display and indent
652 if (parent != "") {
653 string::size_type j = i = name.find('.', 1);
654 while ((j = name.find('.', j + 1)) != string::npos)
655 cout << " ";
656 cout << " " << name.substr(i + 1) << "\n";
657 }
a81f3066 658
f69577be 659 typedef vector<Test*> Tests;
670ec357
VS
660 typedef Tests::const_iterator Iter;
661
f69577be 662 const Tests& tests = suite->getTests();
670ec357
VS
663
664 for (Iter it = tests.begin(); it != tests.end(); ++it)
8dae9169 665 List(*it, name);
670ec357 666 }
bc10103e
VS
667 else if (m_longlist) {
668 string::size_type i = 0;
669 while ((i = parent.find('.', i + 1)) != string::npos)
670 cout << " ";
671 cout << " " << test->getName() << "\n";
672 }
670ec357 673}