1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "QtTest/private/qsignaldumper_p.h"
43 
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>
49 
50 #include "QtTest/private/qtestlog_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(), 0, 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 
memberName(const QMetaMethod & member)67 static QByteArray memberName(const QMetaMethod &member)
68 {
69     QByteArray ba = member.signature();
70     return ba.left(ba.indexOf('('));
71 }
72 
qSignalDumperCallback(QObject * caller,int method_index,void ** argv)73 static void qSignalDumperCallback(QObject *caller, int method_index, void **argv)
74 {
75     Q_ASSERT(caller); Q_ASSERT(argv); Q_UNUSED(argv);
76     const QMetaObject *mo = caller->metaObject();
77     Q_ASSERT(mo);
78     QMetaMethod member = mo->method(method_index);
79     Q_ASSERT(member.signature());
80 
81     if (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(mo->className())) {
82         ++QTest::ignoreLevel;
83         return;
84     }
85 
86     QByteArray str;
87     str.fill(' ', QTest::iLevel++ * QTest::IndentSpacesCount);
88     str += "Signal: ";
89     str += mo->className();
90     str += '(';
91 
92     QString objname = caller->objectName();
93     str += objname.toLocal8Bit();
94     if (!objname.isEmpty())
95         str += ' ';
96     str += QByteArray::number(quintptr(caller), 16);
97 
98     str += ") ";
99     str += QTest::memberName(member);
100     str += " (";
101 
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('&')) {
107             str += '(';
108             str += arg;
109             str += ')';
110             if (arg.endsWith('&'))
111                 str += '@';
112 
113             quintptr addr = quintptr(*reinterpret_cast<void **>(argv[i + 1]));
114             str.append(QByteArray::number(addr, 16));
115         } else if (typeId != QMetaType::Void) {
116             str.append(arg)
117                 .append('(')
118                 .append(QVariant(typeId, argv[i + 1]).toString().toLocal8Bit())
119                 .append(')');
120         }
121         str.append(", ");
122     }
123     if (str.endsWith(", "))
124         str.chop(2);
125     str.append(')');
126     qPrintMessage(str);
127 }
128 
qSignalDumperCallbackSlot(QObject * caller,int method_index,void ** argv)129 static void qSignalDumperCallbackSlot(QObject *caller, int method_index, void **argv)
130 {
131     Q_ASSERT(caller); Q_ASSERT(argv); Q_UNUSED(argv);
132     const QMetaObject *mo = caller->metaObject();
133     Q_ASSERT(mo);
134     QMetaMethod member = mo->method(method_index);
135     if (!member.signature())
136         return;
137 
138     if (QTest::ignoreLevel ||
139             (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(mo->className())))
140         return;
141 
142     QByteArray str;
143     str.fill(' ', QTest::iLevel * QTest::IndentSpacesCount);
144     str += "Slot: ";
145     str += mo->className();
146     str += '(';
147 
148     QString objname = caller->objectName();
149     str += objname.toLocal8Bit();
150     if (!objname.isEmpty())
151         str += ' ';
152     str += QByteArray::number(quintptr(caller), 16);
153 
154     str += ") ";
155     str += member.signature();
156     qPrintMessage(str);
157 }
158 
qSignalDumperCallbackEndSignal(QObject * caller,int)159 static void qSignalDumperCallbackEndSignal(QObject *caller, int /*method_index*/)
160 {
161     Q_ASSERT(caller); Q_ASSERT(caller->metaObject());
162     if (QTest::ignoreClasses()
163             && QTest::ignoreClasses()->contains(caller->metaObject()->className())) {
164         --QTest::ignoreLevel;
165         Q_ASSERT(QTest::ignoreLevel >= 0);
166         return;
167     }
168     --QTest::iLevel;
169     Q_ASSERT(QTest::iLevel >= 0);
170 }
171 
172 }
173 
174 // this struct is copied from qobject_p.h to prevent us
175 // from including private Qt headers.
176 struct QSignalSpyCallbackSet
177 {
178     typedef void (*BeginCallback)(QObject *caller, int method_index, void **argv);
179     typedef void (*EndCallback)(QObject *caller, int method_index);
180     BeginCallback signal_begin_callback,
181                   slot_begin_callback;
182     EndCallback signal_end_callback,
183                 slot_end_callback;
184 };
185 extern void Q_CORE_EXPORT qt_register_signal_spy_callbacks(const QSignalSpyCallbackSet &);
186 
startDump()187 void QSignalDumper::startDump()
188 {
189     static QSignalSpyCallbackSet set = { QTest::qSignalDumperCallback,
190         QTest::qSignalDumperCallbackSlot, QTest::qSignalDumperCallbackEndSignal, 0 };
191     qt_register_signal_spy_callbacks(set);
192 }
193 
endDump()194 void QSignalDumper::endDump()
195 {
196     static QSignalSpyCallbackSet nset = { 0, 0, 0 ,0 };
197     qt_register_signal_spy_callbacks(nset);
198 }
199 
ignoreClass(const QByteArray & klass)200 void QSignalDumper::ignoreClass(const QByteArray &klass)
201 {
202     if (QTest::ignoreClasses())
203         QTest::ignoreClasses()->append(klass);
204 }
205 
clearIgnoredClasses()206 void QSignalDumper::clearIgnoredClasses()
207 {
208     if (QTest::ignoreClasses())
209         QTest::ignoreClasses()->clear();
210 }
211 
212 QT_END_NAMESPACE
213