2008-10-27 18:04:42 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/events/timertest.cpp
|
|
|
|
// Purpose: Test wxTimer events
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2008-10-22
|
|
|
|
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "wx/evtloop.h"
|
|
|
|
#include "wx/timer.h"
|
|
|
|
|
2010-05-16 11:44:17 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2008-10-27 18:04:42 -04:00
|
|
|
// helper class counting the number of timer events
|
2010-05-16 11:44:17 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2008-10-27 18:04:42 -04:00
|
|
|
class TimerCounterHandler : public wxEvtHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimerCounterHandler()
|
|
|
|
{
|
|
|
|
m_events = 0;
|
|
|
|
|
|
|
|
Connect(wxEVT_TIMER, wxTimerEventHandler(TimerCounterHandler::OnTimer));
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetNumEvents() const { return m_events; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void OnTimer(wxTimerEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
m_events++;
|
|
|
|
|
|
|
|
Tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Tick() { /* nothing to do in the base class */ }
|
|
|
|
|
|
|
|
int m_events;
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(TimerCounterHandler);
|
2008-10-27 18:04:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TimerEventTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimerEventTestCase() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( TimerEventTestCase );
|
|
|
|
CPPUNIT_TEST( OneShot );
|
|
|
|
CPPUNIT_TEST( Multiple );
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void OneShot();
|
|
|
|
void Multiple();
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(TimerEventTestCase);
|
2008-10-27 18:04:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase );
|
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2008-10-27 18:04:42 -04:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase, "TimerEventTestCase" );
|
|
|
|
|
|
|
|
void TimerEventTestCase::OneShot()
|
|
|
|
{
|
|
|
|
class ExitOnTimerHandler : public TimerCounterHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExitOnTimerHandler(wxEventLoopBase& loop)
|
|
|
|
: TimerCounterHandler(),
|
|
|
|
m_loop(loop)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-09-21 13:46:49 -04:00
|
|
|
virtual void Tick() wxOVERRIDE { m_loop.Exit(); }
|
2008-10-27 18:04:42 -04:00
|
|
|
|
|
|
|
wxEventLoopBase& m_loop;
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
// don't use wxDECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
|
2008-10-27 18:04:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
wxEventLoop loop;
|
|
|
|
|
|
|
|
ExitOnTimerHandler handler(loop);
|
|
|
|
wxTimer timer(&handler);
|
|
|
|
timer.Start(200, true);
|
|
|
|
|
|
|
|
loop.Run();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, handler.GetNumEvents() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimerEventTestCase::Multiple()
|
|
|
|
{
|
2010-10-05 09:37:44 -04:00
|
|
|
// FIXME: This test crashes on wxGTK ANSI build slave for unknown reason,
|
|
|
|
// disable it here to let the rest of the test suite run until this
|
|
|
|
// can be fixed.
|
|
|
|
#if !defined(__WXGTK__) || wxUSE_UNICODE
|
2008-10-27 18:04:42 -04:00
|
|
|
wxEventLoop loop;
|
|
|
|
|
|
|
|
TimerCounterHandler handler;
|
|
|
|
wxTimer timer(&handler);
|
|
|
|
timer.Start(100);
|
|
|
|
|
|
|
|
// run the loop for 2 seconds
|
|
|
|
time_t t;
|
|
|
|
time(&t);
|
|
|
|
const time_t tEnd = t + 2;
|
|
|
|
while ( time(&t) < tEnd )
|
|
|
|
{
|
|
|
|
loop.Dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can't count on getting exactly 20 ticks but we shouldn't get more
|
|
|
|
// than this
|
|
|
|
const int numTicks = handler.GetNumEvents();
|
|
|
|
CPPUNIT_ASSERT( numTicks <= 20 );
|
|
|
|
|
2009-03-26 10:58:22 -04:00
|
|
|
// and we should get a decent number of them but if the system is very
|
|
|
|
// loaded (as happens with build bot slaves running a couple of builds in
|
|
|
|
// parallel actually) it may be much less than 20 so just check that we get
|
|
|
|
// more than one
|
|
|
|
CPPUNIT_ASSERT( numTicks > 1 );
|
2010-10-05 09:37:44 -04:00
|
|
|
#endif // !(wxGTK Unicode)
|
2008-10-27 18:04:42 -04:00
|
|
|
}
|