Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2016 The Qt Company Ltd.
4 : ** Contact: https://www.qt.io/licensing/
5 : **
6 : ** This file is part of the QtTest module of the Qt Toolkit.
7 : **
8 : ** $QT_BEGIN_LICENSE:LGPL$
9 : ** Commercial License Usage
10 : ** Licensees holding valid commercial Qt licenses may use this file in
11 : ** accordance with the commercial license agreement provided with the
12 : ** Software or, alternatively, in accordance with the terms contained in
13 : ** a written agreement between you and The Qt Company. For licensing terms
14 : ** and conditions see https://www.qt.io/terms-conditions. For further
15 : ** information use the contact form at https://www.qt.io/contact-us.
16 : **
17 : ** GNU Lesser General Public License Usage
18 : ** Alternatively, this file may be used under the terms of the GNU Lesser
19 : ** General Public License version 3 as published by the Free Software
20 : ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 : ** packaging of this file. Please review the following information to
22 : ** ensure the GNU Lesser General Public License version 3 requirements
23 : ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 : **
25 : ** GNU General Public License Usage
26 : ** Alternatively, this file may be used under the terms of the GNU
27 : ** General Public License version 2.0 or (at your option) the GNU General
28 : ** Public license version 3 or any later version approved by the KDE Free
29 : ** Qt Foundation. The licenses are as published by the Free Software
30 : ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 : ** included in the packaging of this file. Please review the following
32 : ** information to ensure the GNU General Public License requirements will
33 : ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 : ** https://www.gnu.org/licenses/gpl-3.0.html.
35 : **
36 : ** $QT_END_LICENSE$
37 : **
38 : ****************************************************************************/
39 :
40 : #ifndef QSIGNALSPY_H
41 : #define QSIGNALSPY_H
42 :
43 : #include <QtCore/qbytearray.h>
44 : #include <QtCore/qlist.h>
45 : #include <QtCore/qobject.h>
46 : #include <QtCore/qmetaobject.h>
47 : #include <QtCore/qvariant.h>
48 : #include <QtCore/qvector.h>
49 : #include <QtTest/qtesteventloop.h>
50 :
51 : QT_BEGIN_NAMESPACE
52 :
53 :
54 : class QVariant;
55 :
56 0 : class QSignalSpy: public QObject, public QList<QList<QVariant> >
57 : {
58 : public:
59 0 : explicit QSignalSpy(const QObject *obj, const char *aSignal)
60 0 : : m_waiting(false)
61 : {
62 : #ifdef Q_CC_BOR
63 : const int memberOffset = QObject::staticMetaObject.methodCount();
64 : #else
65 0 : static const int memberOffset = QObject::staticMetaObject.methodCount();
66 : #endif
67 0 : if (!obj) {
68 0 : qWarning("QSignalSpy: Cannot spy on a null object");
69 0 : return;
70 : }
71 :
72 0 : if (!aSignal) {
73 0 : qWarning("QSignalSpy: Null signal name is not valid");
74 0 : return;
75 : }
76 :
77 0 : if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE) {
78 0 : qWarning("QSignalSpy: Not a valid signal, use the SIGNAL macro");
79 0 : return;
80 : }
81 :
82 0 : const QByteArray ba = QMetaObject::normalizedSignature(aSignal + 1);
83 0 : const QMetaObject * const mo = obj->metaObject();
84 0 : const int sigIndex = mo->indexOfMethod(ba.constData());
85 0 : if (sigIndex < 0) {
86 0 : qWarning("QSignalSpy: No such signal: '%s'", ba.constData());
87 0 : return;
88 : }
89 :
90 0 : if (!QMetaObject::connect(obj, sigIndex, this, memberOffset,
91 0 : Qt::DirectConnection, Q_NULLPTR)) {
92 0 : qWarning("QSignalSpy: QMetaObject::connect returned false. Unable to connect.");
93 0 : return;
94 : }
95 0 : sig = ba;
96 0 : initArgs(mo->method(sigIndex), obj);
97 : }
98 :
99 : #ifdef Q_QDOC
100 : QSignalSpy(const QObject *object, PointerToMemberFunction signal);
101 : #else
102 : template <typename Func>
103 : QSignalSpy(const typename QtPrivate::FunctionPointer<Func>::Object *obj, Func signal0)
104 : : m_waiting(false)
105 : {
106 : #ifdef Q_CC_BOR
107 : const int memberOffset = QObject::staticMetaObject.methodCount();
108 : #else
109 : static const int memberOffset = QObject::staticMetaObject.methodCount();
110 : #endif
111 : if (!obj) {
112 : qWarning("QSignalSpy: Cannot spy on a null object");
113 : return;
114 : }
115 :
116 : if (!signal0) {
117 : qWarning("QSignalSpy: Null signal name is not valid");
118 : return;
119 : }
120 :
121 : const QMetaObject * const mo = obj->metaObject();
122 : const QMetaMethod signalMetaMethod = QMetaMethod::fromSignal(signal0);
123 : const int sigIndex = signalMetaMethod.methodIndex();
124 : if (!signalMetaMethod.isValid() ||
125 : signalMetaMethod.methodType() != QMetaMethod::Signal) {
126 : qWarning("QSignalSpy: Not a valid signal: '%s'",
127 : signalMetaMethod.methodSignature().constData());
128 : return;
129 : }
130 :
131 : if (!QMetaObject::connect(obj, sigIndex, this, memberOffset,
132 : Qt::DirectConnection, 0)) {
133 : qWarning("QSignalSpy: QMetaObject::connect returned false. Unable to connect.");
134 : return;
135 : }
136 : sig = signalMetaMethod.methodSignature();
137 : initArgs(mo->method(sigIndex), obj);
138 : }
139 : #endif // Q_QDOC
140 :
141 : inline bool isValid() const { return !sig.isEmpty(); }
142 : inline QByteArray signal() const { return sig; }
143 :
144 0 : bool wait(int timeout = 5000)
145 : {
146 0 : Q_ASSERT(!m_waiting);
147 0 : const int origCount = count();
148 0 : m_waiting = true;
149 0 : m_loop.enterLoopMSecs(timeout);
150 0 : m_waiting = false;
151 0 : return count() > origCount;
152 : }
153 :
154 0 : int qt_metacall(QMetaObject::Call call, int methodId, void **a) Q_DECL_OVERRIDE
155 : {
156 0 : methodId = QObject::qt_metacall(call, methodId, a);
157 0 : if (methodId < 0)
158 0 : return methodId;
159 :
160 0 : if (call == QMetaObject::InvokeMetaMethod) {
161 0 : if (methodId == 0) {
162 0 : appendArgs(a);
163 : }
164 0 : --methodId;
165 : }
166 0 : return methodId;
167 : }
168 :
169 : private:
170 0 : void initArgs(const QMetaMethod &member, const QObject *obj)
171 : {
172 0 : args.reserve(member.parameterCount());
173 0 : for (int i = 0; i < member.parameterCount(); ++i) {
174 0 : int tp = member.parameterType(i);
175 0 : if (tp == QMetaType::UnknownType && obj) {
176 0 : void *argv[] = { &tp, &i };
177 0 : QMetaObject::metacall(const_cast<QObject*>(obj),
178 : QMetaObject::RegisterMethodArgumentMetaType,
179 0 : member.methodIndex(), argv);
180 0 : if (tp == -1)
181 0 : tp = QMetaType::UnknownType;
182 : }
183 0 : if (tp == QMetaType::UnknownType) {
184 0 : qWarning("QSignalSpy: Unable to handle parameter '%s' of type '%s' of method '%s',"
185 : " use qRegisterMetaType to register it.",
186 0 : member.parameterNames().at(i).constData(),
187 0 : member.parameterTypes().at(i).constData(),
188 0 : member.name().constData());
189 : }
190 0 : args << tp;
191 : }
192 0 : }
193 :
194 0 : void appendArgs(void **a)
195 : {
196 0 : QList<QVariant> list;
197 0 : list.reserve(args.count());
198 0 : for (int i = 0; i < args.count(); ++i) {
199 0 : const QMetaType::Type type = static_cast<QMetaType::Type>(args.at(i));
200 0 : if (type == QMetaType::QVariant)
201 0 : list << *reinterpret_cast<QVariant *>(a[i + 1]);
202 : else
203 0 : list << QVariant(type, a[i + 1]);
204 : }
205 0 : append(list);
206 :
207 0 : if (m_waiting)
208 0 : m_loop.exitLoop();
209 0 : }
210 :
211 : // the full, normalized signal name
212 : QByteArray sig;
213 : // holds the QMetaType types for the argument list of the signal
214 : QVector<int> args;
215 :
216 : QTestEventLoop m_loop;
217 : bool m_waiting;
218 : };
219 :
220 : QT_END_NAMESPACE
221 :
222 : #endif
|