]>
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
7 // Copyright: (c) 2009 Francesco Montorsi
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
20 #include "wx/filename.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"
29 #define GARBAGE_DATA_SIZE 1000000 // in bytes; ~ 1MB
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 class GarbageTestCase
: public CppUnit::TestCase
41 CPPUNIT_TEST_SUITE( GarbageTestCase
);
42 CPPUNIT_TEST( LoadGarbage
);
43 CPPUNIT_TEST_SUITE_END();
46 void DoLoadFile(const wxString
& fullname
);
47 void DoLoadStream(wxInputStream
& stream
);
49 DECLARE_NO_COPY_CLASS(GarbageTestCase
)
52 // register in the unnamed registry so that these tests are run by default
53 CPPUNIT_TEST_SUITE_REGISTRATION( GarbageTestCase
);
55 // also include in it's own registry so that these tests can be run alone
56 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GarbageTestCase
, "GarbageTestCase" );
59 void GarbageTestCase::LoadGarbage()
63 wxInitAllImageHandlers();
65 for (size_t size
= 1; size
< GARBAGE_DATA_SIZE
; size
*= size
+1)
67 // first, generate some garbage data
68 unsigned char *data
= new unsigned char[size
];
69 for (size_t i
= 0; i
< size
; i
++)
73 wxString garbagename
= wxFileName::CreateTempFileName("garbage");
74 CPPUNIT_ASSERT( !garbagename
.empty() );
76 wxFile
garbage(garbagename
, wxFile::write
);
77 CPPUNIT_ASSERT( garbage
.IsOpened() );
79 CPPUNIT_ASSERT( garbage
.Write(data
, size
) == size
);
82 // try to load it by name
83 DoLoadFile(garbagename
);
85 // try to load it from a wxInputStream
86 wxMemoryInputStream
stream(data
, size
);
93 void GarbageTestCase::DoLoadFile(const wxString
& fullname
)
99 CPPUNIT_ASSERT( img
.LoadFile(fullname
) == false );
100 // test with the default wxBITMAP_TYPE_ANY
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
108 CPPUNIT_ASSERT( bmp
.LoadFile(fullname
) == false );
109 // test with the default wxBITMAP_TYPE_ANY
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
117 CPPUNIT_ASSERT( icon
.LoadFile(fullname
) == false );
118 // test with the default wxICON_DEFAULT_TYPE
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
127 CPPUNIT_ASSERT( anim
.LoadFile(fullname
) == false );
128 // test with the default wxANIMATION_TYPE_ANY
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
135 // test wxDynamicLibrary
136 wxDynamicLibrary lib
;
137 CPPUNIT_ASSERT( lib
.Load(fullname
) == false );
138 // test with the default wxANIMATION_TYPE_ANY
142 wxMediaCtrl *media = new wxMediaCtrl(wxTheApp->GetTopWindow());
143 CPPUNIT_ASSERT( media->Load(fullname) == false );
147 wxHtmlWindow *htmlwin = new wxHtmlWindow(wxTheApp->GetTopWindow());
148 CPPUNIT_ASSERT( htmlwin->LoadFile(fullname) == false );
151 // test wxXmlResource
152 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(fullname
) == false );
155 void GarbageTestCase::DoLoadStream(wxInputStream
& stream
)
159 // NOTE: not all classes tested by DoLoadFile() supports loading
160 // from an input stream!
164 CPPUNIT_ASSERT( img
.LoadFile(stream
) == false );
165 // test with the default wxBITMAP_TYPE_ANY
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
174 CPPUNIT_ASSERT( anim
.Load(stream
) == false );
175 // test with the default wxANIMATION_TYPE_ANY
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
182 wxHtmlWindow *htmlwin = new wxHtmlWindow(wxTheApp->GetTopWindow());
183 CPPUNIT_ASSERT( htmlwin->LoadFile(fullname) == false );