| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QSIGNALSPY_H |
| 5 | #define QSIGNALSPY_H |
| 6 | |
| 7 | #include <QtCore/qbytearray.h> |
| 8 | #include <QtCore/qlist.h> |
| 9 | #include <QtCore/qmetaobject.h> |
| 10 | #include <QtTest/qtesteventloop.h> |
| 11 | #include <QtCore/qvariant.h> |
| 12 | #include <QtCore/qmutex.h> |
| 13 | |
| 14 | #include <memory> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | |
| 19 | class QVariant; |
| 20 | class QSignalSpyPrivate; |
| 21 | class QSignalSpy : public QList<QList<QVariant> > |
| 22 | { |
| 23 | struct ObjectSignal { |
| 24 | const QObject *obj; |
| 25 | QMetaMethod sig; |
| 26 | }; |
| 27 | friend class QSignalSpyPrivate; |
| 28 | std::unique_ptr<QSignalSpyPrivate> d_ptr; |
| 29 | public: |
| 30 | explicit QSignalSpy(const QObject *obj, const char *aSignal) |
| 31 | : QSignalSpy(verify(obj, aSignal)) {} |
| 32 | #ifdef Q_QDOC |
| 33 | template <typename PointerToMemberFunction> |
| 34 | QSignalSpy(const QObject *object, PointerToMemberFunction signal); |
| 35 | #else |
| 36 | template <typename Func> |
| 37 | QSignalSpy(const typename QtPrivate::FunctionPointer<Func>::Object *obj, Func signal0) |
| 38 | : QSignalSpy(verify(obj, QMetaMethod::fromSignal(signal0))) {} |
| 39 | #endif // Q_QDOC |
| 40 | QSignalSpy(const QObject *obj, QMetaMethod signal) |
| 41 | : QSignalSpy(verify(obj, signal)) {} |
| 42 | Q_TESTLIB_EXPORT ~QSignalSpy(); |
| 43 | |
| 44 | bool isValid() const noexcept { return d_ptr != nullptr; } |
| 45 | inline QByteArray signal() const { return sig; } |
| 46 | |
| 47 | bool wait(int timeout) |
| 48 | { return wait(timeout: std::chrono::milliseconds{timeout}); } |
| 49 | |
| 50 | Q_TESTLIB_EXPORT bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{5}); |
| 51 | |
| 52 | private: |
| 53 | Q_TESTLIB_EXPORT explicit QSignalSpy(ObjectSignal os); |
| 54 | |
| 55 | Q_TESTLIB_EXPORT static ObjectSignal verify(const QObject *obj, QMetaMethod signal); |
| 56 | Q_TESTLIB_EXPORT static ObjectSignal verify(const QObject *obj, const char *aSignal); |
| 57 | |
| 58 | Q_TESTLIB_EXPORT void appendArgs(void **a); |
| 59 | |
| 60 | // the full, normalized signal name |
| 61 | const QByteArray sig; |
| 62 | // holds the QMetaType types for the argument list of the signal |
| 63 | const QList<int> args; |
| 64 | |
| 65 | QTestEventLoop m_loop; |
| 66 | bool m_waiting = false; |
| 67 | QMutex m_mutex; // protects m_waiting and the QList base class, between appendArgs() and wait() |
| 68 | }; |
| 69 | |
| 70 | QT_END_NAMESPACE |
| 71 | |
| 72 | #endif |
| 73 | |