]>
git.saurik.com Git - wxWidgets.git/blob - tests/misc/garbage.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/garbage.cpp
3 // Purpose: test if loading garbage fails
4 // Author: Francesco Montorsi
6 // Copyright: (c) 2009 Francesco Montorsi
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
19 #include "wx/filename.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"
28 #define GARBAGE_DATA_SIZE 1000000 // in bytes; ~ 1MB
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class GarbageTestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE( GarbageTestCase
);
41 CPPUNIT_TEST( LoadGarbage
);
42 CPPUNIT_TEST_SUITE_END();
45 void DoLoadFile(const wxString
& fullname
);
46 void DoLoadStream(wxInputStream
& stream
);
48 DECLARE_NO_COPY_CLASS(GarbageTestCase
)
51 // register in the unnamed registry so that these tests are run by default
52 CPPUNIT_TEST_SUITE_REGISTRATION( GarbageTestCase
);
54 // also include in its own registry so that these tests can be run alone
55 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GarbageTestCase
, "GarbageTestCase" );
58 void GarbageTestCase::LoadGarbage()
62 wxInitAllImageHandlers();
64 for (size_t size
= 1; size
< GARBAGE_DATA_SIZE
; size
*= size
+1)
66 // first, generate some garbage data
67 unsigned char *data
= new unsigned char[size
];
68 for (size_t i
= 0; i
< size
; i
++)
72 wxString garbagename
= wxFileName::CreateTempFileName("garbage");
73 CPPUNIT_ASSERT( !garbagename
.empty() );
75 wxFile
garbage(garbagename
, wxFile::write
);
76 CPPUNIT_ASSERT( garbage
.IsOpened() );
78 CPPUNIT_ASSERT( garbage
.Write(data
, size
) == size
);
81 // try to load it by name
82 DoLoadFile(garbagename
);
84 // try to load it from a wxInputStream
85 wxMemoryInputStream
stream(data
, size
);
92 void GarbageTestCase::DoLoadFile(const wxString
& fullname
)
98 CPPUNIT_ASSERT( img
.LoadFile(fullname
) == false );
99 // test with the default wxBITMAP_TYPE_ANY
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
107 CPPUNIT_ASSERT( bmp
.LoadFile(fullname
) == false );
108 // test with the default wxBITMAP_TYPE_ANY
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
116 CPPUNIT_ASSERT( icon
.LoadFile(fullname
) == false );
117 // test with the default wxICON_DEFAULT_TYPE
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
126 CPPUNIT_ASSERT( anim
.LoadFile(fullname
) == false );
127 // test with the default wxANIMATION_TYPE_ANY
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
134 // test wxDynamicLibrary
135 wxDynamicLibrary lib
;
136 CPPUNIT_ASSERT( lib
.Load(fullname
) == false );
137 // test with the default wxANIMATION_TYPE_ANY
141 wxMediaCtrl *media = new wxMediaCtrl(wxTheApp->GetTopWindow());
142 CPPUNIT_ASSERT( media->Load(fullname) == false );
146 wxHtmlWindow *htmlwin = new wxHtmlWindow(wxTheApp->GetTopWindow());
147 CPPUNIT_ASSERT( htmlwin->LoadFile(fullname) == false );
150 // test wxXmlResource
151 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(fullname
) == false );
154 void GarbageTestCase::DoLoadStream(wxInputStream
& stream
)
158 // NOTE: not all classes tested by DoLoadFile() supports loading
159 // from an input stream!
163 CPPUNIT_ASSERT( img
.LoadFile(stream
) == false );
164 // test with the default wxBITMAP_TYPE_ANY
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
173 CPPUNIT_ASSERT( anim
.Load(stream
) == false );
174 // test with the default wxANIMATION_TYPE_ANY
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
181 wxHtmlWindow *htmlwin = new wxHtmlWindow(wxTheApp->GetTopWindow());
182 CPPUNIT_ASSERT( htmlwin->LoadFile(fullname) == false );