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