1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/filesys/filesys.cpp
3 // Purpose: wxFileSystem unit test
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2004 Vaclav Slavik
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include "wx/filesys.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 // a hack to let us use wxFileSystemHandler's protected methods:
32 class UrlTester
: public wxFileSystemHandler
35 UrlTester() : wxFileSystemHandler() {}
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
); }
42 bool CanOpen(const wxString
& WXUNUSED(url
)) { return false; }
43 wxFSFile
*OpenFile(wxFileSystem
& WXUNUSED(fs
),
44 const wxString
& WXUNUSED(url
)) { return NULL
; }
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class FileSystemTestCase
: public CppUnit::TestCase
57 FileSystemTestCase() { }
60 CPPUNIT_TEST_SUITE( FileSystemTestCase
);
61 CPPUNIT_TEST( UrlParsing
);
62 CPPUNIT_TEST( FileNameToUrlConversion
);
63 CPPUNIT_TEST( UnicodeFileNameToUrlConversion
);
64 CPPUNIT_TEST_SUITE_END();
67 void FileNameToUrlConversion();
68 void UnicodeFileNameToUrlConversion();
70 DECLARE_NO_COPY_CLASS(FileSystemTestCase
)
73 // register in the unnamed registry so that these tests are run by default
74 CPPUNIT_TEST_SUITE_REGISTRATION( FileSystemTestCase
);
76 // also include in its own registry so that these tests can be run alone
77 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileSystemTestCase
, "FileSystemTestCase" );
79 void FileSystemTestCase::UrlParsing()
81 static const struct Data
84 const wxChar
*protocol
, *left
, *right
, *anchor
;
88 { wxT("http://www.root.cz/index.html"),
89 wxT("http"), wxT(""), wxT("//www.root.cz/index.html"), wxT("")},
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("")},
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("")}
105 for ( size_t n
= 0; n
< WXSIZEOF(data
); n
++ )
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
);
115 void FileSystemTestCase::FileNameToUrlConversion()
118 wxFileName
fn1(wxT("\\\\server\\share\\path\\to\\file"));
119 wxString url1
= wxFileSystem::FileNameToURL(fn1
);
121 CPPUNIT_ASSERT( fn1
.SameAs(wxFileSystem::URLToFileName(url1
)) );
125 void FileSystemTestCase::UnicodeFileNameToUrlConversion()
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
133 wxFileName
filename(wxString::FromUTF8((const char*)filename_utf8
));
135 wxString url
= wxFileSystem::FileNameToURL(filename
);
137 CPPUNIT_ASSERT( filename
.SameAs(wxFileSystem::URLToFileName(url
)) );
140 #endif // wxUSE_FILESYSTEM