]> git.saurik.com Git - wxWidgets.git/blob - tests/testprec.h
avoid duplicate move events
[wxWidgets.git] / tests / testprec.h
1 #ifndef WX_TESTPREC_INCLUDED
2 #define WX_TESTPREC_INCLUDED 1
3
4 #include "wx/wxprec.h"
5 #include "wx/stopwatch.h"
6 #include "wx/evtloop.h"
7 #include "wx/cppunit.h"
8
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
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
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
32 #endif
33
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__) || \
46 (defined(__MINGW32__) && \
47 (!defined(__MINGW_FEATURES__) || !__USE_MINGW_ANSI_STDIO))
48 #define wxUSING_VC_CRT_IO
49 #endif
50
51 // thrown when assert fails in debug build
52 class TestAssertFailure
53 {
54 public:
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 };
76
77 // macro to use for the functions which are supposed to fail an assertion
78 #if wxDEBUG_LEVEL
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
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)
96 #endif
97
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
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;
120
121 typedef int (*FilterEventFunc)(wxEvent&);
122 typedef bool (*ProcessEventFunc)(wxEvent&);
123
124 extern void SetFilterEventFunc(FilterEventFunc func);
125 extern void SetProcessEventFunc(ProcessEventFunc func);
126
127 extern bool IsNetworkAvailable();
128
129 extern bool IsAutomaticTest();
130
131 // Helper class setting the locale to the given one for its lifetime.
132 class LocaleSetter
133 {
134 public:
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 }
146
147 private:
148 char * const m_locOld;
149
150 wxDECLARE_NO_COPY_CLASS(LocaleSetter);
151 };
152
153 // An even simpler helper for setting the locale to "C" one during its lifetime.
154 class CLocaleSetter : private LocaleSetter
155 {
156 public:
157 CLocaleSetter() : LocaleSetter("C") { }
158
159 private:
160 wxDECLARE_NO_COPY_CLASS(CLocaleSetter);
161 };
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")
179
180 #endif