testprec.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef WX_TESTPREC_INCLUDED
  2. #define WX_TESTPREC_INCLUDED 1
  3. #include "wx/wxprec.h"
  4. #include "wx/stopwatch.h"
  5. #include "wx/evtloop.h"
  6. #include "wx/cppunit.h"
  7. // Custom test macro that is only defined when wxUIActionSimulator is available
  8. // this allows the tests that do not rely on it to run on platforms that don't
  9. // support it.
  10. //
  11. // FIXME: And while OS X does support it, more or less, too many tests
  12. // currently fail under it so disable all interactive tests there. They
  13. // should, of course, be reenabled a.s.a.p.
  14. #if wxUSE_UIACTIONSIMULATOR && !defined(__WXOSX__)
  15. #define WXUISIM_TEST(test) CPPUNIT_TEST(test)
  16. #else
  17. #define WXUISIM_TEST(test) (void)0
  18. #endif
  19. // define wxHAVE_U_ESCAPE if the compiler supports \uxxxx character constants
  20. #if (defined(__VISUALC__) && (__VISUALC__ >= 1300)) || \
  21. (defined(__GNUC__) && (__GNUC__ >= 3))
  22. #define wxHAVE_U_ESCAPE
  23. // and disable warning that using them results in with MSVC 8+
  24. #if wxCHECK_VISUALC_VERSION(8)
  25. // universal-character-name encountered in source
  26. #pragma warning(disable:4428)
  27. #endif
  28. #endif
  29. // Define wxUSING_VC_CRT_IO when using MSVC CRT STDIO library as its standard
  30. // functions give different results from glibc ones in several cases (of
  31. // course, any code relying on this is not portable and probably won't work,
  32. // i.e. will result in tests failures, with other platforms/compilers which
  33. // should have checks for them added as well).
  34. //
  35. // Notice that MinGW uses VC CRT by default but may use its own printf()
  36. // implementation if __USE_MINGW_ANSI_STDIO is defined. And finally also notice
  37. // that testing for __USE_MINGW_ANSI_STDIO directly results in a warning with
  38. // -Wundef if it involves an operation with undefined __MINGW_FEATURES__ so
  39. // test for the latter too to avoid it.
  40. #if defined(__VISUALC__) || \
  41. (defined(__MINGW32__) && \
  42. (!defined(__MINGW_FEATURES__) || !__USE_MINGW_ANSI_STDIO))
  43. #define wxUSING_VC_CRT_IO
  44. #endif
  45. // thrown when assert fails in debug build
  46. class TestAssertFailure
  47. {
  48. public:
  49. TestAssertFailure(const wxString& file,
  50. int line,
  51. const wxString& func,
  52. const wxString& cond,
  53. const wxString& msg)
  54. : m_file(file),
  55. m_line(line),
  56. m_func(func),
  57. m_cond(cond),
  58. m_msg(msg)
  59. {
  60. }
  61. const wxString m_file;
  62. const int m_line;
  63. const wxString m_func;
  64. const wxString m_cond;
  65. const wxString m_msg;
  66. wxDECLARE_NO_ASSIGN_CLASS(TestAssertFailure);
  67. };
  68. // macro to use for the functions which are supposed to fail an assertion
  69. #if wxDEBUG_LEVEL
  70. // some old cppunit versions don't define CPPUNIT_ASSERT_THROW so roll our
  71. // own
  72. #define WX_ASSERT_FAILS_WITH_ASSERT(cond) \
  73. { \
  74. bool throwsAssert = false; \
  75. try { cond ; } \
  76. catch ( const TestAssertFailure& ) { throwsAssert = true; } \
  77. if ( !throwsAssert ) \
  78. CPPUNIT_FAIL("expected assertion not generated"); \
  79. }
  80. #else
  81. // there are no assertions in this build so we can't do anything (we used
  82. // to check that the condition failed but this didn't work well as in
  83. // normal build with wxDEBUG_LEVEL != 0 we can pass something not
  84. // evaluating to a bool at all but it then would fail to compile in
  85. // wxDEBUG_LEVEL == 0 case, so just don't do anything at all now).
  86. #define WX_ASSERT_FAILS_WITH_ASSERT(cond)
  87. #endif
  88. #define WX_ASSERT_EVENT_OCCURS(eventcounter, count) \
  89. {\
  90. wxStopWatch sw; \
  91. wxEventLoopBase* loop = wxEventLoopBase::GetActive(); \
  92. while(eventcounter.GetCount() < count) \
  93. { \
  94. if(sw.Time() < 100) \
  95. loop->Dispatch(); \
  96. else \
  97. { \
  98. CPPUNIT_FAIL(wxString::Format("timeout reached with %d " \
  99. "events received, %d expected", \
  100. eventcounter.GetCount(), count).ToStdString()); \
  101. break; \
  102. } \
  103. } \
  104. eventcounter.Clear(); \
  105. }
  106. // these functions can be used to hook into wxApp event processing and are
  107. // currently used by the events propagation test
  108. class WXDLLIMPEXP_FWD_BASE wxEvent;
  109. typedef int (*FilterEventFunc)(wxEvent&);
  110. typedef bool (*ProcessEventFunc)(wxEvent&);
  111. extern void SetFilterEventFunc(FilterEventFunc func);
  112. extern void SetProcessEventFunc(ProcessEventFunc func);
  113. extern bool IsNetworkAvailable();
  114. extern bool IsAutomaticTest();
  115. // Helper class setting the locale to the given one for its lifetime.
  116. class LocaleSetter
  117. {
  118. public:
  119. LocaleSetter(const char *loc)
  120. : m_locOld(wxStrdupA(setlocale(LC_ALL, NULL)))
  121. {
  122. setlocale(LC_ALL, loc);
  123. }
  124. ~LocaleSetter()
  125. {
  126. setlocale(LC_ALL, m_locOld);
  127. free(m_locOld);
  128. }
  129. private:
  130. char * const m_locOld;
  131. wxDECLARE_NO_COPY_CLASS(LocaleSetter);
  132. };
  133. // An even simpler helper for setting the locale to "C" one during its lifetime.
  134. class CLocaleSetter : private LocaleSetter
  135. {
  136. public:
  137. CLocaleSetter() : LocaleSetter("C") { }
  138. private:
  139. wxDECLARE_NO_COPY_CLASS(CLocaleSetter);
  140. };
  141. // Macro that can be used to register the test with the given name in both the
  142. // global unnamed registry so that it is ran by default and a registry with the
  143. // same name as this test to allow running just this test individually.
  144. //
  145. // Notice that the name shouldn't include the "TestCase" suffix, it's added
  146. // automatically by this macro.
  147. //
  148. // Implementation note: CPPUNIT_TEST_SUITE_[NAMED_]REGISTRATION macros can't be
  149. // used here because they both declare the variable with the same name (as the
  150. // "unique" name they generate is based on the line number which is the same
  151. // for both calls inside the macro), so we need to do it manually.
  152. #define wxREGISTER_UNIT_TEST(name) \
  153. static CPPUNIT_NS::AutoRegisterSuite< name##TestCase > \
  154. CPPUNIT_MAKE_UNIQUE_NAME( autoRegisterRegistry__ ); \
  155. static CPPUNIT_NS::AutoRegisterSuite< name##TestCase > \
  156. CPPUNIT_MAKE_UNIQUE_NAME( autoRegisterNamedRegistry__ )(#name "TestCase")
  157. #endif