]>
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
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 its 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 //We use the internal class EventCounter which handles connecting and
84 //disconnecting the control to the wxTestableFrame
85 EventCounter
clicked(m_button
, wxEVT_BUTTON
);
87 wxUIActionSimulator sim
;
89 //We move in slightly to account for window decorations, we need to yield
90 //after every wxUIActionSimulator action to keep everything working in GTK
91 sim
.MouseMove(m_button
->GetScreenPosition() + wxPoint(10, 10));
97 CPPUNIT_ASSERT_EQUAL( 1, clicked
.GetCount() );
100 void ButtonTestCase::Disabled()
102 EventCounter
clicked(m_button
, wxEVT_BUTTON
);
104 wxUIActionSimulator sim
;
106 //In this test we disable the button and check events are not sent
109 sim
.MouseMove(m_button
->GetScreenPosition() + wxPoint(10, 10));
115 CPPUNIT_ASSERT_EQUAL( 0, clicked
.GetCount() );
118 #endif // wxUSE_UIACTIONSIMULATOR
120 void ButtonTestCase::Auth()
122 //Some functions only work on specific operating system versions, for
123 //this we need a runtime check
126 if(wxGetOsVersion(&major
) != wxOS_WINDOWS_NT
|| major
< 6)
129 //We are running Windows Vista or newer
130 CPPUNIT_ASSERT(!m_button
->GetAuthNeeded());
132 m_button
->SetAuthNeeded();
134 CPPUNIT_ASSERT(m_button
->GetAuthNeeded());
136 //We test both states
137 m_button
->SetAuthNeeded(false);
139 CPPUNIT_ASSERT(!m_button
->GetAuthNeeded());
142 void ButtonTestCase::BitmapMargins()
144 //Some functions only work on specific platforms in which case we can use
145 //a preprocessor check
147 //We must set a bitmap before we can set its margins, when writing unit
148 //tests it is easiest to use an image from wxArtProvider
149 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
, wxART_OTHER
,
152 m_button
->SetBitmapMargins(15, 15);
154 CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button
->GetBitmapMargins());
156 m_button
->SetBitmapMargins(wxSize(20, 20));
158 CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button
->GetBitmapMargins());
162 void ButtonTestCase::Bitmap()
164 //We start with no bitmaps
165 CPPUNIT_ASSERT(!m_button
->GetBitmap().IsOk());
168 m_button
->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION
,
172 CPPUNIT_ASSERT(m_button
->GetBitmap().IsOk());
175 #endif //wxUSE_BUTTON