LCOV - code coverage report
Current view: top level - home/aheinecke/dev/main/qt5/include/QtTest - qsignalspy.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 70 60.0 %
Date: 2018-11-15 08:49:49 Functions: 6 7 85.7 %

          Line data    Source code
       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             : #ifndef QSIGNALSPY_H
      41             : #define QSIGNALSPY_H
      42             : 
      43             : #include <QtCore/qbytearray.h>
      44             : #include <QtCore/qlist.h>
      45             : #include <QtCore/qobject.h>
      46             : #include <QtCore/qmetaobject.h>
      47             : #include <QtCore/qvariant.h>
      48             : #include <QtCore/qvector.h>
      49             : #include <QtTest/qtesteventloop.h>
      50             : 
      51             : QT_BEGIN_NAMESPACE
      52             : 
      53             : 
      54             : class QVariant;
      55             : 
      56           6 : class QSignalSpy: public QObject, public QList<QList<QVariant> >
      57             : {
      58             : public:
      59           6 :     explicit QSignalSpy(const QObject *obj, const char *aSignal)
      60           6 :         : m_waiting(false)
      61             :     {
      62             : #ifdef Q_CC_BOR
      63             :         const int memberOffset = QObject::staticMetaObject.methodCount();
      64             : #else
      65           6 :         static const int memberOffset = QObject::staticMetaObject.methodCount();
      66             : #endif
      67           6 :         if (!obj) {
      68           0 :             qWarning("QSignalSpy: Cannot spy on a null object");
      69           0 :             return;
      70             :         }
      71             : 
      72           6 :         if (!aSignal) {
      73           0 :             qWarning("QSignalSpy: Null signal name is not valid");
      74           0 :             return;
      75             :         }
      76             : 
      77           6 :         if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE) {
      78           0 :             qWarning("QSignalSpy: Not a valid signal, use the SIGNAL macro");
      79           0 :             return;
      80             :         }
      81             : 
      82          12 :         const QByteArray ba = QMetaObject::normalizedSignature(aSignal + 1);
      83           6 :         const QMetaObject * const mo = obj->metaObject();
      84           6 :         const int sigIndex = mo->indexOfMethod(ba.constData());
      85           6 :         if (sigIndex < 0) {
      86           0 :             qWarning("QSignalSpy: No such signal: '%s'", ba.constData());
      87           0 :             return;
      88             :         }
      89             : 
      90          12 :         if (!QMetaObject::connect(obj, sigIndex, this, memberOffset,
      91           6 :                     Qt::DirectConnection, Q_NULLPTR)) {
      92           0 :             qWarning("QSignalSpy: QMetaObject::connect returned false. Unable to connect.");
      93           0 :             return;
      94             :         }
      95           6 :         sig = ba;
      96           6 :         initArgs(mo->method(sigIndex), obj);
      97             :     }
      98             : 
      99             : #ifdef Q_QDOC
     100             :     QSignalSpy(const QObject *object, PointerToMemberFunction signal);
     101             : #else
     102             :     template <typename Func>
     103             :     QSignalSpy(const typename QtPrivate::FunctionPointer<Func>::Object *obj, Func signal0)
     104             :         : m_waiting(false)
     105             :     {
     106             : #ifdef Q_CC_BOR
     107             :         const int memberOffset = QObject::staticMetaObject.methodCount();
     108             : #else
     109             :         static const int memberOffset = QObject::staticMetaObject.methodCount();
     110             : #endif
     111             :         if (!obj) {
     112             :             qWarning("QSignalSpy: Cannot spy on a null object");
     113             :             return;
     114             :         }
     115             : 
     116             :         if (!signal0) {
     117             :             qWarning("QSignalSpy: Null signal name is not valid");
     118             :             return;
     119             :         }
     120             : 
     121             :         const QMetaObject * const mo = obj->metaObject();
     122             :         const QMetaMethod signalMetaMethod = QMetaMethod::fromSignal(signal0);
     123             :         const int sigIndex = signalMetaMethod.methodIndex();
     124             :         if (!signalMetaMethod.isValid() ||
     125             :             signalMetaMethod.methodType() != QMetaMethod::Signal) {
     126             :             qWarning("QSignalSpy: Not a valid signal: '%s'",
     127             :                      signalMetaMethod.methodSignature().constData());
     128             :             return;
     129             :         }
     130             : 
     131             :         if (!QMetaObject::connect(obj, sigIndex, this, memberOffset,
     132             :                     Qt::DirectConnection, 0)) {
     133             :             qWarning("QSignalSpy: QMetaObject::connect returned false. Unable to connect.");
     134             :             return;
     135             :         }
     136             :         sig = signalMetaMethod.methodSignature();
     137             :         initArgs(mo->method(sigIndex), obj);
     138             :     }
     139             : #endif // Q_QDOC
     140             : 
     141             :     inline bool isValid() const { return !sig.isEmpty(); }
     142             :     inline QByteArray signal() const { return sig; }
     143             : 
     144           7 :     bool wait(int timeout = 5000)
     145             :     {
     146           7 :         Q_ASSERT(!m_waiting);
     147           7 :         const int origCount = count();
     148           7 :         m_waiting = true;
     149           7 :         m_loop.enterLoopMSecs(timeout);
     150           7 :         m_waiting = false;
     151           7 :         return count() > origCount;
     152             :     }
     153             : 
     154           8 :     int qt_metacall(QMetaObject::Call call, int methodId, void **a) Q_DECL_OVERRIDE
     155             :     {
     156           8 :         methodId = QObject::qt_metacall(call, methodId, a);
     157           8 :         if (methodId < 0)
     158           0 :             return methodId;
     159             : 
     160           8 :         if (call == QMetaObject::InvokeMetaMethod) {
     161           8 :             if (methodId == 0) {
     162           8 :                 appendArgs(a);
     163             :             }
     164           8 :             --methodId;
     165             :         }
     166           8 :         return methodId;
     167             :     }
     168             : 
     169             : private:
     170           6 :     void initArgs(const QMetaMethod &member, const QObject *obj)
     171             :     {
     172           6 :         args.reserve(member.parameterCount());
     173           6 :         for (int i = 0; i < member.parameterCount(); ++i) {
     174           0 :             int tp = member.parameterType(i);
     175           0 :             if (tp == QMetaType::UnknownType && obj) {
     176           0 :                 void *argv[] = { &tp, &i };
     177           0 :                 QMetaObject::metacall(const_cast<QObject*>(obj),
     178             :                                       QMetaObject::RegisterMethodArgumentMetaType,
     179           0 :                                       member.methodIndex(), argv);
     180           0 :                 if (tp == -1)
     181           0 :                     tp = QMetaType::UnknownType;
     182             :             }
     183           0 :             if (tp == QMetaType::UnknownType) {
     184           0 :                 qWarning("QSignalSpy: Unable to handle parameter '%s' of type '%s' of method '%s',"
     185             :                          " use qRegisterMetaType to register it.",
     186           0 :                          member.parameterNames().at(i).constData(),
     187           0 :                          member.parameterTypes().at(i).constData(),
     188           0 :                          member.name().constData());
     189             :             }
     190           0 :             args << tp;
     191             :         }
     192           6 :     }
     193             : 
     194           8 :     void appendArgs(void **a)
     195             :     {
     196          16 :         QList<QVariant> list;
     197           8 :         list.reserve(args.count());
     198           8 :         for (int i = 0; i < args.count(); ++i) {
     199           0 :             const QMetaType::Type type = static_cast<QMetaType::Type>(args.at(i));
     200           0 :             if (type == QMetaType::QVariant)
     201           0 :                 list << *reinterpret_cast<QVariant *>(a[i + 1]);
     202             :             else
     203           0 :                 list << QVariant(type, a[i + 1]);
     204             :         }
     205           8 :         append(list);
     206             : 
     207           8 :         if (m_waiting)
     208           7 :             m_loop.exitLoop();
     209           8 :     }
     210             : 
     211             :     // the full, normalized signal name
     212             :     QByteArray sig;
     213             :     // holds the QMetaType types for the argument list of the signal
     214             :     QVector<int> args;
     215             : 
     216             :     QTestEventLoop m_loop;
     217             :     bool m_waiting;
     218             : };
     219             : 
     220             : QT_END_NAMESPACE
     221             : 
     222             : #endif

Generated by: LCOV version 1.13