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