1/* 2 Copyright (C) 2009 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#if 0 22#pragma qt_no_master_include 23#endif 24 25#include <QEventLoop> 26#include <QSignalSpy> 27#include <QTimer> 28 29#if !defined(TESTS_SOURCE_DIR) 30#define TESTS_SOURCE_DIR "" 31#endif 32 33/** 34 * Starts an event loop that runs until the given signal is received. 35 * Optionally the event loop 36 * can return earlier on a timeout. 37 * 38 * \return \p true if the requested signal was received 39 * \p false on timeout 40 */ 41static inline bool waitForSignal(QObject* obj, const char* signal, int timeout = 10000) 42{ 43 QEventLoop loop; 44 QObject::connect(obj, signal, &loop, SLOT(quit())); 45 QTimer timer; 46 QSignalSpy timeoutSpy(&timer, SIGNAL(timeout())); 47 if (timeout > 0) { 48 QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); 49 timer.setSingleShot(true); 50 timer.start(timeout); 51 } 52 loop.exec(); 53 return timeoutSpy.isEmpty(); 54} 55 56/** 57 * Just like QSignalSpy but facilitates sync and async 58 * signal emission. For example if you want to verify that 59 * page->foo() emitted a signal, it could be that the 60 * implementation decides to emit the signal asynchronously 61 * - in which case we want to spin a local event loop until 62 * emission - or that the call to foo() emits it right away. 63 */ 64class SignalBarrier : private QSignalSpy 65{ 66public: 67 SignalBarrier(const QObject* obj, const char* aSignal) 68 : QSignalSpy(obj, aSignal) 69 { } 70 71 bool ensureSignalEmitted() 72 { 73 bool result = count() > 0; 74 if (!result) 75 result = wait(); 76 clear(); 77 return result; 78 } 79}; 80 81#define W_QSKIP(a, b) QSKIP(a) 82