1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/image/image.cpp
3 // Purpose: Test wxImage
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2009 Francesco Montorsi
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
27 #include "wx/mstream.h"
28 #include "wx/zstream.h"
29 #include "wx/wfstream.h"
36 { "horse.ico", wxBITMAP_TYPE_ICO
},
37 { "horse.xpm", wxBITMAP_TYPE_XPM
},
38 { "horse.png", wxBITMAP_TYPE_PNG
},
39 { "horse.ani", wxBITMAP_TYPE_ANI
},
40 { "horse.bmp", wxBITMAP_TYPE_BMP
},
41 { "horse.cur", wxBITMAP_TYPE_CUR
},
42 { "horse.gif", wxBITMAP_TYPE_GIF
},
43 { "horse.jpg", wxBITMAP_TYPE_JPEG
},
44 { "horse.pcx", wxBITMAP_TYPE_PCX
},
45 { "horse.pnm", wxBITMAP_TYPE_PNM
},
46 { "horse.tga", wxBITMAP_TYPE_TGA
},
47 { "horse.tif", wxBITMAP_TYPE_TIF
}
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 class ImageTestCase
: public CppUnit::TestCase
62 CPPUNIT_TEST_SUITE( ImageTestCase
);
63 CPPUNIT_TEST( LoadFromSocketStream
);
64 CPPUNIT_TEST( LoadFromZipStream
);
65 CPPUNIT_TEST( LoadFromFile
);
66 CPPUNIT_TEST_SUITE_END();
68 void LoadFromSocketStream();
69 void LoadFromZipStream();
72 DECLARE_NO_COPY_CLASS(ImageTestCase
)
75 CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase
);
76 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase
, "ImageTestCase" );
78 ImageTestCase::ImageTestCase()
80 wxSocketBase::Initialize();
82 // the formats we're going to test:
83 wxImage::AddHandler(new wxICOHandler
);
84 wxImage::AddHandler(new wxXPMHandler
);
85 wxImage::AddHandler(new wxPNGHandler
);
86 wxImage::AddHandler(new wxANIHandler
);
87 wxImage::AddHandler(new wxBMPHandler
);
88 wxImage::AddHandler(new wxCURHandler
);
89 wxImage::AddHandler(new wxGIFHandler
);
90 wxImage::AddHandler(new wxJPEGHandler
);
91 wxImage::AddHandler(new wxPCXHandler
);
92 wxImage::AddHandler(new wxPNMHandler
);
93 wxImage::AddHandler(new wxTGAHandler
);
94 wxImage::AddHandler(new wxTIFFHandler
);
97 ImageTestCase::~ImageTestCase()
99 wxSocketBase::Shutdown();
102 void ImageTestCase::LoadFromFile()
105 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
106 CPPUNIT_ASSERT(img
.LoadFile(g_testfiles
[i
].file
));
109 void ImageTestCase::LoadFromSocketStream()
111 if (!IsNetworkAvailable()) // implemented in test.cpp
113 wxLogWarning("No network connectivity; skipping the ImageTestCase::LoadFromSocketStream test unit.");
122 { "http://wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
123 { "http://wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
126 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
128 wxURL
url(testData
[i
].url
);
129 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
131 wxInputStream
*in_stream
= url
.GetInputStream();
132 CPPUNIT_ASSERT(in_stream
&& in_stream
->IsOk());
136 // NOTE: it's important to inform wxImage about the type of the image being
137 // loaded otherwise it will try to autodetect the format, but that
138 // requires a seekable stream!
139 CPPUNIT_ASSERT(img
.LoadFile(*in_stream
, testData
[i
].type
));
145 void ImageTestCase::LoadFromZipStream()
147 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
149 switch (g_testfiles
[i
].type
)
151 case wxBITMAP_TYPE_XPM
:
152 case wxBITMAP_TYPE_GIF
:
153 case wxBITMAP_TYPE_PCX
:
154 case wxBITMAP_TYPE_TGA
:
155 case wxBITMAP_TYPE_TIF
:
156 continue; // skip testing those wxImageHandlers which cannot
157 // load data from non-seekable streams
163 // compress the test file on the fly:
164 wxMemoryOutputStream memOut
;
166 wxFileInputStream
file(g_testfiles
[i
].file
);
167 CPPUNIT_ASSERT(file
.IsOk());
169 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
170 CPPUNIT_ASSERT(compressFilter
.IsOk());
172 file
.Read(compressFilter
);
173 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
176 // now fetch the compressed memory to wxImage, decompressing it on the fly; this
177 // allows us to test loading images from non-seekable streams other than socket streams
178 wxMemoryInputStream
memIn(memOut
);
179 CPPUNIT_ASSERT(memIn
.IsOk());
180 wxZlibInputStream
decompressFilter(memIn
, wxZLIB_GZIP
);
181 CPPUNIT_ASSERT(decompressFilter
.IsOk());
185 // NOTE: it's important to inform wxImage about the type of the image being
186 // loaded otherwise it will try to autodetect the format, but that
187 // requires a seekable stream!
188 WX_ASSERT_MESSAGE(("Could not load file type '%d' after it was zipped", g_testfiles
[i
].type
),
189 img
.LoadFile(decompressFilter
, g_testfiles
[i
].type
));
194 TODO: add lots of more tests to wxImage functions