| 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 its 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 | //We use the internal class EventCounter which handles connecting and |
| 84 | //disconnecting the control to the wxTestableFrame |
| 85 | EventCounter clicked(m_button, wxEVT_COMMAND_BUTTON_CLICKED); |
| 86 | |
| 87 | wxUIActionSimulator sim; |
| 88 | |
| 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)); |
| 92 | wxYield(); |
| 93 | |
| 94 | sim.MouseClick(); |
| 95 | wxYield(); |
| 96 | |
| 97 | CPPUNIT_ASSERT_EQUAL( 1, clicked.GetCount() ); |
| 98 | } |
| 99 | |
| 100 | void ButtonTestCase::Disabled() |
| 101 | { |
| 102 | EventCounter clicked(m_button, wxEVT_COMMAND_BUTTON_CLICKED); |
| 103 | |
| 104 | wxUIActionSimulator sim; |
| 105 | |
| 106 | //In this test we disable the button and check events are not sent |
| 107 | m_button->Disable(); |
| 108 | |
| 109 | sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10)); |
| 110 | wxYield(); |
| 111 | |
| 112 | sim.MouseClick(); |
| 113 | wxYield(); |
| 114 | |
| 115 | CPPUNIT_ASSERT_EQUAL( 0, clicked.GetCount() ); |
| 116 | } |
| 117 | |
| 118 | #endif // wxUSE_UIACTIONSIMULATOR |
| 119 | |
| 120 | void ButtonTestCase::Auth() |
| 121 | { |
| 122 | //Some functions only work on specific operating system versions, for |
| 123 | //this we need a runtime check |
| 124 | int major = 0; |
| 125 | |
| 126 | if(wxGetOsVersion(&major) != wxOS_WINDOWS_NT || major < 6) |
| 127 | return; |
| 128 | |
| 129 | //We are running Windows Vista or newer |
| 130 | CPPUNIT_ASSERT(!m_button->GetAuthNeeded()); |
| 131 | |
| 132 | m_button->SetAuthNeeded(); |
| 133 | |
| 134 | CPPUNIT_ASSERT(m_button->GetAuthNeeded()); |
| 135 | |
| 136 | //We test both states |
| 137 | m_button->SetAuthNeeded(false); |
| 138 | |
| 139 | CPPUNIT_ASSERT(!m_button->GetAuthNeeded()); |
| 140 | } |
| 141 | |
| 142 | void ButtonTestCase::BitmapMargins() |
| 143 | { |
| 144 | //Some functions only work on specific platforms in which case we can use |
| 145 | //a preprocessor check |
| 146 | #ifdef __WXMSW__ |
| 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, |
| 150 | wxSize(32, 32))); |
| 151 | |
| 152 | m_button->SetBitmapMargins(15, 15); |
| 153 | |
| 154 | CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button->GetBitmapMargins()); |
| 155 | |
| 156 | m_button->SetBitmapMargins(wxSize(20, 20)); |
| 157 | |
| 158 | CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button->GetBitmapMargins()); |
| 159 | #endif |
| 160 | } |
| 161 | |
| 162 | void ButtonTestCase::Bitmap() |
| 163 | { |
| 164 | //We start with no bitmaps |
| 165 | CPPUNIT_ASSERT(!m_button->GetBitmap().IsOk()); |
| 166 | |
| 167 | |
| 168 | m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION, |
| 169 | wxART_OTHER, |
| 170 | wxSize(32, 32))); |
| 171 | |
| 172 | CPPUNIT_ASSERT(m_button->GetBitmap().IsOk()); |
| 173 | } |
| 174 | |
| 175 | #endif //wxUSE_BUTTON |