src/testlib/qsignaldumper.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtTest module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    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.signature();
       
    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.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 
       
   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 
       
   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 
       
   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 
       
   194 void QSignalDumper::endDump()
       
   195 {
       
   196     static QSignalSpyCallbackSet nset = { 0, 0, 0 ,0 };
       
   197     qt_register_signal_spy_callbacks(nset);
       
   198 }
       
   199 
       
   200 void QSignalDumper::ignoreClass(const QByteArray &klass)
       
   201 {
       
   202     if (QTest::ignoreClasses())
       
   203         QTest::ignoreClasses()->append(klass);
       
   204 }
       
   205 
       
   206 void QSignalDumper::clearIgnoredClasses()
       
   207 {
       
   208     if (QTest::ignoreClasses())
       
   209         QTest::ignoreClasses()->clear();
       
   210 }
       
   211 
       
   212 QT_END_NAMESPACE