Simplify ButtonTestCase::Bitmap. The old test was not correct as it tested all bitmap...
[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 // RCS-ID: $Id$
7 // Copyright: (c) 2010 Steven Lamerton
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #if wxUSE_BUTTON
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/button.h"
21 #endif // WX_PRECOMP
22
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"
30
31 class ButtonTestCase : public CppUnit::TestCase
32 {
33 public:
34 ButtonTestCase() { }
35
36 void setUp();
37 void tearDown();
38
39 private:
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 );
45 CPPUNIT_TEST( Auth );
46 CPPUNIT_TEST( BitmapMargins );
47 CPPUNIT_TEST( Bitmap );
48 CPPUNIT_TEST_SUITE_END();
49
50 void Click();
51 void Disabled();
52 void Auth();
53 void BitmapMargins();
54 void Bitmap();
55
56 wxButton* m_button;
57
58 DECLARE_NO_COPY_CLASS(ButtonTestCase)
59 };
60
61 // register in the unnamed registry so that these tests are run by default
62 CPPUNIT_TEST_SUITE_REGISTRATION( ButtonTestCase );
63
64 // also include in it's own registry so that these tests can be run alone
65 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ButtonTestCase, "ButtonTestCase" );
66
67 void ButtonTestCase::setUp()
68 {
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");
72 }
73
74 void ButtonTestCase::tearDown()
75 {
76 wxDELETE(m_button);
77 }
78
79 #if wxUSE_UIACTIONSIMULATOR
80
81 void ButtonTestCase::Click()
82 {
83 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
84 wxTestableFrame);
85
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);
89
90 wxUIActionSimulator sim;
91
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));
95 wxYield();
96
97 sim.MouseClick();
98 wxYield();
99
100 CPPUNIT_ASSERT_EQUAL( 1, frame->GetEventCount() );
101 }
102
103 void ButtonTestCase::Disabled()
104 {
105 wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(),
106 wxTestableFrame);
107
108 EventCounter count(m_button, wxEVT_COMMAND_BUTTON_CLICKED);
109
110 wxUIActionSimulator sim;
111
112 //In this test we disable the button and check events are not sent
113 m_button->Disable();
114
115 sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
116 wxYield();
117
118 sim.MouseClick();
119 wxYield();
120
121 CPPUNIT_ASSERT_EQUAL( 0, frame->GetEventCount() );
122 }
123
124 #endif // wxUSE_UIACTIONSIMULATOR
125
126 void ButtonTestCase::Auth()
127 {
128 //Some functions only work on specific operating system versions, for
129 //this we need a runtime check
130 int major = 0;
131
132 if(wxGetOsVersion(&major) != wxOS_WINDOWS_NT || major < 6)
133 return;
134
135 //We are running Windows Vista or newer
136 CPPUNIT_ASSERT(!m_button->GetAuthNeeded());
137
138 m_button->SetAuthNeeded();
139
140 CPPUNIT_ASSERT(m_button->GetAuthNeeded());
141
142 //We test both states
143 m_button->SetAuthNeeded(false);
144
145 CPPUNIT_ASSERT(!m_button->GetAuthNeeded());
146 }
147
148 void ButtonTestCase::BitmapMargins()
149 {
150 //Some functions only work on specific platfroms in which case we can use
151 //a preprocessor check
152 #ifdef __WXMSW__
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,
156 wxSize(32, 32)));
157
158 m_button->SetBitmapMargins(15, 15);
159
160 CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button->GetBitmapMargins());
161
162 m_button->SetBitmapMargins(wxSize(20, 20));
163
164 CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button->GetBitmapMargins());
165 #endif
166 }
167
168 void ButtonTestCase::Bitmap()
169 {
170 //We start with no bitmaps
171 CPPUNIT_ASSERT(!m_button->GetBitmap().IsOk());
172
173
174 m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION,
175 wxART_OTHER,
176 wxSize(32, 32)));
177
178 CPPUNIT_ASSERT(m_button->GetBitmap().IsOk());
179 }
180
181 #endif //wxUSE_BUTTON