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