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