]>
git.saurik.com Git - wxWidgets.git/blob - tests/controls/buttontest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/buttontest.cpp
3 // Purpose: wxButton unit test
4 // Author: Steven Lamerton
6 // Copyright: (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
19 #include "wx/button.h"
22 #include "testableframe.h"
23 #include "wx/uiaction.h"
24 #include "wx/artprov.h"
25 //For CPPUNIT_ASSERT_EQUAL to work a class must have a stream output function
26 //for those classes which do not have them by default we define them in
27 //asserthelper.h so they can be reused
28 #include "asserthelper.h"
30 class ButtonTestCase
: public CppUnit::TestCase
39 CPPUNIT_TEST_SUITE( ButtonTestCase
);
40 //We add tests that use wxUIActionSimulator with WXUISIM_TEST so they
41 //are not run on platofrms were wxUIActionSimulator isn't supported
42 WXUISIM_TEST( Click
);
43 WXUISIM_TEST( Disabled
);
45 CPPUNIT_TEST( BitmapMargins
);
46 CPPUNIT_TEST( Bitmap
);
47 CPPUNIT_TEST_SUITE_END();
57 DECLARE_NO_COPY_CLASS(ButtonTestCase
)
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( ButtonTestCase
);
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ButtonTestCase
, "ButtonTestCase" );
66 void ButtonTestCase::setUp()
68 //We use wxTheApp->GetTopWindow() as there is only a single testable frame
69 //so it will always be returned
70 m_button
= new wxButton(wxTheApp
->GetTopWindow(), wxID_ANY
, "wxButton");
73 void ButtonTestCase::tearDown()
78 #if wxUSE_UIACTIONSIMULATOR
80 void ButtonTestCase::Click()
82 //We use the internal class EventCounter which handles connecting and
83 //disconnecting the control to the wxTestableFrame
84 EventCounter
clicked(m_button
, wxEVT_BUTTON
);
86 wxUIActionSimulator sim
;
88 //We move in slightly to account for window decorations, we need to yield
89 //after every wxUIActionSimulator action to keep everything working in GTK
90 sim
.MouseMove(m_button
->GetScreenPosition() + wxPoint(10, 10));
96 CPPUNIT_ASSERT_EQUAL( 1, clicked
.GetCount() );
99 void ButtonTestCase::Disabled()
101 EventCounter
clicked(m_button
, wxEVT_BUTTON
);
103 wxUIActionSimulator sim
;
105 //In this test we disable the button and check events are not sent
108 sim
.MouseMove(m_button
->GetScreenPosition() + wxPoint(10, 10));
114 CPPUNIT_ASSERT_EQUAL( 0, clicked
.GetCount() );
117 #endif // wxUSE_UIACTIONSIMULATOR
119 void ButtonTestCase::Auth()
121 //Some functions only work on specific operating system versions, for
122 //this we need a runtime check
125 if(wxGetOsVersion(&major
) != wxOS_WINDOWS_NT
|| major
< 6)
128 //We are running Windows Vista or newer
129 CPPUNIT_ASSERT(!m_button
->GetAuthNeeded());
131 m_button
->SetAuthNeeded();
133 CPPUNIT_ASSERT(m_button
->GetAuthNeeded());
135 //We test both states
136 m_button
->SetAuthNeeded(false);
138 CPPUNIT_ASSERT(!m_button
->GetAuthNeeded());
141 void ButtonTestCase::BitmapMargins()
143 //Some functions only work on specific platforms in which case we can use
144 //a preprocessor check
146 //We must set a bitmap before we can set its margins, when writing unit
147 //tests it is easiest to use an image from wxArtProvider
148 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
,
151 m_button
->SetBitmapMargins(15, 15);
153 CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button
->GetBitmapMargins());
155 m_button
->SetBitmapMargins(wxSize(20, 20));
157 CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button
->GetBitmapMargins());
161 void ButtonTestCase::Bitmap()
163 //We start with no bitmaps
164 CPPUNIT_ASSERT(!m_button
->GetBitmap().IsOk());
167 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
,
171 CPPUNIT_ASSERT(m_button
->GetBitmap().IsOk());
174 #endif //wxUSE_BUTTON