Introduce QMetaType::UnknownType.
[profile/ivi/qtbase.git] / src / testlib / qsignaldumper.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
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
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
67 static QByteArray memberName(const QMetaMethod &member)
68 {
69     QByteArray ba = member.methodSignature();
70     return ba.left(ba.indexOf('('));
71 }
72
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.isValid());
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::UnknownType) {
116             Q_ASSERT(typeId != QMetaType::Void); // void parameter => metaobject is corrupt
117             str.append(arg)
118                 .append('(')
119                 .append(QVariant(typeId, argv[i + 1]).toString().toLocal8Bit())
120                 .append(')');
121         }
122         str.append(", ");
123     }
124     if (str.endsWith(", "))
125         str.chop(2);
126     str.append(')');
127     qPrintMessage(str);
128 }
129
130 static void qSignalDumperCallbackSlot(QObject *caller, int method_index, void **argv)
131 {
132     Q_ASSERT(caller); Q_ASSERT(argv); Q_UNUSED(argv);
133     const QMetaObject *mo = caller->metaObject();
134     Q_ASSERT(mo);
135     QMetaMethod member = mo->method(method_index);
136     if (!member.isValid())
137         return;
138
139     if (QTest::ignoreLevel ||
140             (QTest::ignoreClasses() && QTest::ignoreClasses()->contains(mo->className())))
141         return;
142
143     QByteArray str;
144     str.fill(' ', QTest::iLevel * QTest::IndentSpacesCount);
145     str += "Slot: ";
146     str += mo->className();
147     str += '(';
148
149     QString objname = caller->objectName();
150     str += objname.toLocal8Bit();
151     if (!objname.isEmpty())
152         str += ' ';
153     str += QByteArray::number(quintptr(caller), 16);
154
155     str += ") ";
156     str += member.methodSignature();
157     qPrintMessage(str);
158 }
159
160 static void qSignalDumperCallbackEndSignal(QObject *caller, int /*method_index*/)
161 {
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);
167         return;
168     }
169     --QTest::iLevel;
170     Q_ASSERT(QTest::iLevel >= 0);
171 }
172
173 }
174
175 // this struct is copied from qobject_p.h to prevent us
176 // from including private Qt headers.
177 struct QSignalSpyCallbackSet
178 {
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,
182                   slot_begin_callback;
183     EndCallback signal_end_callback,
184                 slot_end_callback;
185 };
186 extern void Q_CORE_EXPORT qt_register_signal_spy_callbacks(const QSignalSpyCallbackSet &);
187
188 void QSignalDumper::startDump()
189 {
190     static QSignalSpyCallbackSet set = { QTest::qSignalDumperCallback,
191         QTest::qSignalDumperCallbackSlot, QTest::qSignalDumperCallbackEndSignal, 0 };
192     qt_register_signal_spy_callbacks(set);
193 }
194
195 void QSignalDumper::endDump()
196 {
197     static QSignalSpyCallbackSet nset = { 0, 0, 0 ,0 };
198     qt_register_signal_spy_callbacks(nset);
199 }
200
201 void QSignalDumper::ignoreClass(const QByteArray &klass)
202 {
203     if (QTest::ignoreClasses())
204         QTest::ignoreClasses()->append(klass);
205 }
206
207 void QSignalDumper::clearIgnoredClasses()
208 {
209     if (QTest::ignoreClasses())
210         QTest::ignoreClasses()->clear();
211 }
212
213 QT_END_NAMESPACE