]>
Commit | Line | Data |
---|---|---|
21fe35af FM |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/image/image.cpp | |
3 | // Purpose: Test wxImage | |
4 | // Author: Francesco Montorsi | |
5 | // Created: 2009-05-31 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2009 Francesco Montorsi | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ---------------------------------------------------------------------------- | |
12 | // headers | |
13 | // ---------------------------------------------------------------------------- | |
14 | ||
15 | #include "testprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #endif // WX_PRECOMP | |
23 | ||
53c2cdb0 | 24 | #include "wx/image.h" |
21fe35af FM |
25 | #include "wx/url.h" |
26 | #include "wx/mstream.h" | |
27 | #include "wx/zstream.h" | |
28 | #include "wx/wfstream.h" | |
29 | ||
30 | struct testData { | |
31 | const char* file; | |
32 | wxBitmapType type; | |
33 | } g_testfiles[] = | |
34 | { | |
35 | { "horse.ico", wxBITMAP_TYPE_ICO }, | |
36 | { "horse.xpm", wxBITMAP_TYPE_XPM }, | |
37 | { "horse.png", wxBITMAP_TYPE_PNG }, | |
38 | { "horse.ani", wxBITMAP_TYPE_ANI }, | |
39 | { "horse.bmp", wxBITMAP_TYPE_BMP }, | |
40 | { "horse.cur", wxBITMAP_TYPE_CUR }, | |
41 | { "horse.gif", wxBITMAP_TYPE_GIF }, | |
42 | { "horse.jpg", wxBITMAP_TYPE_JPEG }, | |
43 | { "horse.pcx", wxBITMAP_TYPE_PCX }, | |
44 | { "horse.pnm", wxBITMAP_TYPE_PNM }, | |
45 | { "horse.tga", wxBITMAP_TYPE_TGA }, | |
46 | { "horse.tif", wxBITMAP_TYPE_TIF } | |
47 | }; | |
48 | ||
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // test class | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | class ImageTestCase : public CppUnit::TestCase | |
55 | { | |
56 | public: | |
57 | ImageTestCase(); | |
58 | ~ImageTestCase(); | |
59 | ||
60 | private: | |
61 | CPPUNIT_TEST_SUITE( ImageTestCase ); | |
62 | CPPUNIT_TEST( LoadFromSocketStream ); | |
63 | CPPUNIT_TEST( LoadFromZipStream ); | |
64 | CPPUNIT_TEST( LoadFromFile ); | |
65 | CPPUNIT_TEST_SUITE_END(); | |
66 | ||
67 | void LoadFromSocketStream(); | |
68 | void LoadFromZipStream(); | |
69 | void LoadFromFile(); | |
70 | ||
71 | DECLARE_NO_COPY_CLASS(ImageTestCase) | |
72 | }; | |
73 | ||
74 | CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase ); | |
75 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase, "ImageTestCase" ); | |
76 | ||
77 | ImageTestCase::ImageTestCase() | |
78 | { | |
79 | wxSocketBase::Initialize(); | |
80 | ||
81 | // the formats we're going to test: | |
82 | wxImage::AddHandler(new wxICOHandler); | |
83 | wxImage::AddHandler(new wxXPMHandler); | |
84 | wxImage::AddHandler(new wxPNGHandler); | |
85 | wxImage::AddHandler(new wxANIHandler); | |
86 | wxImage::AddHandler(new wxBMPHandler); | |
87 | wxImage::AddHandler(new wxCURHandler); | |
88 | wxImage::AddHandler(new wxGIFHandler); | |
89 | wxImage::AddHandler(new wxJPEGHandler); | |
90 | wxImage::AddHandler(new wxPCXHandler); | |
91 | wxImage::AddHandler(new wxPNMHandler); | |
92 | wxImage::AddHandler(new wxTGAHandler); | |
93 | wxImage::AddHandler(new wxTIFFHandler); | |
94 | } | |
95 | ||
96 | ImageTestCase::~ImageTestCase() | |
97 | { | |
98 | wxSocketBase::Shutdown(); | |
99 | } | |
100 | ||
101 | void ImageTestCase::LoadFromFile() | |
102 | { | |
103 | wxImage img; | |
104 | for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++) | |
105 | CPPUNIT_ASSERT(img.LoadFile(g_testfiles[i].file)); | |
106 | } | |
107 | ||
108 | void ImageTestCase::LoadFromSocketStream() | |
109 | { | |
110 | struct { | |
111 | const char* url; | |
112 | wxBitmapType type; | |
113 | } testData[] = | |
114 | { | |
115 | { "http://wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG }, | |
116 | { "http://wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO } | |
117 | }; | |
118 | ||
119 | for (unsigned int i=0; i<WXSIZEOF(testData); i++) | |
120 | { | |
121 | wxURL url(testData[i].url); | |
122 | CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR); | |
123 | ||
124 | wxInputStream *in_stream = url.GetInputStream(); | |
125 | CPPUNIT_ASSERT(in_stream && in_stream->IsOk()); | |
126 | ||
127 | wxImage img; | |
128 | ||
129 | // NOTE: it's important to inform wxImage about the type of the image being | |
130 | // loaded otherwise it will try to autodetect the format, but that | |
131 | // requires a seekable stream! | |
132 | CPPUNIT_ASSERT(img.LoadFile(*in_stream, testData[i].type)); | |
133 | ||
134 | delete in_stream; | |
135 | } | |
136 | } | |
137 | ||
138 | void ImageTestCase::LoadFromZipStream() | |
139 | { | |
140 | for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++) | |
141 | { | |
142 | switch (g_testfiles[i].type) | |
143 | { | |
144 | case wxBITMAP_TYPE_XPM: | |
145 | case wxBITMAP_TYPE_GIF: | |
146 | case wxBITMAP_TYPE_PCX: | |
147 | case wxBITMAP_TYPE_TGA: | |
148 | case wxBITMAP_TYPE_TIF: | |
149 | continue; // skip testing those wxImageHandlers which cannot | |
150 | // load data from non-seekable streams | |
151 | ||
152 | default: | |
153 | ; // proceed | |
154 | } | |
155 | ||
156 | // compress the test file on the fly: | |
157 | wxMemoryOutputStream memOut; | |
158 | { | |
159 | wxFileInputStream file(g_testfiles[i].file); | |
160 | CPPUNIT_ASSERT(file.IsOk()); | |
161 | ||
162 | wxZlibOutputStream compressFilter(memOut, 5, wxZLIB_GZIP); | |
163 | CPPUNIT_ASSERT(compressFilter.IsOk()); | |
164 | ||
165 | file.Read(compressFilter); | |
166 | CPPUNIT_ASSERT(file.GetLastError() == wxSTREAM_EOF); | |
167 | } | |
168 | ||
169 | // now fetch the compressed memory to wxImage, decompressing it on the fly; this | |
170 | // allows us to test loading images from non-seekable streams other than socket streams | |
171 | wxMemoryInputStream memIn(memOut); | |
172 | CPPUNIT_ASSERT(memIn.IsOk()); | |
173 | wxZlibInputStream decompressFilter(memIn, wxZLIB_GZIP); | |
174 | CPPUNIT_ASSERT(decompressFilter.IsOk()); | |
175 | ||
176 | wxImage img; | |
177 | ||
178 | // NOTE: it's important to inform wxImage about the type of the image being | |
179 | // loaded otherwise it will try to autodetect the format, but that | |
180 | // requires a seekable stream! | |
181 | WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles[i].type), | |
182 | img.LoadFile(decompressFilter, g_testfiles[i].type)); | |
183 | } | |
184 | } | |
185 | ||
186 | /* | |
187 | TODO: add lots of more tests to wxImage functions | |
188 | */ |