Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / tests / controls / buttontest.cpp
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 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #ifndef WX_PRECOMP
18 #include "wx/app.h"
19 #include "wx/button.h"
20 #endif // WX_PRECOMP
21
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"
29
30 class ButtonTestCase : public CppUnit::TestCase
31 {
32 public:
33 ButtonTestCase() { }
34
35 void setUp();
36 void tearDown();
37
38 private:
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 );
44 CPPUNIT_TEST( Auth );
45 CPPUNIT_TEST( BitmapMargins );
46 CPPUNIT_TEST( Bitmap );
47 CPPUNIT_TEST_SUITE_END();
48
49 void Click();
50 void Disabled();
51 void Auth();
52 void BitmapMargins();
53 void Bitmap();
54
55 wxButton* m_button;
56
57 DECLARE_NO_COPY_CLASS(ButtonTestCase)
58 };
59
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( ButtonTestCase );
62
63 // also include in its own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ButtonTestCase, "ButtonTestCase" );
65
66 void ButtonTestCase::setUp()
67 {
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");
71 }
72
73 void ButtonTestCase::tearDown()
74 {
75 wxDELETE(m_button);
76 }
77
78 #if wxUSE_UIACTIONSIMULATOR
79
80 void ButtonTestCase::Click()
81 {
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);
85
86 wxUIActionSimulator sim;
87
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));
91 wxYield();
92
93 sim.MouseClick();
94 wxYield();
95
96 CPPUNIT_ASSERT_EQUAL( 1, clicked.GetCount() );
97 }
98
99 void ButtonTestCase::Disabled()
100 {
101 EventCounter clicked(m_button, wxEVT_BUTTON);
102
103 wxUIActionSimulator sim;
104
105 //In this test we disable the button and check events are not sent
106 m_button->Disable();
107
108 sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
109 wxYield();
110
111 sim.MouseClick();
112 wxYield();
113
114 CPPUNIT_ASSERT_EQUAL( 0, clicked.GetCount() );
115 }
116
117 #endif // wxUSE_UIACTIONSIMULATOR
118
119 void ButtonTestCase::Auth()
120 {
121 //Some functions only work on specific operating system versions, for
122 //this we need a runtime check
123 int major = 0;
124
125 if(wxGetOsVersion(&major) != wxOS_WINDOWS_NT || major < 6)
126 return;
127
128 //We are running Windows Vista or newer
129 CPPUNIT_ASSERT(!m_button->GetAuthNeeded());
130
131 m_button->SetAuthNeeded();
132
133 CPPUNIT_ASSERT(m_button->GetAuthNeeded());
134
135 //We test both states
136 m_button->SetAuthNeeded(false);
137
138 CPPUNIT_ASSERT(!m_button->GetAuthNeeded());
139 }
140
141 void ButtonTestCase::BitmapMargins()
142 {
143 //Some functions only work on specific platforms in which case we can use
144 //a preprocessor check
145 #ifdef __WXMSW__
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,
149 wxSize(32, 32)));
150
151 m_button->SetBitmapMargins(15, 15);
152
153 CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button->GetBitmapMargins());
154
155 m_button->SetBitmapMargins(wxSize(20, 20));
156
157 CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button->GetBitmapMargins());
158 #endif
159 }
160
161 void ButtonTestCase::Bitmap()
162 {
163 //We start with no bitmaps
164 CPPUNIT_ASSERT(!m_button->GetBitmap().IsOk());
165
166
167 m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION,
168 wxART_OTHER,
169 wxSize(32, 32)));
170
171 CPPUNIT_ASSERT(m_button->GetBitmap().IsOk());
172 }
173
174 #endif //wxUSE_BUTTON