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