Rewrite wxExecute() implementation under Unix.
[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 #include "wx/wxcrt.h" // for wxPuts
26 #include "wx/wxcrtvararg.h" // for wxPrintf
27
28 // ----------------------------------------------------------------------------
29 // conditional compilation
30 // ----------------------------------------------------------------------------
31
32 #define TEST_DYNLIB
33 #define TEST_MIME
34 #define TEST_INFO_FUNCTIONS
35 #define TEST_STACKWALKER
36 #define TEST_STDPATHS
37 #define TEST_VOLUME
38
39 // ----------------------------------------------------------------------------
40 // test class
41 // ----------------------------------------------------------------------------
42
43 class InteractiveOutputTestCase : public CppUnit::TestCase
44 {
45 public:
46 InteractiveOutputTestCase() { }
47
48 private:
49 CPPUNIT_TEST_SUITE( InteractiveOutputTestCase );
50 CPPUNIT_TEST( TestDllListLoaded );
51 CPPUNIT_TEST( TestMimeEnum );
52 CPPUNIT_TEST( TestMimeAssociate );
53 CPPUNIT_TEST( TestMimeFilename );
54 CPPUNIT_TEST( TestOsInfo );
55 CPPUNIT_TEST( TestPlatformInfo );
56 CPPUNIT_TEST( TestUserInfo );
57 CPPUNIT_TEST( TestStackWalk );
58 CPPUNIT_TEST( TestStandardPaths );
59 CPPUNIT_TEST( TestFSVolume );
60 CPPUNIT_TEST_SUITE_END();
61
62 void TestDllListLoaded();
63 void TestMimeEnum();
64 void TestMimeAssociate();
65 void TestMimeFilename();
66 void TestOsInfo();
67 void TestPlatformInfo();
68 void TestUserInfo();
69 void TestStackWalk();
70 void TestStandardPaths();
71 void TestFSVolume();
72
73 wxDECLARE_NO_COPY_CLASS(InteractiveOutputTestCase);
74 };
75
76 // ----------------------------------------------------------------------------
77 // CppUnit macros
78 // ----------------------------------------------------------------------------
79
80 //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveOutputTestCase );
81 // do not run this test by default!
82
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveOutputTestCase, "InteractiveOutputTestCase" );
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 // ----------------------------------------------------------------------------
90 // wxDllLoader
91 // ----------------------------------------------------------------------------
92
93 #if !defined(__WINDOWS__) && !defined(__UNIX__)
94 #undef TEST_DYNLIB
95 #endif
96
97 #include "wx/dynlib.h"
98
99 void InteractiveOutputTestCase::TestDllListLoaded()
100 {
101 #ifdef TEST_DYNLIB
102 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
103
104 wxPuts("Loaded modules:");
105 wxDynamicLibraryDetailsArray dlls = wxDynamicLibrary::ListLoaded();
106 const size_t count = dlls.GetCount();
107 for ( size_t n = 0; n < count; ++n )
108 {
109 const wxDynamicLibraryDetails& details = dlls[n];
110 printf("%-45s", (const char *)details.GetPath().mb_str());
111
112 void *addr wxDUMMY_INITIALIZE(NULL);
113 size_t len wxDUMMY_INITIALIZE(0);
114 if ( details.GetAddress(&addr, &len) )
115 {
116 printf(" %08lx:%08lx",
117 (unsigned long)addr, (unsigned long)((char *)addr + len));
118 }
119
120 printf(" %s\n", (const char *)details.GetVersion().mb_str());
121 }
122
123 wxPuts(wxEmptyString);
124 #endif // TEST_DYNLIB
125 }
126
127
128 // ----------------------------------------------------------------------------
129 // MIME types
130 // ----------------------------------------------------------------------------
131
132 #include "wx/mimetype.h"
133
134 void InteractiveOutputTestCase::TestMimeEnum()
135 {
136 #ifdef TEST_MIME
137 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
138
139 wxArrayString mimetypes;
140
141 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
142
143 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count);
144
145 wxArrayString exts;
146 wxString desc;
147
148 for ( size_t n = 0; n < count; n++ )
149 {
150 wxFileType *filetype =
151 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
152 if ( !filetype )
153 {
154 wxPrintf(wxT(" nothing known about the filetype '%s'!\n"),
155 mimetypes[n].c_str());
156 continue;
157 }
158
159 filetype->GetDescription(&desc);
160 filetype->GetExtensions(exts);
161
162 filetype->GetIcon(NULL);
163
164 wxString extsAll;
165 for ( size_t e = 0; e < exts.GetCount(); e++ )
166 {
167 if ( e > 0 )
168 extsAll << wxT(", ");
169 extsAll += exts[e];
170 }
171
172 wxPrintf(wxT(" %s: %s (%s)\n"),
173 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
174 }
175
176 wxPuts(wxEmptyString);
177 #endif // TEST_MIME
178 }
179
180 void InteractiveOutputTestCase::TestMimeFilename()
181 {
182 #ifdef TEST_MIME
183 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
184
185 static const wxChar *filenames[] =
186 {
187 wxT("readme.txt"),
188 wxT("document.pdf"),
189 wxT("image.gif"),
190 wxT("picture.jpeg"),
191 };
192
193 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
194 {
195 const wxString fname = filenames[n];
196 wxString ext = fname.AfterLast(wxT('.'));
197 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
198 if ( !ft )
199 {
200 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext.c_str());
201 }
202 else
203 {
204 wxString desc;
205 if ( !ft->GetDescription(&desc) )
206 desc = wxT("<no description>");
207
208 wxString cmd;
209 if ( !ft->GetOpenCommand(&cmd,
210 wxFileType::MessageParameters(fname, wxEmptyString)) )
211 cmd = wxT("<no command available>");
212 else
213 cmd = wxString(wxT('"')) + cmd + wxT('"');
214
215 wxPrintf(wxT("To open %s (%s) run:\n %s\n"),
216 fname.c_str(), desc.c_str(), cmd.c_str());
217
218 delete ft;
219 }
220 }
221
222 wxPuts(wxEmptyString);
223 #endif // TEST_MIME
224 }
225
226 void InteractiveOutputTestCase::TestMimeAssociate()
227 {
228 #ifdef TEST_MIME
229 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
230
231 wxFileTypeInfo ftInfo("application/x-xyz");
232 ftInfo.SetOpenCommand("xyzview '%s'");
233 ftInfo.SetDescription("XYZ File");
234 ftInfo.AddExtension(".xyz");
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 #if wxUSE_STACKWALKER
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 #endif
369
370 void InteractiveOutputTestCase::TestStackWalk()
371 {
372 #ifdef TEST_STACKWALKER
373 #if wxUSE_STACKWALKER
374 wxPuts(wxT("*** Testing wxStackWalker ***"));
375
376 wxString progname(wxTheApp->argv[0]);
377 StackDump dump(progname.utf8_str());
378 dump.Walk();
379
380 wxPuts("\n");
381 #endif
382 #endif // TEST_STACKWALKER
383 }
384
385
386 // ----------------------------------------------------------------------------
387 // standard paths
388 // ----------------------------------------------------------------------------
389
390 #include "wx/stdpaths.h"
391 #include "wx/wxchar.h" // wxPrintf
392
393 void InteractiveOutputTestCase::TestStandardPaths()
394 {
395 #ifdef TEST_STDPATHS
396 wxPuts(wxT("*** Testing wxStandardPaths ***"));
397
398 wxTheApp->SetAppName(wxT("console"));
399
400 wxStandardPathsBase& stdp = wxStandardPaths::Get();
401 wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str());
402 wxPrintf(wxT("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str());
403 wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str());
404 wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str());
405 wxPrintf(wxT("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str());
406 wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str());
407 wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str());
408 wxPrintf(wxT("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str());
409 wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
410 wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str());
411 wxPrintf(wxT("Localized res. dir:\t%s\n"),
412 stdp.GetLocalizedResourcesDir(wxT("fr")).c_str());
413 wxPrintf(wxT("Message catalogs dir:\t%s\n"),
414 stdp.GetLocalizedResourcesDir
415 (
416 wxT("fr"),
417 wxStandardPaths::ResourceCat_Messages
418 ).c_str());
419
420 wxPuts("\n");
421 #endif // TEST_STDPATHS
422 }
423
424
425 // ----------------------------------------------------------------------------
426 // wxVolume tests
427 // ----------------------------------------------------------------------------
428
429 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
430 #undef TEST_VOLUME
431 #endif
432
433 #ifdef TEST_VOLUME
434
435 #include "wx/volume.h"
436 static const wxChar *volumeKinds[] =
437 {
438 wxT("floppy"),
439 wxT("hard disk"),
440 wxT("CD-ROM"),
441 wxT("DVD-ROM"),
442 wxT("network volume"),
443 wxT("other volume"),
444 };
445
446 #endif
447
448 void InteractiveOutputTestCase::TestFSVolume()
449 {
450 #ifdef TEST_VOLUME
451 wxPuts(wxT("*** Testing wxFSVolume class ***"));
452
453 wxArrayString volumes = wxFSVolume::GetVolumes();
454 size_t count = volumes.GetCount();
455
456 if ( !count )
457 {
458 wxPuts(wxT("ERROR: no mounted volumes?"));
459 return;
460 }
461
462 wxPrintf(wxT("%u mounted volumes found:\n"), count);
463
464 for ( size_t n = 0; n < count; n++ )
465 {
466 wxFSVolume vol(volumes[n]);
467 if ( !vol.IsOk() )
468 {
469 wxPuts(wxT("ERROR: couldn't create volume"));
470 continue;
471 }
472
473 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
474 n + 1,
475 vol.GetDisplayName().c_str(),
476 vol.GetName().c_str(),
477 volumeKinds[vol.GetKind()],
478 vol.IsWritable() ? wxT("rw") : wxT("ro"),
479 vol.GetFlags() & wxFS_VOL_REMOVABLE ? wxT("removable")
480 : wxT("fixed"));
481 }
482
483 wxPuts("\n");
484 #endif // TEST_VOLUME
485 }
486