]> git.saurik.com Git - wxWidgets.git/commitdiff
don't sleep too long to avoid missing the timers; added a simple test for timer event...
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 27 Oct 2008 22:04:42 +0000 (22:04 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 27 Oct 2008 22:04:42 +0000 (22:04 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56537 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/unix/evtloopunix.cpp
tests/Makefile.in
tests/events/timertest.cpp [new file with mode: 0644]
tests/test.bkl
tests/test_test.dsp
tests/test_vc7_test.vcproj
tests/test_vc8_test.vcproj
tests/test_vc9_test.vcproj

index 76272844d3ea02f1a4c3d720cd591f1c6b5a7665..fc1f469ae427e6f626902b971c2377d0a119836d 100644 (file)
@@ -148,18 +148,6 @@ bool wxConsoleEventLoop::Pending() const
 }
 
 bool wxConsoleEventLoop::Dispatch()
-{
-    m_dispatcher->Dispatch();
-    wxTheApp->ProcessPendingEvents();
-    return true;
-}
-
-void wxConsoleEventLoop::WakeUp()
-{
-    m_wakeupPipe.WakeUp();
-}
-
-void wxConsoleEventLoop::OnNextIteration()
 {
     // calculate the timeout until the next timer expiration
     int timeout;
@@ -183,6 +171,17 @@ void wxConsoleEventLoop::OnNextIteration()
     wxTimerScheduler::Get().NotifyExpired();
 #endif
 
+    wxTheApp->ProcessPendingEvents();
+    return true;
+}
+
+void wxConsoleEventLoop::WakeUp()
+{
+    m_wakeupPipe.WakeUp();
+}
+
+void wxConsoleEventLoop::OnNextIteration()
+{
     // call the signal handlers for any signals we caught recently
     wxTheApp->CheckSignal();
 }
index 13f628203d2e825ab0499181a13c5a8b5af03168..59e1d55f5a6175daf6b39603df0b00a4a344e6f7 100644 (file)
@@ -60,6 +60,7 @@ TEST_OBJECTS =  \
        test_cmdlinetest.o \
        test_fileconf.o \
        test_datetimetest.o \
+       test_timertest.o \
        test_filekind.o \
        test_filenametest.o \
        test_filesystest.o \
@@ -365,6 +366,9 @@ test_fileconf.o: $(srcdir)/config/fileconf.cpp $(TEST_ODEP)
 test_datetimetest.o: $(srcdir)/datetime/datetimetest.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/datetime/datetimetest.cpp
 
+test_timertest.o: $(srcdir)/events/timertest.cpp $(TEST_ODEP)
+       $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/events/timertest.cpp
+
 test_filekind.o: $(srcdir)/filekind/filekind.cpp $(TEST_ODEP)
        $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/filekind/filekind.cpp
 
diff --git a/tests/events/timertest.cpp b/tests/events/timertest.cpp
new file mode 100644 (file)
index 0000000..1aed5c4
--- /dev/null
@@ -0,0 +1,139 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        tests/events/timertest.cpp
+// Purpose:     Test wxTimer events
+// Author:      Vadim Zeitlin
+// Created:     2008-10-22
+// RCS-ID:      $Id$
+// 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"
+
+// helper class counting the number of timer events
+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;
+
+    DECLARE_NO_COPY_CLASS(TimerCounterHandler)
+};
+
+// --------------------------------------------------------------------------
+// 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();
+
+    DECLARE_NO_COPY_CLASS(TimerEventTestCase)
+};
+
+// register in the unnamed registry so that these tests are run by default
+CPPUNIT_TEST_SUITE_REGISTRATION( TimerEventTestCase );
+
+// also include in it's own registry so that these tests can be run alone
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TimerEventTestCase, "TimerEventTestCase" );
+
+void TimerEventTestCase::OneShot()
+{
+    class ExitOnTimerHandler : public TimerCounterHandler
+    {
+    public:
+        ExitOnTimerHandler(wxEventLoopBase& loop)
+            : TimerCounterHandler(),
+              m_loop(loop)
+        {
+        }
+
+    private:
+        virtual void Tick() { m_loop.Exit(); }
+
+        wxEventLoopBase& m_loop;
+
+        // don't use DECLARE_NO_COPY_CLASS() to avoid upsetting MSVC
+    };
+
+    wxEventLoop loop;
+
+    ExitOnTimerHandler handler(loop);
+    wxTimer timer(&handler);
+    timer.Start(200, true);
+
+    loop.Run();
+
+    CPPUNIT_ASSERT_EQUAL( 1, handler.GetNumEvents() );
+}
+
+void TimerEventTestCase::Multiple()
+{
+    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 );
+
+    // and we should get a decent number of them (unless the system is horribly
+    // loaded so if it does happen that this test fails we may need to remove
+    // it)
+    CPPUNIT_ASSERT( numTicks > 10 );
+}
index 9d639115fa0079a02c7d96170cb938cb157e064a..ecde829c6a8bad06dfe7cac9f649438ab4a3bac6 100644 (file)
@@ -37,6 +37,7 @@
             cmdline/cmdlinetest.cpp
             config/fileconf.cpp
             datetime/datetimetest.cpp
+            events/timertest.cpp
             filekind/filekind.cpp
             filename/filenametest.cpp
             filesys/filesystest.cpp
index be63a023b1452a0f16697339d8ea218fcfcd0986..b5c8f581237b60deb323e91ddb43c0f0598e1e37 100644 (file)
@@ -417,6 +417,10 @@ SOURCE=.\streams\textstreamtest.cpp
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\events\timertest.cpp\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=.\thread\tls.cpp\r
 # End Source File\r
 # Begin Source File\r
index d561d6c0a773d7bf0bd3ddc9587229acd7c15617..6dafda7a87844b10a2222e7e396c4c584fa19632 100644 (file)
                        <File\r
                                RelativePath=".\streams\textstreamtest.cpp">\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\events\timertest.cpp">\r
+                       </File>\r
                        <File\r
                                RelativePath=".\thread\tls.cpp">\r
                        </File>\r
index 677cd015ae59fa7c381cb9156009fe550e80f782..70770ff1aed2b6e595d088f92b2d0edd6a2c4818 100644 (file)
                                RelativePath=".\streams\textstreamtest.cpp"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\events\timertest.cpp"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath=".\thread\tls.cpp"\r
                                >\r
index 768814209f2455e6a07dd6f48572d599aecc31b3..387a08c444d5340951ef8b976973d07734f9473d 100644 (file)
                                RelativePath=".\streams\textstreamtest.cpp"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath=".\events\timertest.cpp"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath=".\thread\tls.cpp"\r
                                >\r