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.
This commit is contained in:
Vadim Zeitlin
2019-09-27 16:12:18 +02:00
parent 7bba2d09fd
commit e46385e964
2 changed files with 25 additions and 3 deletions

View File

@@ -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);

View File

@@ -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<wxSizer> const sizer1(new wxBoxSizer(wxHORIZONTAL));
wxScopedPtr<wxSizer> 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 );
}