]> git.saurik.com Git - wxWidgets.git/blame - tests/interactive/input.cpp
paint events cannot be constructed outside a native redraw in OSX
[wxWidgets.git] / tests / interactive / input.cpp
CommitLineData
81ec0e15
FM
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/interactive/input.cpp
3// Purpose: Miscellaneous tests requiring user input
4// Author: Francesco Montorsi (extracted from console sample)
5// Created: 2010-06-21
6// RCS-ID: $Id$
7// Copyright: (c) 2010 wxWidgets team
8///////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
18#include "testprec.h"
19
20#ifdef __BORLANDC__
21 #pragma hdrstop
22#endif
23
a2c8b984
SC
24#include "wx/app.h"
25#include "wx/wxcrt.h" // for wxPuts
26#include "wx/wxcrtvararg.h" // for wxPrintf
27
81ec0e15
FM
28// ----------------------------------------------------------------------------
29// conditional compilation
30// ----------------------------------------------------------------------------
31
32#define TEST_SNGLINST
33#define TEST_FTP
34#define TEST_INFO_FUNCTIONS
35#define TEST_REGEX
36#define TEST_DATETIME
37
38// ----------------------------------------------------------------------------
39// test class
40// ----------------------------------------------------------------------------
41
42class InteractiveInputTestCase : public CppUnit::TestCase
43{
44public:
45 InteractiveInputTestCase() { }
46
47private:
48 CPPUNIT_TEST_SUITE( InteractiveInputTestCase );
49 CPPUNIT_TEST( TestSingleIstance );
50 CPPUNIT_TEST( TestFtpInteractive );
51 CPPUNIT_TEST( TestDiskInfo );
52 CPPUNIT_TEST( TestRegExInteractive );
53 CPPUNIT_TEST( TestDateTimeInteractive );
54 CPPUNIT_TEST_SUITE_END();
55
56 void TestSingleIstance();
57 void TestFtpInteractive();
58 void TestDiskInfo();
59 void TestRegExInteractive();
60 void TestDateTimeInteractive();
61
62 wxDECLARE_NO_COPY_CLASS(InteractiveInputTestCase);
63};
64
65// ----------------------------------------------------------------------------
66// CppUnit macros
67// ----------------------------------------------------------------------------
68
69//CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveInputTestCase );
70 // do not run this test by default!
71
72CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveInputTestCase, "InteractiveInputTestCase" );
73
74// ============================================================================
75// implementation
76// ============================================================================
77
78// ----------------------------------------------------------------------------
79// misc information functions
80// ----------------------------------------------------------------------------
81
82#include "wx/utils.h"
83
84void InteractiveInputTestCase::TestDiskInfo()
85{
86#ifdef TEST_INFO_FUNCTIONS
87 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
88
89 for ( ;; )
90 {
91 wxChar pathname[128];
92 wxPrintf(wxT("Enter a directory name (press ENTER or type 'quit' to escape): "));
93 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
94 break;
95
96 // kill the last '\n'
97 pathname[wxStrlen(pathname) - 1] = 0;
98
99 if (pathname[0] == '\0' || wxStrcmp(pathname, "quit") == 0)
100 break;
101
102 wxLongLong total, free;
103 if ( !wxGetDiskSpace(pathname, &total, &free) )
104 {
105 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
106 }
107 else
108 {
109 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
110 (total / 1024).ToString().c_str(),
111 (free / 1024).ToString().c_str(),
112 pathname);
113 }
114
115 wxPuts("\n");
116 }
117
118 wxPuts("\n");
119#endif // TEST_INFO_FUNCTIONS
120}
121
122
123// ----------------------------------------------------------------------------
124// regular expressions
125// ----------------------------------------------------------------------------
126
127#include "wx/regex.h"
128
129void InteractiveInputTestCase::TestRegExInteractive()
130{
131#ifdef TEST_REGEX
132 wxPuts(wxT("*** Testing RE interactively ***"));
133
134 for ( ;; )
135 {
136 wxChar pattern[128];
137 wxPrintf(wxT("Enter a pattern (press ENTER or type 'quit' to escape): "));
138 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
139 break;
140
141 // kill the last '\n'
142 pattern[wxStrlen(pattern) - 1] = 0;
143
144 if (pattern[0] == '\0' || wxStrcmp(pattern, "quit") == 0)
145 break;
146
147 wxRegEx re;
148 if ( !re.Compile(pattern) )
149 {
150 continue;
151 }
152
153 wxChar text[128];
154 for ( ;; )
155 {
156 wxPrintf(wxT("Enter text to match: "));
157 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
158 break;
159
160 // kill the last '\n'
161 text[wxStrlen(text) - 1] = 0;
162
163 if ( !re.Matches(text) )
164 {
165 wxPrintf(wxT("No match.\n"));
166 }
167 else
168 {
169 wxPrintf(wxT("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
170
171 size_t start, len;
172 for ( size_t n = 1; ; n++ )
173 {
174 if ( !re.GetMatch(&start, &len, n) )
175 {
176 break;
177 }
178
179 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
180 n, wxString(text + start, len).c_str());
181 }
182 }
183 }
184
185 wxPuts("\n");
186 }
187#endif // TEST_REGEX
188}
189
190
191// ----------------------------------------------------------------------------
192// FTP
193// ----------------------------------------------------------------------------
194
195#include "wx/protocol/ftp.h"
196#include "wx/protocol/log.h"
197
198#define FTP_ANONYMOUS
199#ifdef FTP_ANONYMOUS
200 static const wxChar *hostname = wxT("ftp.wxwidgets.org");
201#else
202 static const wxChar *hostname = "localhost";
203#endif
204
205void InteractiveInputTestCase::TestFtpInteractive()
206{
207#ifdef TEST_FTP
208 wxFTP ftp;
209
210 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
211
212#ifdef FTP_ANONYMOUS
213 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
214#else // !FTP_ANONYMOUS
215 wxChar user[256];
216 wxFgets(user, WXSIZEOF(user), stdin);
217 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
218 ftp.SetUser(user);
219
220 wxChar password[256];
221 wxPrintf(wxT("Password for %s: "), password);
222 wxFgets(password, WXSIZEOF(password), stdin);
223 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
224 ftp.SetPassword(password);
225
226 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
227#endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
228
229 if ( !ftp.Connect(hostname) )
230 {
231 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname);
232
233 return;
234 }
235 else
236 {
237 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
238 hostname, ftp.Pwd().c_str());
239 }
240
241 wxChar buf[128];
242 for ( ;; )
243 {
244 wxPrintf(wxT("Enter FTP command (press ENTER or type 'quit' to escape): "));
245 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
246 break;
247
248 // kill the last '\n'
249 buf[wxStrlen(buf) - 1] = 0;
250
251 if (buf[0] == '\0' || wxStrcmp(buf, "quit") == 0)
252 break;
253
254 // special handling of LIST and NLST as they require data connection
255 wxString start(buf, 4);
256 start.MakeUpper();
257 if ( start == wxT("LIST") || start == wxT("NLST") )
258 {
259 wxString wildcard;
260 if ( wxStrlen(buf) > 4 )
261 wildcard = buf + 5;
262
263 wxArrayString files;
264 if ( !ftp.GetList(files, wildcard, start == wxT("LIST")) )
265 {
266 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start.c_str());
267 }
268 else
269 {
270 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
271 start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
272 size_t count = files.GetCount();
273 for ( size_t n = 0; n < count; n++ )
274 {
275 wxPrintf(wxT("\t%s\n"), files[n].c_str());
276 }
277 wxPuts(wxT("--- End of the file list"));
278 }
279 }
280 else // !list
281 {
282 wxChar ch = ftp.SendCommand(buf);
283 wxPrintf(wxT("Command %s"), ch ? wxT("succeeded") : wxT("failed"));
284 if ( ch )
285 {
286 wxPrintf(wxT(" (return code %c)"), ch);
287 }
288
289 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
290 }
291 }
292
293 wxPuts(wxT("\n"));
294#endif // TEST_FTP
295}
296
297// ----------------------------------------------------------------------------
298// date time
299// ----------------------------------------------------------------------------
300
301#include "wx/math.h"
302#include "wx/datetime.h"
303
304void InteractiveInputTestCase::TestDateTimeInteractive()
305{
306#ifdef TEST_DATETIME
307 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
308
309 wxChar buf[128];
310
311 for ( ;; )
312 {
313 wxPrintf(wxT("Enter a date (press ENTER or type 'quit' to escape): "));
314 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
315 break;
316
317 // kill the last '\n'
318 buf[wxStrlen(buf) - 1] = 0;
319
320 if ( buf[0] == '\0' || wxStrcmp(buf, "quit") == 0 )
321 break;
322
323 wxDateTime dt;
324 const wxChar *p = dt.ParseDate(buf);
325 if ( !p )
326 {
327 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf);
328
329 continue;
330 }
331 else if ( *p )
332 {
333 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p - buf);
334 }
335
336 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
337 dt.Format(wxT("%b %d, %Y")).c_str(),
338 dt.GetDayOfYear(),
339 dt.GetWeekOfMonth(wxDateTime::Monday_First),
340 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
341 dt.GetWeekOfYear(wxDateTime::Monday_First));
342 }
343
344 wxPuts("\n");
345#endif // TEST_DATETIME
346}
347
348
349// ----------------------------------------------------------------------------
350// single instance
351// ----------------------------------------------------------------------------
352
353#include "wx/snglinst.h"
354
355void InteractiveInputTestCase::TestSingleIstance()
356{
357#ifdef TEST_SNGLINST
358 wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
359
360 wxSingleInstanceChecker checker;
361 if ( checker.Create(wxT(".wxconsole.lock")) )
362 {
363 if ( checker.IsAnotherRunning() )
364 {
365 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
366 return;
367 }
368
369 // wait some time to give time to launch another instance
370 wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
371 wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
372 wxFgetc(stdin);
373 }
374 else // failed to create
375 {
376 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
377 }
378
379 wxPuts("\n");
380#endif // defined(TEST_SNGLINST)
381}
382