File indexing completed on 2024-04-21 14:55:45
0001 /* This file is part of the KDE libraries 0002 Copyright (C) 2006 David Faure <faure@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License version 2 as published by the Free Software Foundation. 0007 0008 This library is distributed in the hope that it will be useful, 0009 but WITHOUT ANY WARRANTY; without even the implied warranty of 0010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0011 Library General Public License for more details. 0012 0013 You should have received a copy of the GNU Library General Public License 0014 along with this library; see the file COPYING.LIB. If not, write to 0015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0016 Boston, MA 02110-1301, USA. 0017 */ 0018 0019 #include "qtest_kde.h" 0020 0021 #include <QTimer> 0022 0023 // A signal spy which exits the event loop when the signal is called, 0024 // and remembers that the signal was emitted. 0025 class KDESignalSpy : public QObject 0026 { 0027 Q_OBJECT 0028 public: 0029 KDESignalSpy(QObject *obj, const char *signal, int timeout) 0030 : QObject(nullptr), m_obj(obj), m_emitted(false) 0031 { 0032 connect(obj, signal, this, SLOT(slotSignalEmitted())); 0033 if (timeout > 0) { 0034 QObject::connect(&m_timer, SIGNAL(timeout()), &m_loop, SLOT(quit())); 0035 m_timer.setSingleShot(true); 0036 m_timer.start(timeout); 0037 } 0038 m_loop.exec(); 0039 } 0040 bool signalEmitted() const 0041 { 0042 return m_emitted; 0043 } 0044 0045 private Q_SLOTS: 0046 void slotSignalEmitted() 0047 { 0048 m_emitted = true; 0049 disconnect(m_obj, nullptr, this, nullptr); 0050 m_timer.stop(); 0051 m_loop.quit(); 0052 } 0053 private: 0054 QObject *m_obj; 0055 bool m_emitted; 0056 QEventLoop m_loop; 0057 QTimer m_timer; 0058 }; 0059 0060 // Unit test for this code: tests/kglobaltest.cpp 0061 0062 bool QTest::kWaitForSignal(QObject *obj, const char *signal, int timeout) 0063 { 0064 KDESignalSpy spy(obj, signal, timeout); 0065 return spy.signalEmitted(); 0066 } 0067 0068 #include "qtest_kde.moc"