From e46385e964bdeb7d70fd98706c0d45be7ad9786b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 27 Sep 2019 16:12:18 +0200 Subject: [PATCH] Test that adding window to 2 sizers results in an assert Check that adding a window to either the same, or different, sizer the second time asserts -- but that it can still be moved to another sizer if it is detached from the first one first. Also document that SetContainingSizer() should never be called from outside the library. See #17166. --- interface/wx/window.h | 11 ++++++++--- tests/controls/windowtest.cpp | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/interface/wx/window.h b/interface/wx/window.h index 6f2df316f8..93f603c50a 100644 --- a/interface/wx/window.h +++ b/interface/wx/window.h @@ -1493,9 +1493,14 @@ public: void SetClientSize(const wxRect& rect); /** - This normally does not need to be called by user code. - It is called when a window is added to a sizer, and is used so the window - can remove itself from the sizer when it is destroyed. + Used by wxSizer internally to notify the window about being managed by + the given sizer. + + This method should not be called from outside the library, unless + you're implementing a custom sizer class -- and in the latter case you + must call this method with the pointer to the sizer itself whenever a + window is added to it and with @NULL argument when the window is + removed from it. */ void SetContainingSizer(wxSizer* sizer); diff --git a/tests/controls/windowtest.cpp b/tests/controls/windowtest.cpp index 926c6cafd7..a9ff914f5d 100644 --- a/tests/controls/windowtest.cpp +++ b/tests/controls/windowtest.cpp @@ -26,6 +26,7 @@ #include "wx/uiaction.h" #include "wx/caret.h" #include "wx/cshelp.h" +#include "wx/scopedptr.h" #include "wx/tooltip.h" class WindowTestCase @@ -379,3 +380,19 @@ TEST_CASE_METHOD(WindowTestCase, "Window::FindWindowBy", "[window]") CHECK( wxWindow::FindWindowByName("noname") == NULL ); CHECK( wxWindow::FindWindowByLabel("nolabel") == NULL ); } + +TEST_CASE_METHOD(WindowTestCase, "Window::SizerErrors", "[window][sizer][error]") +{ + wxWindow* const child = new wxWindow(m_window, wxID_ANY); + wxScopedPtr const sizer1(new wxBoxSizer(wxHORIZONTAL)); + wxScopedPtr const sizer2(new wxBoxSizer(wxHORIZONTAL)); + + REQUIRE_NOTHROW( sizer1->Add(child) ); + CHECK_THROWS_AS( sizer1->Add(child), TestAssertFailure ); + CHECK_THROWS_AS( sizer2->Add(child), TestAssertFailure ); + + CHECK_NOTHROW( sizer1->Detach(child) ); + CHECK_NOTHROW( sizer2->Add(child) ); + + REQUIRE_NOTHROW( delete child ); +}