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 // ----------------------------------------------------------------------------
26 #include "wx/mstream.h"
27 #include "wx/zstream.h"
28 #include "wx/wfstream.h"
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
}
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class ImageTestCase
: public CppUnit::TestCase
61 CPPUNIT_TEST_SUITE( ImageTestCase
);
62 CPPUNIT_TEST( LoadFromSocketStream
);
63 CPPUNIT_TEST( LoadFromZipStream
);
64 CPPUNIT_TEST( LoadFromFile
);
65 CPPUNIT_TEST_SUITE_END();
67 void LoadFromSocketStream();
68 void LoadFromZipStream();
71 DECLARE_NO_COPY_CLASS(ImageTestCase
)
74 CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase
);
75 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase
, "ImageTestCase" );
77 ImageTestCase::ImageTestCase()
79 wxSocketBase::Initialize();
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
);
96 ImageTestCase::~ImageTestCase()
98 wxSocketBase::Shutdown();
101 void ImageTestCase::LoadFromFile()
104 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
105 CPPUNIT_ASSERT(img
.LoadFile(g_testfiles
[i
].file
));
108 void ImageTestCase::LoadFromSocketStream()
110 if (!IsNetworkAvailable()) // implemented in test.cpp
112 wxLogWarning("No network connectivity; skipping the ImageTestCase::LoadFromSocketStream test unit.");
121 { "http://wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG
},
122 { "http://wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO
}
125 for (unsigned int i
=0; i
<WXSIZEOF(testData
); i
++)
127 wxURL
url(testData
[i
].url
);
128 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
130 wxInputStream
*in_stream
= url
.GetInputStream();
131 CPPUNIT_ASSERT(in_stream
&& in_stream
->IsOk());
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
));
144 void ImageTestCase::LoadFromZipStream()
146 for (unsigned int i
=0; i
<WXSIZEOF(g_testfiles
); i
++)
148 switch (g_testfiles
[i
].type
)
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
162 // compress the test file on the fly:
163 wxMemoryOutputStream memOut
;
165 wxFileInputStream
file(g_testfiles
[i
].file
);
166 CPPUNIT_ASSERT(file
.IsOk());
168 wxZlibOutputStream
compressFilter(memOut
, 5, wxZLIB_GZIP
);
169 CPPUNIT_ASSERT(compressFilter
.IsOk());
171 file
.Read(compressFilter
);
172 CPPUNIT_ASSERT(file
.GetLastError() == wxSTREAM_EOF
);
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());
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
));
193 TODO: add lots of more tests to wxImage functions