output.cpp 14 KB

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