From 1237322404b0e060ee6a40a343a4462d5ca7bf90 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 1 Oct 2023 21:30:00 +0200 Subject: [PATCH] Add WaitFor() helper and use it in Button::Click unit test This test also sporadically happens under AppVeyor, check if it happens because we need to wait for some time before the event is processed. --- tests/controls/buttontest.cpp | 6 +++++- tests/waitfor.h | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/controls/buttontest.cpp b/tests/controls/buttontest.cpp index ce958bc5e9..2a5570b354 100644 --- a/tests/controls/buttontest.cpp +++ b/tests/controls/buttontest.cpp @@ -23,6 +23,7 @@ // Get operator<<(wxSize) so that wxSize values are shown correctly in case of // a failure of a CHECK() involving them. #include "asserthelper.h" +#include "waitfor.h" class ButtonTestCase { @@ -64,7 +65,10 @@ TEST_CASE_METHOD(ButtonTestCase, "Button::Click", "[button]") wxYield(); sim.MouseClick(); - wxYield(); + + // At least under wxMSW calling wxYield() just once doesn't always work, so + // try for a while. + WaitFor("button to be clicked", [&]() { return clicked.GetCount() != 0; }); CHECK( clicked.GetCount() == 1 ); } diff --git a/tests/waitfor.h b/tests/waitfor.h index 88a95ab838..35b1c161ca 100644 --- a/tests/waitfor.h +++ b/tests/waitfor.h @@ -12,6 +12,31 @@ #include "wx/stopwatch.h" #include "wx/window.h" +#include + +// Function used to wait until the given predicate becomes true or timeout +// expires. +inline bool +WaitFor(const char* what, const std::function& pred, int timeout = 500) +{ + wxStopWatch sw; + for ( ;; ) + { + wxYield(); + + if ( pred() ) + break; + + if ( sw.Time() > timeout ) + { + WARN("Timed out waiting for " << what); + return false; + } + } + + return true; +} + // Class used to check if we received the (first) paint event: this is // currently used under GTK only, as MSW doesn't seem to need to wait for the // things to work, while under Mac nothing works anyhow.