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