tools/qtestlib/src/qtesteventloop.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the tools applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #ifndef QTESTEVENTLOOP_H
00025 #define QTESTEVENTLOOP_H
00026 
00027 #include <QtTest/qtest_global.h>
00028 
00029 #include <QtCore/qcoreapplication.h>
00030 #include <QtCore/qeventloop.h>
00031 #include <QtCore/qobject.h>
00032 #include <QtCore/qpointer.h>
00033 
00034 QT_BEGIN_HEADER
00035 
00036 class Q_TESTLIB_EXPORT QTestEventLoop : public QObject
00037 {
00038     Q_OBJECT
00039 
00040 public:
00041     inline QTestEventLoop(QObject *aParent = 0)
00042         : QObject(aParent), inLoop(false), _timeout(false), timerId(-1), loop(0) {}
00043     inline void enterLoop(int secs);
00044 
00045 
00046     inline void changeInterval(int secs)
00047     { killTimer(timerId); timerId = startTimer(secs * 1000); }
00048 
00049     inline bool timeout() const
00050     { return _timeout; }
00051 
00052     inline static QTestEventLoop &instance()
00053     {
00054         static QPointer<QTestEventLoop> testLoop;
00055         if (testLoop.isNull())
00056             testLoop = new QTestEventLoop(QCoreApplication::instance());
00057         return *static_cast<QTestEventLoop *>(testLoop);
00058     }
00059 
00060 public Q_SLOTS:
00061     inline void exitLoop();
00062 
00063 protected:
00064     inline void timerEvent(QTimerEvent *e);
00065 
00066 private:
00067     bool inLoop;
00068     bool _timeout;
00069     int timerId;
00070 
00071     QEventLoop *loop;
00072 };
00073 
00074 inline void QTestEventLoop::enterLoop(int secs)
00075 {
00076     Q_ASSERT(!loop);
00077 
00078     QEventLoop l;
00079 
00080     inLoop = true;
00081     _timeout = false;
00082 
00083     timerId = startTimer(secs * 1000);
00084 
00085     loop = &l;
00086     l.exec();
00087     loop = 0;
00088 }
00089 
00090 inline void QTestEventLoop::exitLoop()
00091 {
00092     Q_ASSERT(loop);
00093 
00094     killTimer(timerId); timerId = -1;
00095 
00096     loop->exit();
00097 
00098     inLoop = false;
00099 }
00100 
00101 inline void QTestEventLoop::timerEvent(QTimerEvent *e)
00102 {
00103     if (e->timerId() != timerId)
00104         return;
00105     _timeout = true;
00106     exitLoop();
00107 }
00108 
00109 QT_END_HEADER
00110 
00111 #endif

Generated on Thu Mar 15 12:03:16 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1