1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/buttontest.cpp
3 // Purpose: wxButton unit test
4 // Author: Steven Lamerton
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
20 #include "wx/button.h"
23 #include "testableframe.h"
24 #include "wx/uiaction.h"
25 #include "wx/artprov.h"
26 //For CPPUNIT_ASSERT_EQUAL to work a class must have a stream output function
27 //for those classes which do not have them by default we define them in
28 //asserthelper.h so they can be reused
29 #include "asserthelper.h"
31 class ButtonTestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE( ButtonTestCase
);
41 //We add tests that use wxUIActionSimulator with WXUISIM_TEST so they
42 //are not run on platofrms were wxUIActionSimulator isn't supported
43 WXUISIM_TEST( Click
);
44 WXUISIM_TEST( Disabled
);
46 CPPUNIT_TEST( BitmapMargins
);
47 CPPUNIT_TEST( Bitmap
);
48 CPPUNIT_TEST_SUITE_END();
58 DECLARE_NO_COPY_CLASS(ButtonTestCase
)
61 // register in the unnamed registry so that these tests are run by default
62 CPPUNIT_TEST_SUITE_REGISTRATION( ButtonTestCase
);
64 // also include in it's own registry so that these tests can be run alone
65 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ButtonTestCase
, "ButtonTestCase" );
67 void ButtonTestCase::setUp()
69 //We use wxTheApp->GetTopWindow() as there is only a single testable frame
70 //so it will always be returned
71 m_button
= new wxButton(wxTheApp
->GetTopWindow(), wxID_ANY
, "wxButton");
74 void ButtonTestCase::tearDown()
79 #if wxUSE_UIACTIONSIMULATOR
81 void ButtonTestCase::Click()
83 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
86 //We use the internal class EventCounter which handles connecting and
87 //disconnecting the control to the wxTestableFrame
88 EventCounter
count(m_button
, wxEVT_COMMAND_BUTTON_CLICKED
);
90 wxUIActionSimulator sim
;
92 //We move in slightly to account for window decorations, we need to yield
93 //after every wxUIActionSimulator action to keep everything working in GTK
94 sim
.MouseMove(m_button
->GetScreenPosition() + wxPoint(10, 10));
100 CPPUNIT_ASSERT_EQUAL( 1, frame
->GetEventCount() );
103 void ButtonTestCase::Disabled()
105 wxTestableFrame
* frame
= wxStaticCast(wxTheApp
->GetTopWindow(),
108 EventCounter
count(m_button
, wxEVT_COMMAND_BUTTON_CLICKED
);
110 wxUIActionSimulator sim
;
112 //In this test we disable the button and check events are not sent
115 sim
.MouseMove(m_button
->GetScreenPosition() + wxPoint(10, 10));
121 CPPUNIT_ASSERT_EQUAL( 0, frame
->GetEventCount() );
124 #endif // wxUSE_UIACTIONSIMULATOR
126 void ButtonTestCase::Auth()
128 //Some functions only work on specific operating system versions, for
129 //this we need a runtime check
132 if(wxGetOsVersion(&major
) != wxOS_WINDOWS_NT
|| major
< 6)
135 //We are running Windows Vista or newer
136 CPPUNIT_ASSERT(!m_button
->GetAuthNeeded());
138 m_button
->SetAuthNeeded();
140 CPPUNIT_ASSERT(m_button
->GetAuthNeeded());
142 //We test both states
143 m_button
->SetAuthNeeded(false);
145 CPPUNIT_ASSERT(!m_button
->GetAuthNeeded());
148 void ButtonTestCase::BitmapMargins()
150 //Some functions only work on specific platfroms in which case we can use
151 //a preprocessor check
153 //We must set a bitmap before we can set its margins, when writing unit
154 //tests it is easiest to use an image from wxArtProvider
155 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
,
158 m_button
->SetBitmapMargins(15, 15);
160 CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button
->GetBitmapMargins());
162 m_button
->SetBitmapMargins(wxSize(20, 20));
164 CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button
->GetBitmapMargins());
168 void ButtonTestCase::Bitmap()
170 //We start with no bitmaps
171 CPPUNIT_ASSERT(!m_button
->GetBitmap().IsOk());
174 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
,
178 CPPUNIT_ASSERT(m_button
->GetBitmap().IsOk());
181 #endif //wxUSE_BUTTON