]> git.saurik.com Git - wxWidgets.git/blob - tests/misc/garbage.cpp
ba5cb6746b309e8d277b07a429c453c96854844c
[wxWidgets.git] / tests / misc / garbage.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/garbage.cpp
3 // Purpose: test if loading garbage fails
4 // Author: Francesco Montorsi
5 // Created: 2009-01-10
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Francesco Montorsi
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/filename.h"
21 #include "wx/image.h"
22 #include "wx/animate.h"
23 #include "wx/mstream.h"
24 #include "wx/dynlib.h"
25 #include "wx/mediactrl.h"
26 #include "wx/html/htmlwin.h"
27 #include "wx/xrc/xmlres.h"
28
29 #define GARBAGE_DATA_SIZE 1000000 // in bytes; ~ 1MB
30
31 // ----------------------------------------------------------------------------
32 // test class
33 // ----------------------------------------------------------------------------
34
35 class GarbageTestCase : public CppUnit::TestCase
36 {
37 public:
38 GarbageTestCase() { }
39
40 private:
41 CPPUNIT_TEST_SUITE( GarbageTestCase );
42 CPPUNIT_TEST( LoadGarbage );
43 CPPUNIT_TEST_SUITE_END();
44
45 void LoadGarbage();
46 void DoLoadFile(const wxString& fullname);
47 void DoLoadStream(wxInputStream& stream);
48
49 DECLARE_NO_COPY_CLASS(GarbageTestCase)
50 };
51
52 // register in the unnamed registry so that these tests are run by default
53 CPPUNIT_TEST_SUITE_REGISTRATION( GarbageTestCase );
54
55 // also include in its own registry so that these tests can be run alone
56 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GarbageTestCase, "GarbageTestCase" );
57
58
59 void GarbageTestCase::LoadGarbage()
60 {
61 srand(1234);
62
63 wxInitAllImageHandlers();
64
65 for (size_t size = 1; size < GARBAGE_DATA_SIZE; size *= size+1)
66 {
67 // first, generate some garbage data
68 unsigned char *data = new unsigned char[size];
69 for (size_t i = 0; i < size; i++)
70 data[i] = rand();
71
72 // write it to a file
73 wxString garbagename = wxFileName::CreateTempFileName("garbage");
74 CPPUNIT_ASSERT( !garbagename.empty() );
75
76 wxFile garbage(garbagename, wxFile::write);
77 CPPUNIT_ASSERT( garbage.IsOpened() );
78
79 CPPUNIT_ASSERT( garbage.Write(data, size) == size );
80 garbage.Close();
81
82 // try to load it by name
83 DoLoadFile(garbagename);
84
85 // try to load it from a wxInputStream
86 wxMemoryInputStream stream(data, size);
87 DoLoadStream(stream);
88
89 wxDELETEA(data);
90 }
91 }
92
93 void GarbageTestCase::DoLoadFile(const wxString& fullname)
94 {
95 int type;
96
97 // test wxImage
98 wxImage img;
99 CPPUNIT_ASSERT( img.LoadFile(fullname) == false );
100 // test with the default wxBITMAP_TYPE_ANY
101
102 for (type = wxBITMAP_TYPE_BMP; type < wxBITMAP_TYPE_ANY; type++)
103 CPPUNIT_ASSERT( img.LoadFile(fullname, (wxBitmapType)type) == false );
104 // test with all other possible wxBITMAP_TYPE_* flags
105
106 // test wxBitmap
107 wxBitmap bmp;
108 CPPUNIT_ASSERT( bmp.LoadFile(fullname) == false );
109 // test with the default wxBITMAP_TYPE_ANY
110
111 for (type = wxBITMAP_TYPE_BMP; type < wxBITMAP_TYPE_ANY; type++)
112 CPPUNIT_ASSERT( bmp.LoadFile(fullname, (wxBitmapType)type) == false );
113 // test with all other possible wxBITMAP_TYPE_* flags
114
115 // test wxIcon
116 wxIcon icon;
117 CPPUNIT_ASSERT( icon.LoadFile(fullname) == false );
118 // test with the default wxICON_DEFAULT_TYPE
119
120 for (type = wxBITMAP_TYPE_BMP; type < wxBITMAP_TYPE_ANY; type++)
121 CPPUNIT_ASSERT( icon.LoadFile(fullname, (wxBitmapType)type) == false );
122 // test with all other possible wxBITMAP_TYPE_* flags
123
124
125 // test wxAnimation
126 wxAnimation anim;
127 CPPUNIT_ASSERT( anim.LoadFile(fullname) == false );
128 // test with the default wxANIMATION_TYPE_ANY
129
130 for (type = wxANIMATION_TYPE_INVALID+1; type < wxANIMATION_TYPE_ANY; type++)
131 CPPUNIT_ASSERT( anim.LoadFile(fullname, (wxAnimationType)type) == false );
132 // test with all other possible wxANIMATION_TYPE_* flags
133
134
135 // test wxDynamicLibrary
136 wxDynamicLibrary lib;
137 CPPUNIT_ASSERT( lib.Load(fullname) == false );
138 // test with the default wxANIMATION_TYPE_ANY
139 /*
140 #if wxUSE_MEDIACTRL
141 // test wxMediaCtrl
142 wxMediaCtrl *media = new wxMediaCtrl(wxTheApp->GetTopWindow());
143 CPPUNIT_ASSERT( media->Load(fullname) == false );
144 #endif
145
146 // test wxHtmlWindow
147 wxHtmlWindow *htmlwin = new wxHtmlWindow(wxTheApp->GetTopWindow());
148 CPPUNIT_ASSERT( htmlwin->LoadFile(fullname) == false );
149 delete htmlwin;
150 */
151 // test wxXmlResource
152 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(fullname) == false );
153 }
154
155 void GarbageTestCase::DoLoadStream(wxInputStream& stream)
156 {
157 int type;
158
159 // NOTE: not all classes tested by DoLoadFile() supports loading
160 // from an input stream!
161
162 // test wxImage
163 wxImage img;
164 CPPUNIT_ASSERT( img.LoadFile(stream) == false );
165 // test with the default wxBITMAP_TYPE_ANY
166
167 for (type = wxBITMAP_TYPE_INVALID+1; type < wxBITMAP_TYPE_ANY; type++)
168 CPPUNIT_ASSERT( img.LoadFile(stream, (wxBitmapType)type) == false );
169 // test with all other possible wxBITMAP_TYPE_* flags
170
171
172 // test wxAnimation
173 wxAnimation anim;
174 CPPUNIT_ASSERT( anim.Load(stream) == false );
175 // test with the default wxANIMATION_TYPE_ANY
176
177 for (type = wxANIMATION_TYPE_INVALID+1; type < wxANIMATION_TYPE_ANY; type++)
178 CPPUNIT_ASSERT( anim.Load(stream, (wxAnimationType)type) == false );
179 // test with all other possible wxANIMATION_TYPE_* flags
180 /*
181 // test wxHtmlWindow
182 wxHtmlWindow *htmlwin = new wxHtmlWindow(wxTheApp->GetTopWindow());
183 CPPUNIT_ASSERT( htmlwin->LoadFile(fullname) == false );
184 delete htmlwin;
185 */
186 }
187