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)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
25 #include "wx/wxcrt.h" // for wxPuts
26 #include "wx/wxcrtvararg.h" // for wxPrintf
28 // ----------------------------------------------------------------------------
29 // conditional compilation
30 // ----------------------------------------------------------------------------
34 #define TEST_INFO_FUNCTIONS
35 #define TEST_STACKWALKER
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 class InteractiveOutputTestCase
: public CppUnit::TestCase
46 InteractiveOutputTestCase() { }
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();
62 void TestDllListLoaded();
64 void TestMimeAssociate();
65 void TestMimeFilename();
67 void TestPlatformInfo();
70 void TestStandardPaths();
73 wxDECLARE_NO_COPY_CLASS(InteractiveOutputTestCase
);
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveOutputTestCase );
81 // do not run this test by default!
83 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveOutputTestCase
, "InteractiveOutputTestCase" );
85 // ============================================================================
87 // ============================================================================
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 #if !defined(__WXMSW__) && !defined(__UNIX__)
97 #include "wx/dynlib.h"
99 void InteractiveOutputTestCase::TestDllListLoaded()
102 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
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
)
109 const wxDynamicLibraryDetails
& details
= dlls
[n
];
110 printf("%-45s", (const char *)details
.GetPath().mb_str());
112 void *addr
wxDUMMY_INITIALIZE(NULL
);
113 size_t len
wxDUMMY_INITIALIZE(0);
114 if ( details
.GetAddress(&addr
, &len
) )
116 printf(" %08lx:%08lx",
117 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
120 printf(" %s\n", (const char *)details
.GetVersion().mb_str());
123 wxPuts(wxEmptyString
);
124 #endif // TEST_DYNLIB
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 #include "wx/mimetype.h"
134 void InteractiveOutputTestCase::TestMimeEnum()
137 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
139 wxArrayString mimetypes
;
141 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
143 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count
);
148 for ( size_t n
= 0; n
< count
; n
++ )
150 wxFileType
*filetype
=
151 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
154 wxPrintf(wxT(" nothing known about the filetype '%s'!\n"),
155 mimetypes
[n
].c_str());
159 filetype
->GetDescription(&desc
);
160 filetype
->GetExtensions(exts
);
162 filetype
->GetIcon(NULL
);
165 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
168 extsAll
<< wxT(", ");
172 wxPrintf(wxT(" %s: %s (%s)\n"),
173 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
176 wxPuts(wxEmptyString
);
180 void InteractiveOutputTestCase::TestMimeFilename()
183 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
185 static const wxChar
*filenames
[] =
193 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
195 const wxString fname
= filenames
[n
];
196 wxString ext
= fname
.AfterLast(wxT('.'));
197 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
200 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
205 if ( !ft
->GetDescription(&desc
) )
206 desc
= wxT("<no description>");
209 if ( !ft
->GetOpenCommand(&cmd
,
210 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
211 cmd
= wxT("<no command available>");
213 cmd
= wxString(wxT('"')) + cmd
+ wxT('"');
215 wxPrintf(wxT("To open %s (%s) run:\n %s\n"),
216 fname
.c_str(), desc
.c_str(), cmd
.c_str());
222 wxPuts(wxEmptyString
);
226 void InteractiveOutputTestCase::TestMimeAssociate()
229 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
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
237 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
240 wxPuts(wxT("ERROR: failed to create association!"));
244 // TODO: read it back
248 wxPuts(wxEmptyString
);
253 // ----------------------------------------------------------------------------
254 // misc information functions
255 // ----------------------------------------------------------------------------
257 #include "wx/utils.h"
259 void InteractiveOutputTestCase::TestOsInfo()
261 #ifdef TEST_INFO_FUNCTIONS
262 wxPuts(wxT("*** Testing OS info functions ***\n"));
265 wxGetOsVersion(&major
, &minor
);
266 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
267 wxGetOsDescription().c_str(), major
, minor
);
269 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
271 wxPrintf(wxT("Host name is %s (%s).\n"),
272 wxGetHostName().c_str(), wxGetFullHostName().c_str());
274 wxPuts(wxEmptyString
);
275 #endif // TEST_INFO_FUNCTIONS
278 void InteractiveOutputTestCase::TestPlatformInfo()
280 #ifdef TEST_INFO_FUNCTIONS
281 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
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());
293 wxPuts(wxEmptyString
);
294 #endif // TEST_INFO_FUNCTIONS
297 void InteractiveOutputTestCase::TestUserInfo()
299 #ifdef TEST_INFO_FUNCTIONS
300 wxPuts(wxT("*** Testing user info functions ***\n"));
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());
307 wxPuts(wxEmptyString
);
308 #endif // TEST_INFO_FUNCTIONS
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 #if wxUSE_STACKWALKER
318 #include "wx/stackwalk.h"
320 class StackDump
: public wxStackWalker
323 StackDump(const char *argv0
)
324 : wxStackWalker(argv0
)
328 virtual void Walk(size_t skip
= 1, size_t maxdepth
= wxSTACKWALKER_MAX_DEPTH
)
330 wxPuts(wxT("Stack dump:"));
332 wxStackWalker::Walk(skip
, maxdepth
);
336 virtual void OnStackFrame(const wxStackFrame
& frame
)
338 printf("[%2d] ", (int) frame
.GetLevel());
340 wxString name
= frame
.GetName();
343 printf("%-20.40s", (const char*)name
.mb_str());
347 printf("0x%08lx", (unsigned long)frame
.GetAddress());
350 if ( frame
.HasSourceLocation() )
353 (const char*)frame
.GetFileName().mb_str(),
354 (int)frame
.GetLine());
360 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
362 printf("\t%s %s = %s\n", (const char*)type
.mb_str(),
363 (const char*)name
.mb_str(),
364 (const char*)val
.mb_str());
370 void InteractiveOutputTestCase::TestStackWalk()
372 #ifdef TEST_STACKWALKER
373 #if wxUSE_STACKWALKER
374 wxPuts(wxT("*** Testing wxStackWalker ***"));
376 wxString
progname(wxTheApp
->argv
[0]);
377 StackDump
dump(progname
.utf8_str());
382 #endif // TEST_STACKWALKER
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 #include "wx/stdpaths.h"
391 #include "wx/wxchar.h" // wxPrintf
393 void InteractiveOutputTestCase::TestStandardPaths()
396 wxPuts(wxT("*** Testing wxStandardPaths ***"));
398 wxTheApp
->SetAppName(wxT("console"));
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
417 wxStandardPaths::ResourceCat_Messages
421 #endif // TEST_STDPATHS
425 // ----------------------------------------------------------------------------
427 // ----------------------------------------------------------------------------
429 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
435 #include "wx/volume.h"
436 static const wxChar
*volumeKinds
[] =
442 wxT("network volume"),
448 void InteractiveOutputTestCase::TestFSVolume()
451 wxPuts(wxT("*** Testing wxFSVolume class ***"));
453 wxArrayString volumes
= wxFSVolume::GetVolumes();
454 size_t count
= volumes
.GetCount();
458 wxPuts(wxT("ERROR: no mounted volumes?"));
462 wxPrintf(wxT("%u mounted volumes found:\n"), count
);
464 for ( size_t n
= 0; n
< count
; n
++ )
466 wxFSVolume
vol(volumes
[n
]);
469 wxPuts(wxT("ERROR: couldn't create volume"));
473 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
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")
484 #endif // TEST_VOLUME