move console sample's interactive tests to two different CppUnit testsuites: Interact...
[wxWidgets.git] / tests / interactive / output.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/interactive/output.cpp
3 // Purpose: Miscellaneous tests NOT requiring user input, just user checks
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
24 // ----------------------------------------------------------------------------
25 // conditional compilation
26 // ----------------------------------------------------------------------------
27
28 #define TEST_DYNLIB
29 #define TEST_MIME
30 #define TEST_INFO_FUNCTIONS
31 #define TEST_STACKWALKER
32 #define TEST_STDPATHS
33 #define TEST_VOLUME
34
35 // ----------------------------------------------------------------------------
36 // test class
37 // ----------------------------------------------------------------------------
38
39 class InteractiveOutputTestCase : public CppUnit::TestCase
40 {
41 public:
42 InteractiveOutputTestCase() { }
43
44 private:
45 CPPUNIT_TEST_SUITE( InteractiveOutputTestCase );
46 CPPUNIT_TEST( TestDllListLoaded );
47 CPPUNIT_TEST( TestMimeEnum );
48 CPPUNIT_TEST( TestMimeAssociate );
49 CPPUNIT_TEST( TestMimeFilename );
50 CPPUNIT_TEST( TestOsInfo );
51 CPPUNIT_TEST( TestPlatformInfo );
52 CPPUNIT_TEST( TestUserInfo );
53 CPPUNIT_TEST( TestStackWalk );
54 CPPUNIT_TEST( TestStandardPaths );
55 CPPUNIT_TEST( TestFSVolume );
56 CPPUNIT_TEST_SUITE_END();
57
58 void TestDllListLoaded();
59 void TestMimeEnum();
60 void TestMimeAssociate();
61 void TestMimeFilename();
62 void TestOsInfo();
63 void TestPlatformInfo();
64 void TestUserInfo();
65 void TestStackWalk();
66 void TestStandardPaths();
67 void TestFSVolume();
68
69 wxDECLARE_NO_COPY_CLASS(InteractiveOutputTestCase);
70 };
71
72 // ----------------------------------------------------------------------------
73 // CppUnit macros
74 // ----------------------------------------------------------------------------
75
76 //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveOutputTestCase );
77 // do not run this test by default!
78
79 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveOutputTestCase, "InteractiveOutputTestCase" );
80
81 // ============================================================================
82 // implementation
83 // ============================================================================
84
85 // ----------------------------------------------------------------------------
86 // wxDllLoader
87 // ----------------------------------------------------------------------------
88
89 #if !defined(__WXMSW__) && !defined(__UNIX__)
90 #undef TEST_DYNLIB
91 #endif
92
93 #include "wx/dynlib.h"
94
95 void InteractiveOutputTestCase::TestDllListLoaded()
96 {
97 #ifdef TEST_DYNLIB
98 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
99
100 wxPuts("Loaded modules:");
101 wxDynamicLibraryDetailsArray dlls = wxDynamicLibrary::ListLoaded();
102 const size_t count = dlls.GetCount();
103 for ( size_t n = 0; n < count; ++n )
104 {
105 const wxDynamicLibraryDetails& details = dlls[n];
106 printf("%-45s", (const char *)details.GetPath().mb_str());
107
108 void *addr wxDUMMY_INITIALIZE(NULL);
109 size_t len wxDUMMY_INITIALIZE(0);
110 if ( details.GetAddress(&addr, &len) )
111 {
112 printf(" %08lx:%08lx",
113 (unsigned long)addr, (unsigned long)((char *)addr + len));
114 }
115
116 printf(" %s\n", (const char *)details.GetVersion().mb_str());
117 }
118
119 wxPuts(wxEmptyString);
120 #endif // TEST_DYNLIB
121 }
122
123
124 // ----------------------------------------------------------------------------
125 // MIME types
126 // ----------------------------------------------------------------------------
127
128 #include "wx/mimetype.h"
129
130 void InteractiveOutputTestCase::TestMimeEnum()
131 {
132 #ifdef TEST_MIME
133 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
134
135 wxArrayString mimetypes;
136
137 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
138
139 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count);
140
141 wxArrayString exts;
142 wxString desc;
143
144 for ( size_t n = 0; n < count; n++ )
145 {
146 wxFileType *filetype =
147 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
148 if ( !filetype )
149 {
150 wxPrintf(wxT(" nothing known about the filetype '%s'!\n"),
151 mimetypes[n].c_str());
152 continue;
153 }
154
155 filetype->GetDescription(&desc);
156 filetype->GetExtensions(exts);
157
158 filetype->GetIcon(NULL);
159
160 wxString extsAll;
161 for ( size_t e = 0; e < exts.GetCount(); e++ )
162 {
163 if ( e > 0 )
164 extsAll << wxT(", ");
165 extsAll += exts[e];
166 }
167
168 wxPrintf(wxT(" %s: %s (%s)\n"),
169 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
170 }
171
172 wxPuts(wxEmptyString);
173 #endif // TEST_MIME
174 }
175
176 void InteractiveOutputTestCase::TestMimeFilename()
177 {
178 #ifdef TEST_MIME
179 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
180
181 static const wxChar *filenames[] =
182 {
183 wxT("readme.txt"),
184 wxT("document.pdf"),
185 wxT("image.gif"),
186 wxT("picture.jpeg"),
187 };
188
189 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
190 {
191 const wxString fname = filenames[n];
192 wxString ext = fname.AfterLast(wxT('.'));
193 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
194 if ( !ft )
195 {
196 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext.c_str());
197 }
198 else
199 {
200 wxString desc;
201 if ( !ft->GetDescription(&desc) )
202 desc = wxT("<no description>");
203
204 wxString cmd;
205 if ( !ft->GetOpenCommand(&cmd,
206 wxFileType::MessageParameters(fname, wxEmptyString)) )
207 cmd = wxT("<no command available>");
208 else
209 cmd = wxString(wxT('"')) + cmd + wxT('"');
210
211 wxPrintf(wxT("To open %s (%s) run:\n %s\n"),
212 fname.c_str(), desc.c_str(), cmd.c_str());
213
214 delete ft;
215 }
216 }
217
218 wxPuts(wxEmptyString);
219 #endif // TEST_MIME
220 }
221
222 void InteractiveOutputTestCase::TestMimeAssociate()
223 {
224 #ifdef TEST_MIME
225 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
226
227 wxFileTypeInfo ftInfo(
228 wxT("application/x-xyz"),
229 wxT("xyzview '%s'"), // open cmd
230 wxT(""), // print cmd
231 wxT("XYZ File"), // description
232 wxT(".xyz"), // extensions
233 wxNullPtr // end of extensions
234 );
235 ftInfo.SetShortDesc(wxT("XYZFile")); // used under Win32 only
236
237 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
238 if ( !ft )
239 {
240 wxPuts(wxT("ERROR: failed to create association!"));
241 }
242 else
243 {
244 // TODO: read it back
245 delete ft;
246 }
247
248 wxPuts(wxEmptyString);
249 #endif // TEST_MIME
250 }
251
252
253 // ----------------------------------------------------------------------------
254 // misc information functions
255 // ----------------------------------------------------------------------------
256
257 #include "wx/utils.h"
258
259 void InteractiveOutputTestCase::TestOsInfo()
260 {
261 #ifdef TEST_INFO_FUNCTIONS
262 wxPuts(wxT("*** Testing OS info functions ***\n"));
263
264 int major, minor;
265 wxGetOsVersion(&major, &minor);
266 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
267 wxGetOsDescription().c_str(), major, minor);
268
269 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
270
271 wxPrintf(wxT("Host name is %s (%s).\n"),
272 wxGetHostName().c_str(), wxGetFullHostName().c_str());
273
274 wxPuts(wxEmptyString);
275 #endif // TEST_INFO_FUNCTIONS
276 }
277
278 void InteractiveOutputTestCase::TestPlatformInfo()
279 {
280 #ifdef TEST_INFO_FUNCTIONS
281 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
282
283 // get this platform
284 wxPlatformInfo plat;
285
286 wxPrintf(wxT("Operating system family name is: %s\n"), plat.GetOperatingSystemFamilyName().c_str());
287 wxPrintf(wxT("Operating system name is: %s\n"), plat.GetOperatingSystemIdName().c_str());
288 wxPrintf(wxT("Port ID name is: %s\n"), plat.GetPortIdName().c_str());
289 wxPrintf(wxT("Port ID short name is: %s\n"), plat.GetPortIdShortName().c_str());
290 wxPrintf(wxT("Architecture is: %s\n"), plat.GetArchName().c_str());
291 wxPrintf(wxT("Endianness is: %s\n"), plat.GetEndiannessName().c_str());
292
293 wxPuts(wxEmptyString);
294 #endif // TEST_INFO_FUNCTIONS
295 }
296
297 void InteractiveOutputTestCase::TestUserInfo()
298 {
299 #ifdef TEST_INFO_FUNCTIONS
300 wxPuts(wxT("*** Testing user info functions ***\n"));
301
302 wxPrintf(wxT("User id is:\t%s\n"), wxGetUserId().c_str());
303 wxPrintf(wxT("User name is:\t%s\n"), wxGetUserName().c_str());
304 wxPrintf(wxT("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
305 wxPrintf(wxT("Email address:\t%s\n"), wxGetEmailAddress().c_str());
306
307 wxPuts(wxEmptyString);
308 #endif // TEST_INFO_FUNCTIONS
309 }
310
311
312 // ----------------------------------------------------------------------------
313 // stack backtrace
314 // ----------------------------------------------------------------------------
315
316 #include "wx/stackwalk.h"
317
318 class StackDump : public wxStackWalker
319 {
320 public:
321 StackDump(const char *argv0)
322 : wxStackWalker(argv0)
323 {
324 }
325
326 virtual void Walk(size_t skip = 1, size_t maxdepth = wxSTACKWALKER_MAX_DEPTH)
327 {
328 wxPuts(wxT("Stack dump:"));
329
330 wxStackWalker::Walk(skip, maxdepth);
331 }
332
333 protected:
334 virtual void OnStackFrame(const wxStackFrame& frame)
335 {
336 printf("[%2d] ", (int) frame.GetLevel());
337
338 wxString name = frame.GetName();
339 if ( !name.empty() )
340 {
341 printf("%-20.40s", (const char*)name.mb_str());
342 }
343 else
344 {
345 printf("0x%08lx", (unsigned long)frame.GetAddress());
346 }
347
348 if ( frame.HasSourceLocation() )
349 {
350 printf("\t%s:%d",
351 (const char*)frame.GetFileName().mb_str(),
352 (int)frame.GetLine());
353 }
354
355 puts("");
356
357 wxString type, val;
358 for ( size_t n = 0; frame.GetParam(n, &type, &name, &val); n++ )
359 {
360 printf("\t%s %s = %s\n", (const char*)type.mb_str(),
361 (const char*)name.mb_str(),
362 (const char*)val.mb_str());
363 }
364 }
365 };
366
367 void InteractiveOutputTestCase::TestStackWalk()
368 {
369 #ifdef TEST_STACKWALKER
370 wxPuts(wxT("*** Testing wxStackWalker ***"));
371
372 StackDump dump(wxTheApp->argv[0]);
373 dump.Walk();
374
375 wxPuts("\n");
376 #endif // TEST_STACKWALKER
377 }
378
379
380 // ----------------------------------------------------------------------------
381 // standard paths
382 // ----------------------------------------------------------------------------
383
384
385 #include "wx/stdpaths.h"
386 #include "wx/wxchar.h" // wxPrintf
387
388 void InteractiveOutputTestCase::TestStandardPaths()
389 {
390 #ifdef TEST_STDPATHS
391 wxPuts(wxT("*** Testing wxStandardPaths ***"));
392
393 wxTheApp->SetAppName(wxT("console"));
394
395 wxStandardPathsBase& stdp = wxStandardPaths::Get();
396 wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str());
397 wxPrintf(wxT("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str());
398 wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str());
399 wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str());
400 wxPrintf(wxT("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str());
401 wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str());
402 wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str());
403 wxPrintf(wxT("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str());
404 wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
405 wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str());
406 wxPrintf(wxT("Localized res. dir:\t%s\n"),
407 stdp.GetLocalizedResourcesDir(wxT("fr")).c_str());
408 wxPrintf(wxT("Message catalogs dir:\t%s\n"),
409 stdp.GetLocalizedResourcesDir
410 (
411 wxT("fr"),
412 wxStandardPaths::ResourceCat_Messages
413 ).c_str());
414
415 wxPuts("\n");
416 #endif // TEST_STDPATHS
417 }
418
419
420 // ----------------------------------------------------------------------------
421 // wxVolume tests
422 // ----------------------------------------------------------------------------
423
424 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
425 #undef TEST_VOLUME
426 #endif
427
428 #include "wx/volume.h"
429
430 static const wxChar *volumeKinds[] =
431 {
432 wxT("floppy"),
433 wxT("hard disk"),
434 wxT("CD-ROM"),
435 wxT("DVD-ROM"),
436 wxT("network volume"),
437 wxT("other volume"),
438 };
439
440 void InteractiveOutputTestCase::TestFSVolume()
441 {
442 #ifdef TEST_VOLUME
443 wxPuts(wxT("*** Testing wxFSVolume class ***"));
444
445 wxArrayString volumes = wxFSVolume::GetVolumes();
446 size_t count = volumes.GetCount();
447
448 if ( !count )
449 {
450 wxPuts(wxT("ERROR: no mounted volumes?"));
451 return;
452 }
453
454 wxPrintf(wxT("%u mounted volumes found:\n"), count);
455
456 for ( size_t n = 0; n < count; n++ )
457 {
458 wxFSVolume vol(volumes[n]);
459 if ( !vol.IsOk() )
460 {
461 wxPuts(wxT("ERROR: couldn't create volume"));
462 continue;
463 }
464
465 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
466 n + 1,
467 vol.GetDisplayName().c_str(),
468 vol.GetName().c_str(),
469 volumeKinds[vol.GetKind()],
470 vol.IsWritable() ? wxT("rw") : wxT("ro"),
471 vol.GetFlags() & wxFS_VOL_REMOVABLE ? wxT("removable")
472 : wxT("fixed"));
473 }
474
475 wxPuts("\n");
476 #endif // TEST_VOLUME
477 }
478