KDELibs4Support

qtest_kde.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2006 David Faure <[email protected]>
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 version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "qtest_kde.h"
20 
21 // A signal spy which exits the event loop when the signal is called,
22 // and remembers that the signal was emitted.
23 class KDESignalSpy : public QObject
24 {
25  Q_OBJECT
26 public:
27  KDESignalSpy(QObject *obj, const char *signal, int timeout)
28  : QObject(nullptr), m_obj(obj), m_emitted(false)
29  {
30  connect(obj, signal, this, SLOT(slotSignalEmitted()));
31  if (timeout > 0) {
32  QObject::connect(&m_timer, SIGNAL(timeout()), &m_loop, SLOT(quit()));
33  m_timer.setSingleShot(true);
34  m_timer.start(timeout);
35  }
36  m_loop.exec();
37  }
38  bool signalEmitted() const
39  {
40  return m_emitted;
41  }
42 
43 private Q_SLOTS:
44  void slotSignalEmitted()
45  {
46  m_emitted = true;
47  disconnect(m_obj, nullptr, this, nullptr);
48  m_timer.stop();
49  m_loop.quit();
50  }
51 private:
52  QObject *m_obj;
53  bool m_emitted;
54  QEventLoop m_loop;
55  QTimer m_timer;
56 };
57 
58 // Unit test for this code: tests/kglobaltest.cpp
59 
60 bool QTest::kWaitForSignal(QObject *obj, const char *signal, int timeout)
61 {
62  KDESignalSpy spy(obj, signal, timeout);
63  return spy.signalEmitted();
64 }
65 
66 #include "qtest_kde.moc"
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
const QList< QKeySequence > & quit()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jul 13 2020 22:58:32 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.