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.
This commit is contained in:
Vadim Zeitlin 2023-10-01 21:30:00 +02:00
parent 55e074f5bc
commit 1237322404
2 changed files with 30 additions and 1 deletions

View file

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

View file

@ -12,6 +12,31 @@
#include "wx/stopwatch.h"
#include "wx/window.h"
#include <functional>
// Function used to wait until the given predicate becomes true or timeout
// expires.
inline bool
WaitFor(const char* what, const std::function<bool ()>& 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.