From 790aca0ea1bbd33a8ff29c83c0ab0bd968304f03 Mon Sep 17 00:00:00 2001 From: David Faure Date: Sat, 14 Jul 2012 01:37:53 +0200 Subject: Add QSignalSpy::wait() method. Change-Id: I1f3b49e3dee19bf0b1d2933c6e6ad7972186e0d0 Reviewed-by: Rohan McGovern Reviewed-by: Jason McDonald --- src/testlib/qsignalspy.h | 17 +++++++++++++++++ src/testlib/qsignalspy.qdoc | 9 +++++++++ src/testlib/qtestcase.cpp | 2 +- src/testlib/qtesteventloop.h | 7 ++++--- 4 files changed, 31 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/testlib/qsignalspy.h b/src/testlib/qsignalspy.h index 70944baadf..18def8f6c4 100644 --- a/src/testlib/qsignalspy.h +++ b/src/testlib/qsignalspy.h @@ -47,6 +47,7 @@ #include #include #include +#include QT_BEGIN_HEADER @@ -59,6 +60,7 @@ class QSignalSpy: public QObject, public QList > { public: QSignalSpy(QObject *obj, const char *aSignal) + : m_waiting(false) { #ifdef Q_CC_BOR const int memberOffset = QObject::staticMetaObject.methodCount(); @@ -100,6 +102,15 @@ public: inline bool isValid() const { return !sig.isEmpty(); } inline QByteArray signal() const { return sig; } + bool wait(int timeout = 5000) + { + Q_ASSERT(!m_waiting); + const int origCount = count(); + m_waiting = true; + m_loop.enterLoopMSecs(timeout); + m_waiting = false; + return count() > origCount; + } int qt_metacall(QMetaObject::Call call, int methodId, void **a) { @@ -139,12 +150,18 @@ private: list << QVariant(type, a[i + 1]); } append(list); + + if (m_waiting) + m_loop.exitLoop(); } // the full, normalized signal name QByteArray sig; // holds the QMetaType types for the argument list of the signal QList args; + + QTestEventLoop m_loop; + bool m_waiting; }; QT_END_NAMESPACE diff --git a/src/testlib/qsignalspy.qdoc b/src/testlib/qsignalspy.qdoc index 7ce0a51505..e9e1f4d5e6 100644 --- a/src/testlib/qsignalspy.qdoc +++ b/src/testlib/qsignalspy.qdoc @@ -86,3 +86,12 @@ \internal */ +/*! \fn QSignalSpy::wait(int timeout) const + + \since 5.0 + + Starts an event loop that runs until the given signal is received. + Optionally the event loop can return earlier on a \a timeout (in milliseconds). + + Returns true if the signal was emitted at least once in \a timeout milliseconds, otherwise returns false. +*/ diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index bf402181c1..296bc96c4b 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -856,7 +856,7 @@ QT_BEGIN_NAMESPACE The code above will wait until the network server is responding for a maximum of about 12.5 seconds. - \sa QTest::qSleep() + \sa QTest::qSleep(), QSignalSpy::wait() */ /*! \fn bool QTest::qWaitForWindowExposed(QWindow *window, int timeout) diff --git a/src/testlib/qtesteventloop.h b/src/testlib/qtesteventloop.h index 98d69963ca..3f08a93b69 100644 --- a/src/testlib/qtesteventloop.h +++ b/src/testlib/qtesteventloop.h @@ -61,8 +61,9 @@ class Q_TESTLIB_EXPORT QTestEventLoop : public QObject public: inline QTestEventLoop(QObject *aParent = 0) : QObject(aParent), inLoop(false), _timeout(false), timerId(-1), loop(0) {} - inline void enterLoop(int secs); + inline void enterLoopMSecs(int ms); + inline void enterLoop(int secs) { enterLoopMSecs(secs * 1000); } inline void changeInterval(int secs) { killTimer(timerId); timerId = startTimer(secs * 1000); } @@ -92,7 +93,7 @@ private: QEventLoop *loop; }; -inline void QTestEventLoop::enterLoop(int secs) +inline void QTestEventLoop::enterLoopMSecs(int ms) { Q_ASSERT(!loop); @@ -101,7 +102,7 @@ inline void QTestEventLoop::enterLoop(int secs) inLoop = true; _timeout = false; - timerId = startTimer(secs * 1000); + timerId = startTimer(ms); loop = &l; l.exec(); -- cgit v1.2.3