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)
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
24 #include "wx/wxcrt.h" // for wxPuts
25 #include "wx/wxcrtvararg.h" // for wxPrintf
27 // ----------------------------------------------------------------------------
28 // conditional compilation
29 // ----------------------------------------------------------------------------
33 #define TEST_INFO_FUNCTIONS
34 #define TEST_STACKWALKER
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class InteractiveOutputTestCase
: public CppUnit::TestCase
45 InteractiveOutputTestCase() { }
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();
61 void TestDllListLoaded();
63 void TestMimeAssociate();
64 void TestMimeFilename();
66 void TestPlatformInfo();
69 void TestStandardPaths();
72 wxDECLARE_NO_COPY_CLASS(InteractiveOutputTestCase
);
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveOutputTestCase );
80 // do not run this test by default!
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveOutputTestCase
, "InteractiveOutputTestCase" );
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 #if !defined(__WINDOWS__) && !defined(__UNIX__)
96 #include "wx/dynlib.h"
98 void InteractiveOutputTestCase::TestDllListLoaded()
101 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
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
)
108 const wxDynamicLibraryDetails
& details
= dlls
[n
];
109 printf("%-45s", (const char *)details
.GetPath().mb_str());
111 void *addr
wxDUMMY_INITIALIZE(NULL
);
112 size_t len
wxDUMMY_INITIALIZE(0);
113 if ( details
.GetAddress(&addr
, &len
) )
115 printf(" %08lx:%08lx",
116 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
119 printf(" %s\n", (const char *)details
.GetVersion().mb_str());
122 wxPuts(wxEmptyString
);
123 #endif // TEST_DYNLIB
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 #include "wx/mimetype.h"
133 void InteractiveOutputTestCase::TestMimeEnum()
136 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
138 wxArrayString mimetypes
;
140 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
142 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count
);
147 for ( size_t n
= 0; n
< count
; n
++ )
149 wxFileType
*filetype
=
150 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
153 wxPrintf(wxT(" nothing known about the filetype '%s'!\n"),
154 mimetypes
[n
].c_str());
158 filetype
->GetDescription(&desc
);
159 filetype
->GetExtensions(exts
);
161 filetype
->GetIcon(NULL
);
164 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
167 extsAll
<< wxT(", ");
171 wxPrintf(wxT(" %s: %s (%s)\n"),
172 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
175 wxPuts(wxEmptyString
);
179 void InteractiveOutputTestCase::TestMimeFilename()
182 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
184 static const wxChar
*filenames
[] =
192 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
194 const wxString fname
= filenames
[n
];
195 wxString ext
= fname
.AfterLast(wxT('.'));
196 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
199 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
204 if ( !ft
->GetDescription(&desc
) )
205 desc
= wxT("<no description>");
208 if ( !ft
->GetOpenCommand(&cmd
,
209 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
210 cmd
= wxT("<no command available>");
212 cmd
= wxString(wxT('"')) + cmd
+ wxT('"');
214 wxPrintf(wxT("To open %s (%s) run:\n %s\n"),
215 fname
.c_str(), desc
.c_str(), cmd
.c_str());
221 wxPuts(wxEmptyString
);
225 void InteractiveOutputTestCase::TestMimeAssociate()
228 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
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
236 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
239 wxPuts(wxT("ERROR: failed to create association!"));
243 // TODO: read it back
247 wxPuts(wxEmptyString
);
252 // ----------------------------------------------------------------------------
253 // misc information functions
254 // ----------------------------------------------------------------------------
256 #include "wx/utils.h"
258 void InteractiveOutputTestCase::TestOsInfo()
260 #ifdef TEST_INFO_FUNCTIONS
261 wxPuts(wxT("*** Testing OS info functions ***\n"));
264 wxGetOsVersion(&major
, &minor
);
265 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
266 wxGetOsDescription().c_str(), major
, minor
);
268 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
270 wxPrintf(wxT("Host name is %s (%s).\n"),
271 wxGetHostName().c_str(), wxGetFullHostName().c_str());
273 wxPuts(wxEmptyString
);
274 #endif // TEST_INFO_FUNCTIONS
277 void InteractiveOutputTestCase::TestPlatformInfo()
279 #ifdef TEST_INFO_FUNCTIONS
280 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
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());
292 wxPuts(wxEmptyString
);
293 #endif // TEST_INFO_FUNCTIONS
296 void InteractiveOutputTestCase::TestUserInfo()
298 #ifdef TEST_INFO_FUNCTIONS
299 wxPuts(wxT("*** Testing user info functions ***\n"));
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());
306 wxPuts(wxEmptyString
);
307 #endif // TEST_INFO_FUNCTIONS
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 #if wxUSE_STACKWALKER
317 #include "wx/stackwalk.h"
319 class StackDump
: public wxStackWalker
322 StackDump(const char *argv0
)
323 : wxStackWalker(argv0
)
327 virtual void Walk(size_t skip
= 1, size_t maxdepth
= wxSTACKWALKER_MAX_DEPTH
)
329 wxPuts(wxT("Stack dump:"));
331 wxStackWalker::Walk(skip
, maxdepth
);
335 virtual void OnStackFrame(const wxStackFrame
& frame
)
337 printf("[%2d] ", (int) frame
.GetLevel());
339 wxString name
= frame
.GetName();
342 printf("%-20.40s", (const char*)name
.mb_str());
346 printf("0x%08lx", (unsigned long)frame
.GetAddress());
349 if ( frame
.HasSourceLocation() )
352 (const char*)frame
.GetFileName().mb_str(),
353 (int)frame
.GetLine());
359 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
361 printf("\t%s %s = %s\n", (const char*)type
.mb_str(),
362 (const char*)name
.mb_str(),
363 (const char*)val
.mb_str());
369 void InteractiveOutputTestCase::TestStackWalk()
371 #ifdef TEST_STACKWALKER
372 #if wxUSE_STACKWALKER
373 wxPuts(wxT("*** Testing wxStackWalker ***"));
375 wxString
progname(wxTheApp
->argv
[0]);
376 StackDump
dump(progname
.utf8_str());
381 #endif // TEST_STACKWALKER
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 #include "wx/stdpaths.h"
390 #include "wx/wxchar.h" // wxPrintf
392 void InteractiveOutputTestCase::TestStandardPaths()
395 wxPuts(wxT("*** Testing wxStandardPaths ***"));
397 wxTheApp
->SetAppName(wxT("console"));
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
416 wxStandardPaths::ResourceCat_Messages
420 #endif // TEST_STDPATHS
424 // ----------------------------------------------------------------------------
426 // ----------------------------------------------------------------------------
428 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
434 #include "wx/volume.h"
435 static const wxChar
*volumeKinds
[] =
441 wxT("network volume"),
447 void InteractiveOutputTestCase::TestFSVolume()
450 wxPuts(wxT("*** Testing wxFSVolume class ***"));
452 wxArrayString volumes
= wxFSVolume::GetVolumes();
453 size_t count
= volumes
.GetCount();
457 wxPuts(wxT("ERROR: no mounted volumes?"));
461 wxPrintf(wxT("%u mounted volumes found:\n"), count
);
463 for ( size_t n
= 0; n
< count
; n
++ )
465 wxFSVolume
vol(volumes
[n
]);
468 wxPuts(wxT("ERROR: couldn't create volume"));
472 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
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")
483 #endif // TEST_VOLUME