From 08d8d128729a0646528e3cddabe83e1cf1233ace Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 25 Jan 2021 00:48:30 +0100 Subject: [PATCH 1/3] Get rid of CppUnit boilerplate in wxGridSizer unit test No real changes, just simplify and use CHECK() instead of CPPUNIT_ASSERT_EQUAL() (which is the same as REQUIRE()). --- tests/sizers/gridsizer.cpp | 76 ++++++++++++++------------------------ 1 file changed, 28 insertions(+), 48 deletions(-) diff --git a/tests/sizers/gridsizer.cpp b/tests/sizers/gridsizer.cpp index fde57efc39..0be0479a42 100644 --- a/tests/sizers/gridsizer.cpp +++ b/tests/sizers/gridsizer.cpp @@ -25,23 +25,11 @@ // test class // ---------------------------------------------------------------------------- -class GridSizerTestCase : public CppUnit::TestCase +class GridSizerTestCase { -public: - GridSizerTestCase() { } - - virtual void setUp() wxOVERRIDE; - virtual void tearDown() wxOVERRIDE; - -private: - CPPUNIT_TEST_SUITE( GridSizerTestCase ); - CPPUNIT_TEST( Expand ); - CPPUNIT_TEST( IncompatibleFlags ); - CPPUNIT_TEST_SUITE_END(); - - void Expand(); - void IncompatibleFlags(); - +protected: + GridSizerTestCase(); + ~GridSizerTestCase(); // Clear the current sizer contents and add the specified windows to it, // using the same flags for all of them. void SetChildren(const wxVector& children, @@ -53,17 +41,11 @@ private: wxDECLARE_NO_COPY_CLASS(GridSizerTestCase); }; -// register in the unnamed registry so that these tests are run by default -CPPUNIT_TEST_SUITE_REGISTRATION( GridSizerTestCase ); - -// also include in its own registry so that these tests can be run alone -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GridSizerTestCase, "GridSizerTestCase" ); - // ---------------------------------------------------------------------------- // test initialization // ---------------------------------------------------------------------------- -void GridSizerTestCase::setUp() +GridSizerTestCase::GridSizerTestCase() { m_win = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY); m_win->SetClientSize(127, 35); @@ -72,7 +54,7 @@ void GridSizerTestCase::setUp() m_win->SetSizer(m_sizer); } -void GridSizerTestCase::tearDown() +GridSizerTestCase::~GridSizerTestCase() { delete m_win; m_win = NULL; @@ -102,7 +84,9 @@ void GridSizerTestCase::SetChildren(const wxVector& children, // tests themselves // ---------------------------------------------------------------------------- -void GridSizerTestCase::Expand() +TEST_CASE_METHOD(GridSizerTestCase, + "wxGridSizer::Expand", + "[grid-sizer][sizer]") { const wxSize sizeTotal = m_win->GetClientSize(); const wxSize sizeChild(sizeTotal.x / 4, sizeTotal.y / 4); @@ -121,42 +105,38 @@ void GridSizerTestCase::Expand() // Without Expand() windows have their initial size. SetChildren(children, wxSizerFlags()); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[0]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[1]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[2]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[3]->GetSize() ); + CHECK( children[0]->GetSize() == sizeChild ); + CHECK( children[1]->GetSize() == sizeChild ); + CHECK( children[2]->GetSize() == sizeChild ); + CHECK( children[3]->GetSize() == sizeChild ); // With just expand, they expand to fill the entire column and the row // containing them (which may or not expand on its own). SetChildren(children, wxSizerFlags().Expand()); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[0]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( wxSize(sizeRest.x, sizeChild.y), - children[1]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( wxSize(sizeChild.x, sizeRest.y), - children[2]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( sizeRest, children[3]->GetSize() ); + CHECK( children[0]->GetSize() == sizeChild ); + CHECK( children[1]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); + CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); + CHECK( children[3]->GetSize() == sizeRest ); // With expand and another alignment flag, they should expand only in the // direction in which the alignment is not given. SetChildren(children, wxSizerFlags().Expand().CentreVertical()); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[0]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( wxSize(sizeRest.x, sizeChild.y), - children[1]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[2]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( wxSize(sizeRest.x, sizeChild.y), - children[3]->GetSize() ); + CHECK( children[0]->GetSize() == sizeChild ); + CHECK( children[1]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); + CHECK( children[2]->GetSize() == sizeChild ); + CHECK( children[3]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); // Same as above but mirrored. SetChildren(children, wxSizerFlags().Expand().CentreHorizontal()); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[0]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( sizeChild, children[1]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( wxSize(sizeChild.x, sizeRest.y), - children[2]->GetSize() ); - CPPUNIT_ASSERT_EQUAL( wxSize(sizeChild.x, sizeRest.y), - children[3]->GetSize() ); + CHECK( children[0]->GetSize() == sizeChild ); + CHECK( children[1]->GetSize() == sizeChild ); + CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); + CHECK( children[3]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); } -void GridSizerTestCase::IncompatibleFlags() +TEST_CASE_METHOD(GridSizerTestCase, + "wxGridSizer::IncompatibleFlags", + "[grid-sizer][sizer]") { WX_ASSERT_FAILS_WITH_ASSERT_MESSAGE ( From caf5a62fd954df28ed232df6e41eac4b506875ec Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 25 Jan 2021 00:53:24 +0100 Subject: [PATCH 2/3] Use different sections for different flags in wxGridSizer test No real changes, just organize the tests a bit better. --- tests/sizers/gridsizer.cpp | 52 +++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/tests/sizers/gridsizer.cpp b/tests/sizers/gridsizer.cpp index 0be0479a42..fc3c130f03 100644 --- a/tests/sizers/gridsizer.cpp +++ b/tests/sizers/gridsizer.cpp @@ -104,34 +104,46 @@ TEST_CASE_METHOD(GridSizerTestCase, m_sizer->AddGrowableCol(1); // Without Expand() windows have their initial size. - SetChildren(children, wxSizerFlags()); - CHECK( children[0]->GetSize() == sizeChild ); - CHECK( children[1]->GetSize() == sizeChild ); - CHECK( children[2]->GetSize() == sizeChild ); - CHECK( children[3]->GetSize() == sizeChild ); + SECTION("No flags") + { + SetChildren(children, wxSizerFlags()); + CHECK( children[0]->GetSize() == sizeChild ); + CHECK( children[1]->GetSize() == sizeChild ); + CHECK( children[2]->GetSize() == sizeChild ); + CHECK( children[3]->GetSize() == sizeChild ); + } // With just expand, they expand to fill the entire column and the row // containing them (which may or not expand on its own). - SetChildren(children, wxSizerFlags().Expand()); - CHECK( children[0]->GetSize() == sizeChild ); - CHECK( children[1]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); - CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); - CHECK( children[3]->GetSize() == sizeRest ); + SECTION("Expand") + { + SetChildren(children, wxSizerFlags().Expand()); + CHECK( children[0]->GetSize() == sizeChild ); + CHECK( children[1]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); + CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); + CHECK( children[3]->GetSize() == sizeRest ); + } // With expand and another alignment flag, they should expand only in the // direction in which the alignment is not given. - SetChildren(children, wxSizerFlags().Expand().CentreVertical()); - CHECK( children[0]->GetSize() == sizeChild ); - CHECK( children[1]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); - CHECK( children[2]->GetSize() == sizeChild ); - CHECK( children[3]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); + SECTION("Expand and center vertically") + { + SetChildren(children, wxSizerFlags().Expand().CentreVertical()); + CHECK( children[0]->GetSize() == sizeChild ); + CHECK( children[1]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); + CHECK( children[2]->GetSize() == sizeChild ); + CHECK( children[3]->GetSize() == wxSize(sizeRest.x, sizeChild.y) ); + } // Same as above but mirrored. - SetChildren(children, wxSizerFlags().Expand().CentreHorizontal()); - CHECK( children[0]->GetSize() == sizeChild ); - CHECK( children[1]->GetSize() == sizeChild ); - CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); - CHECK( children[3]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); + SECTION("Expand and center horizontally") + { + SetChildren(children, wxSizerFlags().Expand().CentreHorizontal()); + CHECK( children[0]->GetSize() == sizeChild ); + CHECK( children[1]->GetSize() == sizeChild ); + CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); + CHECK( children[3]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); + } } TEST_CASE_METHOD(GridSizerTestCase, From 9130f8bfc65e3453b0081640d1c1efeb24e6bb1b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 25 Jan 2021 12:58:04 +0100 Subject: [PATCH 3/3] Don't change orthogonal alignment in wxSizerFlags::CenterXXX() It was unexpected that using wxSizerFlags().Right().CentreVertical() didn't right-align the item because CentreVertical() reset the alignment in the horizontal direction rather than just setting it in the vertical one, so change this, document the new behaviour explicitly and add a new unit test checking for it. Closes #18989. --- include/wx/sizer.h | 18 +++++++++++++++--- interface/wx/sizer.h | 6 ++++++ tests/sizers/gridsizer.cpp | 25 ++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/include/wx/sizer.h b/include/wx/sizer.h index dd15744a64..c1f234734f 100644 --- a/include/wx/sizer.h +++ b/include/wx/sizer.h @@ -69,14 +69,26 @@ public: return *this; } - // some shortcuts for Align() + // this is just a shortcut for Align() wxSizerFlags& Centre() { return Align(wxALIGN_CENTRE); } wxSizerFlags& Center() { return Centre(); } - wxSizerFlags& CentreVertical() { return Align(wxALIGN_CENTRE_VERTICAL); } + // but all the remaining methods turn on the corresponding alignment flag + // without affecting the existing ones + wxSizerFlags& CentreVertical() + { + m_flags = (m_flags & ~wxALIGN_BOTTOM) | wxALIGN_CENTRE_VERTICAL; + return *this; + } + wxSizerFlags& CenterVertical() { return CentreVertical(); } - wxSizerFlags& CentreHorizontal() { return Align(wxALIGN_CENTRE_HORIZONTAL); } + wxSizerFlags& CentreHorizontal() + { + m_flags = (m_flags & ~wxALIGN_RIGHT) | wxALIGN_CENTRE_HORIZONTAL; + return *this; + } + wxSizerFlags& CenterHorizontal() { return CentreHorizontal(); } wxSizerFlags& Top() diff --git a/interface/wx/sizer.h b/interface/wx/sizer.h index ad85a0ccad..9e2e9b6a82 100644 --- a/interface/wx/sizer.h +++ b/interface/wx/sizer.h @@ -1455,6 +1455,9 @@ public: them anyhow. For 2D sizers, centering an item in one direction is quite different from centering it in both directions however. + Note that, unlike Align(), this method doesn't change the vertical + alignment. + @see CentreVertical() @since 3.1.0 @@ -1467,6 +1470,9 @@ public: The remarks in CentreHorizontal() documentation also apply to this function. + Note that, unlike Align(), this method doesn't change the horizontal + alignment. + @since 3.1.0 */ wxSizerFlags& CentreVertical(); diff --git a/tests/sizers/gridsizer.cpp b/tests/sizers/gridsizer.cpp index fc3c130f03..492bf9aab8 100644 --- a/tests/sizers/gridsizer.cpp +++ b/tests/sizers/gridsizer.cpp @@ -85,7 +85,7 @@ void GridSizerTestCase::SetChildren(const wxVector& children, // ---------------------------------------------------------------------------- TEST_CASE_METHOD(GridSizerTestCase, - "wxGridSizer::Expand", + "wxGridSizer::Layout", "[grid-sizer][sizer]") { const wxSize sizeTotal = m_win->GetClientSize(); @@ -144,6 +144,29 @@ TEST_CASE_METHOD(GridSizerTestCase, CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); CHECK( children[3]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); } + + // Test alignment flag too. + SECTION("Right align") + { + SetChildren(children, wxSizerFlags().Right()); + CHECK( children[0]->GetPosition() == wxPoint( 0, 0) ); + CHECK( children[1]->GetPosition() == wxPoint(sizeRest.x, 0) ); + CHECK( children[2]->GetPosition() == wxPoint( 0, sizeChild.y) ); + CHECK( children[3]->GetPosition() == wxPoint(sizeRest.x, sizeChild.y) ); + } + + // Also test combining centering in one direction and aligning in another. + SECTION("Right align and center vertically") + { + SetChildren(children, wxSizerFlags().Right().CentreVertical()); + + const int yMid = sizeChild.y + (sizeRest.y - sizeChild.y) / 2; + + CHECK( children[0]->GetPosition() == wxPoint( 0, 0) ); + CHECK( children[1]->GetPosition() == wxPoint(sizeRest.x, 0) ); + CHECK( children[2]->GetPosition() == wxPoint( 0, yMid) ); + CHECK( children[3]->GetPosition() == wxPoint(sizeRest.x, yMid) ); + } } TEST_CASE_METHOD(GridSizerTestCase,