From c810bfad4716db5062b74ee4c66a0410a3ddcbad Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 17 Oct 2019 21:59:06 +0200 Subject: [PATCH] Move WaitForPaint helper into a header No real changes, just make this class, used in wxWindow::SetSize() tests only so far, reusable in the other tests. --- tests/waitforpaint.h | 75 ++++++++++++++++++++++++++++++++++++++++ tests/window/setsize.cpp | 61 ++------------------------------ 2 files changed, 78 insertions(+), 58 deletions(-) create mode 100644 tests/waitforpaint.h diff --git a/tests/waitforpaint.h b/tests/waitforpaint.h new file mode 100644 index 0000000000..2d749bcf79 --- /dev/null +++ b/tests/waitforpaint.h @@ -0,0 +1,75 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/waitforpaint.h +// Purpose: Helper WaitForPaint class +// Author: Vadim Zeitlin +// Created: 2019-10-17 (extracted from tests/window/setsize.cpp) +// Copyright: (c) 2019 Vadim Zeitlin +/////////////////////////////////////////////////////////////////////////////// + +#ifndef _WX_TESTS_WAITFORPAINT_H_ +#define _WX_TESTS_WAITFORPAINT_H_ + +#include "wx/stopwatch.h" +#include "wx/window.h" + +// 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); + } + + // This function waits up to the given number of milliseconds for the paint + // event to come and returns true if we did get it or false otherwise. + bool YieldUntilPainted(int timeoutInMS = 250) const + { + wxStopWatch sw; + for ( ;; ) + { + wxYield(); + + if ( m_painted ) + return true; + + if ( sw.Time() > timeoutInMS ) + return false; + } + } + + ~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; +}; + +#endif // _WX_TESTS_WAITFORPAINT_H_ diff --git a/tests/window/setsize.cpp b/tests/window/setsize.cpp index 8728413336..e908da2257 100644 --- a/tests/window/setsize.cpp +++ b/tests/window/setsize.cpp @@ -23,9 +23,9 @@ #endif // WX_PRECOMP #include "wx/scopedptr.h" -#include "wx/stopwatch.h" #include "asserthelper.h" +#include "waitforpaint.h" // ---------------------------------------------------------------------------- // tests helpers @@ -47,54 +47,6 @@ 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) @@ -108,16 +60,9 @@ void ShowAndWaitForPaint(wxWindow* w) w->Show(); - wxStopWatch sw; - while ( !waitForPaint.GotPaintEvent() ) + if ( !waitForPaint.YieldUntilPainted() ) { - wxYield(); - - if ( sw.Time() > 250 ) - { - WARN("Didn't get a paint event until timeout expiration"); - break; - } + WARN("Didn't get a paint event until timeout expiration"); } }