Refactor code waiting for events in the test suite
We already had WX_ASSERT_EVENT_OCCURS_IN macro and a recent commit also added code doing almost the same thing manually in wxTopLevelWindow unit test, which was one version too many. Replace both of them with the new EventCounter::WaitEvent(). No real changes, this is just a refactoring.
This commit is contained in:
@@ -72,7 +72,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
//Convenience macro
|
//Convenience macro
|
||||||
#define ENSURE_LOADED WX_ASSERT_EVENT_OCCURS_IN((*m_loaded), 1, 1000)
|
#define ENSURE_LOADED CHECK( m_loaded->WaitEvent() )
|
||||||
|
|
||||||
// register in the unnamed registry so that these tests are run by default
|
// register in the unnamed registry so that these tests are run by default
|
||||||
CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
|
CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
|
||||||
|
@@ -152,7 +152,7 @@ void WindowTestCase::FocusEvent()
|
|||||||
|
|
||||||
m_window->SetFocus();
|
m_window->SetFocus();
|
||||||
|
|
||||||
WX_ASSERT_EVENT_OCCURS_IN(setfocus, 1, 500);
|
CPPUNIT_ASSERT(setfocus.WaitEvent(500));
|
||||||
CPPUNIT_ASSERT(m_window->HasFocus());
|
CPPUNIT_ASSERT(m_window->HasFocus());
|
||||||
|
|
||||||
wxButton* button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY);
|
wxButton* button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY);
|
||||||
|
@@ -63,3 +63,26 @@ EventCounter::~EventCounter()
|
|||||||
m_frame = NULL;
|
m_frame = NULL;
|
||||||
m_win = NULL;
|
m_win = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool EventCounter::WaitEvent(int timeInMs)
|
||||||
|
{
|
||||||
|
static const int SINGLE_WAIT_DURATION = 50;
|
||||||
|
|
||||||
|
for ( int i = 0; i < timeInMs / SINGLE_WAIT_DURATION; ++i )
|
||||||
|
{
|
||||||
|
wxYield();
|
||||||
|
|
||||||
|
const int count = GetCount();
|
||||||
|
if ( count )
|
||||||
|
{
|
||||||
|
CHECK( count == 1 );
|
||||||
|
|
||||||
|
Clear();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
wxMilliSleep(SINGLE_WAIT_DURATION);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
@@ -35,6 +35,13 @@ public:
|
|||||||
int GetCount() { return m_frame->GetEventCount(m_type); }
|
int GetCount() { return m_frame->GetEventCount(m_type); }
|
||||||
void Clear() { m_frame->ClearEventCount(m_type); }
|
void Clear() { m_frame->ClearEventCount(m_type); }
|
||||||
|
|
||||||
|
// Sometimes we need to yield a few times before getting the event we
|
||||||
|
// expect, so provide a function waiting for the expected event for up to
|
||||||
|
// the given number of milliseconds (supposed to be divisible by 50).
|
||||||
|
//
|
||||||
|
// Return true if we did receive the event or false otherwise.
|
||||||
|
bool WaitEvent(int timeInMs = 1000);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxEventType m_type;
|
wxEventType m_type;
|
||||||
wxTestableFrame* m_frame;
|
wxTestableFrame* m_frame;
|
||||||
|
@@ -2,7 +2,6 @@
|
|||||||
#define WX_TESTPREC_INCLUDED 1
|
#define WX_TESTPREC_INCLUDED 1
|
||||||
|
|
||||||
#include "wx/wxprec.h"
|
#include "wx/wxprec.h"
|
||||||
#include "wx/stopwatch.h"
|
|
||||||
#include "wx/evtloop.h"
|
#include "wx/evtloop.h"
|
||||||
|
|
||||||
// This needs to be included before catch.hpp to be taken into account.
|
// This needs to be included before catch.hpp to be taken into account.
|
||||||
@@ -108,27 +107,6 @@ public:
|
|||||||
#define WX_ASSERT_FAILS_WITH_ASSERT(cond)
|
#define WX_ASSERT_FAILS_WITH_ASSERT(cond)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define WX_ASSERT_EVENT_OCCURS_IN(eventcounter, count, ms) \
|
|
||||||
{\
|
|
||||||
wxStopWatch sw; \
|
|
||||||
wxEventLoopBase* loop = wxEventLoopBase::GetActive(); \
|
|
||||||
while(eventcounter.GetCount() < count) \
|
|
||||||
{ \
|
|
||||||
if(sw.Time() < ms) \
|
|
||||||
loop->Dispatch(); \
|
|
||||||
else \
|
|
||||||
{ \
|
|
||||||
CPPUNIT_FAIL(wxString::Format("timeout reached with %d " \
|
|
||||||
"events received, %d expected", \
|
|
||||||
eventcounter.GetCount(), count).ToStdString()); \
|
|
||||||
break; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
eventcounter.Clear(); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define WX_ASSERT_EVENT_OCCURS(eventcounter,count) WX_ASSERT_EVENT_OCCURS_IN(eventcounter, count, 100)
|
|
||||||
|
|
||||||
// these functions can be used to hook into wxApp event processing and are
|
// these functions can be used to hook into wxApp event processing and are
|
||||||
// currently used by the events propagation test
|
// currently used by the events propagation test
|
||||||
class WXDLLIMPEXP_FWD_BASE wxEvent;
|
class WXDLLIMPEXP_FWD_BASE wxEvent;
|
||||||
|
@@ -88,19 +88,5 @@ TEST_CASE("wxTopLevel::ShowEvent", "[tlw][show][event]")
|
|||||||
frame->Maximize();
|
frame->Maximize();
|
||||||
frame->Show();
|
frame->Show();
|
||||||
|
|
||||||
// Wait for the event to be received for the maximum of 2 seconds.
|
CHECK( countShow.WaitEvent() );
|
||||||
int showCount = 0;
|
|
||||||
for ( int i = 0; i < 40; i++ )
|
|
||||||
{
|
|
||||||
wxYield();
|
|
||||||
|
|
||||||
showCount = countShow.GetCount();
|
|
||||||
|
|
||||||
if ( showCount )
|
|
||||||
break;
|
|
||||||
|
|
||||||
wxMilliSleep(50);
|
|
||||||
}
|
|
||||||
|
|
||||||
CHECK( showCount == 1 );
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user