1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtTest module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/private/qsignaldumper_p.h>
44 #include <QtCore/qlist.h>
45 #include <QtCore/qmetaobject.h>
46 #include <QtCore/qmetatype.h>
47 #include <QtCore/qobject.h>
48 #include <QtCore/qvariant.h>
50 #include <QtTest/private/qtestlog_p.h>
57 inline static void qPrintMessage(const QByteArray &ba)
59 QTestLog::info(ba.constData(), 0, 0);
62 Q_GLOBAL_STATIC(QList<QByteArray>, ignoreClasses)
63 static int iLevel = 0;
64 static int ignoreLevel = 0;
65 enum { IndentSpacesCount = 4 };
67 static QByteArray memberName(const QMetaMethod &member)
69 QByteArray ba = member.methodSignature();
70 return ba.left(ba.indexOf('('));
73 static void qSignalDumperCallback(QObject *caller, int method_index, void **argv)
75 Q_ASSERT(caller); Q_ASSERT(argv); Q_UNUSED(argv);
76 const QMetaObject *mo = caller->metaObject();
78 QMetaMethod member = mo->method(method_index);
79 Q_ASSERT(member.isValid());
81 if (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(mo->className())) {
87 str.fill(' ', QTest::iLevel++ * QTest::IndentSpacesCount);
89 str += mo->className();
92 QString objname = caller->objectName();
93 str += objname.toLocal8Bit();
94 if (!objname.isEmpty())
96 str += QByteArray::number(quintptr(caller), 16);
99 str += QTest::memberName(member);
102 QList<QByteArray> args = member.parameterTypes();
103 for (int i = 0; i < args.count(); ++i) {
104 const QByteArray &arg = args.at(i);
105 int typeId = QMetaType::type(args.at(i).constData());
106 if (arg.endsWith('*') || arg.endsWith('&')) {
110 if (arg.endsWith('&'))
113 quintptr addr = quintptr(*reinterpret_cast<void **>(argv[i + 1]));
114 str.append(QByteArray::number(addr, 16));
115 } else if (typeId != QMetaType::UnknownType) {
116 Q_ASSERT(typeId != QMetaType::Void); // void parameter => metaobject is corrupt
119 .append(QVariant(typeId, argv[i + 1]).toString().toLocal8Bit())
124 if (str.endsWith(", "))
130 static void qSignalDumperCallbackSlot(QObject *caller, int method_index, void **argv)
132 Q_ASSERT(caller); Q_ASSERT(argv); Q_UNUSED(argv);
133 const QMetaObject *mo = caller->metaObject();
135 QMetaMethod member = mo->method(method_index);
136 if (!member.isValid())
139 if (QTest::ignoreLevel ||
140 (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(mo->className())))
144 str.fill(' ', QTest::iLevel * QTest::IndentSpacesCount);
146 str += mo->className();
149 QString objname = caller->objectName();
150 str += objname.toLocal8Bit();
151 if (!objname.isEmpty())
153 str += QByteArray::number(quintptr(caller), 16);
156 str += member.methodSignature();
160 static void qSignalDumperCallbackEndSignal(QObject *caller, int /*method_index*/)
162 Q_ASSERT(caller); Q_ASSERT(caller->metaObject());
163 if (QTest::ignoreClasses()
164 && QTest::ignoreClasses()->contains(caller->metaObject()->className())) {
165 --QTest::ignoreLevel;
166 Q_ASSERT(QTest::ignoreLevel >= 0);
170 Q_ASSERT(QTest::iLevel >= 0);
175 // this struct is copied from qobject_p.h to prevent us
176 // from including private Qt headers.
177 struct QSignalSpyCallbackSet
179 typedef void (*BeginCallback)(QObject *caller, int method_index, void **argv);
180 typedef void (*EndCallback)(QObject *caller, int method_index);
181 BeginCallback signal_begin_callback,
183 EndCallback signal_end_callback,
186 extern void Q_CORE_EXPORT qt_register_signal_spy_callbacks(const QSignalSpyCallbackSet &);
188 void QSignalDumper::startDump()
190 static QSignalSpyCallbackSet set = { QTest::qSignalDumperCallback,
191 QTest::qSignalDumperCallbackSlot, QTest::qSignalDumperCallbackEndSignal, 0 };
192 qt_register_signal_spy_callbacks(set);
195 void QSignalDumper::endDump()
197 static QSignalSpyCallbackSet nset = { 0, 0, 0 ,0 };
198 qt_register_signal_spy_callbacks(nset);
201 void QSignalDumper::ignoreClass(const QByteArray &klass)
203 if (QTest::ignoreClasses())
204 QTest::ignoreClasses()->append(klass);
207 void QSignalDumper::clearIgnoredClasses()
209 if (QTest::ignoreClasses())
210 QTest::ignoreClasses()->clear();