mirror of
https://github.com/wxWidgets/wxWidgets.git
synced 2026-08-20 22:06:25 +08:00
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:
@@ -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 );
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user