1 /* 2 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies) 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Library General Public 6 License as published by the Free Software Foundation; either 7 version 2 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Library General Public License for more details. 13 14 You should have received a copy of the GNU Library General Public License 15 along with this library; see the file COPYING.LIB. If not, write to 16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 Boston, MA 02110-1301, USA. 18 */ 19 // Functions and macros that really need to be in QTestLib 20 21 #include <QEventLoop> 22 #include <QSignalSpy> 23 #include <QTimer> 24 25 #if !defined(TESTS_SOURCE_DIR) 26 #define TESTS_SOURCE_DIR "" 27 #endif 28 29 /** 30 * Starts an event loop that runs until the given signal is received. 31 * Optionally the event loop 32 * can return earlier on a timeout. 33 * 34 * \return \p true if the requested signal was received 35 * \p false on timeout 36 */ 37 static bool waitForSignal(QObject* obj, const char* signal, int timeout = 10000) 38 { 39 QEventLoop loop; 40 QObject::connect(obj, signal, &loop, SLOT(quit())); 41 QTimer timer; 42 QSignalSpy timeoutSpy(&timer, SIGNAL(timeout())); 43 if (timeout > 0) { 44 QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); 45 timer.setSingleShot(true); 46 timer.start(timeout); 47 } 48 loop.exec(); 49 return timeoutSpy.isEmpty(); 50 } 51 52 // Will try to wait for the condition while allowing event processing 53 #define QTRY_VERIFY(__expr) \ 54 do { \ 55 const int __step = 50; \ 56 const int __timeout = 5000; \ 57 if (!(__expr)) { \ 58 QTest::qWait(0); \ 59 } \ 60 for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \ 61 QTest::qWait(__step); \ 62 } \ 63 QVERIFY(__expr); \ 64 } while(0) 65 66 // Will try to wait for the condition while allowing event processing 67 #define QTRY_COMPARE(__expr, __expected) \ 68 do { \ 69 const int __step = 50; \ 70 const int __timeout = 5000; \ 71 if ((__expr) != (__expected)) { \ 72 QTest::qWait(0); \ 73 } \ 74 for (int __i = 0; __i < __timeout && ((__expr) != (__expected)); __i+=__step) { \ 75 QTest::qWait(__step); \ 76 } \ 77 QCOMPARE(__expr, __expected); \ 78 } while(0) 79