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