buttontest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "testprec.h"
  9. #if wxUSE_BUTTON
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. #ifndef WX_PRECOMP
  14. #include "wx/app.h"
  15. #include "wx/button.h"
  16. #endif // WX_PRECOMP
  17. #include "testableframe.h"
  18. #include "wx/uiaction.h"
  19. #include "wx/artprov.h"
  20. //For CPPUNIT_ASSERT_EQUAL to work a class must have a stream output function
  21. //for those classes which do not have them by default we define them in
  22. //asserthelper.h so they can be reused
  23. #include "asserthelper.h"
  24. class ButtonTestCase : public CppUnit::TestCase
  25. {
  26. public:
  27. ButtonTestCase() { }
  28. void setUp();
  29. void tearDown();
  30. private:
  31. CPPUNIT_TEST_SUITE( ButtonTestCase );
  32. //We add tests that use wxUIActionSimulator with WXUISIM_TEST so they
  33. //are not run on platofrms were wxUIActionSimulator isn't supported
  34. WXUISIM_TEST( Click );
  35. WXUISIM_TEST( Disabled );
  36. CPPUNIT_TEST( Auth );
  37. CPPUNIT_TEST( BitmapMargins );
  38. CPPUNIT_TEST( Bitmap );
  39. CPPUNIT_TEST_SUITE_END();
  40. void Click();
  41. void Disabled();
  42. void Auth();
  43. void BitmapMargins();
  44. void Bitmap();
  45. wxButton* m_button;
  46. DECLARE_NO_COPY_CLASS(ButtonTestCase)
  47. };
  48. // register in the unnamed registry so that these tests are run by default
  49. CPPUNIT_TEST_SUITE_REGISTRATION( ButtonTestCase );
  50. // also include in its own registry so that these tests can be run alone
  51. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ButtonTestCase, "ButtonTestCase" );
  52. void ButtonTestCase::setUp()
  53. {
  54. //We use wxTheApp->GetTopWindow() as there is only a single testable frame
  55. //so it will always be returned
  56. m_button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY, "wxButton");
  57. }
  58. void ButtonTestCase::tearDown()
  59. {
  60. wxDELETE(m_button);
  61. }
  62. #if wxUSE_UIACTIONSIMULATOR
  63. void ButtonTestCase::Click()
  64. {
  65. //We use the internal class EventCounter which handles connecting and
  66. //disconnecting the control to the wxTestableFrame
  67. EventCounter clicked(m_button, wxEVT_BUTTON);
  68. wxUIActionSimulator sim;
  69. //We move in slightly to account for window decorations, we need to yield
  70. //after every wxUIActionSimulator action to keep everything working in GTK
  71. sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
  72. wxYield();
  73. sim.MouseClick();
  74. wxYield();
  75. CPPUNIT_ASSERT_EQUAL( 1, clicked.GetCount() );
  76. }
  77. void ButtonTestCase::Disabled()
  78. {
  79. EventCounter clicked(m_button, wxEVT_BUTTON);
  80. wxUIActionSimulator sim;
  81. //In this test we disable the button and check events are not sent
  82. m_button->Disable();
  83. sim.MouseMove(m_button->GetScreenPosition() + wxPoint(10, 10));
  84. wxYield();
  85. sim.MouseClick();
  86. wxYield();
  87. CPPUNIT_ASSERT_EQUAL( 0, clicked.GetCount() );
  88. }
  89. #endif // wxUSE_UIACTIONSIMULATOR
  90. void ButtonTestCase::Auth()
  91. {
  92. //Some functions only work on specific operating system versions, for
  93. //this we need a runtime check
  94. int major = 0;
  95. if(wxGetOsVersion(&major) != wxOS_WINDOWS_NT || major < 6)
  96. return;
  97. //We are running Windows Vista or newer
  98. CPPUNIT_ASSERT(!m_button->GetAuthNeeded());
  99. m_button->SetAuthNeeded();
  100. CPPUNIT_ASSERT(m_button->GetAuthNeeded());
  101. //We test both states
  102. m_button->SetAuthNeeded(false);
  103. CPPUNIT_ASSERT(!m_button->GetAuthNeeded());
  104. }
  105. void ButtonTestCase::BitmapMargins()
  106. {
  107. //Some functions only work on specific platforms in which case we can use
  108. //a preprocessor check
  109. #ifdef __WXMSW__
  110. //We must set a bitmap before we can set its margins, when writing unit
  111. //tests it is easiest to use an image from wxArtProvider
  112. m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION, wxART_OTHER,
  113. wxSize(32, 32)));
  114. m_button->SetBitmapMargins(15, 15);
  115. CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button->GetBitmapMargins());
  116. m_button->SetBitmapMargins(wxSize(20, 20));
  117. CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button->GetBitmapMargins());
  118. #endif
  119. }
  120. void ButtonTestCase::Bitmap()
  121. {
  122. //We start with no bitmaps
  123. CPPUNIT_ASSERT(!m_button->GetBitmap().IsOk());
  124. m_button->SetBitmap(wxArtProvider::GetIcon(wxART_INFORMATION,
  125. wxART_OTHER,
  126. wxSize(32, 32)));
  127. CPPUNIT_ASSERT(m_button->GetBitmap().IsOk());
  128. }
  129. #endif //wxUSE_BUTTON