add check about network connectivity; don't run the test if it's not available
[wxWidgets.git] / tests / image / image.cpp
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
24 #include "wx/image.h"
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 if (!IsNetworkAvailable()) // implemented in test.cpp
111 {
112 wxLogWarning("No network connectivity; skipping the ImageTestCase::LoadFromSocketStream test unit.");
113 return;
114 }
115
116 struct {
117 const char* url;
118 wxBitmapType type;
119 } testData[] =
120 {
121 { "http://wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG },
122 { "http://wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO }
123 };
124
125 for (unsigned int i=0; i<WXSIZEOF(testData); i++)
126 {
127 wxURL url(testData[i].url);
128 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
129
130 wxInputStream *in_stream = url.GetInputStream();
131 CPPUNIT_ASSERT(in_stream && in_stream->IsOk());
132
133 wxImage img;
134
135 // NOTE: it's important to inform wxImage about the type of the image being
136 // loaded otherwise it will try to autodetect the format, but that
137 // requires a seekable stream!
138 CPPUNIT_ASSERT(img.LoadFile(*in_stream, testData[i].type));
139
140 delete in_stream;
141 }
142 }
143
144 void ImageTestCase::LoadFromZipStream()
145 {
146 for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++)
147 {
148 switch (g_testfiles[i].type)
149 {
150 case wxBITMAP_TYPE_XPM:
151 case wxBITMAP_TYPE_GIF:
152 case wxBITMAP_TYPE_PCX:
153 case wxBITMAP_TYPE_TGA:
154 case wxBITMAP_TYPE_TIF:
155 continue; // skip testing those wxImageHandlers which cannot
156 // load data from non-seekable streams
157
158 default:
159 ; // proceed
160 }
161
162 // compress the test file on the fly:
163 wxMemoryOutputStream memOut;
164 {
165 wxFileInputStream file(g_testfiles[i].file);
166 CPPUNIT_ASSERT(file.IsOk());
167
168 wxZlibOutputStream compressFilter(memOut, 5, wxZLIB_GZIP);
169 CPPUNIT_ASSERT(compressFilter.IsOk());
170
171 file.Read(compressFilter);
172 CPPUNIT_ASSERT(file.GetLastError() == wxSTREAM_EOF);
173 }
174
175 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
176 // allows us to test loading images from non-seekable streams other than socket streams
177 wxMemoryInputStream memIn(memOut);
178 CPPUNIT_ASSERT(memIn.IsOk());
179 wxZlibInputStream decompressFilter(memIn, wxZLIB_GZIP);
180 CPPUNIT_ASSERT(decompressFilter.IsOk());
181
182 wxImage img;
183
184 // NOTE: it's important to inform wxImage about the type of the image being
185 // loaded otherwise it will try to autodetect the format, but that
186 // requires a seekable stream!
187 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles[i].type),
188 img.LoadFile(decompressFilter, g_testfiles[i].type));
189 }
190 }
191
192 /*
193 TODO: add lots of more tests to wxImage functions
194 */