]> git.saurik.com Git - wxWidgets.git/blame - tests/image/image.cpp
better variant support, fixes #11114
[wxWidgets.git] / tests / image / image.cpp
CommitLineData
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 25#include "wx/url.h"
9e91447c 26#include "wx/log.h"
21fe35af
FM
27#include "wx/mstream.h"
28#include "wx/zstream.h"
29#include "wx/wfstream.h"
30
31struct testData {
32 const char* file;
33 wxBitmapType type;
34} g_testfiles[] =
35{
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 }
48};
49
50
51// ----------------------------------------------------------------------------
52// test class
53// ----------------------------------------------------------------------------
54
55class ImageTestCase : public CppUnit::TestCase
56{
57public:
58 ImageTestCase();
59 ~ImageTestCase();
60
61private:
62 CPPUNIT_TEST_SUITE( ImageTestCase );
63 CPPUNIT_TEST( LoadFromSocketStream );
64 CPPUNIT_TEST( LoadFromZipStream );
65 CPPUNIT_TEST( LoadFromFile );
66 CPPUNIT_TEST_SUITE_END();
67
68 void LoadFromSocketStream();
69 void LoadFromZipStream();
70 void LoadFromFile();
71
72 DECLARE_NO_COPY_CLASS(ImageTestCase)
73};
74
75CPPUNIT_TEST_SUITE_REGISTRATION( ImageTestCase );
76CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ImageTestCase, "ImageTestCase" );
77
78ImageTestCase::ImageTestCase()
79{
80 wxSocketBase::Initialize();
81
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);
95}
96
97ImageTestCase::~ImageTestCase()
98{
99 wxSocketBase::Shutdown();
100}
101
102void ImageTestCase::LoadFromFile()
103{
104 wxImage img;
105 for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++)
106 CPPUNIT_ASSERT(img.LoadFile(g_testfiles[i].file));
107}
108
109void ImageTestCase::LoadFromSocketStream()
110{
c99214eb
FM
111 if (!IsNetworkAvailable()) // implemented in test.cpp
112 {
113 wxLogWarning("No network connectivity; skipping the ImageTestCase::LoadFromSocketStream test unit.");
114 return;
115 }
116
21fe35af
FM
117 struct {
118 const char* url;
119 wxBitmapType type;
120 } testData[] =
121 {
122 { "http://wxwidgets.org/logo9.jpg", wxBITMAP_TYPE_JPEG },
123 { "http://wxwidgets.org/favicon.ico", wxBITMAP_TYPE_ICO }
124 };
125
126 for (unsigned int i=0; i<WXSIZEOF(testData); i++)
127 {
128 wxURL url(testData[i].url);
129 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
130
131 wxInputStream *in_stream = url.GetInputStream();
132 CPPUNIT_ASSERT(in_stream && in_stream->IsOk());
133
134 wxImage img;
135
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));
140
141 delete in_stream;
142 }
143}
144
145void ImageTestCase::LoadFromZipStream()
146{
147 for (unsigned int i=0; i<WXSIZEOF(g_testfiles); i++)
148 {
149 switch (g_testfiles[i].type)
150 {
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
158
159 default:
160 ; // proceed
161 }
162
163 // compress the test file on the fly:
164 wxMemoryOutputStream memOut;
165 {
166 wxFileInputStream file(g_testfiles[i].file);
167 CPPUNIT_ASSERT(file.IsOk());
168
169 wxZlibOutputStream compressFilter(memOut, 5, wxZLIB_GZIP);
170 CPPUNIT_ASSERT(compressFilter.IsOk());
171
172 file.Read(compressFilter);
173 CPPUNIT_ASSERT(file.GetLastError() == wxSTREAM_EOF);
174 }
175
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());
182
183 wxImage img;
184
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));
190 }
191}
192
193/*
194 TODO: add lots of more tests to wxImage functions
195*/