wxWidgets/tests/window/setsize.cpp
Vadim Zeitlin 7d6d514984 Improve WaitForPaint helper used in window unit tests
Unbind the event handler referencing a local variable, as leaving it
bound could result in a crash later if another paint event was generated
for the window for whatever reason.

Doing it like this requires using 2 different objects, but the
complexity can be still hidden inside WaitForPaint class, with the 2nd
object being just a member of it, and, in fact, makes the code using it
simpler as it doesn't need to use a boolean variable with it.
2019-10-17 17:07:00 +02:00

177 lines
4.4 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// Name: tests/window/setsize.cpp
// Purpose: Tests for SetSize() and related wxWindow methods
// Author: Vadim Zeitlin
// Created: 2008-05-25
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/frame.h"
#include "wx/window.h"
#endif // WX_PRECOMP
#include "wx/scopedptr.h"
#include "wx/stopwatch.h"
#include "asserthelper.h"
// ----------------------------------------------------------------------------
// tests helpers
// ----------------------------------------------------------------------------
namespace
{
// Helper class overriding DoGetBestSize() for testing purposes.
class MyWindow : public wxWindow
{
public:
MyWindow(wxWindow* parent)
: wxWindow(parent, wxID_ANY)
{
}
protected:
virtual wxSize DoGetBestSize() const wxOVERRIDE { return wxSize(50, 250); }
};
// Class used to check if we received the (first) paint event.
class WaitForPaint
{
public:
explicit WaitForPaint(wxWindow* win)
: m_win(*win),
m_painted(false),
m_handler(&m_painted)
{
m_win.Bind(wxEVT_PAINT, m_handler);
}
bool GotPaintEvent() const
{
return m_painted;
}
~WaitForPaint()
{
m_win.Unbind(wxEVT_PAINT, m_handler);
}
private:
wxWindow& m_win;
bool m_painted;
class PaintHandler
{
public:
// Note that we have to use a pointer here, i.e. we can't just store
// the flag inside the class itself because it's going to be cloned
// inside wx and querying the flag of the original copy wouldtn' work.
explicit PaintHandler(bool* painted)
: m_painted(*painted)
{
}
void operator()(wxPaintEvent& event)
{
event.Skip();
m_painted = true;
}
private:
bool& m_painted;
} m_handler;
};
// This function should be used to show the window and wait until we can get
// its real geometry.
void ShowAndWaitForPaint(wxWindow* w)
{
// Unfortunately showing the window is asynchronous, at least when using
// X11, so we have to wait for some time before retrieving its true
// geometry. And it's not clear how long should we wait, so we do it until
// we get the first paint event -- by then the window really should have
// its final size.
WaitForPaint waitForPaint(w);
w->Show();
wxStopWatch sw;
while ( !waitForPaint.GotPaintEvent() )
{
wxYield();
if ( sw.Time() > 250 )
{
WARN("Didn't get a paint event until timeout expiration");
break;
}
}
}
} // anonymous namespace
// ----------------------------------------------------------------------------
// tests themselves
// ----------------------------------------------------------------------------
TEST_CASE("wxWindow::SetSize", "[window][size]")
{
wxScopedPtr<wxWindow> w(new MyWindow(wxTheApp->GetTopWindow()));
SECTION("Simple")
{
const wxSize size(127, 35);
w->SetSize(size);
CHECK( size == w->GetSize() );
}
SECTION("With min size")
{
w->SetMinSize(wxSize(100, 100));
const wxSize size(200, 50);
w->SetSize(size);
CHECK( size == w->GetSize() );
}
}
TEST_CASE("wxWindow::GetBestSize", "[window][size][best-size]")
{
wxScopedPtr<wxWindow> w(new MyWindow(wxTheApp->GetTopWindow()));
CHECK( wxSize(50, 250) == w->GetBestSize() );
w->SetMinSize(wxSize(100, 100));
CHECK( wxSize(100, 250) == w->GetBestSize() );
w->SetMaxSize(wxSize(200, 200));
CHECK( wxSize(100, 200) == w->GetBestSize() );
}
TEST_CASE("wxWindow::MovePreservesSize", "[window][size][move]")
{
wxScopedPtr<wxWindow>
w(new wxFrame(wxTheApp->GetTopWindow(), wxID_ANY, "Test child frame"));
ShowAndWaitForPaint(w.get());
const wxRect rectOrig = w->GetRect();
// Check that moving the window doesn't change its size.
w->Move(rectOrig.GetPosition() + wxPoint(100, 100));
CHECK( w->GetSize() == rectOrig.GetSize() );
}