]>
Commit | Line | Data |
---|---|---|
81ec0e15 FM |
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 | ||
995202d0 | 24 | #include "wx/app.h" |
ddd4f327 FM |
25 | #include "wx/wxcrt.h" // for wxPuts |
26 | #include "wx/wxcrtvararg.h" // for wxPrintf | |
995202d0 | 27 | |
81ec0e15 FM |
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(__WXMSW__) && !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 | } | |
da569065 | 122 | |
81ec0e15 FM |
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( | |
232 | wxT("application/x-xyz"), | |
233 | wxT("xyzview '%s'"), // open cmd | |
234 | wxT(""), // print cmd | |
235 | wxT("XYZ File"), // description | |
236 | wxT(".xyz"), // extensions | |
237 | wxNullPtr // end of extensions | |
238 | ); | |
239 | ftInfo.SetShortDesc(wxT("XYZFile")); // used under Win32 only | |
240 | ||
241 | wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo); | |
242 | if ( !ft ) | |
243 | { | |
244 | wxPuts(wxT("ERROR: failed to create association!")); | |
245 | } | |
246 | else | |
247 | { | |
248 | // TODO: read it back | |
249 | delete ft; | |
250 | } | |
251 | ||
252 | wxPuts(wxEmptyString); | |
253 | #endif // TEST_MIME | |
254 | } | |
255 | ||
256 | ||
257 | // ---------------------------------------------------------------------------- | |
258 | // misc information functions | |
259 | // ---------------------------------------------------------------------------- | |
260 | ||
261 | #include "wx/utils.h" | |
262 | ||
263 | void InteractiveOutputTestCase::TestOsInfo() | |
264 | { | |
265 | #ifdef TEST_INFO_FUNCTIONS | |
266 | wxPuts(wxT("*** Testing OS info functions ***\n")); | |
267 | ||
268 | int major, minor; | |
269 | wxGetOsVersion(&major, &minor); | |
270 | wxPrintf(wxT("Running under: %s, version %d.%d\n"), | |
271 | wxGetOsDescription().c_str(), major, minor); | |
272 | ||
273 | wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong()); | |
274 | ||
275 | wxPrintf(wxT("Host name is %s (%s).\n"), | |
276 | wxGetHostName().c_str(), wxGetFullHostName().c_str()); | |
277 | ||
278 | wxPuts(wxEmptyString); | |
279 | #endif // TEST_INFO_FUNCTIONS | |
280 | } | |
281 | ||
282 | void InteractiveOutputTestCase::TestPlatformInfo() | |
283 | { | |
284 | #ifdef TEST_INFO_FUNCTIONS | |
285 | wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n")); | |
286 | ||
287 | // get this platform | |
288 | wxPlatformInfo plat; | |
289 | ||
290 | wxPrintf(wxT("Operating system family name is: %s\n"), plat.GetOperatingSystemFamilyName().c_str()); | |
291 | wxPrintf(wxT("Operating system name is: %s\n"), plat.GetOperatingSystemIdName().c_str()); | |
292 | wxPrintf(wxT("Port ID name is: %s\n"), plat.GetPortIdName().c_str()); | |
293 | wxPrintf(wxT("Port ID short name is: %s\n"), plat.GetPortIdShortName().c_str()); | |
294 | wxPrintf(wxT("Architecture is: %s\n"), plat.GetArchName().c_str()); | |
295 | wxPrintf(wxT("Endianness is: %s\n"), plat.GetEndiannessName().c_str()); | |
296 | ||
297 | wxPuts(wxEmptyString); | |
298 | #endif // TEST_INFO_FUNCTIONS | |
299 | } | |
300 | ||
301 | void InteractiveOutputTestCase::TestUserInfo() | |
302 | { | |
303 | #ifdef TEST_INFO_FUNCTIONS | |
304 | wxPuts(wxT("*** Testing user info functions ***\n")); | |
305 | ||
306 | wxPrintf(wxT("User id is:\t%s\n"), wxGetUserId().c_str()); | |
307 | wxPrintf(wxT("User name is:\t%s\n"), wxGetUserName().c_str()); | |
308 | wxPrintf(wxT("Home dir is:\t%s\n"), wxGetHomeDir().c_str()); | |
309 | wxPrintf(wxT("Email address:\t%s\n"), wxGetEmailAddress().c_str()); | |
310 | ||
311 | wxPuts(wxEmptyString); | |
312 | #endif // TEST_INFO_FUNCTIONS | |
313 | } | |
314 | ||
315 | ||
316 | // ---------------------------------------------------------------------------- | |
317 | // stack backtrace | |
318 | // ---------------------------------------------------------------------------- | |
319 | ||
ddd4f327 FM |
320 | #if wxUSE_STACKWALKER |
321 | ||
81ec0e15 FM |
322 | #include "wx/stackwalk.h" |
323 | ||
324 | class StackDump : public wxStackWalker | |
325 | { | |
326 | public: | |
327 | StackDump(const char *argv0) | |
328 | : wxStackWalker(argv0) | |
329 | { | |
330 | } | |
da569065 | 331 | |
81ec0e15 FM |
332 | virtual void Walk(size_t skip = 1, size_t maxdepth = wxSTACKWALKER_MAX_DEPTH) |
333 | { | |
334 | wxPuts(wxT("Stack dump:")); | |
335 | ||
336 | wxStackWalker::Walk(skip, maxdepth); | |
337 | } | |
338 | ||
339 | protected: | |
340 | virtual void OnStackFrame(const wxStackFrame& frame) | |
341 | { | |
342 | printf("[%2d] ", (int) frame.GetLevel()); | |
343 | ||
344 | wxString name = frame.GetName(); | |
345 | if ( !name.empty() ) | |
346 | { | |
347 | printf("%-20.40s", (const char*)name.mb_str()); | |
348 | } | |
349 | else | |
350 | { | |
351 | printf("0x%08lx", (unsigned long)frame.GetAddress()); | |
352 | } | |
353 | ||
354 | if ( frame.HasSourceLocation() ) | |
355 | { | |
356 | printf("\t%s:%d", | |
357 | (const char*)frame.GetFileName().mb_str(), | |
358 | (int)frame.GetLine()); | |
359 | } | |
360 | ||
361 | puts(""); | |
362 | ||
363 | wxString type, val; | |
364 | for ( size_t n = 0; frame.GetParam(n, &type, &name, &val); n++ ) | |
365 | { | |
366 | printf("\t%s %s = %s\n", (const char*)type.mb_str(), | |
367 | (const char*)name.mb_str(), | |
368 | (const char*)val.mb_str()); | |
369 | } | |
370 | } | |
371 | }; | |
ddd4f327 | 372 | #endif |
81ec0e15 FM |
373 | |
374 | void InteractiveOutputTestCase::TestStackWalk() | |
375 | { | |
376 | #ifdef TEST_STACKWALKER | |
ddd4f327 | 377 | #if wxUSE_STACKWALKER |
81ec0e15 FM |
378 | wxPuts(wxT("*** Testing wxStackWalker ***")); |
379 | ||
0fbd8b9b VZ |
380 | wxString progname(wxTheApp->argv[0]); |
381 | StackDump dump(progname.utf8_str()); | |
81ec0e15 | 382 | dump.Walk(); |
da569065 | 383 | |
81ec0e15 | 384 | wxPuts("\n"); |
ddd4f327 | 385 | #endif |
81ec0e15 FM |
386 | #endif // TEST_STACKWALKER |
387 | } | |
388 | ||
389 | ||
390 | // ---------------------------------------------------------------------------- | |
391 | // standard paths | |
392 | // ---------------------------------------------------------------------------- | |
393 | ||
81ec0e15 FM |
394 | #include "wx/stdpaths.h" |
395 | #include "wx/wxchar.h" // wxPrintf | |
396 | ||
397 | void InteractiveOutputTestCase::TestStandardPaths() | |
398 | { | |
399 | #ifdef TEST_STDPATHS | |
400 | wxPuts(wxT("*** Testing wxStandardPaths ***")); | |
401 | ||
402 | wxTheApp->SetAppName(wxT("console")); | |
403 | ||
404 | wxStandardPathsBase& stdp = wxStandardPaths::Get(); | |
405 | wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str()); | |
406 | wxPrintf(wxT("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str()); | |
407 | wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str()); | |
408 | wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str()); | |
409 | wxPrintf(wxT("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str()); | |
410 | wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str()); | |
411 | wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str()); | |
412 | wxPrintf(wxT("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str()); | |
413 | wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str()); | |
414 | wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str()); | |
415 | wxPrintf(wxT("Localized res. dir:\t%s\n"), | |
416 | stdp.GetLocalizedResourcesDir(wxT("fr")).c_str()); | |
417 | wxPrintf(wxT("Message catalogs dir:\t%s\n"), | |
418 | stdp.GetLocalizedResourcesDir | |
419 | ( | |
420 | wxT("fr"), | |
421 | wxStandardPaths::ResourceCat_Messages | |
422 | ).c_str()); | |
da569065 | 423 | |
81ec0e15 FM |
424 | wxPuts("\n"); |
425 | #endif // TEST_STDPATHS | |
426 | } | |
427 | ||
428 | ||
429 | // ---------------------------------------------------------------------------- | |
430 | // wxVolume tests | |
431 | // ---------------------------------------------------------------------------- | |
432 | ||
433 | #if !defined(__WIN32__) || !wxUSE_FSVOLUME | |
434 | #undef TEST_VOLUME | |
435 | #endif | |
436 | ||
995202d0 | 437 | #ifdef TEST_VOLUME |
81ec0e15 | 438 | |
995202d0 | 439 | #include "wx/volume.h" |
81ec0e15 FM |
440 | static const wxChar *volumeKinds[] = |
441 | { | |
442 | wxT("floppy"), | |
443 | wxT("hard disk"), | |
444 | wxT("CD-ROM"), | |
445 | wxT("DVD-ROM"), | |
446 | wxT("network volume"), | |
447 | wxT("other volume"), | |
448 | }; | |
449 | ||
995202d0 FM |
450 | #endif |
451 | ||
81ec0e15 FM |
452 | void InteractiveOutputTestCase::TestFSVolume() |
453 | { | |
454 | #ifdef TEST_VOLUME | |
455 | wxPuts(wxT("*** Testing wxFSVolume class ***")); | |
456 | ||
457 | wxArrayString volumes = wxFSVolume::GetVolumes(); | |
458 | size_t count = volumes.GetCount(); | |
459 | ||
460 | if ( !count ) | |
461 | { | |
462 | wxPuts(wxT("ERROR: no mounted volumes?")); | |
463 | return; | |
464 | } | |
465 | ||
466 | wxPrintf(wxT("%u mounted volumes found:\n"), count); | |
467 | ||
468 | for ( size_t n = 0; n < count; n++ ) | |
469 | { | |
470 | wxFSVolume vol(volumes[n]); | |
471 | if ( !vol.IsOk() ) | |
472 | { | |
473 | wxPuts(wxT("ERROR: couldn't create volume")); | |
474 | continue; | |
475 | } | |
476 | ||
477 | wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"), | |
478 | n + 1, | |
479 | vol.GetDisplayName().c_str(), | |
480 | vol.GetName().c_str(), | |
481 | volumeKinds[vol.GetKind()], | |
482 | vol.IsWritable() ? wxT("rw") : wxT("ro"), | |
483 | vol.GetFlags() & wxFS_VOL_REMOVABLE ? wxT("removable") | |
484 | : wxT("fixed")); | |
485 | } | |
da569065 | 486 | |
81ec0e15 FM |
487 | wxPuts("\n"); |
488 | #endif // TEST_VOLUME | |
489 | } | |
490 |