From e6bac095ca26e1c46a50698403cb5b4004c07622 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 22 Jul 2021 19:15:07 +0200 Subject: [PATCH 1/2] Get rid of CppUnit boilerplate in wxButton unit tests No real changes, just simplify the code by using CATCH directly. --- tests/controls/buttontest.cpp | 72 ++++++++++++----------------------- 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/tests/controls/buttontest.cpp b/tests/controls/buttontest.cpp index 945337a567..c6a5725646 100644 --- a/tests/controls/buttontest.cpp +++ b/tests/controls/buttontest.cpp @@ -19,62 +19,38 @@ #include "testableframe.h" #include "wx/uiaction.h" #include "wx/artprov.h" -//For CPPUNIT_ASSERT_EQUAL to work a class must have a stream output function -//for those classes which do not have them by default we define them in -//asserthelper.h so they can be reused + +// Get operator<<(wxSize) so that wxSize values are shown correctly in case of +// a failure of a CHECK() involving them. #include "asserthelper.h" -class ButtonTestCase : public CppUnit::TestCase +class ButtonTestCase { public: - ButtonTestCase() { } - - void setUp() wxOVERRIDE; - void tearDown() wxOVERRIDE; - -private: - CPPUNIT_TEST_SUITE( ButtonTestCase ); - //We add tests that use wxUIActionSimulator with WXUISIM_TEST so they - //are not run on platofrms were wxUIActionSimulator isn't supported - WXUISIM_TEST( Click ); - WXUISIM_TEST( Disabled ); - CPPUNIT_TEST( Auth ); - CPPUNIT_TEST( BitmapMargins ); - CPPUNIT_TEST( Bitmap ); - CPPUNIT_TEST_SUITE_END(); - - void Click(); - void Disabled(); - void Auth(); - void BitmapMargins(); - void Bitmap(); + ButtonTestCase(); + ~ButtonTestCase(); +protected: wxButton* m_button; wxDECLARE_NO_COPY_CLASS(ButtonTestCase); }; -// register in the unnamed registry so that these tests are run by default -CPPUNIT_TEST_SUITE_REGISTRATION( ButtonTestCase ); - -// also include in its own registry so that these tests can be run alone -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ButtonTestCase, "ButtonTestCase" ); - -void ButtonTestCase::setUp() +ButtonTestCase::ButtonTestCase() { //We use wxTheApp->GetTopWindow() as there is only a single testable frame //so it will always be returned m_button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY, "wxButton"); } -void ButtonTestCase::tearDown() +ButtonTestCase::~ButtonTestCase() { - wxDELETE(m_button); + delete m_button; } #if wxUSE_UIACTIONSIMULATOR -void ButtonTestCase::Click() +TEST_CASE_METHOD(ButtonTestCase, "Button::Click", "[button]") { //We use the internal class EventCounter which handles connecting and //disconnecting the control to the wxTestableFrame @@ -90,10 +66,10 @@ void ButtonTestCase::Click() sim.MouseClick(); wxYield(); - CPPUNIT_ASSERT_EQUAL( 1, clicked.GetCount() ); + CHECK( clicked.GetCount() == 1 ); } -void ButtonTestCase::Disabled() +TEST_CASE_METHOD(ButtonTestCase, "Button::Disabled", "[button]") { wxUIActionSimulator sim; @@ -121,12 +97,12 @@ void ButtonTestCase::Disabled() sim.MouseClick(); wxYield(); - CPPUNIT_ASSERT_EQUAL( 0, clicked.GetCount() ); + CHECK( clicked.GetCount() == 0 ); } #endif // wxUSE_UIACTIONSIMULATOR -void ButtonTestCase::Auth() +TEST_CASE_METHOD(ButtonTestCase, "Button::Auth", "[button]") { //Some functions only work on specific operating system versions, for //this we need a runtime check @@ -136,19 +112,19 @@ void ButtonTestCase::Auth() return; //We are running Windows Vista or newer - CPPUNIT_ASSERT(!m_button->GetAuthNeeded()); + CHECK(!m_button->GetAuthNeeded()); m_button->SetAuthNeeded(); - CPPUNIT_ASSERT(m_button->GetAuthNeeded()); + CHECK(m_button->GetAuthNeeded()); //We test both states m_button->SetAuthNeeded(false); - CPPUNIT_ASSERT(!m_button->GetAuthNeeded()); + CHECK(!m_button->GetAuthNeeded()); } -void ButtonTestCase::BitmapMargins() +TEST_CASE_METHOD(ButtonTestCase, "Button::BitmapMargins", "[button]") { //Some functions only work on specific platforms in which case we can use //a preprocessor check @@ -160,25 +136,25 @@ void ButtonTestCase::BitmapMargins() m_button->SetBitmapMargins(15, 15); - CPPUNIT_ASSERT_EQUAL(wxSize(15, 15), m_button->GetBitmapMargins()); + CHECK( m_button->GetBitmapMargins() == wxSize(15, 15) ); m_button->SetBitmapMargins(wxSize(20, 20)); - CPPUNIT_ASSERT_EQUAL(wxSize(20, 20), m_button->GetBitmapMargins()); + CHECK( m_button->GetBitmapMargins() == wxSize(20, 20) ); #endif } -void ButtonTestCase::Bitmap() +TEST_CASE_METHOD(ButtonTestCase, "Button::Bitmap", "[button]") { //We start with no bitmaps - CPPUNIT_ASSERT(!m_button->GetBitmap().IsOk()); + CHECK(!m_button->GetBitmap().IsOk()); // Some bitmap, doesn't really matter which. const wxBitmap bmp = wxArtProvider::GetBitmap(wxART_INFORMATION); m_button->SetBitmap(bmp); - CPPUNIT_ASSERT(m_button->GetBitmap().IsOk()); + CHECK(m_button->GetBitmap().IsOk()); // Check that resetting the button label doesn't result in problems when // updating the bitmap later, as it used to be the case in wxGTK (#18898). From 9dbf063a75773a25b991cd8170d729fd195149bc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 22 Jul 2021 19:15:50 +0200 Subject: [PATCH 2/2] Fix ProcessEnter unit tests when running it on its own with wxGTK Apparently setting focus doesn't work until a mouse click is simulated to bring up the main test window to the front. This happened implicitly when running the full test suite, due to e.g. doing it in wxButton unit tests, which come before wxComboBox::ProcessEnter test, but not when running just this test (or wxTextCtrl::ProcessEnter) on its own. Make the test self-contained by simulating a mouse click before simulating pressing "Enter". --- tests/controls/textentrytest.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/controls/textentrytest.cpp b/tests/controls/textentrytest.cpp index e24db45622..ee689bad64 100644 --- a/tests/controls/textentrytest.cpp +++ b/tests/controls/textentrytest.cpp @@ -464,7 +464,21 @@ private: void SimulateEnter() { wxUIActionSimulator sim; + + // Calling SetFocus() is somehow not enough to give the focus to this + // window when running this test with wxGTK, apparently because the + // dialog itself needs to be raised to the front first, so simulate a + // click doing this. + sim.MouseMove(m_control->GetScreenPosition() + wxPoint(5, 5)); + wxYield(); + sim.MouseClick(); + wxYield(); + + // Note that clicking it is still not enough to give it focus with + // wxGTK neither, so we still need to call SetFocus() nevertheless: but + // now it works. m_control->SetFocus(); + sim.Char(WXK_RETURN); }