filesystest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/filesys/filesys.cpp
  3. // Purpose: wxFileSystem unit test
  4. // Author: Vaclav Slavik
  5. // Created: 2004-03-28
  6. // Copyright: (c) 2004 Vaclav Slavik
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/filesys.h"
  19. #if wxUSE_FILESYSTEM
  20. // ----------------------------------------------------------------------------
  21. // helpers
  22. // ----------------------------------------------------------------------------
  23. // a hack to let us use wxFileSystemHandler's protected methods:
  24. class UrlTester : public wxFileSystemHandler
  25. {
  26. public:
  27. UrlTester() : wxFileSystemHandler() {}
  28. wxString Protocol(const wxString& p) { return GetProtocol(p); }
  29. wxString LeftLocation(const wxString& p) { return GetLeftLocation(p); }
  30. wxString RightLocation(const wxString& p) { return GetRightLocation(p); }
  31. wxString Anchor(const wxString& p) { return GetAnchor(p); }
  32. bool CanOpen(const wxString& WXUNUSED(url)) { return false; }
  33. wxFSFile *OpenFile(wxFileSystem& WXUNUSED(fs),
  34. const wxString& WXUNUSED(url)) { return NULL; }
  35. };
  36. // ----------------------------------------------------------------------------
  37. // test class
  38. // ----------------------------------------------------------------------------
  39. class FileSystemTestCase : public CppUnit::TestCase
  40. {
  41. public:
  42. FileSystemTestCase() { }
  43. private:
  44. CPPUNIT_TEST_SUITE( FileSystemTestCase );
  45. CPPUNIT_TEST( UrlParsing );
  46. CPPUNIT_TEST( FileNameToUrlConversion );
  47. CPPUNIT_TEST( UnicodeFileNameToUrlConversion );
  48. CPPUNIT_TEST_SUITE_END();
  49. void UrlParsing();
  50. void FileNameToUrlConversion();
  51. void UnicodeFileNameToUrlConversion();
  52. DECLARE_NO_COPY_CLASS(FileSystemTestCase)
  53. };
  54. // register in the unnamed registry so that these tests are run by default
  55. CPPUNIT_TEST_SUITE_REGISTRATION( FileSystemTestCase );
  56. // also include in its own registry so that these tests can be run alone
  57. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileSystemTestCase, "FileSystemTestCase" );
  58. void FileSystemTestCase::UrlParsing()
  59. {
  60. static const struct Data
  61. {
  62. const wxChar *url;
  63. const wxChar *protocol, *left, *right, *anchor;
  64. } data[] =
  65. {
  66. // simple case:
  67. { wxT("http://www.root.cz/index.html"),
  68. wxT("http"), wxT(""), wxT("//www.root.cz/index.html"), wxT("")},
  69. // anchors:
  70. { wxT("http://www.root.cz/index.html#lbl"),
  71. wxT("http"), wxT(""), wxT("//www.root.cz/index.html"), wxT("lbl")},
  72. // file is default protocol:
  73. { wxT("testfile.html"),
  74. wxT("file"), wxT(""), wxT("testfile.html"), wxT("")},
  75. // stacked protocols:
  76. { wxT("file:myzipfile.zip#zip:index.htm"),
  77. wxT("zip"), wxT("file:myzipfile.zip"), wxT("index.htm"), wxT("")},
  78. // changes to ':' parsing often break things:
  79. { wxT("file:a#b:foo"),
  80. wxT("b"), wxT("file:a"), wxT("foo"), wxT("")}
  81. };
  82. UrlTester tst;
  83. for ( size_t n = 0; n < WXSIZEOF(data); n++ )
  84. {
  85. const Data& d = data[n];
  86. CPPUNIT_ASSERT( tst.Protocol(d.url) == d.protocol );
  87. CPPUNIT_ASSERT( tst.LeftLocation(d.url) == d.left );
  88. CPPUNIT_ASSERT( tst.RightLocation(d.url) == d.right );
  89. CPPUNIT_ASSERT( tst.Anchor(d.url) == d.anchor );
  90. }
  91. }
  92. void FileSystemTestCase::FileNameToUrlConversion()
  93. {
  94. #ifdef __WINDOWS__
  95. wxFileName fn1(wxT("\\\\server\\share\\path\\to\\file"));
  96. wxString url1 = wxFileSystem::FileNameToURL(fn1);
  97. CPPUNIT_ASSERT( fn1.SameAs(wxFileSystem::URLToFileName(url1)) );
  98. #endif
  99. }
  100. void FileSystemTestCase::UnicodeFileNameToUrlConversion()
  101. {
  102. const unsigned char filename_utf8[] = {
  103. 0x4b, 0x72, 0xc3, 0xa1, 0x73, 0x79, 0x50, 0xc5,
  104. 0x99, 0xc3, 0xad, 0x72, 0x6f, 0x64, 0x79, 0x2e,
  105. 0x6a, 0x70, 0x67, 0x00
  106. // KrásyPřírody.jpg
  107. };
  108. wxFileName filename(wxString::FromUTF8((const char*)filename_utf8));
  109. wxString url = wxFileSystem::FileNameToURL(filename);
  110. CPPUNIT_ASSERT( filename.SameAs(wxFileSystem::URLToFileName(url)) );
  111. }
  112. #endif // wxUSE_FILESYSTEM