]> git.saurik.com Git - wxWidgets.git/blame - tests/log/logtest.cpp
Made wxWindow::HasScrollbar() do what it says.
[wxWidgets.git] / tests / log / logtest.cpp
CommitLineData
ece5e6a4
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/log/logtest.cpp
3// Purpose: wxLog unit test
4// Author: Vadim Zeitlin
5// Created: 2009-07-07
6// RCS-ID: $Id$
7// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14#include "testprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21 #include "wx/log.h"
22#endif // WX_PRECOMP
23
bc73d5ae
VZ
24#include "wx/scopeguard.h"
25
48557701
VZ
26#if WXWIN_COMPATIBILITY_2_8
27 // we override deprecated DoLog() and DoLogString() in this test, suppress
28 // warnings about it
29 #if wxCHECK_VISUALC_VERSION(7)
30 #pragma warning(disable: 4996)
31 #endif // VC++ 7+
32#endif // WXWIN_COMPATIBILITY_2_8
33
c602c59b
VZ
34// all calls to wxLogXXX() functions from this file will use this log component
35#define wxLOG_COMPONENT "test"
36
ece5e6a4 37// ----------------------------------------------------------------------------
bc73d5ae 38// test loggers
ece5e6a4
VZ
39// ----------------------------------------------------------------------------
40
bc73d5ae
VZ
41// base class for all test loggers which simply store all logged messages for
42// future examination in the test code
43class TestLogBase : public wxLog
ece5e6a4
VZ
44{
45public:
bc73d5ae 46 TestLogBase() { }
ece5e6a4 47
c602c59b 48 const wxString& GetLog(wxLogLevel level) const
ece5e6a4
VZ
49 {
50 return m_logs[level];
51 }
52
c602c59b
VZ
53 const wxLogRecordInfo& GetInfo(wxLogLevel level) const
54 {
55 return m_logsInfo[level];
56 }
57
ece5e6a4
VZ
58 void Clear()
59 {
60 for ( unsigned n = 0; n < WXSIZEOF(m_logs); n++ )
c602c59b 61 {
ece5e6a4 62 m_logs[n].clear();
c602c59b
VZ
63 m_logsInfo[n] = wxLogRecordInfo();
64 }
ece5e6a4
VZ
65 }
66
67protected:
bc73d5ae 68 wxString m_logs[wxLOG_Trace + 1];
c602c59b 69 wxLogRecordInfo m_logsInfo[wxLOG_Trace + 1];
bc73d5ae
VZ
70
71 wxDECLARE_NO_COPY_CLASS(TestLogBase);
72};
73
74// simple log sink which just stores the messages logged for each level
75class TestLog : public TestLogBase
76{
77public:
78 TestLog() { }
79
80protected:
81 virtual void DoLogRecord(wxLogLevel level,
82 const wxString& msg,
c602c59b 83 const wxLogRecordInfo& info)
bc73d5ae
VZ
84 {
85 m_logs[level] = msg;
c602c59b 86 m_logsInfo[level] = info;
bc73d5ae
VZ
87 }
88
89private:
90 wxDECLARE_NO_COPY_CLASS(TestLog);
91};
92
93#if WXWIN_COMPATIBILITY_2_8
94
95// log sink overriding the old DoLogXXX() functions should still work too
96
97// this one overrides DoLog(char*)
98class CompatTestLog : public TestLogBase
99{
100public:
101 CompatTestLog() { }
102
103protected:
104 virtual void DoLog(wxLogLevel level, const char *str, time_t WXUNUSED(t))
ece5e6a4
VZ
105 {
106 m_logs[level] = str;
107 }
108
bc73d5ae
VZ
109 // get rid of the warning about hiding the other overload
110 virtual void DoLog(wxLogLevel WXUNUSED(level),
111 const wchar_t *WXUNUSED(str),
112 time_t WXUNUSED(t))
113 {
114 }
ece5e6a4
VZ
115
116private:
bc73d5ae
VZ
117 wxDECLARE_NO_COPY_CLASS(CompatTestLog);
118};
ece5e6a4 119
bc73d5ae
VZ
120// and this one overload DoLogString(wchar_t*)
121class CompatTestLog2 : public wxLog
122{
123public:
124 CompatTestLog2() { }
125
126 const wxString& Get() const { return m_msg; }
127
128protected:
129 virtual void DoLogString(const wchar_t *msg, time_t WXUNUSED(t))
130 {
131 m_msg = msg;
132 }
133
134 // get rid of the warning
135 virtual void DoLogString(const char *WXUNUSED(msg), time_t WXUNUSED(t))
136 {
137 }
138
139private:
140 wxString m_msg;
141
142 wxDECLARE_NO_COPY_CLASS(CompatTestLog2);
ece5e6a4
VZ
143};
144
bc73d5ae
VZ
145#endif // WXWIN_COMPATIBILITY_2_8
146
ece5e6a4
VZ
147// ----------------------------------------------------------------------------
148// test class
149// ----------------------------------------------------------------------------
150
151class LogTestCase : public CppUnit::TestCase
152{
153public:
154 LogTestCase() { }
155
156 virtual void setUp();
157 virtual void tearDown();
158
159private:
160 CPPUNIT_TEST_SUITE( LogTestCase );
161 CPPUNIT_TEST( Functions );
162 CPPUNIT_TEST( Null );
c602c59b 163 CPPUNIT_TEST( Component );
ece5e6a4
VZ
164#if wxDEBUG_LEVEL
165 CPPUNIT_TEST( Trace );
166#endif // wxDEBUG_LEVEL
bc73d5ae
VZ
167#if WXWIN_COMPATIBILITY_2_8
168 CPPUNIT_TEST( CompatLogger );
169 CPPUNIT_TEST( CompatLogger2 );
170#endif // WXWIN_COMPATIBILITY_2_8
ece5e6a4
VZ
171 CPPUNIT_TEST_SUITE_END();
172
173 void Functions();
174 void Null();
c602c59b 175 void Component();
ece5e6a4
VZ
176#if wxDEBUG_LEVEL
177 void Trace();
178#endif // wxDEBUG_LEVEL
bc73d5ae
VZ
179#if WXWIN_COMPATIBILITY_2_8
180 void CompatLogger();
181 void CompatLogger2();
182#endif // WXWIN_COMPATIBILITY_2_8
ece5e6a4
VZ
183
184 TestLog *m_log;
185 wxLog *m_logOld;
186 bool m_logWasEnabled;
187
188 wxDECLARE_NO_COPY_CLASS(LogTestCase);
189};
190
191// register in the unnamed registry so that these tests are run by default
192CPPUNIT_TEST_SUITE_REGISTRATION( LogTestCase );
193
194// also include in it's own registry so that these tests can be run alone
195CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LogTestCase, "LogTestCase" );
196
197void LogTestCase::setUp()
198{
199 m_logOld = wxLog::SetActiveTarget(m_log = new TestLog);
200 m_logWasEnabled = wxLog::EnableLogging();
201}
202
203void LogTestCase::tearDown()
204{
205 delete wxLog::SetActiveTarget(m_logOld);
206 wxLog::EnableLogging(m_logWasEnabled);
207}
208
209void LogTestCase::Functions()
210{
211 wxLogMessage("Message");
212 CPPUNIT_ASSERT_EQUAL( "Message", m_log->GetLog(wxLOG_Message) );
213
214 wxLogError("Error %d", 17);
215 CPPUNIT_ASSERT_EQUAL( "Error 17", m_log->GetLog(wxLOG_Error) );
216
217 wxLogDebug("Debug");
218#if wxDEBUG_LEVEL
219 CPPUNIT_ASSERT_EQUAL( "Debug", m_log->GetLog(wxLOG_Debug) );
220#else
221 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Debug) );
222#endif
223}
224
225void LogTestCase::Null()
226{
227 {
228 wxLogNull noLog;
229 wxLogWarning("%s warning", "Not important");
230
231 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Warning) );
232 }
233
234 wxLogWarning("%s warning", "Important");
235 CPPUNIT_ASSERT_EQUAL( "Important warning", m_log->GetLog(wxLOG_Warning) );
236}
237
c602c59b
VZ
238void LogTestCase::Component()
239{
240 wxLogMessage("Message");
241 CPPUNIT_ASSERT_EQUAL( wxLOG_COMPONENT,
242 m_log->GetInfo(wxLOG_Message).component );
243
244 // completely disable logging for this component
245 wxLog::SetComponentLevel("test/ignore", wxLOG_FatalError);
246
247 // but enable it for one of its subcomponents
248 wxLog::SetComponentLevel("test/ignore/not", wxLOG_Max);
249
250 #undef wxLOG_COMPONENT
251 #define wxLOG_COMPONENT "test/ignore"
252
253 // this shouldn't be output as this component is ignored
254 wxLogError("Error");
255 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Error) );
256
257 // and so are its subcomponents
258 #undef wxLOG_COMPONENT
259 #define wxLOG_COMPONENT "test/ignore/sub/subsub"
260 wxLogError("Error");
261 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Error) );
262
263 // but one subcomponent is not
264 #undef wxLOG_COMPONENT
265 #define wxLOG_COMPONENT "test/ignore/not"
266 wxLogError("Error");
267 CPPUNIT_ASSERT_EQUAL( "Error", m_log->GetLog(wxLOG_Error) );
268
269 // restore the original value
270 #undef wxLOG_COMPONENT
271 #define wxLOG_COMPONENT "test"
272}
273
ece5e6a4
VZ
274#if wxDEBUG_LEVEL
275
89d62794
VZ
276namespace
277{
278
279const char *TEST_MASK = "test";
280
281// this is a test vararg function (a real one, not a variadic-template-like as
282// wxVLogTrace(), so care should be taken with its arguments)
283void TraceTest(const char *format, ...)
284{
285 va_list argptr;
286 va_start(argptr, format);
287 wxVLogTrace(TEST_MASK, format, argptr);
288 va_end(argptr);
289}
290
291} // anonymous namespace
292
ece5e6a4
VZ
293void LogTestCase::Trace()
294{
89d62794
VZ
295 // we use wxLogTrace() or wxVLogTrace() from inside TraceTest()
296 // interchangeably here, it shouldn't make any difference
ece5e6a4
VZ
297
298 wxLogTrace(TEST_MASK, "Not shown");
299 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
300
301 wxLog::AddTraceMask(TEST_MASK);
89d62794 302 TraceTest("Shown");
ece5e6a4
VZ
303 CPPUNIT_ASSERT_EQUAL( wxString::Format("(%s) Shown", TEST_MASK),
304 m_log->GetLog(wxLOG_Trace) );
305
306 wxLog::RemoveTraceMask(TEST_MASK);
307 m_log->Clear();
308
89d62794 309 TraceTest("Not shown again");
ece5e6a4
VZ
310 CPPUNIT_ASSERT_EQUAL( "", m_log->GetLog(wxLOG_Trace) );
311}
312
313#endif // wxDEBUG_LEVEL
bc73d5ae
VZ
314
315#if WXWIN_COMPATIBILITY_2_8
316
317void LogTestCase::CompatLogger()
318{
319 CompatTestLog log;
320 wxLog * const logOld = wxLog::SetActiveTarget(&log);
321 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget, logOld );
322
323 wxLogError("Old error");
324 CPPUNIT_ASSERT_EQUAL( "Old error", log.GetLog(wxLOG_Error) );
325}
326
327void LogTestCase::CompatLogger2()
328{
329 CompatTestLog2 log;
330 wxLog * const logOld = wxLog::SetActiveTarget(&log);
331 wxON_BLOCK_EXIT1( wxLog::SetActiveTarget, logOld );
332
333 wxLogWarning("Old warning");
334 CPPUNIT_ASSERT_EQUAL( "Old warning", log.Get() );
335}
336
337#endif // WXWIN_COMPATIBILITY_2_8