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