]> git.saurik.com Git - wxWidgets.git/blame - tests/testprec.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / tests / testprec.h
CommitLineData
38e54405
JJ
1#ifndef WX_TESTPREC_INCLUDED
2#define WX_TESTPREC_INCLUDED 1
3
8899b155 4#include "wx/wxprec.h"
1e0b0d7c
SL
5#include "wx/stopwatch.h"
6#include "wx/evtloop.h"
8899b155 7#include "wx/cppunit.h"
8da7a00a 8
232fdc63
VZ
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
11// support it.
12//
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)
18#else
19 #define WXUISIM_TEST(test) (void)0
20#endif
21
8da7a00a
VZ
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
a8f17758
VZ
26
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)
31 #endif
8da7a00a 32#endif
0468a58a 33
43f8864b
VZ
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).
39//
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__) || \
c4cb46c1
VZ
46 (defined(__MINGW32__) && \
47 (!defined(__MINGW_FEATURES__) || !__USE_MINGW_ANSI_STDIO))
43f8864b
VZ
48 #define wxUSING_VC_CRT_IO
49#endif
50
0468a58a 51// thrown when assert fails in debug build
b33e98f0
VZ
52class TestAssertFailure
53{
54public:
55 TestAssertFailure(const wxString& file,
56 int line,
57 const wxString& func,
58 const wxString& cond,
59 const wxString& msg)
60 : m_file(file),
61 m_line(line),
62 m_func(func),
63 m_cond(cond),
64 m_msg(msg)
65 {
66 }
67
68 const wxString m_file;
69 const int m_line;
70 const wxString m_func;
71 const wxString m_cond;
72 const wxString m_msg;
73
74 wxDECLARE_NO_ASSIGN_CLASS(TestAssertFailure);
75};
0468a58a
VZ
76
77// macro to use for the functions which are supposed to fail an assertion
657a8a35 78#if wxDEBUG_LEVEL
0468a58a
VZ
79 // some old cppunit versions don't define CPPUNIT_ASSERT_THROW so roll our
80 // own
81 #define WX_ASSERT_FAILS_WITH_ASSERT(cond) \
82 { \
83 bool throwsAssert = false; \
84 try { cond ; } \
85 catch ( const TestAssertFailure& ) { throwsAssert = true; } \
86 if ( !throwsAssert ) \
87 CPPUNIT_FAIL("expected assertion not generated"); \
88 }
89#else
e042c3b5
VZ
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)
0468a58a
VZ
96#endif
97
1e0b0d7c
SL
98#define WX_ASSERT_EVENT_OCCURS(eventcounter, count) \
99{\
100 wxStopWatch sw; \
101 wxEventLoopBase* loop = wxEventLoopBase::GetActive(); \
102 while(eventcounter.GetCount() < count) \
103 { \
104 if(sw.Time() < 100) \
105 loop->Dispatch(); \
106 else \
107 { \
108 CPPUNIT_FAIL(wxString::Format("timeout reached with %d " \
109 "events received, %d expected", \
110 eventcounter.GetCount(), count).ToStdString()); \
111 break; \
112 } \
113 } \
114 eventcounter.Clear(); \
115}
116
1649d288
VZ
117// these functions can be used to hook into wxApp event processing and are
118// currently used by the events propagation test
9eab6725
VZ
119class WXDLLIMPEXP_FWD_BASE wxEvent;
120
1649d288
VZ
121typedef int (*FilterEventFunc)(wxEvent&);
122typedef bool (*ProcessEventFunc)(wxEvent&);
123
124extern void SetFilterEventFunc(FilterEventFunc func);
125extern void SetProcessEventFunc(ProcessEventFunc func);
1f51673b
FM
126
127extern bool IsNetworkAvailable();
701aa4d8 128
f1287154
VZ
129extern bool IsAutomaticTest();
130
12249b19
VZ
131// Helper class setting the locale to the given one for its lifetime.
132class LocaleSetter
701aa4d8
FM
133{
134public:
2416cd03
VZ
135 LocaleSetter(const char *loc)
136 : m_locOld(wxStrdupA(setlocale(LC_ALL, NULL)))
137 {
138 setlocale(LC_ALL, loc);
139 }
140
141 ~LocaleSetter()
142 {
143 setlocale(LC_ALL, m_locOld);
144 free(m_locOld);
145 }
701aa4d8
FM
146
147private:
2416cd03 148 char * const m_locOld;
12249b19
VZ
149
150 wxDECLARE_NO_COPY_CLASS(LocaleSetter);
151};
152
153// An even simpler helper for setting the locale to "C" one during its lifetime.
154class CLocaleSetter : private LocaleSetter
155{
156public:
157 CLocaleSetter() : LocaleSetter("C") { }
158
159private:
701aa4d8
FM
160 wxDECLARE_NO_COPY_CLASS(CLocaleSetter);
161};
8cdd00f2
VZ
162
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.
166//
167// Notice that the name shouldn't include the "TestCase" suffix, it's added
168// automatically by this macro.
169//
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")
38e54405
JJ
179
180#endif