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#include <QtTest/private/qsignaldumper_p.h>
41
42#include <QtCore/qlist.h>
43#include <QtCore/qmetaobject.h>
44#include <QtCore/qmetatype.h>
45#include <QtCore/qobject.h>
46#include <QtCore/qvariant.h>
47
48#include <QtTest/private/qtestlog_p.h>
49
50#include <QtCore/private/qmetaobject_p.h>
51
52QT_BEGIN_NAMESPACE
53
54namespace QTest
55{
56
57inline static void qPrintMessage(const QByteArray &ba)
58{
59 QTestLog::info(ba.constData(), nullptr, 0);
60}
61
62Q_GLOBAL_STATIC(QList<QByteArray>, ignoreClasses)
63static int iLevel = 0;
64static int ignoreLevel = 0;
65enum { IndentSpacesCount = 4 };
66
67static void qSignalDumperCallback(QObject *caller, int signal_index, void **argv)
68{
69 Q_ASSERT(caller);
70 Q_ASSERT(argv);
71 Q_UNUSED(argv);
72 const QMetaObject *mo = caller->metaObject();
73 Q_ASSERT(mo);
74 QMetaMethod member = QMetaObjectPrivate::signal(mo, signal_index);
75 Q_ASSERT(member.isValid());
76
77 if (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(mo->className())) {
78 ++QTest::ignoreLevel;
79 return;
80 }
81
82 QByteArray str;
83 str.fill(' ', QTest::iLevel++ * QTest::IndentSpacesCount);
84 str += "Signal: ";
85 str += mo->className();
86 str += '(';
87
88 QString objname = caller->objectName();
89 str += objname.toLocal8Bit();
90 if (!objname.isEmpty())
91 str += ' ';
92 str += QByteArray::number(quintptr(caller), 16).rightJustified(8, '0');
93
94 str += ") ";
95 str += member.name();
96 str += " (";
97
98 QList<QByteArray> args = member.parameterTypes();
99 for (int i = 0; i < args.count(); ++i) {
100 const QByteArray &arg = args.at(i);
101 int typeId = QMetaType::fromName(args.at(i).constData()).id();
102 if (arg.endsWith('*') || arg.endsWith('&')) {
103 str += '(';
104 str += arg;
105 str += ')';
106 if (arg.endsWith('&'))
107 str += '@';
108
109 quintptr addr = quintptr(*reinterpret_cast<void **>(argv[i + 1]));
110 str.append(QByteArray::number(addr, 16).rightJustified(8, '0'));
111 } else if (typeId != QMetaType::UnknownType) {
112 Q_ASSERT(typeId != QMetaType::Void); // void parameter => metaobject is corrupt
113 str.append(arg)
114 .append('(')
115 .append(QVariant(QMetaType(typeId), argv[i + 1]).toString().toLocal8Bit())
116 .append(')');
117 }
118 str.append(", ");
119 }
120 if (str.endsWith(", "))
121 str.chop(2);
122 str.append(')');
123 qPrintMessage(str);
124}
125
126static void qSignalDumperCallbackSlot(QObject *caller, int method_index, void **argv)
127{
128 Q_ASSERT(caller);
129 Q_ASSERT(argv);
130 Q_UNUSED(argv);
131 const QMetaObject *mo = caller->metaObject();
132 Q_ASSERT(mo);
133 QMetaMethod member = mo->method(method_index);
134 if (!member.isValid())
135 return;
136
137 if (QTest::ignoreLevel ||
138 (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(mo->className())))
139 return;
140
141 QByteArray str;
142 str.fill(' ', QTest::iLevel * QTest::IndentSpacesCount);
143 str += "Slot: ";
144 str += mo->className();
145 str += '(';
146
147 QString objname = caller->objectName();
148 str += objname.toLocal8Bit();
149 if (!objname.isEmpty())
150 str += ' ';
151 str += QByteArray::number(quintptr(caller), 16).rightJustified(8, '0');
152
153 str += ") ";
154 str += member.methodSignature();
155 qPrintMessage(str);
156}
157
158static void qSignalDumperCallbackEndSignal(QObject *caller, int /*signal_index*/)
159{
160 Q_ASSERT(caller); Q_ASSERT(caller->metaObject());
161 if (QTest::ignoreClasses()
162 && QTest::ignoreClasses()->contains(caller->metaObject()->className())) {
163 --QTest::ignoreLevel;
164 Q_ASSERT(QTest::ignoreLevel >= 0);
165 return;
166 }
167 --QTest::iLevel;
168 Q_ASSERT(QTest::iLevel >= 0);
169}
170
171}
172
173void QSignalDumper::setEnabled(bool enabled)
174{
175 s_isEnabled = enabled;
176}
177
178void QSignalDumper::startDump()
179{
180 if (!s_isEnabled)
181 return;
182
183 static QSignalSpyCallbackSet set = { QTest::qSignalDumperCallback,
184 QTest::qSignalDumperCallbackSlot, QTest::qSignalDumperCallbackEndSignal, nullptr };
185 qt_register_signal_spy_callbacks(&set);
186}
187
188void QSignalDumper::endDump()
189{
190 qt_register_signal_spy_callbacks(nullptr);
191}
192
193void QSignalDumper::ignoreClass(const QByteArray &klass)
194{
195 if (QTest::ignoreClasses())
196 QTest::ignoreClasses()->append(klass);
197}
198
199void QSignalDumper::clearIgnoredClasses()
200{
201 if (QTest::ignoreClasses())
202 QTest::ignoreClasses()->clear();
203}
204
205bool QSignalDumper::s_isEnabled = false;
206
207QT_END_NAMESPACE
208