KDECore
qtest_kde.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2006 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "qtest_kde.h" 00020 00021 // A signal spy which exits the event loop when the signal is called, 00022 // and remembers that the signal was emitted. 00023 class KDESignalSpy : public QObject 00024 { 00025 Q_OBJECT 00026 public: 00027 KDESignalSpy(QObject *obj, const char *signal, int timeout) 00028 : QObject(0), m_obj(obj), m_emitted(false) 00029 { 00030 connect(obj, signal, this, SLOT(slotSignalEmitted())); 00031 if (timeout > 0) { 00032 QObject::connect(&m_timer, SIGNAL(timeout()), &m_loop, SLOT(quit())); 00033 m_timer.setSingleShot(true); 00034 m_timer.start(timeout); 00035 } 00036 m_loop.exec(); 00037 } 00038 bool signalEmitted() const { return m_emitted; } 00039 00040 private Q_SLOTS: 00041 void slotSignalEmitted() 00042 { 00043 m_emitted = true; 00044 disconnect(m_obj, 0, this, 0); 00045 m_timer.stop(); 00046 m_loop.quit(); 00047 } 00048 private: 00049 QObject* m_obj; 00050 bool m_emitted; 00051 QEventLoop m_loop; 00052 QTimer m_timer; 00053 }; 00054 00055 // Unit test for this code: tests/kglobaltest.cpp 00056 00057 bool QTest::kWaitForSignal(QObject *obj, const char *signal, int timeout ) 00058 { 00059 KDESignalSpy spy(obj, signal, timeout); 00060 return spy.signalEmitted(); 00061 } 00062 00063 #include "qtest_kde.moc"
KDE 4.7 API Reference