1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/controls/buttontest.cpp
3 // Purpose:     wxButton unit test
4 // Author:      Steven Lamerton
5 // Created:     2010-06-21
6 // Copyright:   (c) 2010 Steven Lamerton
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 #include "testprec.h"
10 
11 #if wxUSE_BUTTON
12 
13 
14 #ifndef WX_PRECOMP
15     #include "wx/app.h"
16     #include "wx/button.h"
17 #endif // WX_PRECOMP
18 
19 #include "testableframe.h"
20 #include "wx/uiaction.h"
21 #include "wx/artprov.h"
22 //For CPPUNIT_ASSERT_EQUAL to work a class must have a stream output function
23 //for those classes which do not have them by default we define them in
24 //asserthelper.h so they can be reused
25 #include "asserthelper.h"
26 
27 class ButtonTestCase : public CppUnit::TestCase
28 {
29 public:
ButtonTestCase()30     ButtonTestCase() { }
31 
32     void setUp() wxOVERRIDE;
33     void tearDown() wxOVERRIDE;
34 
35 private:
36     CPPUNIT_TEST_SUITE( ButtonTestCase );
37         //We add tests that use wxUIActionSimulator with WXUISIM_TEST so they
38         //are not run on platofrms were wxUIActionSimulator isn't supported
39         WXUISIM_TEST( Click );
40         WXUISIM_TEST( Disabled );
41         CPPUNIT_TEST( Auth );
42         CPPUNIT_TEST( BitmapMargins );
43         CPPUNIT_TEST( Bitmap );
44     CPPUNIT_TEST_SUITE_END();
45 
46     void Click();
47     void Disabled();
48     void Auth();
49     void BitmapMargins();
50     void Bitmap();
51 
52     wxButton* m_button;
53 
54     wxDECLARE_NO_COPY_CLASS(ButtonTestCase);
55 };
56 
57 // register in the unnamed registry so that these tests are run by default
58 CPPUNIT_TEST_SUITE_REGISTRATION( ButtonTestCase );
59 
60 // also include in its own registry so that these tests can be run alone
61 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ButtonTestCase, "ButtonTestCase" );
62 
setUp()63 void ButtonTestCase::setUp()
64 {
65     //We use wxTheApp->GetTopWindow() as there is only a single testable frame
66     //so it will always be returned
67     m_button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY, "wxButton");
68 }
69 
tearDown()70 void ButtonTestCase::tearDown()
71 {
72     wxDELETE(m_button);
73 }
74 
75 #if wxUSE_UIACTIONSIMULATOR
76 
Click()77 void ButtonTestCase::Click()
78 {
79     //We use the internal class EventCounter which handles connecting and
80     //disconnecting the control to the wxTestableFrame
81     EventCounter clicked(m_button, wxEVT_BUTTON);
82 
83     wxUIActionSimulator sim;
84 
85     //We move in slightly to account for window decorations, we need to yield
86     //after every wxUIActionSimulator action to keep everything working in GTK
87     sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
88     wxYield();
89 
90     sim.MouseClick();
91     wxYield();
92 
93     CPPUNIT_ASSERT_EQUAL( 1, clicked.GetCount() );
94 }
95 
Disabled()96 void ButtonTestCase::Disabled()
97 {
98     wxUIActionSimulator sim;
99 
100     // In this test we disable the button and check events are not sent and we
101     // do it once by disabling the previously enabled button and once by
102     // creating the button in the disabled state.
103     SECTION("Disable after creation")
104     {
105         m_button->Disable();
106     }
107 
108     SECTION("Create disabled")
109     {
110         delete m_button;
111         m_button = new wxButton();
112         m_button->Disable();
113         m_button->Create(wxTheApp->GetTopWindow(), wxID_ANY, "wxButton");
114     }
115 
116     EventCounter clicked(m_button, wxEVT_BUTTON);
117 
118     sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
119     wxYield();
120 
121     sim.MouseClick();
122     wxYield();
123 
124     CPPUNIT_ASSERT_EQUAL( 0, clicked.GetCount() );
125 }
126 
127 #endif // wxUSE_UIACTIONSIMULATOR
128 
Auth()129 void ButtonTestCase::Auth()
130 {
131     //Some functions only work on specific operating system versions, for
132     //this we need a runtime check
133     int major = 0;
134 
135     if(wxGetOsVersion(&major) != wxOS_WINDOWS_NT || major < 6)
136         return;
137 
138     //We are running Windows Vista or newer
139     CPPUNIT_ASSERT(!m_button->GetAuthNeeded());
140 
141     m_button->SetAuthNeeded();
142 
143     CPPUNIT_ASSERT(m_button->GetAuthNeeded());
144 
145     //We test both states
146     m_button->SetAuthNeeded(false);
147 
148     CPPUNIT_ASSERT(!m_button->GetAuthNeeded());
149 }
150 
BitmapMargins()151 void ButtonTestCase::BitmapMargins()
152 {
153     //Some functions only work on specific platforms in which case we can use
154     //a preprocessor check
155 #ifdef __WXMSW__
156     //We must set a bitmap before we can set its margins, when writing unit
157     //tests it is easiest to use an image from wxArtProvider
158     m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER,
159                                                wxSize(32, 32)));
160 
161     m_button->SetBitmapMargins(15, 15);
162 
163     CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button->GetBitmapMargins());
164 
165     m_button->SetBitmapMargins(wxSize(20, 20));
166 
167     CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button->GetBitmapMargins());
168 #endif
169 }
170 
Bitmap()171 void ButtonTestCase::Bitmap()
172 {
173     //We start with no bitmaps
174     CPPUNIT_ASSERT(!m_button->GetBitmap().IsOk());
175 
176     // Some bitmap, doesn't really matter which.
177     const wxBitmap bmp = wxArtProvider::GetBitmap(wxART_INFORMATION);
178 
179     m_button->SetBitmap(bmp);
180 
181     CPPUNIT_ASSERT(m_button->GetBitmap().IsOk());
182 
183     // Check that resetting the button label doesn't result in problems when
184     // updating the bitmap later, as it used to be the case in wxGTK (#18898).
185     m_button->SetLabel(wxString());
186     CHECK_NOTHROW( m_button->Disable() );
187 }
188 
189 #endif //wxUSE_BUTTON
190